TaskData.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package Task
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "fmt"
  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. o := orm.NewOrm()
  75. // 开始插入数据
  76. 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
  77. sql = sql + " WHERE t_time >= '" + Time_start + "' AND t_time <= '" + Time_end + "'"
  78. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  79. logs.Println(sql)
  80. res, err := o.Raw(sql).Exec()
  81. if err != nil {
  82. logs.Error(lib.FuncName(), err)
  83. return false
  84. }
  85. // 强行替换 ID
  86. sql = "UPDATE z_task_data_" + T_task_id + " SET `t_sn` = '" + Sn + "',`t_id` = '" + T_id + "' WHERE `t_sn` IS NULL"
  87. // 设备 ID
  88. //sql = "UPDATE z_task_data_"+T_task_id+" SET `t_sn` = '"+Sn+"' WHERE `t_sn` IS NULL"
  89. logs.Println(sql)
  90. _, err = o.Raw(sql).Exec()
  91. if err != nil {
  92. logs.Error(lib.FuncName(), err)
  93. return false
  94. }
  95. res.RowsAffected()
  96. return true
  97. }
  98. func Import_TaskData_Back(Sn string, T_id string, T_task_id string, Time_start string, Time_end string) error {
  99. o := orm.NewOrm()
  100. // 开始插入数据
  101. 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
  102. if len(Time_start) > 0 && len(Time_end) > 0 {
  103. sql = sql + " WHERE t_time >= '" + Time_start + "' AND t_time <= '" + Time_end + "'"
  104. }
  105. logs.Println(sql)
  106. _, err := o.Raw(sql).Exec()
  107. if err != nil {
  108. logs.Error(lib.FuncName(), err)
  109. return err
  110. }
  111. //// 强行替换 ID
  112. //sql = "UPDATE z_task_data_" + T_task_id + " SET `t_sn` = '" + Sn + "',`t_id` = '" + T_id + "' WHERE `t_sn` IS NULL"
  113. //logs.Println(sql)
  114. //_, err = o.Raw(sql).Exec()
  115. //if err != nil {
  116. // logs.Error(lib.FuncName(), err)
  117. // return err
  118. //}
  119. return nil
  120. }
  121. func Read_TaskData_Count(T_task_id string) int {
  122. var maps_z []orm2.ParamsList
  123. o := orm.NewOrm()
  124. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id
  125. fmt.Println(sql)
  126. _, err := o.Raw(sql).ValuesList(&maps_z)
  127. if err != nil {
  128. logs.Error(lib.FuncName(), err)
  129. return 0
  130. }
  131. if len(maps_z) == 0 {
  132. return 0
  133. }
  134. key, _ := strconv.Atoi(maps_z[0][0].(string))
  135. return key
  136. }
  137. type TaskData_ struct {
  138. ID int `orm:"column(ID);size(100);null"` // ID
  139. T_sn string `orm:"column(t_sn);size(256);null"` // 标题
  140. T_id int `orm:"column(t_id);size(10);null"` // ID
  141. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  142. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  143. T_time string `orm:"column(t_times);null;"` // 采集时间
  144. }
  145. type TaskDataClass_ struct {
  146. T_sn string `orm:"column(t_sn);size(256);null"` // 标题
  147. T_id int `orm:"column(t_id);size(10);null"` // ID
  148. }
  149. 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) {
  150. o := orm.NewOrm()
  151. var maps []TaskData_
  152. var maps_z []orm2.ParamsList
  153. pagez := page_z
  154. var offset int
  155. if page <= 1 {
  156. offset = 0
  157. } else {
  158. page -= 1
  159. offset = page * pagez
  160. }
  161. sql_condition := ""
  162. if len(Time_start_) > 1 {
  163. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  164. }
  165. if len(Time_end_) > 1 {
  166. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  167. }
  168. if T_id != -1 {
  169. sql_condition += " AND t_id = " + strconv.Itoa(T_id)
  170. }
  171. if len(SN) > 0 {
  172. sql_condition += " AND t_sn = '" + SN + "'"
  173. }
  174. if len(sql_condition) > 0 {
  175. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  176. }
  177. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition
  178. fmt.Println(sql)
  179. _, err := o.Raw(sql).ValuesList(&maps_z)
  180. if err != nil {
  181. logs.Error(lib.FuncName(), err)
  182. return maps, 0
  183. }
  184. if len(maps_z) == 0 {
  185. return maps, 0
  186. }
  187. //fmt.Println("maps_z;",maps_z[0][0])
  188. sql = "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_times,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time DESC "
  189. if page_z != 9999 {
  190. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  191. }
  192. fmt.Println(sql)
  193. _, err = o.Raw(sql).QueryRows(&maps)
  194. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  195. key, _ := strconv.Atoi(maps_z[0][0].(string))
  196. return maps, int64(key)
  197. }
  198. func Read_TaskData_ById_List_(T_task_id string, SN string) []TaskData_ {
  199. o := orm.NewOrm()
  200. var maps []TaskData_
  201. sql_condition := ""
  202. //sql_condition += " t_id = " + strconv.Itoa(T_id) + " AND "
  203. sql_condition += "t_sn = '" + SN + "'"
  204. //fmt.Println("maps_z;",maps_z[0][0])
  205. sql := "SELECT ID,t_sn,t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i') AS t_times,t_time FROM z_task_data_" + T_task_id + " WHERE " + sql_condition + " ORDER BY t_time "
  206. fmt.Println(sql)
  207. o.Raw(sql).QueryRows(&maps)
  208. return maps
  209. }
  210. func Read_TaskData_ById_ClassList(T_task_id string) []TaskDataClass_ {
  211. o := orm.NewOrm()
  212. var maps []TaskDataClass_
  213. //fmt.Println("maps_z;",maps_z[0][0])
  214. sql := "SELECT DISTINCT t_sn FROM z_task_data_" + T_task_id + " ORDER BY t_sn"
  215. fmt.Println(sql)
  216. o.Raw(sql).QueryRows(&maps)
  217. return maps
  218. }
  219. // 添加
  220. func Add_TaskData(T_task_id string, T_sn string, T_id string, T_t string, T_rh string, T_time string) bool {
  221. o := orm.NewOrm()
  222. // 开始插入数据
  223. //
  224. //sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_time`) " +
  225. // "VALUES ('" + T_sn + "', " + T_id + ", '" + T_time + "') " +
  226. // "ON DUPLICATE KEY UPDATE t_t=" + T_t + ", t_rh="+ T_rh +";"
  227. // 去重复数据
  228. //sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE " + " t_id = " + T_id + " AND "+ " t_sn = '"+T_sn+"' " + "AND t_time = '" + T_time + "' "
  229. //
  230. //// 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  231. //fmt.Println(sql)
  232. //res, err := o.Raw(sql).Exec()
  233. //if err != nil {
  234. // logs.Error(lib.FuncName(),err)
  235. // return false
  236. //}
  237. //res.RowsAffected()
  238. sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) " +
  239. "VALUES ('" + T_sn + "', " + T_id + ", " + T_t + "," + T_rh + ", '" + T_time + "')"
  240. fmt.Println(sql)
  241. res, err := o.Raw(sql).Exec()
  242. if err != nil {
  243. logs.Error(lib.FuncName(), err)
  244. return false
  245. }
  246. res.RowsAffected()
  247. //fmt.Println("mysql row affected nums: ", num)
  248. return true
  249. }
  250. // 修改
  251. func Up_TaskData(T_task_id string, Id string, T_t string, T_rh string, T_time string) bool {
  252. o := orm.NewOrm()
  253. SET_str := " SET "
  254. if len(T_t) > 0 {
  255. SET_str = SET_str + " t_t = " + T_t + " ,"
  256. }
  257. if len(T_rh) > 0 {
  258. SET_str = SET_str + " t_rh = " + T_rh + " ,"
  259. }
  260. if len(T_time) > 0 {
  261. SET_str = SET_str + " t_time = '" + T_time + "' ,"
  262. }
  263. if len(SET_str) > 7 {
  264. SET_str = SET_str[:(len(SET_str) - 1)]
  265. }
  266. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  267. sql := "UPDATE z_task_data_" + T_task_id + " " + SET_str + " WHERE ID = " + Id
  268. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  269. //fmt.Println(sql)
  270. res, err := o.Raw(sql).Exec()
  271. if err != nil {
  272. logs.Error(lib.FuncName(), err)
  273. return false
  274. }
  275. res.RowsAffected()
  276. //fmt.Println("mysql row affected nums: ", num)
  277. return true
  278. }
  279. // 添加
  280. func Del_TaskData(T_task_id string, Id string) bool {
  281. o := orm.NewOrm()
  282. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  283. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE ID = " + Id
  284. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  285. logs.Println(sql)
  286. res, err := o.Raw(sql).Exec()
  287. if err != nil {
  288. logs.Error(lib.FuncName(), err)
  289. return false
  290. }
  291. res.RowsAffected()
  292. //fmt.Println("mysql row affected nums: ", num)
  293. return true
  294. }
  295. // 添加
  296. func Del_TaskData_t_id(T_task_id string, Id string) bool {
  297. o := orm.NewOrm()
  298. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  299. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE t_id = " + Id
  300. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  301. logs.Println(sql)
  302. res, err := o.Raw(sql).Exec()
  303. if err != nil {
  304. logs.Error(lib.FuncName(), err)
  305. return false
  306. }
  307. res.RowsAffected()
  308. //fmt.Println("mysql row affected nums: ", num)
  309. return true
  310. }