DeviceData.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/astaxie/beego/logs"
  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. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 建表
  19. // var CREATEsql = "CREATE TABLE IF NOT EXISTS `z_device_data_" + SN + " ( " +
  20. var CREATEsql = " ( " +
  21. "`t_id` int(6) NOT NULL," +
  22. "`t_sp` int(6) NOT NULL," +
  23. "`t_time` datetime NOT NULL," +
  24. "`t_t` double(6, 1) NULL DEFAULT NULL," +
  25. "`t_rh` double(6, 1) NULL DEFAULT NULL," +
  26. "`t_site` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
  27. "`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP," +
  28. "PRIMARY KEY (`t_time`, `t_id`) USING BTREE" +
  29. ") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;"
  30. type DeviceData_ struct {
  31. T_id int // 传感器id
  32. T_sp int // 传感器参数id
  33. T_t float32 // 温度
  34. T_rh float32 // 湿度
  35. T_site string // GPS
  36. T_time time.Time // 采集时间
  37. }
  38. // 模板
  39. type DeviceData_R struct {
  40. T_sn string // sn
  41. T_id int // 传感器id
  42. T_name string // 传感器名称
  43. T_t float32 // 温度
  44. T_rh float32 // 湿度
  45. T_site string // GPS
  46. T_tl float32 // 温度下限
  47. T_tu float32 // 温度上限
  48. T_rhl float32 // 湿度下限
  49. T_rhu float32 // 湿度上限
  50. T_time string // 采集时间
  51. T_sp int // 传感器参数id
  52. T_ist int // 温度 1开启 2关闭
  53. T_ish int // 湿度 1开启 2关闭
  54. T_free int // 空库
  55. T_remark string // 备注
  56. }
  57. // 模板
  58. type DeviceData_Docking struct {
  59. T_monitor int // 监控状态 0 未监控 1 监控
  60. T_online int // 在线状态 0 未启用 1 在线 2 离线
  61. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  62. T_sn string // sn
  63. T_id int // 传感器id
  64. T_name string // 传感器名称
  65. T_t float32 // 温度
  66. T_rh float32 // 湿度
  67. T_site string // GPS
  68. T_tl float32 // 温度下限
  69. T_tu float32 // 温度上限
  70. T_rhl float32 // 湿度下限
  71. T_rhu float32 // 湿度上限
  72. T_time string // 采集时间
  73. T_sp int // 传感器参数id
  74. }
  75. type DeviceData_Pdf struct {
  76. T_time string // sn
  77. T_id1 *float32 // 传感器id1温度
  78. T_id2 *float32 // 传感器id2温度
  79. }
  80. type DeviceData_BackUp struct {
  81. T_sn string
  82. T_id int // 传感器id
  83. T_name string // 传感器名称
  84. T_date string // 采集日期
  85. T_num int // 数据量
  86. }
  87. //func (t *DeviceData) TableName() string {
  88. // return "DeviceData" // 数据库名称 // ************** 替换 FormulaList **************
  89. //}
  90. var redis_DeviceData cache.Cache
  91. func init() {
  92. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  93. "redis_DeviceData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  94. fmt.Println(config)
  95. var err error
  96. redis_DeviceData, err = cache.NewCache("redis", config)
  97. if err != nil || redis_DeviceData == nil {
  98. errMsg := "failed to init redis"
  99. logs.Error(errMsg, err)
  100. panic(errMsg)
  101. }
  102. }
  103. func DeviceData_ToDeviceData_R(d Device, r DeviceData_) (t DeviceData_R) {
  104. t.T_id = r.T_id
  105. t.T_t = r.T_t
  106. t.T_rh = r.T_rh
  107. t.T_site = r.T_site
  108. t.T_time = r.T_time.Format("2006-01-02 15:04:05")
  109. t.T_sp = r.T_sp
  110. sp := Read_DeviceSensorParameter_Map_Get(r.T_sp)
  111. t.T_name = sp.T_name
  112. t.T_tl = sp.T_Tlower
  113. t.T_tu = sp.T_Tupper
  114. t.T_rhl = sp.T_RHlower
  115. t.T_rhu = sp.T_RHupper
  116. t.T_free = sp.T_free
  117. if sp.T_free == 1 {
  118. t.T_remark = "空库"
  119. } else {
  120. t.T_remark = "-"
  121. }
  122. t.T_sn = d.T_sn
  123. t.T_ist = d.T_ist
  124. t.T_ish = d.T_ish
  125. return t
  126. }
  127. func DeviceDataToDeviceData_R2(d Device, sp DeviceSensorParameter_R, r DeviceData_) (t DeviceData_R) {
  128. t.T_id = r.T_id
  129. t.T_t = r.T_t
  130. t.T_rh = r.T_rh
  131. t.T_site = r.T_site
  132. t.T_time = r.T_time.Format("2006-01-02 15:04:05")
  133. // 传感器参数信息
  134. t.T_sp = r.T_sp
  135. t.T_name = sp.T_name
  136. t.T_tl = sp.T_Tlower
  137. t.T_tu = sp.T_Tupper
  138. t.T_rhl = sp.T_RHlower
  139. t.T_rhu = sp.T_RHupper
  140. // 设备信息
  141. t.T_ist = d.T_ist
  142. t.T_ish = d.T_ish
  143. return t
  144. }
  145. // ---------------- Redis -------------------
  146. // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
  147. func RedisDeviceData_Set(key string, r DeviceData_) (err error) {
  148. if redis_DeviceData.IsExist(key) {
  149. var t DeviceData_
  150. v := redis_DeviceData.Get(key)
  151. json.Unmarshal(v.([]byte), &t)
  152. if t.T_time.Unix() > r.T_time.Unix() {
  153. // 储存的 是最新数据
  154. return
  155. }
  156. }
  157. //json序列化
  158. str, err := json.Marshal(r)
  159. if err != nil {
  160. logs.Error(lib.FuncName(), err)
  161. return
  162. }
  163. err = redis_DeviceData.Put(key, str, 1*time.Hour)
  164. if err != nil {
  165. logs.Error("set key:", key, ",value:", str, err)
  166. }
  167. return
  168. }
  169. func RedisDeviceData_Get(key string) (r DeviceData_, is bool) {
  170. if redis_DeviceData.IsExist(key) {
  171. v := redis_DeviceData.Get(key)
  172. err := json.Unmarshal(v.([]byte), &r)
  173. if err != nil {
  174. logs.Error(lib.FuncName(), err)
  175. return DeviceData_{}, false
  176. }
  177. return r, true
  178. }
  179. return DeviceData_{}, false
  180. }
  181. // -------------------------------------------------------
  182. // 创建数据库 Device.CREATE_DeviceData("")
  183. func CREATE_DeviceData(SN string) bool {
  184. sql := "CREATE TABLE IF NOT EXISTS `z_device_data_" + SN + "` " + CREATEsql
  185. o := orm.NewOrm()
  186. _, err := o.Raw(sql).Exec()
  187. if err != nil {
  188. logs.Error(lib.FuncName(), err)
  189. return false
  190. }
  191. return true
  192. }
  193. func DELETE_DeviceDatar(SN string) bool {
  194. timeStr := time.Now().Format("2006_01_02_15_04_05")
  195. //sql := "DROP TABLE z_device_data_" + SN
  196. sql := "ALTER TABLE z_device_data_" + SN + " RENAME TO " + "z_device_data_" + SN + "_dle_" + timeStr + ";"
  197. logs.Info("DELETE_DeviceDatar sql:", sql)
  198. o := orm.NewOrm()
  199. _, err := o.Raw(sql).Exec()
  200. if err != nil {
  201. logs.Error(lib.FuncName(), err)
  202. return false
  203. }
  204. return true
  205. }
  206. // ---------------- 特殊方法 ------------------
  207. type DeviceData_T struct {
  208. T_id int // ID
  209. T_t float32 // 温度
  210. T_rh float32 // 湿度
  211. T_Site string // GPS
  212. T_time time.Time // 采集时间
  213. }
  214. // 添加
  215. func Add_DeviceData(SN string, v DeviceData_T) bool {
  216. //if(conf.Test_server){
  217. // return true
  218. //}
  219. key_time := SN + "|" + strconv.Itoa(v.T_id) + "|" + v.T_time.Format("2006-01-02 15:04:05")
  220. logs.Debug(key_time)
  221. if redis_DeviceData.IsExist(key_time) {
  222. //println("找到key:",key)
  223. return true
  224. }
  225. o := orm.NewOrm()
  226. // 检查 超过时间,查询 数据库
  227. logs.Info("Add_DeviceData 时间差s:", time.Now().Unix()-v.T_time.Unix())
  228. if time.Now().Unix()-v.T_time.Unix() >= 60*40 {
  229. // 查看是否 有记录
  230. var maps_z []orm2.ParamsList
  231. sql_c := "SELECT COUNT(ID) FROM z_device_data_" + SN + " WHERE t_time = '" + v.T_time.Format("2006-01-02 15:04:05") + "' AND t_id = " + strconv.Itoa(v.T_id)
  232. logs.Info("检查 超过时间,查询 数据库 SQL:", sql_c)
  233. _, err := o.Raw(sql_c).ValuesList(&maps_z)
  234. if err != nil {
  235. logs.Info(err)
  236. return false
  237. }
  238. if lib.To_int(maps_z[0][0]) > 0 {
  239. logs.Info("存在记录,跳过!")
  240. return true
  241. }
  242. }
  243. // 开始插入数据
  244. sql := "INSERT INTO z_device_data_" + SN + " ( `t_id`, `t_t`, `t_rh`, `t_site`, `t_time`) " +
  245. "VALUES (" + strconv.Itoa(v.T_id) + ", " + lib.To_string(v.T_t) + ", " + lib.To_string(v.T_rh) + ", '" + v.T_Site + "'," + v.T_time.Format("2006-01-02 15:04:05") + "')"
  246. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  247. logs.Debug(sql)
  248. res, err := o.Raw(sql).Exec()
  249. if err != nil {
  250. logs.Error(lib.FuncName(), err)
  251. return false
  252. }
  253. res.RowsAffected()
  254. return true
  255. }
  256. func Read_DeviceData_ById_List(SN string, T_id int, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_R, int) {
  257. o := orm.NewOrm()
  258. var maps []DeviceData_
  259. var maps_z []orm2.ParamsList
  260. var r []DeviceData_R
  261. var offset int
  262. if page_z == 0 {
  263. page_z = conf.Page_size
  264. }
  265. if page <= 1 {
  266. offset = 0
  267. } else {
  268. offset = (page - 1) * page_z
  269. }
  270. sql_time := ""
  271. if len(Time_start_) > 1 {
  272. sql_time += " t_time >= '" + Time_start_ + "' AND "
  273. }
  274. if len(Time_end_) > 1 {
  275. sql_time += " t_time <= '" + Time_end_ + "' AND "
  276. }
  277. sql := "SELECT COUNT(t_id) FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id)
  278. logs.Debug(sql)
  279. _, err := o.Raw(sql).ValuesList(&maps_z)
  280. if err != nil {
  281. logs.Error(lib.FuncName(), err)
  282. return r, 0
  283. }
  284. if len(maps_z) == 0 {
  285. return r, 0
  286. }
  287. sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time1 DESC,t_id DESC "
  288. if page_z != 9999 {
  289. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  290. }
  291. logs.Debug(sql)
  292. _, err = o.Raw(sql).QueryRows(&maps)
  293. if err != nil {
  294. logs.Error(lib.FuncName(), err)
  295. return r, 0
  296. }
  297. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  298. key, _ := strconv.Atoi(maps_z[0][0].(string))
  299. device, err := Read_Device_ByT_sn(SN)
  300. if err != nil {
  301. logs.Error(lib.FuncName(), err)
  302. }
  303. for _, v := range maps {
  304. r = append(r, DeviceData_ToDeviceData_R(device, v))
  305. }
  306. return r, key
  307. }
  308. func Read_DeviceData_ById_List_OrderByTimeAes(SN string, T_id int, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_R, int) {
  309. o := orm.NewOrm()
  310. var maps []DeviceData_
  311. var maps_z []orm2.ParamsList
  312. var r []DeviceData_R
  313. var offset int
  314. if page_z == 0 {
  315. page_z = conf.Page_size
  316. }
  317. if page <= 1 {
  318. offset = 0
  319. } else {
  320. offset = (page - 1) * page_z
  321. }
  322. sql_time := ""
  323. if len(Time_start_) > 1 {
  324. sql_time += " t_time >= '" + Time_start_ + "' AND "
  325. }
  326. if len(Time_end_) > 1 {
  327. sql_time += " t_time <= '" + Time_end_ + "' AND "
  328. }
  329. sql := "SELECT COUNT(t_id) FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id)
  330. logs.Debug(sql)
  331. _, err := o.Raw(sql).ValuesList(&maps_z)
  332. if err != nil {
  333. logs.Error(lib.FuncName(), err)
  334. return r, 0
  335. }
  336. if len(maps_z) == 0 {
  337. return r, 0
  338. }
  339. sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time1,t_id DESC "
  340. if page_z != 9999 {
  341. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  342. }
  343. logs.Debug(sql)
  344. _, err = o.Raw(sql).QueryRows(&maps)
  345. if err != nil {
  346. logs.Error(lib.FuncName(), err)
  347. return r, 0
  348. }
  349. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  350. key, _ := strconv.Atoi(maps_z[0][0].(string))
  351. device, err := Read_Device_ByT_sn(SN)
  352. if err != nil {
  353. logs.Error(lib.FuncName(), err)
  354. }
  355. for _, v := range maps {
  356. r = append(r, DeviceData_ToDeviceData_R(device, v))
  357. }
  358. return r, key
  359. }
  360. func Read_DeviceData_ByIds(SN string, ids []int, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_R, int) {
  361. o := orm.NewOrm()
  362. var maps []DeviceData_
  363. var maps_z []orm2.ParamsList
  364. var r []DeviceData_R
  365. var offset int
  366. if page_z == 0 {
  367. page_z = conf.Page_size
  368. }
  369. if page <= 1 {
  370. offset = 0
  371. } else {
  372. offset = (page - 1) * page_z
  373. }
  374. sql_time := ""
  375. if len(Time_start_) > 1 {
  376. sql_time += " t_time >= '" + Time_start_ + "' AND "
  377. }
  378. if len(Time_end_) > 1 {
  379. sql_time += " t_time <= '" + Time_end_ + "' AND "
  380. }
  381. sql := "SELECT COUNT(t_id) FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id in (" + lib.IntListToDotStr(ids) + ")"
  382. logs.Debug(sql)
  383. _, err := o.Raw(sql).ValuesList(&maps_z)
  384. if err != nil {
  385. logs.Error(lib.FuncName(), err)
  386. return r, 0
  387. }
  388. if len(maps_z) == 0 {
  389. return r, 0
  390. }
  391. //t_tl,t_tu,t_rhl,t_rhu,
  392. sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id in (" + lib.IntListToDotStr(ids) + ") ORDER BY t_time1 DESC,t_id ASC "
  393. if page_z != 9999 {
  394. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  395. }
  396. logs.Debug(sql)
  397. _, err = o.Raw(sql).QueryRows(&maps)
  398. if err != nil {
  399. logs.Error(lib.FuncName(), err)
  400. return r, 0
  401. }
  402. key, _ := strconv.Atoi(maps_z[0][0].(string))
  403. device, err := Read_Device_ByT_sn(SN)
  404. if err != nil {
  405. logs.Error(lib.FuncName(), err)
  406. }
  407. for _, v := range maps {
  408. r = append(r, DeviceData_ToDeviceData_R(device, v))
  409. }
  410. return r, key
  411. }
  412. func Read_DeviceData_ByIds_ForPDF(SN string, ids []int, Time_start_ string, Time_end_ string) []DeviceData_Pdf {
  413. o := orm.NewOrm()
  414. var r []DeviceData_Pdf
  415. if len(ids) == 0 || len(ids) > 2 {
  416. return r
  417. }
  418. sql_time := ""
  419. if len(Time_start_) > 1 {
  420. sql_time += " t_time >= '" + Time_start_ + "' AND "
  421. }
  422. if len(Time_end_) > 1 {
  423. sql_time += " t_time <= '" + Time_end_ + "' AND "
  424. }
  425. selectSql := ""
  426. for i, v := range ids {
  427. selectSql += fmt.Sprintf("MAX(CASE WHEN t_id = %d THEN t_t END) AS t_id%d,", v, i+1)
  428. }
  429. selectSql = strings.Trim(selectSql, ",")
  430. sql := "SELECT DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time," + selectSql + " FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id in (" + lib.IntListToDotStr(ids) + ") GROUP BY t_time ORDER BY t_time"
  431. logs.Debug(sql)
  432. _, err := o.Raw(sql).QueryRows(&r)
  433. if err != nil {
  434. logs.Error(lib.FuncName(), err)
  435. return r
  436. }
  437. return r
  438. }
  439. func Read_SqlRawL(T_SQL string, T_data []string) (string, []orm2.Params) {
  440. o := orm.NewOrm()
  441. var lists []orm2.Params
  442. logs.Debug(T_SQL)
  443. _, err := o.Raw(T_SQL, T_data).Values(&lists)
  444. if err != nil {
  445. logs.Error(lib.FuncName(), err)
  446. return err.Error(), lists
  447. }
  448. return "", lists
  449. }
  450. func Read_DeviceData_By_T_snid_List(T_snid string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_R, int64) {
  451. T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
  452. var maps []DeviceData_R
  453. var maps_num int64
  454. var offset, offset_z int
  455. for _, v := range T_snid_list {
  456. sn_id := strings.Split(v, ",")
  457. if len(sn_id) == 2 {
  458. Read_DeviceSensorParameter_All_Map(sn_id[0], lib.To_int(sn_id[1]))
  459. r_maps, r_maps_num := Read_DeviceData_ById_List(sn_id[0], lib.To_int(sn_id[1]), Time_start_, Time_end_, 0, 9999)
  460. //for i, _ := range r_maps {
  461. // r_maps[i].T_sn = sn_id[0]
  462. //}
  463. maps = append(maps, r_maps...)
  464. maps_num = maps_num + int64(r_maps_num)
  465. logs.Debug("加载数据:", sn_id[0], sn_id[1], r_maps_num)
  466. }
  467. }
  468. if maps_num == 0 {
  469. return maps, maps_num
  470. }
  471. // 倒序
  472. sort.Slice(maps, func(i, j int) bool {
  473. if maps[i].T_time > maps[j].T_time {
  474. return true
  475. }
  476. return false
  477. })
  478. //sort.Slice(maps, func(i, j int) bool {
  479. // // 先按 T_time 字段排序,如果 T_time 相同则按 T_id 字段排序
  480. // if maps[i].T_time == maps[j].T_time {
  481. // return maps[i].T_name < maps[j].T_name
  482. // }
  483. // return maps[i].T_time > maps[j].T_time
  484. //})
  485. if page <= 1 {
  486. offset = 0
  487. } else {
  488. page -= 1
  489. offset = page * page_z
  490. }
  491. offset_z = offset + page_z
  492. if maps_num < int64(offset_z) {
  493. offset_z = int(maps_num)
  494. }
  495. if offset > offset_z {
  496. offset = offset_z
  497. }
  498. if page_z == 9999 {
  499. logs.Debug("总数据:", maps_num, " 导出")
  500. return maps, maps_num
  501. }
  502. logs.Debug("总数据:", maps_num, " 截取", offset, offset+page_z)
  503. return maps[offset:offset_z], maps_num
  504. }
  505. func Read_DeviceData_ById_Year_List(SN string) []orm2.ParamsList {
  506. o := orm.NewOrm()
  507. var maps_z []orm2.ParamsList
  508. //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_device_data_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  509. sql := "SELECT DATE_FORMAT(t_time,\"%m\") AS m ,DATE_FORMAT(t_time,\"%d\") AS d FROM z_device_data_" + SN + " WHERE t_time > '" + strconv.Itoa(time.Now().Year()) + "-0-0 00:00:00' GROUP BY DATE_FORMAT(t_time,\"%m\");"
  510. logs.Debug(sql)
  511. num, err := o.Raw(sql).ValuesList(&maps_z)
  512. if err == nil && num > 0 {
  513. logs.Debug(maps_z[0][0]) // slene
  514. }
  515. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  516. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  517. return maps_z
  518. } //
  519. func Read_DeviceData_ById_Month_List(SN string) []orm2.ParamsList {
  520. o := orm.NewOrm()
  521. var maps_z []orm2.ParamsList
  522. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  523. time_x := currentTime.Format("2006-01") + "-00 00:00:00"
  524. //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_device_data_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  525. sql := "SELECT DATE_FORMAT(t_time,\"%d\") AS d FROM z_device_data_" + SN + " WHERE t_time > '" + time_x + "' GROUP BY DATE_FORMAT(t_time,\"%d\");"
  526. logs.Debug(sql)
  527. o.Raw(sql).ValuesList(&maps_z)
  528. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  529. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  530. return maps_z
  531. }
  532. func Read_DeviceData_ById_Day_List(SN string) []orm2.ParamsList {
  533. o := orm.NewOrm()
  534. var maps_z []orm2.ParamsList
  535. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  536. time_x := currentTime.Format("2006-01-02") + " 00:00:00"
  537. //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_device_data_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  538. sql := "SELECT DATE_FORMAT(t_time,\"%H\") AS m FROM z_device_data_" + SN + " WHERE t_time > '" + time_x + "' GROUP BY DATE_FORMAT(t_time,\"%H\");"
  539. logs.Debug(sql)
  540. o.Raw(sql).ValuesList(&maps_z)
  541. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  542. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  543. return maps_z
  544. }
  545. func Read_DeviceData_List_GROUP_BY_t_time(SN string, Time_start_ string, Time_end_ string) []orm2.ParamsList {
  546. o := orm.NewOrm()
  547. var maps_z []orm2.ParamsList
  548. sql_time := ""
  549. if len(Time_start_) > 1 {
  550. sql_time += " t_time >= '" + Time_start_ + "' AND " + " t_time <= '" + Time_end_ + "' "
  551. }
  552. sql := "SELECT DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM z_device_data_" + SN + " WHERE " + sql_time + " GROUP BY t_time ORDER BY t_time DESC "
  553. logs.Debug(sql)
  554. _, err := o.Raw(sql).ValuesList(&maps_z)
  555. if err != nil {
  556. logs.Error(lib.FuncName(), err)
  557. }
  558. return maps_z
  559. }
  560. func Read_DeviceData(T_sn string, T_id int) (t DeviceData_) {
  561. key := T_sn + "|" + strconv.Itoa(T_id)
  562. if t, is := RedisDeviceData_Get(key); is {
  563. return t
  564. }
  565. o := orm.NewOrm()
  566. var maps []DeviceData_
  567. sql := "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + T_sn + " WHERE" + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time1 DESC LIMIT 0,1"
  568. logs.Debug(sql)
  569. _, err := o.Raw(sql).QueryRows(&maps)
  570. if err != nil {
  571. logs.Error(lib.FuncName(), err)
  572. return t
  573. }
  574. if len(maps) == 0 {
  575. RedisDeviceData_Set(key, t)
  576. return t
  577. }
  578. RedisDeviceData_Set(key, maps[0])
  579. return maps[0]
  580. }
  581. // 获取数据备份
  582. func Read_DeviceData_BackUp_List(T_snid string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_BackUp, int64) {
  583. T_snid_list := strings.Split(T_snid, "|")
  584. var maps []DeviceData_BackUp
  585. var maps_num int64
  586. var offset, offset_z int
  587. for _, v := range T_snid_list {
  588. sn_id := strings.Split(v, ",")
  589. if len(sn_id) == 2 {
  590. r_maps, r_maps_num := Read_DeviceData_BackUp(sn_id[0], lib.To_int(sn_id[1]), Time_start_, Time_end_)
  591. maps = append(maps, r_maps...)
  592. maps_num = maps_num + int64(r_maps_num)
  593. logs.Debug("加载数据:", sn_id[0], sn_id[1], r_maps_num)
  594. }
  595. }
  596. if page <= 1 {
  597. offset = 0
  598. } else {
  599. page -= 1
  600. offset = page * page_z
  601. }
  602. offset_z = offset + page_z
  603. if maps_num < int64(offset_z) {
  604. offset_z = int(maps_num)
  605. }
  606. if offset > offset_z {
  607. offset = offset_z
  608. }
  609. if page_z == 9999 {
  610. logs.Debug("总数据:", maps_num, " 导出")
  611. return maps, maps_num
  612. }
  613. logs.Debug("总数据:", maps_num, " 截取", offset, offset+page_z)
  614. return maps[offset:offset_z], maps_num
  615. }
  616. func Read_DeviceData_BackUp(T_sn string, T_id int, Time_start_ string, Time_end_ string) ([]DeviceData_BackUp, int) {
  617. o := orm.NewOrm()
  618. var maps []DeviceData_BackUp
  619. sql_time := ""
  620. if len(Time_start_) > 0 {
  621. sql_time += " t_time >= '" + Time_start_ + "' AND"
  622. }
  623. if len(Time_end_) > 0 {
  624. sql_time += " t_time <= '" + Time_end_ + "' AND"
  625. }
  626. sql := "SELECT t_id,date_format(t_time, '%Y-%m-%d') t_date, count(*) t_num FROM z_device_data_" + T_sn + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " GROUP BY t_date ORDER BY t_time DESC "
  627. logs.Debug(sql)
  628. _, err := o.Raw(sql).QueryRows(&maps)
  629. if err != nil {
  630. logs.Error(lib.FuncName(), err)
  631. return maps, 0
  632. }
  633. deviceSensor, _ := Read_DeviceSensor_ByT_sn(T_sn, T_id)
  634. for i := 0; i < len(maps); i++ {
  635. maps[i].T_sn = T_sn
  636. maps[i].T_name = deviceSensor.T_name
  637. }
  638. return maps, len(maps)
  639. }
  640. func Read_DeviceData_By_Time(T_sn string, T_id int, Time string) (t DeviceData_) {
  641. o := orm.NewOrm()
  642. var maps DeviceData_
  643. sql := "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + T_sn + " WHERE" + " t_id = " + strconv.Itoa(T_id) + " AND t_time <= '" + Time + "'" + " ORDER BY t_time1 DESC LIMIT 0,1"
  644. logs.Debug(sql)
  645. err := o.Raw(sql).QueryRow(&maps)
  646. if err != nil {
  647. logs.Error(lib.FuncName(), err)
  648. return t
  649. }
  650. return maps
  651. }