DeviceData.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. package Device
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. _ "github.com/go-sql-driver/mysql"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // 模板
  18. type DeviceData struct {
  19. Id int `orm:"column(ID);size(11);auto;pk"`
  20. T_id string `orm:"size(256);null"` // 标题
  21. T_t float32 `orm:"size(10);null"` // 温度
  22. T_rh float32 `orm:"size(10);null"` // 湿度
  23. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  25. }
  26. func (t *DeviceData) TableName() string {
  27. return "device_data" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. var redisCache_DeviceData cache.Cache
  30. var redisCache_DeviceDataJPG cache.Cache
  31. func init() {
  32. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  33. "redis_DeviceData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  34. configJPG := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  35. "redis_DeviceDataJPG", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  36. fmt.Println(config)
  37. var err error
  38. redisCache_DeviceData, err = cache.NewCache("redis", config)
  39. if err != nil || redisCache_DeviceData == nil {
  40. errMsg := "failed to init redis"
  41. fmt.Println(errMsg, err)
  42. }
  43. redisCache_DeviceDataJPG, err = cache.NewCache("redis", configJPG)
  44. if err != nil || redisCache_DeviceDataJPG == nil {
  45. errMsg := "failed to init redis"
  46. fmt.Println(errMsg, err)
  47. }
  48. }
  49. func DeviceData_Set(key string) (err error) {
  50. err = redisCache_DeviceData.Put(key, "", 2*time.Hour)
  51. if err != nil {
  52. fmt.Println("set key:", key, err)
  53. }
  54. return
  55. }
  56. // ---------------- Redis -------------------
  57. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  58. func RedisDeviceData_Set(key string, r DeviceData_New) (err error) {
  59. //json序列化
  60. str, err := json.Marshal(r)
  61. if err != nil {
  62. fmt.Print(err)
  63. return
  64. }
  65. err = redisCache_DeviceData.Put(key, str, 24*time.Hour)
  66. if err != nil {
  67. fmt.Println("set key:", key, ",value:", str, err)
  68. }
  69. return
  70. }
  71. // if r,is :=Redis_Get(T_sn);is{
  72. // return r,nil
  73. // }
  74. func RedisDeviceData_Get(key string) (r DeviceData_New, is bool) {
  75. v := redisCache_DeviceData.Get(key)
  76. json.Unmarshal(v.([]byte), &r)
  77. return r, true
  78. }
  79. type DeviceCount struct {
  80. T_id string `json:"T_id"`
  81. Num int `json:"Num"`
  82. }
  83. type DeviceDataJPG struct {
  84. State int `json:"State"` //1:生成中 2:已完成 3:失败
  85. Msg string `json:"Msg"`
  86. Url string `json:"Url"` //url
  87. Device []DeviceCount `json:"Device"`
  88. }
  89. // ---------------- Redis -------------------
  90. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  91. func Redis_DeviceDataJPG_Set(key string, r DeviceDataJPG) (err error) {
  92. //json序列化
  93. str, err := json.Marshal(r)
  94. if err != nil {
  95. logs.Error(lib.FuncName(), err)
  96. return
  97. }
  98. err = redisCache_DeviceDataJPG.Put(key, str, 5*time.Minute)
  99. if err != nil {
  100. logs.Println("set key:", key, ",value:", str, err)
  101. }
  102. return
  103. }
  104. // if r,is :=Redis_Get(T_sn);is{
  105. // return r,nil
  106. // }
  107. func Redis_DeviceDataJPG_Get(key string) (r DeviceDataJPG, is bool) {
  108. if redisCache_DeviceDataJPG.IsExist(key) {
  109. logs.Println("找到key:", key)
  110. v := redisCache_DeviceDataJPG.Get(key)
  111. json.Unmarshal(v.([]byte), &r)
  112. return r, true
  113. }
  114. logs.Println("没有 找到key:", key)
  115. return DeviceDataJPG{}, false
  116. }
  117. func Redis_DeviceDataJPG_Del(key string) (err error) {
  118. err = redisCache_DeviceDataJPG.Delete(key)
  119. return
  120. }
  121. // 创建数据库 Device.CREATE_DeviceData("")
  122. func CREATE_DeviceData(SN string) bool {
  123. o := orm.NewOrm()
  124. sql := "CREATE TABLE IF NOT EXISTS `z_device_data_" + SN + "` ( " +
  125. " `ID` int(11) NOT NULL AUTO_INCREMENT," +
  126. " `t_id` int(11) NULL DEFAULT NULL," +
  127. " `t_t` float(6, 1) NULL DEFAULT NULL," +
  128. " `t_rh` float(6, 1) NULL DEFAULT NULL," +
  129. " `t_time` datetime(0) NULL DEFAULT NULL," +
  130. " `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0)," +
  131. " PRIMARY KEY (`ID`) USING BTREE" +
  132. ") ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"
  133. _, err := o.Raw(sql).Exec()
  134. if err != nil {
  135. return false
  136. }
  137. return true
  138. }
  139. // ---------------- 特殊方法 -------------------
  140. // 获取 ById
  141. //func Read_List_ById(id int) (r DeviceData) {
  142. // o := orm.NewOrm()
  143. //
  144. // return r
  145. //}
  146. //
  147. //// 添加
  148. //func Add_DeviceData(SN string, t_id string, t_t string, t_rh string, t_time string) bool {
  149. // key := SN + "|" + t_id + "|" + t_time
  150. // //fmt.Println(key)
  151. // if redisCache_DeviceData.IsExist(key) {
  152. // //println("找到key:",key)
  153. // return true
  154. // }
  155. // //println("没有 找到key:",key)
  156. //
  157. // o := orm.NewOrm()
  158. // // 查看是否 有记录
  159. // var maps_z []orm2.ParamsList
  160. //
  161. // sql := "SELECT COUNT(ID) FROM z_devicedata_" + SN + " WHERE t_time = '" + t_time + "' AND t_id = " + t_id
  162. // //fmt.Println(sql)
  163. // _, err := o.Raw(sql).ValuesList(&maps_z)
  164. //
  165. // if err != nil {
  166. // logs.Error(lib.FuncName(),err)
  167. // return false
  168. // }
  169. // //fmt.Println("maps_z[0][0]:",maps_z[0][0])
  170. // if lib.To_int(maps_z[0][0]) > 0 {
  171. // // 存在 写入 redis
  172. // DeviceData_Set(key)
  173. // return true
  174. // }
  175. // // 开始插入数据
  176. // sql = "INSERT INTO z_devicedata_" + SN + " (`t_id`, `t_t`, `t_rh`, `t_time`) VALUES (" + t_id + ", " + t_t + ", " + t_rh + ", '" + t_time + "')"
  177. // // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  178. // //fmt.Println(sql)
  179. // res, err := o.Raw(sql).Exec()
  180. // if err != nil {
  181. // logs.Error(lib.FuncName(),err)
  182. // return false
  183. // }
  184. // res.RowsAffected()
  185. // // 存在 写入 redis
  186. // DeviceData_Set(key)
  187. // //fmt.Println("mysql row affected nums: ", num)
  188. // return true
  189. //}
  190. type DeviceData_ struct {
  191. T_id int `orm:"column(t_id);size(10);null"` // ID
  192. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  193. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  194. T_time string `orm:"column(t_time);type(timestamp);null;"` // 采集时间
  195. }
  196. type DeviceData_New struct {
  197. T_id int `orm:"column(t_id);size(10);null"` // ID
  198. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  199. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  200. T_time time.Time `orm:"column(t_time);type(timestamp);null;"` // 采集时间
  201. }
  202. func Read_DeviceSensorData_ById_List(SN string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  203. o := orm.NewOrm()
  204. var maps []DeviceData_
  205. var maps_z []orm2.ParamsList
  206. pagez := page_z
  207. var offset int
  208. if page <= 1 {
  209. offset = 0
  210. } else {
  211. page -= 1
  212. offset = page * pagez
  213. }
  214. sql_time := ""
  215. if len(Time_start_) > 1 {
  216. sql_time += " t_time >= '" + Time_start_ + "' AND "
  217. }
  218. if len(Time_end_) > 1 {
  219. sql_time += " t_time <= '" + Time_end_ + "' AND "
  220. }
  221. if len(sql_time) > 0 {
  222. sql_time = strings.TrimRight(sql_time, "AND ")
  223. }
  224. sql := "SELECT COUNT(ID) FROM z_devicedata_" + SN
  225. if len(sql_time) > 0 {
  226. sql += " WHERE " + sql_time
  227. }
  228. fmt.Println(sql)
  229. _, err := o.Raw(sql).ValuesList(&maps_z)
  230. if err != nil {
  231. return maps, 0
  232. }
  233. if len(maps_z) == 0 {
  234. return maps, 0
  235. }
  236. //fmt.Println("maps_z;",maps_z[0][0])
  237. sql = "SELECT t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN
  238. if len(sql_time) > 0 {
  239. sql += " WHERE " + sql_time
  240. }
  241. sql += " ORDER BY t_time"
  242. if page_z != 9999 {
  243. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  244. }
  245. fmt.Println(sql)
  246. _, err = o.Raw(sql).QueryRows(&maps)
  247. if err != nil {
  248. logs.Println(lib.FuncName(), err)
  249. return maps, 0
  250. }
  251. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  252. key, _ := strconv.Atoi(maps_z[0][0].(string))
  253. return maps, key
  254. }
  255. func Read_DeviceData_By_snid(SN string, T_id int, Time_start_ string, Time_end_ string, offset, limit int) ([]DeviceData_, int) {
  256. o := orm.NewOrm()
  257. var maps []DeviceData_
  258. sql_time := ""
  259. if len(Time_start_) > 1 {
  260. sql_time += " t_time >= '" + Time_start_ + "' AND "
  261. }
  262. if len(Time_end_) > 1 {
  263. sql_time += " t_time <= '" + Time_end_ + "' AND "
  264. }
  265. sql := "SELECT t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time DESC "
  266. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(limit)
  267. fmt.Println(sql)
  268. _, err := o.Raw(sql).QueryRows(&maps)
  269. if err != nil {
  270. logs.Println(lib.FuncName(), err)
  271. return maps, 0
  272. }
  273. return maps, 0
  274. }
  275. func Read_DeviceData_Count_By_snid(SN string, T_id int, Time_start_ string, Time_end_ string) int {
  276. o := orm.NewOrm()
  277. var maps_z []orm2.ParamsList
  278. sql_time := ""
  279. if len(Time_start_) > 1 {
  280. sql_time += " t_time >= '" + Time_start_ + "' AND "
  281. }
  282. if len(Time_end_) > 1 {
  283. sql_time += " t_time <= '" + Time_end_ + "' AND "
  284. }
  285. sql := "SELECT COUNT(ID) FROM z_devicedata_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id)
  286. fmt.Println(sql)
  287. _, err := o.Raw(sql).ValuesList(&maps_z)
  288. if err != nil {
  289. return 0
  290. }
  291. if len(maps_z) == 0 {
  292. return 0
  293. }
  294. key, _ := strconv.Atoi(maps_z[0][0].(string))
  295. return key
  296. }
  297. func Read_DeviceSensorData_List(SN string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  298. o := orm.NewOrm()
  299. var maps []DeviceData_
  300. var maps_z []orm2.ParamsList
  301. pagez := page_z
  302. var offset int
  303. if page <= 1 {
  304. offset = 0
  305. } else {
  306. page -= 1
  307. offset = page * pagez
  308. }
  309. sql_time := ""
  310. if len(Time_start_) > 1 && len(Time_end_) > 1 {
  311. sql_time = "WHERE "
  312. sql_time += " t_time >= '" + Time_start_ + "' AND "
  313. sql_time += " t_time <= '" + Time_end_ + "' "
  314. }
  315. sql := "SELECT COUNT(ID) FROM z_devicedata_" + SN + " " + sql_time
  316. fmt.Println(sql)
  317. _, err := o.Raw(sql).ValuesList(&maps_z)
  318. if err != nil {
  319. return maps, 0
  320. }
  321. if len(maps_z) == 0 {
  322. return maps, 0
  323. }
  324. //fmt.Println("maps_z;",maps_z[0][0])
  325. sql = "SELECT t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN + " " + sql_time + " ORDER BY t_time DESC "
  326. if page_z != 9999 {
  327. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  328. }
  329. fmt.Println(sql)
  330. _, err = o.Raw(sql).QueryRows(&maps)
  331. if err != nil {
  332. logs.Println(lib.FuncName(), err)
  333. return maps, 0
  334. }
  335. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  336. key, _ := strconv.Atoi(maps_z[0][0].(string))
  337. return maps, key
  338. }
  339. func Read_DeviceSensorData_List_z(SN string, Time_start_ string) int {
  340. o := orm.NewOrm()
  341. var maps_z []orm2.ParamsList
  342. sql_time := ""
  343. if len(Time_start_) > 1 {
  344. sql_time = "WHERE "
  345. sql_time += " t_time >= '" + Time_start_ + "' "
  346. //sql_time += " t_time <= '" + Time_end_ + "' AND "
  347. }
  348. sql := "SELECT COUNT(ID) FROM z_devicedata_" + SN + " " + sql_time
  349. fmt.Println(sql)
  350. _, err := o.Raw(sql).ValuesList(&maps_z)
  351. if err != nil {
  352. logs.Println(lib.FuncName(), err)
  353. return 0
  354. }
  355. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  356. key, _ := strconv.Atoi(maps_z[0][0].(string))
  357. return key
  358. }
  359. // 获取最新数据
  360. func Read_DeviceSensorData_ById_New(SN string, T_id int) (DeviceData_New, bool) {
  361. o := orm.NewOrm()
  362. var maps DeviceData_New
  363. key_data := SN + "|" + strconv.Itoa(T_id)
  364. if redisCache_DeviceData.IsExist(key_data) {
  365. r, _ := RedisDeviceData_Get(key_data)
  366. //println("Redis_Get OK")
  367. return r, true
  368. }
  369. //fmt.Println("maps_z;",maps_z[0][0])
  370. sql := "SELECT t_id,t_t,t_rh,t_time FROM z_devicedata_" + SN + " WHERE " + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time DESC "
  371. sql = sql + " LIMIT 0,1 "
  372. fmt.Println(sql)
  373. err := o.Raw(sql).QueryRow(&maps)
  374. if err != nil {
  375. logs.Error(lib.FuncName(), err)
  376. return maps, false
  377. }
  378. fmt.Println("T_time:", maps.T_time)
  379. RedisDeviceData_Set(key_data, maps)
  380. return maps, true
  381. }
  382. func Read_DeviceSensorData_By_T_snid_List(T_snid string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  383. T_snid_list := strings.Split(T_snid, "|")
  384. var maps []DeviceData_
  385. var maps_num int
  386. var offset, offset_z int
  387. for _, v := range T_snid_list {
  388. sn_id := strings.Split(v, ",")
  389. if len(sn_id) == 2 {
  390. r_maps, r_maps_num := Read_DeviceSensorData_ById_List(sn_id[0], Time_start_, Time_end_, 0, 9999)
  391. maps = append(maps, r_maps...)
  392. maps_num = maps_num + r_maps_num
  393. fmt.Println("加载数据:", sn_id[0], sn_id[1], r_maps_num)
  394. }
  395. }
  396. if page <= 1 {
  397. offset = 0
  398. } else {
  399. page -= 1
  400. offset = page * page_z
  401. }
  402. offset_z = offset + page_z
  403. if maps_num < offset_z {
  404. offset_z = maps_num
  405. }
  406. if page_z == 9999 {
  407. fmt.Println("总数据:", maps_num, " 导出")
  408. return maps, maps_num
  409. }
  410. fmt.Println("总数据:", maps_num, " 截取", offset, offset+page_z)
  411. return maps[offset:offset_z], maps_num
  412. }
  413. func Read_DeviceData_ById_Year_List(SN string) []orm2.ParamsList {
  414. o := orm.NewOrm()
  415. var maps_z []orm2.ParamsList
  416. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_devicedata_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  417. sql := "SELECT DATE_FORMAT(t_time,\"%m\") AS m ,DATE_FORMAT(t_time,\"%d\") AS d FROM z_devicedata_" + SN + " WHERE t_time > '" + strconv.Itoa(time.Now().Year()) + "-0-0 00:00:00' GROUP BY DATE_FORMAT(t_time,\"%m\");"
  418. fmt.Println(sql)
  419. num, err := o.Raw(sql).ValuesList(&maps_z)
  420. if err == nil && num > 0 {
  421. fmt.Println(maps_z[0][0]) // slene
  422. }
  423. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  424. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  425. return maps_z
  426. } //
  427. func Read_DeviceData_ById_Month_List(SN string) []orm2.ParamsList {
  428. o := orm.NewOrm()
  429. var maps_z []orm2.ParamsList
  430. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  431. time_x := currentTime.Format("2006-01") + "-00 00:00:00"
  432. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_devicedata_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  433. sql := "SELECT DATE_FORMAT(t_time,\"%d\") AS d FROM z_devicedata_" + SN + " WHERE t_time > '" + time_x + "' GROUP BY DATE_FORMAT(t_time,\"%d\");"
  434. fmt.Println(sql)
  435. o.Raw(sql).ValuesList(&maps_z)
  436. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  437. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  438. return maps_z
  439. }
  440. func Read_DeviceSensorData_ById_Day_List(SN string) []orm2.ParamsList {
  441. o := orm.NewOrm()
  442. var maps_z []orm2.ParamsList
  443. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  444. time_x := currentTime.Format("2006-01-02") + " 00:00:00"
  445. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_devicedata_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  446. sql := "SELECT DATE_FORMAT(t_time,\"%H\") AS m FROM z_devicedata_" + SN + " WHERE t_time > '" + time_x + "' GROUP BY DATE_FORMAT(t_time,\"%H\");"
  447. fmt.Println(sql)
  448. o.Raw(sql).ValuesList(&maps_z)
  449. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  450. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  451. return maps_z
  452. }
  453. func Read_DeviceSensorData_List_GROUP_BY_t_time(SN string, Time_start_ string, Time_end_ string) []orm2.ParamsList {
  454. o := orm.NewOrm()
  455. var maps_z []orm2.ParamsList
  456. sql_time := ""
  457. if len(Time_start_) > 1 {
  458. sql_time += " t_time >= '" + Time_start_ + "' AND " + " t_time <= '" + Time_end_ + "' "
  459. }
  460. sql := "SELECT DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN + " WHERE " + sql_time + " GROUP BY t_time ORDER BY t_time DESC "
  461. fmt.Println(sql)
  462. o.Raw(sql).ValuesList(&maps_z)
  463. return maps_z
  464. }
  465. func Read_DeviceData_T_Min_Max_Time_Min_Max(SN string, Time_start_ string, Time_end_ string) (minT, maxT float64, minTime, maxTime time.Time) {
  466. o := orm.NewOrm()
  467. sql_condition := ""
  468. if len(Time_start_) > 1 {
  469. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  470. }
  471. if len(Time_end_) > 1 {
  472. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  473. }
  474. if len(sql_condition) > 0 {
  475. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  476. }
  477. //fmt.Println("maps_z;",maps_z[0][0])
  478. sql := "SELECT MIN(t_t) AS min_t, MAX(t_t) AS max_t,MIN(t_time) AS min_time, MAX(t_time) AS max_time FROM z_devicedata_" + SN + sql_condition
  479. fmt.Println(sql)
  480. err := o.Raw(sql).QueryRow(&minT, &maxT, &minTime, &maxTime)
  481. if err != nil {
  482. logs.Error(lib.FuncName(), err)
  483. }
  484. return
  485. }