TaskData.go 11 KB

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