TaskData.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package Task
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "errors"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. _ "github.com/go-sql-driver/mysql"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. const (
  15. Temperature = "Temperature"
  16. Humidity = "Humidity"
  17. )
  18. // 模板
  19. type TaskData struct {
  20. Id int `orm:"column(ID);size(11);auto;pk"`
  21. T_id string `orm:"size(256);null"` // 标题
  22. T_t float32 `orm:"size(10);null"` // 温度
  23. T_rh float32 `orm:"size(10);null"` // 湿度
  24. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  25. 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 保存时都会对时间自动更新
  26. }
  27. func (t *TaskData) TableName() string {
  28. return "task_data" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. func init() {
  31. }
  32. // 创建数据库 Device.CREATE_TaskData("")
  33. func CREATE_TaskData(T_task_id string) bool {
  34. o := orm.NewOrm()
  35. sql := "DROP TABLE `z_task_data_" + T_task_id + "`"
  36. o.Raw(sql).Exec()
  37. sql = "CREATE TABLE IF NOT EXISTS `z_task_data_" + T_task_id + "` ( " +
  38. " `ID` int(11) NOT NULL AUTO_INCREMENT," +
  39. " `t_sn` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
  40. " `t_id` varchar(256) DEFAULT NULL," +
  41. " `t_t` float(6, 1) NULL DEFAULT NULL," +
  42. " `t_rh` float(6, 1) NULL DEFAULT NULL," +
  43. " `t_time` datetime(0) NULL DEFAULT NULL," +
  44. " PRIMARY KEY (`ID`) USING BTREE," +
  45. " KEY `t_sn` (`t_sn`)," +
  46. " KEY `t_time` (`t_time`)" +
  47. ") ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"
  48. _, err := o.Raw(sql).Exec()
  49. if err != nil {
  50. return false
  51. }
  52. return true
  53. }
  54. // ---------------- 特殊方法 -------------------
  55. // 获取 ById
  56. //func Read_List_ById(id int) (r TaskData) {
  57. // o := orm.NewOrm()
  58. //
  59. // return r
  60. //}
  61. // 清空
  62. func Truncate_TaskData(T_task_id string) bool {
  63. o := orm.NewOrm()
  64. sql := "truncate table z_task_data_" + T_task_id
  65. logs.Println(sql)
  66. _, err := o.Raw(sql).Exec()
  67. if err != nil {
  68. logs.Error(lib.FuncName(), err)
  69. return false
  70. }
  71. return true
  72. }
  73. // 添加
  74. func Import_TaskData(Sn string, T_id string, T_task_id string, Time_start string, Time_end string) bool {
  75. //if conf.Server_test {
  76. // return true
  77. //}
  78. if len(Sn) == 0 {
  79. return false
  80. }
  81. o := orm.NewOrm()
  82. // 开始插入数据
  83. sql := "insert into z_task_data_" + T_task_id + "(t_id,t_t,t_rh,t_time) select " + T_id + ",t_t,t_rh,t_time from z_devicedata_" + Sn
  84. sql = sql + " WHERE t_time >= '" + Time_start + "' AND t_time <= '" + Time_end + "'"
  85. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  86. logs.Println(sql)
  87. res, err := o.Raw(sql).Exec()
  88. if err != nil {
  89. logs.Error(lib.FuncName(), err)
  90. return false
  91. }
  92. // 强行替换 ID
  93. sql = "UPDATE z_task_data_" + T_task_id + " SET `t_sn` = '" + Sn + "',`t_id` = '" + T_id + "' WHERE `t_sn` IS NULL"
  94. // 设备 ID
  95. //sql = "UPDATE z_task_data_"+T_task_id+" SET `t_sn` = '"+Sn+"' WHERE `t_sn` IS NULL"
  96. logs.Println(sql)
  97. _, err = o.Raw(sql).Exec()
  98. if err != nil {
  99. logs.Error(lib.FuncName(), err)
  100. return false
  101. }
  102. res.RowsAffected()
  103. return true
  104. }
  105. func Import_TaskData_Back(Sn string, T_id string, T_task_id string, Time_start string, Time_end string) error {
  106. if len(Sn) == 0 {
  107. return errors.New("sn不能为空")
  108. }
  109. o := orm.NewOrm()
  110. // 开始插入数据
  111. sql := "insert into z_task_data_" + T_task_id + "(t_sn,t_id,t_t,t_rh,t_time) select '" + Sn + "','" + T_id + "',t_t,t_rh,t_time from z_devicedata_" + Sn
  112. if len(Time_start) > 0 && len(Time_end) > 0 {
  113. sql = sql + " WHERE t_time >= '" + Time_start + "' AND t_time <= '" + Time_end + "'"
  114. }
  115. logs.Println(sql)
  116. _, err := o.Raw(sql).Exec()
  117. if err != nil {
  118. logs.Error(lib.FuncName(), err)
  119. return err
  120. }
  121. //// 强行替换 ID
  122. //sql = "UPDATE z_task_data_" + T_task_id + " SET `t_sn` = '" + Sn + "',`t_id` = '" + T_id + "' WHERE `t_sn` IS NULL"
  123. //logs.Println(sql)
  124. //_, err = o.Raw(sql).Exec()
  125. //if err != nil {
  126. // logs.Error(lib.FuncName(), err)
  127. // return err
  128. //}
  129. return nil
  130. }
  131. func Read_TaskData_Count(T_task_id string) int {
  132. var maps_z []orm2.ParamsList
  133. o := orm.NewOrm()
  134. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id
  135. logs.Println(sql)
  136. _, err := o.Raw(sql).ValuesList(&maps_z)
  137. if err != nil {
  138. logs.Error(lib.FuncName(), err)
  139. return 0
  140. }
  141. if len(maps_z) == 0 {
  142. return 0
  143. }
  144. key, _ := strconv.Atoi(maps_z[0][0].(string))
  145. return key
  146. }
  147. type TaskData_ struct {
  148. ID int `orm:"column(ID);size(100);null"` // ID
  149. T_sn string `orm:"column(t_sn);size(256);null"` // 标题
  150. T_id string `orm:"column(t_id);size(256);null"` // ID
  151. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  152. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  153. T_time string `orm:"column(t_times);null;"` // 采集时间
  154. T_Certificate_sn string `orm:"size(256);null"` // 证书编号
  155. }
  156. type TaskDataClass_ struct {
  157. T_sn string `orm:"column(t_sn);size(256);null"` // 标题
  158. T_id int `orm:"column(t_id);size(10);null"` // ID
  159. }
  160. func Read_TaskData_ById_List(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string, page int, page_z int) ([]TaskData_, int64) {
  161. o := orm.NewOrm()
  162. var maps []TaskData_
  163. var maps_z []orm2.ParamsList
  164. pagez := page_z
  165. var offset int
  166. if page <= 1 {
  167. offset = 0
  168. } else {
  169. page -= 1
  170. offset = page * pagez
  171. }
  172. sql_condition := ""
  173. if len(Time_start_) > 1 {
  174. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  175. }
  176. if len(Time_end_) > 1 {
  177. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  178. }
  179. if len(T_id) > 0 {
  180. sql_condition += " AND t_id = '" + T_id + "'"
  181. }
  182. if len(SN) > 0 {
  183. sql_condition += " AND t_sn = '" + SN + "'"
  184. }
  185. if len(sql_condition) > 0 {
  186. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  187. }
  188. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition
  189. logs.Println(sql)
  190. _, err := o.Raw(sql).ValuesList(&maps_z)
  191. if err != nil {
  192. logs.Error(lib.FuncName(), err)
  193. return maps, 0
  194. }
  195. if len(maps_z) == 0 {
  196. return maps, 0
  197. }
  198. //logs.Println("maps_z;",maps_z[0][0])
  199. sql = "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time DESC "
  200. if page_z != 9999 {
  201. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  202. }
  203. logs.Println(sql)
  204. _, err = o.Raw(sql).QueryRows(&maps)
  205. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  206. key, _ := strconv.Atoi(maps_z[0][0].(string))
  207. return maps, int64(key)
  208. }
  209. func Read_TaskData_ById_List_(T_task_id string, SN string) []TaskData_ {
  210. o := orm.NewOrm()
  211. var maps []TaskData_
  212. sql_condition := ""
  213. //sql_condition += " t_id = " + strconv.Itoa(T_id) + " AND "
  214. sql_condition += "t_sn = '" + SN + "'"
  215. //logs.Println("maps_z;",maps_z[0][0])
  216. sql := "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + " WHERE " + sql_condition + " ORDER BY t_time "
  217. logs.Println(sql)
  218. o.Raw(sql).QueryRows(&maps)
  219. return maps
  220. }
  221. func Read_TaskData_ById_ClassList(T_task_id string) []TaskDataClass_ {
  222. o := orm.NewOrm()
  223. var maps []TaskDataClass_
  224. //logs.Println("maps_z;",maps_z[0][0])
  225. sql := "SELECT DISTINCT t_sn FROM z_task_data_" + T_task_id + " ORDER BY t_sn"
  226. logs.Println(sql)
  227. o.Raw(sql).QueryRows(&maps)
  228. return maps
  229. }
  230. // 添加
  231. func Add_TaskData(T_task_id string, T_sn string, T_id string, T_t string, T_rh string, T_time string) bool {
  232. o := orm.NewOrm()
  233. // 开始插入数据
  234. //
  235. //sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_time`) " +
  236. // "VALUES ('" + T_sn + "', " + T_id + ", '" + T_time + "') " +
  237. // "ON DUPLICATE KEY UPDATE t_t=" + T_t + ", t_rh="+ T_rh +";"
  238. // 去重复数据
  239. //sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE " + " t_id = " + T_id + " AND "+ " t_sn = '"+T_sn+"' " + "AND t_time = '" + T_time + "' "
  240. //
  241. //// 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  242. //logs.Println(sql)
  243. //res, err := o.Raw(sql).Exec()
  244. //if err != nil {
  245. // logs.Error(lib.FuncName(),err)
  246. // return false
  247. //}
  248. //res.RowsAffected()
  249. sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) " +
  250. "VALUES ('" + T_sn + "', " + T_id + ", " + T_t + "," + T_rh + ", '" + T_time + "')"
  251. logs.Println(sql)
  252. res, err := o.Raw(sql).Exec()
  253. if err != nil {
  254. logs.Error(lib.FuncName(), err)
  255. return false
  256. }
  257. res.RowsAffected()
  258. //logs.Println("mysql row affected nums: ", num)
  259. return true
  260. }
  261. // 修改
  262. func Up_TaskData(T_task_id string, Id string, T_t string, T_rh string, T_time string) bool {
  263. o := orm.NewOrm()
  264. SET_str := " SET "
  265. if len(T_t) > 0 {
  266. SET_str = SET_str + " t_t = " + T_t + " ,"
  267. }
  268. if len(T_rh) > 0 {
  269. SET_str = SET_str + " t_rh = " + T_rh + " ,"
  270. }
  271. if len(T_time) > 0 {
  272. SET_str = SET_str + " t_time = '" + T_time + "' ,"
  273. }
  274. if len(SET_str) > 7 {
  275. SET_str = SET_str[:(len(SET_str) - 1)]
  276. }
  277. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  278. sql := "UPDATE z_task_data_" + T_task_id + " " + SET_str + " WHERE ID = " + Id
  279. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  280. //logs.Println(sql)
  281. res, err := o.Raw(sql).Exec()
  282. if err != nil {
  283. logs.Error(lib.FuncName(), err)
  284. return false
  285. }
  286. res.RowsAffected()
  287. //logs.Println("mysql row affected nums: ", num)
  288. return true
  289. }
  290. // 添加
  291. func Del_TaskData(T_task_id string, Id string) bool {
  292. o := orm.NewOrm()
  293. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  294. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE ID = " + Id
  295. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  296. logs.Println(sql)
  297. res, err := o.Raw(sql).Exec()
  298. if err != nil {
  299. logs.Error(lib.FuncName(), err)
  300. return false
  301. }
  302. res.RowsAffected()
  303. //logs.Println("mysql row affected nums: ", num)
  304. return true
  305. }
  306. // 添加
  307. func Del_TaskData_t_id(T_task_id string, Id string) bool {
  308. o := orm.NewOrm()
  309. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  310. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE t_id = " + Id
  311. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  312. logs.Println(sql)
  313. res, err := o.Raw(sql).Exec()
  314. if err != nil {
  315. logs.Error(lib.FuncName(), err)
  316. return false
  317. }
  318. res.RowsAffected()
  319. //logs.Println("mysql row affected nums: ", num)
  320. return true
  321. }
  322. func Del_TaskData_t_idByT_BindDeviceDataTime(T_task_id, Id, startTime, endTime string) bool {
  323. o := orm.NewOrm()
  324. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  325. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE t_id = " + Id + " AND NOT BETWEEN '" + startTime + "' AND '" + endTime + "'"
  326. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  327. logs.Println(sql)
  328. res, err := o.Raw(sql).Exec()
  329. if err != nil {
  330. logs.Error(lib.FuncName(), err)
  331. return false
  332. }
  333. res.RowsAffected()
  334. //logs.Println("mysql row affected nums: ", num)
  335. return true
  336. }