DeviceData.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. logs.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. logs.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. logs.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. logs.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. logs.Error(lib.FuncName(), err)
  63. return
  64. }
  65. err = redisCache_DeviceData.Put(key, str, 24*time.Hour)
  66. if err != nil {
  67. logs.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. // //logs.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(*) FROM z_devicedata_" + SN + " WHERE t_time = '" + t_time + "' AND t_id = " + t_id
  162. // //logs.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. // //logs.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. // //logs.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. // //logs.Println("mysql row affected nums: ", num)
  188. // return true
  189. //}
  190. type DeviceData_ struct {
  191. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  192. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  193. T_time string `orm:"column(t_time);type(timestamp);null;"` // 采集时间
  194. }
  195. type DeviceData_New struct {
  196. T_id int `orm:"column(t_id);size(10);null"` // ID
  197. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  198. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  199. T_time time.Time `orm:"column(t_time);type(timestamp);null;"` // 采集时间
  200. }
  201. func Read_DeviceSensorData_ById_List(SN string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  202. o := orm.NewOrm()
  203. var maps []DeviceData_
  204. var maps_z []orm2.ParamsList
  205. pagez := page_z
  206. if len(SN) == 0 {
  207. return maps, 0
  208. }
  209. var offset int
  210. if page <= 1 {
  211. offset = 0
  212. } else {
  213. page -= 1
  214. offset = page * pagez
  215. }
  216. sql_time := ""
  217. if len(Time_start_) > 1 {
  218. sql_time += " t_time >= '" + Time_start_ + "' AND "
  219. }
  220. if len(Time_end_) > 1 {
  221. sql_time += " t_time <= '" + Time_end_ + "' AND "
  222. }
  223. if len(sql_time) > 0 {
  224. sql_time = strings.TrimRight(sql_time, "AND ")
  225. }
  226. sql := "SELECT COUNT(*) FROM z_devicedata_" + SN
  227. if len(sql_time) > 0 {
  228. sql += " WHERE " + sql_time
  229. }
  230. logs.Println(sql)
  231. _, err := o.Raw(sql).ValuesList(&maps_z)
  232. if err != nil {
  233. return maps, 0
  234. }
  235. if len(maps_z) == 0 {
  236. return maps, 0
  237. }
  238. //logs.Println("maps_z;",maps_z[0][0])
  239. //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
  240. sql = "SELECT t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN
  241. if len(sql_time) > 0 {
  242. sql += " WHERE " + sql_time
  243. }
  244. sql += " ORDER BY t_time"
  245. if page_z != 9999 {
  246. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  247. }
  248. logs.Println(sql)
  249. _, err = o.Raw(sql).QueryRows(&maps)
  250. if err != nil {
  251. logs.Println(lib.FuncName(), err)
  252. return maps, 0
  253. }
  254. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  255. key, _ := strconv.Atoi(maps_z[0][0].(string))
  256. return maps, key
  257. }
  258. func Read_DeviceData_By_snid(SN string, T_id int, Time_start_ string, Time_end_ string, offset, limit int) ([]DeviceData_, int) {
  259. var maps []DeviceData_
  260. o := orm.NewOrm()
  261. if len(SN) == 0 {
  262. return maps, 0
  263. }
  264. sql_time := ""
  265. if len(Time_start_) > 1 {
  266. sql_time += " t_time >= '" + Time_start_ + "' AND "
  267. }
  268. if len(Time_end_) > 1 {
  269. sql_time += " t_time <= '" + Time_end_ + "' AND "
  270. }
  271. //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 + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time DESC "
  272. sql := "SELECT t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%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 "
  273. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(limit)
  274. logs.Println(sql)
  275. _, err := o.Raw(sql).QueryRows(&maps)
  276. if err != nil {
  277. logs.Println(lib.FuncName(), err)
  278. return maps, 0
  279. }
  280. return maps, 0
  281. }
  282. func Read_DeviceData_Count_By_snid(SN string, T_id int, Time_start_ string, Time_end_ string) int {
  283. if len(SN) == 0 {
  284. return 0
  285. }
  286. o := orm.NewOrm()
  287. var maps_z []orm2.ParamsList
  288. sql_time := ""
  289. if len(Time_start_) > 1 {
  290. sql_time += " t_time >= '" + Time_start_ + "' AND "
  291. }
  292. if len(Time_end_) > 1 {
  293. sql_time += " t_time <= '" + Time_end_ + "' AND "
  294. }
  295. sql := "SELECT COUNT(*) FROM z_devicedata_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id)
  296. logs.Println(sql)
  297. _, err := o.Raw(sql).ValuesList(&maps_z)
  298. if err != nil {
  299. return 0
  300. }
  301. if len(maps_z) == 0 {
  302. return 0
  303. }
  304. key, _ := strconv.Atoi(maps_z[0][0].(string))
  305. return key
  306. }
  307. func Read_DeviceSensorData_List(SN string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  308. o := orm.NewOrm()
  309. var maps []DeviceData_
  310. var maps_z []orm2.ParamsList
  311. pagez := page_z
  312. if len(SN) == 0 {
  313. return maps, 0
  314. }
  315. var offset int
  316. if page <= 1 {
  317. offset = 0
  318. } else {
  319. page -= 1
  320. offset = page * pagez
  321. }
  322. sql_time := ""
  323. if len(Time_start_) > 1 && len(Time_end_) > 1 {
  324. sql_time = "WHERE "
  325. sql_time += " t_time >= '" + Time_start_ + "' AND "
  326. sql_time += " t_time <= '" + Time_end_ + "' "
  327. }
  328. sql := "SELECT COUNT(*) FROM z_devicedata_" + SN + " " + sql_time
  329. logs.Println(sql)
  330. _, err := o.Raw(sql).ValuesList(&maps_z)
  331. if err != nil {
  332. return maps, 0
  333. }
  334. if len(maps_z) == 0 {
  335. return maps, 0
  336. }
  337. //logs.Println("maps_z;",maps_z[0][0])
  338. //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 + " " + sql_time + " ORDER BY t_time DESC "
  339. sql = "SELECT t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN + " " + sql_time + " ORDER BY t_time DESC "
  340. if page_z != 9999 {
  341. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  342. }
  343. logs.Println(sql)
  344. _, err = o.Raw(sql).QueryRows(&maps)
  345. if err != nil {
  346. logs.Println(lib.FuncName(), err)
  347. return maps, 0
  348. }
  349. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  350. key, _ := strconv.Atoi(maps_z[0][0].(string))
  351. return maps, key
  352. }
  353. func Read_DeviceSensorData_List_z(SN string, Time_start_ string) int {
  354. if len(SN) == 0 {
  355. return 0
  356. }
  357. o := orm.NewOrm()
  358. var maps_z []orm2.ParamsList
  359. sql_time := ""
  360. if len(Time_start_) > 1 {
  361. sql_time = "WHERE "
  362. sql_time += " t_time >= '" + Time_start_ + "' "
  363. //sql_time += " t_time <= '" + Time_end_ + "' AND "
  364. }
  365. sql := "SELECT COUNT(*) FROM z_devicedata_" + SN + " " + sql_time
  366. logs.Println(sql)
  367. _, err := o.Raw(sql).ValuesList(&maps_z)
  368. if err != nil {
  369. logs.Println(lib.FuncName(), err)
  370. return 0
  371. }
  372. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  373. key, _ := strconv.Atoi(maps_z[0][0].(string))
  374. return key
  375. }
  376. // 获取最新数据
  377. func Read_DeviceSensorData_ById_New(SN string, T_id int) (DeviceData_New, bool) {
  378. o := orm.NewOrm()
  379. var maps DeviceData_New
  380. if len(SN) == 0 {
  381. return maps, false
  382. }
  383. key_data := SN + "|" + strconv.Itoa(T_id)
  384. if redisCache_DeviceData.IsExist(key_data) {
  385. r, _ := RedisDeviceData_Get(key_data)
  386. //println("Redis_Get OK")
  387. return r, true
  388. }
  389. //logs.Println("maps_z;",maps_z[0][0])
  390. //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 "
  391. sql := "SELECT t_t,t_rh,t_time FROM z_devicedata_" + SN + " WHERE " + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time DESC "
  392. sql = sql + " LIMIT 0,1 "
  393. logs.Println(sql)
  394. err := o.Raw(sql).QueryRow(&maps)
  395. if err != nil {
  396. logs.Error(lib.FuncName(), err)
  397. return maps, false
  398. }
  399. logs.Println("T_time:", maps.T_time)
  400. RedisDeviceData_Set(key_data, maps)
  401. return maps, true
  402. }
  403. func Read_DeviceSensorData_By_T_snid_List(T_snid string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  404. T_snid_list := strings.Split(T_snid, "|")
  405. var maps []DeviceData_
  406. var maps_num int
  407. var offset, offset_z int
  408. for _, v := range T_snid_list {
  409. sn_id := strings.Split(v, ",")
  410. if len(sn_id) == 2 {
  411. r_maps, r_maps_num := Read_DeviceSensorData_ById_List(sn_id[0], Time_start_, Time_end_, 0, 9999)
  412. maps = append(maps, r_maps...)
  413. maps_num = maps_num + r_maps_num
  414. logs.Println("加载数据:", sn_id[0], sn_id[1], r_maps_num)
  415. }
  416. }
  417. if page <= 1 {
  418. offset = 0
  419. } else {
  420. page -= 1
  421. offset = page * page_z
  422. }
  423. offset_z = offset + page_z
  424. if maps_num < offset_z {
  425. offset_z = maps_num
  426. }
  427. if page_z == 9999 {
  428. logs.Println("总数据:", maps_num, " 导出")
  429. return maps, maps_num
  430. }
  431. logs.Println("总数据:", maps_num, " 截取", offset, offset+page_z)
  432. return maps[offset:offset_z], maps_num
  433. }
  434. func Read_DeviceData_ById_Year_List(SN string) []orm2.ParamsList {
  435. o := orm.NewOrm()
  436. var maps_z []orm2.ParamsList
  437. if len(SN) == 0 {
  438. return maps_z
  439. }
  440. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%m-%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 "
  441. 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\");"
  442. logs.Println(sql)
  443. num, err := o.Raw(sql).ValuesList(&maps_z)
  444. if err == nil && num > 0 {
  445. logs.Println("", maps_z[0][0]) // slene
  446. }
  447. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  448. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  449. return maps_z
  450. } //
  451. func Read_DeviceData_ById_Month_List(SN string) []orm2.ParamsList {
  452. o := orm.NewOrm()
  453. var maps_z []orm2.ParamsList
  454. if len(SN) == 0 {
  455. return maps_z
  456. }
  457. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  458. time_x := currentTime.Format("2006-01") + "-00 00:00:00"
  459. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%m-%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 "
  460. 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\");"
  461. logs.Println(sql)
  462. o.Raw(sql).ValuesList(&maps_z)
  463. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  464. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  465. return maps_z
  466. }
  467. func Read_DeviceSensorData_ById_Day_List(SN string) []orm2.ParamsList {
  468. o := orm.NewOrm()
  469. var maps_z []orm2.ParamsList
  470. if len(SN) == 0 {
  471. return maps_z
  472. }
  473. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  474. time_x := currentTime.Format("2006-01-02") + " 00:00:00"
  475. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%m-%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 "
  476. 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\");"
  477. logs.Println(sql)
  478. o.Raw(sql).ValuesList(&maps_z)
  479. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  480. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  481. return maps_z
  482. }
  483. func Read_DeviceSensorData_List_GROUP_BY_t_time(SN string, Time_start_ string, Time_end_ string) []orm2.ParamsList {
  484. o := orm.NewOrm()
  485. var maps_z []orm2.ParamsList
  486. if len(SN) == 0 {
  487. return maps_z
  488. }
  489. sql_time := ""
  490. if len(Time_start_) > 1 {
  491. sql_time += " t_time >= '" + Time_start_ + "' AND " + " t_time <= '" + Time_end_ + "' "
  492. }
  493. sql := "SELECT DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time FROM z_devicedata_" + SN + " WHERE " + sql_time + " GROUP BY t_time ORDER BY t_time DESC "
  494. logs.Println(sql)
  495. o.Raw(sql).ValuesList(&maps_z)
  496. return maps_z
  497. }
  498. func Read_DeviceData_T_Min_Max_Time_Min_Max(SN string, Time_start_ string, Time_end_ string) (minT, maxT float64, minTime, maxTime time.Time) {
  499. o := orm.NewOrm()
  500. if len(SN) == 0 {
  501. return
  502. }
  503. sql_condition := ""
  504. if len(Time_start_) > 1 {
  505. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  506. }
  507. if len(Time_end_) > 1 {
  508. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  509. }
  510. if len(sql_condition) > 0 {
  511. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  512. }
  513. //logs.Println("maps_z;",maps_z[0][0])
  514. 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
  515. logs.Println(sql)
  516. err := o.Raw(sql).QueryRow(&minT, &maxT, &minTime, &maxTime)
  517. if err != nil {
  518. logs.Error(lib.FuncName(), err)
  519. }
  520. return
  521. }