TaskData.go 11 KB

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