TaskData.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. package Task
  2. import (
  3. "ColdVerify_local/conf"
  4. "ColdVerify_local/lib"
  5. "ColdVerify_local/logs"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  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. _ "github.com/go-sql-driver/mysql"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. const (
  19. Temperature = "Temperature"
  20. Humidity = "Humidity"
  21. )
  22. // 模板
  23. type TaskData struct {
  24. Id int `orm:"column(ID);size(11);auto;pk"`
  25. T_id string `orm:"size(256);null"` // 标题
  26. T_t float32 `orm:"size(10);null"` // 温度
  27. T_rh float32 `orm:"size(10);null"` // 湿度
  28. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  29. 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 保存时都会对时间自动更新
  30. }
  31. func (t *TaskData) TableName() string {
  32. return "task_data" // 数据库名称 // ************** 替换 FormulaList **************
  33. }
  34. var redisCache_TaskData cache.Cache
  35. func init() {
  36. //orm2.Debug = true
  37. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  38. "redis_"+"TaskData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  39. logs.Println(config)
  40. var err error
  41. redisCache_TaskData, err = cache.NewCache("redis", config)
  42. if err != nil || redisCache_TaskData == nil {
  43. errMsg := "failed to init redis"
  44. logs.Println(errMsg, err)
  45. }
  46. }
  47. type TaskDataJPG struct {
  48. State int `json:"state"` //1:生成中 2:已完成 3:失败
  49. Msg string `json:"msg"`
  50. Url string `json:"url"` //url
  51. }
  52. // ---------------- Redis -------------------
  53. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  54. func Redis_TaskDataJPG_Set(key string, r TaskDataJPG) (err error) {
  55. //json序列化
  56. str, err := json.Marshal(r)
  57. if err != nil {
  58. logs.Error(lib.FuncName(), err)
  59. return
  60. }
  61. err = redisCache_TaskData.Put(key, str, 5*time.Minute)
  62. if err != nil {
  63. logs.Println("set key:", key, ",value:", str, err)
  64. }
  65. return
  66. }
  67. // if r,is :=Redis_Get(T_sn);is{
  68. // return r,nil
  69. // }
  70. func Redis_TaskDataJPG_Get(key string) (r TaskDataJPG, is bool) {
  71. if redisCache_TaskData.IsExist(key) {
  72. logs.Println("找到key:", key)
  73. v := redisCache_TaskData.Get(key)
  74. json.Unmarshal(v.([]byte), &r)
  75. return r, true
  76. }
  77. logs.Println("没有 找到key:", key)
  78. return TaskDataJPG{}, false
  79. }
  80. func Redis_TaskDataJPG_Del(key string) (err error) {
  81. err = redisCache_TaskData.Delete(key)
  82. return
  83. }
  84. // 创建数据库 Device.CREATE_TaskData("")
  85. func CREATE_TaskData(alias_name, T_task_id string) bool {
  86. o := orm2.NewOrmUsingDB(alias_name)
  87. sql := "DROP TABLE IF EXISTS `z_task_data_" + T_task_id + "`"
  88. o.Raw(sql).Exec()
  89. sql = "CREATE TABLE IF NOT EXISTS `z_task_data_" + T_task_id + "` ( " +
  90. " `ID` int(11) NOT NULL AUTO_INCREMENT," +
  91. " `t_sn` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
  92. " `t_id` varchar(256) DEFAULT NULL," +
  93. " `t_t` float(6, 1) NULL DEFAULT NULL," +
  94. " `t_rh` float(6, 1) NULL DEFAULT NULL," +
  95. " `t_time` datetime(0) NULL DEFAULT NULL," +
  96. " PRIMARY KEY (`ID`) USING BTREE," +
  97. " KEY `t_sn` (`t_sn`)," +
  98. " KEY `t_id` (`t_id`)," +
  99. " KEY `t_time` (`t_time`)" +
  100. ") ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"
  101. _, err := o.Raw(sql).Exec()
  102. if err != nil {
  103. return false
  104. }
  105. //sqlIndex := "ALTER TABLE `z_task_data_" + T_task_id + "` add unique index(`t_sn`,`t_id`,`t_time`);"
  106. //logs.Debug(sqlIndex)
  107. //o.Raw(sqlIndex).Exec()
  108. return true
  109. }
  110. // ---------------- 特殊方法 -------------------
  111. // 清空
  112. func Truncate_TaskData(alias_name, T_task_id string) bool {
  113. o := orm2.NewOrmUsingDB(alias_name)
  114. sql := "truncate table z_task_data_" + T_task_id
  115. logs.Println(sql)
  116. _, err := o.Raw(sql).Exec()
  117. if err != nil {
  118. // 数据库表不存在,则创建数据库
  119. if err.(*mysql.MySQLError).Number == 1146 {
  120. CREATE_TaskData(alias_name, T_task_id)
  121. return true
  122. }
  123. logs.Error(lib.FuncName(), err)
  124. return false
  125. }
  126. return true
  127. }
  128. type TaskData_ struct {
  129. ID int `orm:"column(ID);size(100);null"` // ID
  130. T_sn string `orm:"column(t_sn);size(256);null"` // sn
  131. T_id string `orm:"column(t_id);size(256);null"` // 标题
  132. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  133. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  134. T_time string `orm:"column(t_times);null;"` // 采集时间
  135. T_Certificate_sn string `orm:"size(256);null"` // 证书编号
  136. }
  137. type TaskDataClass_ struct {
  138. T_sn string `orm:"column(t_sn);size(256);null"` // 标题
  139. T_id string `orm:"column(t_id);size(256);null"` // 名称
  140. }
  141. 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) {
  142. o := orm.NewOrm()
  143. var maps []TaskData_
  144. var maps_z []orm2.ParamsList
  145. pagez := page_z
  146. var offset int
  147. if page <= 1 {
  148. offset = 0
  149. } else {
  150. page -= 1
  151. offset = page * pagez
  152. }
  153. sql_condition := ""
  154. if len(Time_start_) > 1 {
  155. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  156. }
  157. if len(Time_end_) > 1 {
  158. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  159. }
  160. if len(T_id) > 0 {
  161. sql_condition += " AND t_id = '" + T_id + "'"
  162. }
  163. if len(SN) > 0 {
  164. sql_condition += " AND t_sn = '" + SN + "'"
  165. }
  166. if len(sql_condition) > 0 {
  167. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  168. }
  169. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition
  170. fmt.Println(sql)
  171. _, err := o.Raw(sql).ValuesList(&maps_z)
  172. if err != nil {
  173. return maps, 0
  174. }
  175. if len(maps_z) == 0 {
  176. return maps, 0
  177. }
  178. //fmt.Println("maps_z;",maps_z[0][0])
  179. 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"
  180. if page_z != 9999 {
  181. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  182. }
  183. fmt.Println(sql)
  184. _, err = o.Raw(sql).QueryRows(&maps)
  185. if err != nil {
  186. logs.Error(lib.FuncName(), err)
  187. }
  188. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  189. key, _ := strconv.Atoi(maps_z[0][0].(string))
  190. return maps, int64(key)
  191. }
  192. func Read_TaskData_ById_List_AES(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string, page int, page_z int) ([]TaskData_, int64) {
  193. o := orm.NewOrm()
  194. var maps []TaskData_
  195. var maps_z []orm2.ParamsList
  196. pagez := page_z
  197. var offset int
  198. if page <= 1 {
  199. offset = 0
  200. } else {
  201. page -= 1
  202. offset = page * pagez
  203. }
  204. sql_condition := ""
  205. if len(Time_start_) > 1 {
  206. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  207. }
  208. if len(Time_end_) > 1 {
  209. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  210. }
  211. if len(T_id) > 0 {
  212. sql_condition += " AND t_id = '" + T_id + "'"
  213. }
  214. if len(SN) > 0 {
  215. sql_condition += " AND t_sn = '" + SN + "'"
  216. }
  217. if len(sql_condition) > 0 {
  218. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  219. }
  220. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id + sql_condition
  221. fmt.Println(sql)
  222. _, err := o.Raw(sql).ValuesList(&maps_z)
  223. if err != nil {
  224. return maps, 0
  225. }
  226. if len(maps_z) == 0 {
  227. return maps, 0
  228. }
  229. //fmt.Println("maps_z;",maps_z[0][0])
  230. 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"
  231. if page_z != 9999 {
  232. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  233. }
  234. fmt.Println(sql)
  235. _, err = o.Raw(sql).QueryRows(&maps)
  236. if err != nil {
  237. logs.Error(lib.FuncName(), err)
  238. }
  239. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  240. key, _ := strconv.Atoi(maps_z[0][0].(string))
  241. return maps, int64(key)
  242. }
  243. func Read_TaskData_ById_List_(T_task_id string, SN string) []TaskData_ {
  244. o := orm.NewOrm()
  245. var maps []TaskData_
  246. sql_condition := ""
  247. sql_condition += " t_sn ='" + SN + "'"
  248. //fmt.Println("maps_z;",maps_z[0][0])
  249. 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 "
  250. fmt.Println(sql)
  251. _, err := o.Raw(sql).QueryRows(&maps)
  252. if err != nil {
  253. logs.Error(lib.FuncName(), err)
  254. return maps
  255. }
  256. return maps
  257. }
  258. func Read_TaskData_ById_ClassList(T_task_id string) []TaskDataClass_ {
  259. o := orm.NewOrm()
  260. var maps []TaskDataClass_
  261. //fmt.Println("maps_z;",maps_z[0][0])
  262. //sql := "SELECT DISTINCT t_sn,t_id FROM z_task_data_" + T_task_id + " ORDER BY t_id "
  263. //sql := "SELECT DISTINCT t_sn FROM z_task_data_" + T_task_id + " ORDER BY t_id "
  264. sql := "SELECT t_sn,t_id FROM z_task_data_" + T_task_id + " GROUP BY t_sn,t_id ORDER BY t_id+0 "
  265. fmt.Println(sql)
  266. _, err := o.Raw(sql).QueryRows(&maps)
  267. if err != nil {
  268. logs.Error(lib.FuncName(), err)
  269. return maps
  270. }
  271. return maps
  272. }
  273. func Read_TaskData_sn(T_task_id string) []TaskDataClass_ {
  274. o := orm.NewOrm()
  275. var maps []TaskDataClass_
  276. sql := "SELECT DISTINCT t_sn FROM z_task_data_" + T_task_id + " ORDER BY t_sn "
  277. fmt.Println(sql)
  278. _, err := o.Raw(sql).QueryRows(&maps)
  279. if err != nil {
  280. logs.Error(lib.FuncName(), err)
  281. return maps
  282. }
  283. return maps
  284. }
  285. func Update_TaskData_ByT_sn(T_task_id, T_sn, T_id string) error {
  286. o := orm.NewOrm()
  287. // 修改id
  288. sql := "UPDATE z_task_data_" + T_task_id + " SET `t_id` = '" + T_id + "' WHERE `t_sn` = '" + T_sn + "'"
  289. logs.Println(sql)
  290. _, err := o.Raw(sql).Exec()
  291. if err != nil {
  292. logs.Error(lib.FuncName(), err)
  293. return err
  294. }
  295. return nil
  296. }
  297. func Delete_TaskData_ByT_sn(T_task_id, T_sn string) error {
  298. o := orm.NewOrm()
  299. // 修改id
  300. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE `t_sn` = '" + T_sn + "'"
  301. logs.Println(sql)
  302. _, err := o.Raw(sql).Exec()
  303. if err != nil {
  304. logs.Error(lib.FuncName(), err)
  305. return err
  306. }
  307. return nil
  308. }
  309. // 添加
  310. func Add_TaskData(T_task_id string, T_sn string, T_id string, T_t string, T_rh string, T_time string) bool {
  311. o := orm2.NewOrm()
  312. tx, err := o.Begin()
  313. if err != nil {
  314. logs.Error("start the transaction failed")
  315. return false
  316. }
  317. // 开始插入数据
  318. //
  319. //sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_time`) " +
  320. // "VALUES ('" + T_sn + "', " + T_id + ", '" + T_time + "') " +
  321. // "ON DUPLICATE KEY UPDATE t_t=" + T_t + ", t_rh="+ T_rh +";"
  322. // 去重复数据
  323. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE " + " t_id = '" + T_id + "' AND " + " t_sn = '" + T_sn + "' " + "AND t_time = '" + T_time + "' "
  324. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  325. //fmt.Println(sql)
  326. res, err := tx.Raw(sql).Exec()
  327. if err != nil {
  328. tx.Rollback()
  329. logs.Error(lib.FuncName(), err)
  330. return false
  331. }
  332. res.RowsAffected()
  333. sql = "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) " +
  334. "VALUES ('" + T_sn + "', '" + T_id + "', " + T_t + "," + T_rh + ", '" + T_time + "')"
  335. // 更新数据
  336. //sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) " +
  337. // "VALUES ('" + T_sn + "', " + T_id + ", " + T_t + "," + T_rh + ", '" + T_time + "')" +
  338. // "on duplicate key update `t_t`=" + T_t + ",`t_rh`=" + T_rh
  339. //fmt.Println(sql)
  340. res, err = tx.Raw(sql).Exec()
  341. if err != nil {
  342. tx.Rollback()
  343. logs.Error(lib.FuncName(), err)
  344. return false
  345. }
  346. tx.Commit()
  347. //fmt.Println("mysql row affected nums: ", num)
  348. return true
  349. }
  350. // 创建唯一索引
  351. func Create_Unique_Index(T_task_id string) error {
  352. // 创建索引
  353. o := orm.NewOrm()
  354. createIndexSql := "ALTER TABLE z_task_data_" + T_task_id + " ADD CONSTRAINT unique_index_t_sn_t_id_t_time UNIQUE (t_sn, t_id, t_time);"
  355. //fmt.Println(sql)
  356. _, err := o.Raw(createIndexSql).Exec()
  357. if err != nil && !strings.Contains(err.Error(), "Duplicate key name 'unique_index_t_sn_t_id_t_time'") {
  358. logs.Error(lib.FuncName(), err)
  359. return err
  360. }
  361. return nil
  362. }
  363. // 删除唯一索引
  364. func Delete_Unique_Index(T_task_id string) error {
  365. // 创建索引
  366. o := orm.NewOrm()
  367. // 删除索引
  368. deleteIndexSql := "ALTER TABLE z_task_data_" + T_task_id + " DROP INDEX unique_index_t_sn_t_id_t_time;"
  369. //fmt.Println(sql)
  370. _, err := o.Raw(deleteIndexSql).Exec()
  371. if err != nil {
  372. logs.Error(lib.FuncName(), err)
  373. return err
  374. }
  375. return nil
  376. }
  377. func Adds_TaskData(T_task_id string, valueStrings []string) error {
  378. o := orm.NewOrm()
  379. // 插入数据
  380. sql := "INSERT INTO z_task_data_" + T_task_id + " (`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) VALUES"
  381. sql += strings.Join(valueStrings, ",")
  382. sql += " ON DUPLICATE KEY UPDATE t_t = VALUES(t_t), t_rh = VALUES(t_rh)"
  383. //fmt.Println(sql)
  384. _, err := o.Raw(sql).Exec()
  385. if err != nil {
  386. logs.Error(lib.FuncName(), err)
  387. return err
  388. }
  389. //fmt.Println("mysql row affected nums: ", num)
  390. return nil
  391. }
  392. // 修改
  393. func Up_TaskData(T_task_id string, Id string, T_t string, T_rh string, T_time string) bool {
  394. o := orm.NewOrm()
  395. SET_str := " SET "
  396. if len(T_t) > 0 {
  397. SET_str = SET_str + " t_t = " + T_t + " ,"
  398. }
  399. if len(T_rh) > 0 {
  400. SET_str = SET_str + " t_rh = " + T_rh + " ,"
  401. }
  402. if len(T_time) > 0 {
  403. SET_str = SET_str + " t_time = '" + T_time + "' ,"
  404. }
  405. if len(SET_str) > 7 {
  406. SET_str = SET_str[:(len(SET_str) - 1)]
  407. }
  408. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  409. sql := "UPDATE z_task_data_" + T_task_id + " " + SET_str + " WHERE ID = " + Id
  410. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  411. //fmt.Println(sql)
  412. res, err := o.Raw(sql).Exec()
  413. if err != nil {
  414. logs.Error(lib.FuncName(), err)
  415. return false
  416. }
  417. res.RowsAffected()
  418. //fmt.Println("mysql row affected nums: ", num)
  419. return true
  420. }
  421. // 删除
  422. func Del_TaskData(T_task_id string, Id string) bool {
  423. o := orm.NewOrm()
  424. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  425. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE ID = " + Id
  426. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  427. logs.Println(sql)
  428. res, err := o.Raw(sql).Exec()
  429. if err != nil {
  430. logs.Error(lib.FuncName(), err)
  431. return false
  432. }
  433. res.RowsAffected()
  434. //fmt.Println("mysql row affected nums: ", num)
  435. return true
  436. }
  437. func Read_TaskData_ByT_id(T_task_id string, Id string) (t_sn string, err error) {
  438. o := orm.NewOrm()
  439. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  440. sql := "SELECT t_sn FROM z_task_data_" + T_task_id + " WHERE t_id = '" + Id + "'"
  441. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  442. logs.Println(sql)
  443. err = o.Raw(sql).QueryRow(&t_sn)
  444. if err != nil {
  445. logs.Error(lib.FuncName(), err)
  446. return t_sn, err
  447. }
  448. //fmt.Println("mysql row affected nums: ", num)
  449. return t_sn, err
  450. }
  451. func Read_TaskData_ByT_sn(T_task_id string, sn string) (t_id string, err error) {
  452. o := orm.NewOrm()
  453. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  454. sql := "SELECT t_id FROM z_task_data_" + T_task_id + " WHERE t_sn = '" + sn + "'"
  455. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  456. logs.Println(sql)
  457. err = o.Raw(sql).QueryRow(&t_id)
  458. if err != nil {
  459. logs.Error(lib.FuncName(), err)
  460. return t_id, err
  461. }
  462. //fmt.Println("mysql row affected nums: ", num)
  463. return t_id, err
  464. }
  465. // 添加
  466. func Del_TaskData_t_id(T_task_id string, Id string) bool {
  467. o := orm.NewOrm()
  468. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  469. sql := "DELETE FROM z_task_data_" + T_task_id + " WHERE t_id = '" + Id + "'"
  470. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  471. logs.Println(sql)
  472. res, err := o.Raw(sql).Exec()
  473. if err != nil {
  474. logs.Error(lib.FuncName(), err)
  475. return false
  476. }
  477. res.RowsAffected()
  478. //fmt.Println("mysql row affected nums: ", num)
  479. return true
  480. }
  481. // 检查导出表总数
  482. func Check_TaskData_Num(T_task_id string) bool {
  483. o1 := orm2.NewOrmUsingDB(conf.Local_AliasName)
  484. o2 := orm2.NewOrmUsingDB(conf.Server_AliasName)
  485. var maps_z1, maps_z2 []orm2.ParamsList
  486. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id
  487. fmt.Println(sql)
  488. _, err := o1.Raw(sql).ValuesList(&maps_z1)
  489. if err != nil {
  490. return false
  491. }
  492. _, err = o2.Raw(sql).ValuesList(&maps_z2)
  493. if err != nil {
  494. return false
  495. }
  496. if len(maps_z1) != len(maps_z2) {
  497. return false
  498. }
  499. return true
  500. }
  501. // 检查导出表总数
  502. func Check_TaskData_Num_Verify1(T_task_id string) bool {
  503. o1 := orm2.NewOrmUsingDB(conf.Local_AliasName)
  504. o2 := orm2.NewOrmUsingDB(conf.Verify1_AliasName)
  505. var maps_z1, maps_z2 []orm2.ParamsList
  506. sql := "SELECT COUNT(ID) FROM z_task_data_" + strings.ToLower(T_task_id)
  507. fmt.Println(sql)
  508. _, err := o1.Raw(sql).ValuesList(&maps_z1)
  509. if err != nil {
  510. return false
  511. }
  512. sql2 := "SELECT COUNT(ID) FROM Z_TaskData_" + T_task_id
  513. _, err = o2.Raw(sql2).ValuesList(&maps_z2)
  514. if err != nil {
  515. return false
  516. }
  517. if len(maps_z1) != len(maps_z2) {
  518. return false
  519. }
  520. return true
  521. }
  522. func Read_TaskData_Num(T_task_id string) int {
  523. o1 := orm2.NewOrmUsingDB(conf.Local_AliasName)
  524. var maps_z []orm2.ParamsList
  525. sql := "SELECT COUNT(ID) FROM z_task_data_" + T_task_id
  526. fmt.Println(sql)
  527. _, err := o1.Raw(sql).ValuesList(&maps_z)
  528. if err != nil {
  529. logs.Error(lib.FuncName(), err)
  530. return 0
  531. }
  532. key, _ := strconv.Atoi(maps_z[0][0].(string))
  533. return key
  534. }
  535. func Dump_TaskData(T_task_id, root, password, url_port, database, sql_file string) (string, error) {
  536. // url_port 127.0.0.1:3306
  537. // mysql8.0 以上加 --column-statistics=0
  538. // mysqldump --column-statistics=0 -uroot -proot -h127.0.0.1 -P3306 cold_verify Z_TaskData_ixEfo5zk2Oeb > /Data/Z_TaskData_ixEfo5zk2Oeb.sql
  539. v := Read_Local_Mysql_Version()
  540. host_port := strings.Split(url_port, ":")
  541. table_name := "z_task_data_" + T_task_id
  542. org := "mysqldump "
  543. if v >= 8 {
  544. org += "--column-statistics=0 "
  545. }
  546. //--no-create-info 只导出数据,而不添加 CREATE TABLE 语句。
  547. //--single-transaction 在备份库的时候并不锁定数据表
  548. //--add-locks:在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(默认为打开状态,使用--skip-add-locks取消选项)
  549. //--compact:压缩模式,产生更少的输出;
  550. org = org + fmt.Sprintf("-u%s -p%s -h%s -P%s --no-create-info --set-gtid-purged=OFF --skip-add-locks --compact --quick %s %s > %s ",
  551. root, password, host_port[0], host_port[1], database, table_name, sql_file)
  552. logs.Println(org)
  553. _, err := lib.Command(org)
  554. if err != nil {
  555. logs.Error(lib.FuncName(), err)
  556. }
  557. return org, err
  558. }
  559. func Dump_TaskData_Verify(T_task_id, root, password, url_port, database, sql_file string) (string, error) {
  560. // url_port 127.0.0.1:3306
  561. // mysql8.0 以上加 --column-statistics=0
  562. // mysqldump --column-statistics=0 -uroot -proot -h127.0.0.1 -P3306 cold_verify Z_TaskData_ixEfo5zk2Oeb > /Data/Z_TaskData_ixEfo5zk2Oeb.sql
  563. v := Read_Local_Mysql_Version()
  564. host_port := strings.Split(url_port, ":")
  565. table_name := "Z_TaskData_" + T_task_id
  566. org := "mysqldump "
  567. if v >= 8 {
  568. org += "--column-statistics=0 "
  569. }
  570. //--no-create-info 只导出数据,而不添加 CREATE TABLE 语句。
  571. //--single-transaction 在备份库的时候并不锁定数据表
  572. //--add-locks:在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(默认为打开状态,使用--skip-add-locks取消选项)
  573. //--compact:压缩模式,产生更少的输出;
  574. org = org + fmt.Sprintf("-u%s -p%s -h%s -P%s --no-create-info --set-gtid-purged=OFF --skip-add-locks --compact --quick %s %s > %s ",
  575. root, password, host_port[0], host_port[1], database, table_name, sql_file)
  576. logs.Println(org)
  577. _, err := lib.Command(org)
  578. if err != nil {
  579. logs.Error(lib.FuncName(), err)
  580. }
  581. return org, err
  582. }
  583. func Insert_TaskData(root, password, url_port, database, sql_file string) (string, error) {
  584. // url_port 127.0.0.1:3306
  585. // mysql -u root -p root -h127.0.0.1 -P3306 cold_verify_local < /data/Z_TaskData_ixEfo5zk2Oeb.sql
  586. host_port := strings.Split(url_port, ":")
  587. org := fmt.Sprintf("mysql -u%s -p%s -h%s -P%s %s < %s",
  588. root, password, host_port[0], host_port[1], database, sql_file)
  589. logs.Println(org)
  590. _, err := lib.Command(org)
  591. if err != nil {
  592. logs.Error(lib.FuncName(), err)
  593. }
  594. return org, err
  595. }
  596. func Read_Local_Mysql_Version() int {
  597. o := orm2.NewOrmUsingDB(conf.Local_AliasName)
  598. var params []orm2.Params
  599. o.Raw("select version();").Values(&params)
  600. version, _ := strconv.Atoi(params[0]["version()"].(string)[0:1])
  601. return version
  602. }
  603. func Import_TaskData(T_task_id string, offset, pagez int) bool {
  604. var maps []TaskData_
  605. serverOrm := orm2.NewOrmUsingDB(conf.Server_AliasName)
  606. localOrm := orm2.NewOrmUsingDB(conf.Local_AliasName)
  607. tb_name := "z_task_data_" + T_task_id
  608. // 开从先上版查询数据
  609. sql := "select `ID`, `t_sn`, `t_id`, `t_t`, `t_rh`, DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_times from " + tb_name + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  610. logs.Println(sql)
  611. _, err := serverOrm.Raw(sql).QueryRows(&maps)
  612. if err != nil {
  613. logs.Error(lib.FuncName(), err)
  614. return false
  615. }
  616. values := []string{}
  617. for _, row := range maps {
  618. // 处理数据
  619. values = append(values, fmt.Sprintf("('%s',%d,%v,%v,'%s')", row.T_sn, row.T_id, row.T_t, row.T_rh, row.T_time))
  620. }
  621. // 向本地版插入数据
  622. sql = "INSERT IGNORE INTO " + tb_name + "( `t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`) VALUES " + strings.Join(values, ",")
  623. //logs.Println(sql)
  624. _, err = localOrm.Raw(sql).Exec()
  625. if err != nil {
  626. logs.Error(lib.FuncName(), err)
  627. return false
  628. }
  629. return true
  630. }
  631. // InsertTaskData 添加设备数据
  632. func InsertTaskData(T_task_id string, d TaskData_) {
  633. o := orm.NewOrm()
  634. var maps TaskData_
  635. sql := fmt.Sprintf("SELECT * FROM z_task_data_%s where t_time='%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_time, d.T_id, d.T_sn)
  636. err := o.Raw(sql).QueryRow(&maps)
  637. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  638. logs.Error(lib.FuncName(), err)
  639. }
  640. // 添加
  641. if maps.ID == 0 {
  642. sqlStatement := fmt.Sprintf("insert into z_task_data_%s(t_sn, t_id, t_t, t_rh, t_time) value('%s','%s',%f,%f,'%s')", T_task_id, d.T_sn, d.T_id, d.T_t, d.T_rh, d.T_time)
  643. _, err = o.Raw(sqlStatement).Exec()
  644. if err != nil {
  645. logs.Error(lib.FuncName(), err)
  646. }
  647. return
  648. }
  649. //已经存在该条目,更新
  650. sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = %f,t_rh = %f where t_time= '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, d.T_t, d.T_rh, d.T_time, d.T_id, d.T_sn)
  651. _, err = o.Raw(sqlStatement).Exec()
  652. if err != nil {
  653. logs.Error(lib.FuncName(), err)
  654. }
  655. }
  656. // DeleteTaskDataByTimeRange 删除时间范围内的数据
  657. func DeleteTaskDataByTimeRange(T_task_id, sn string, id string, start, end string) {
  658. sqlStatement := fmt.Sprintf("delete from z_task_data_%s where t_time between '%s' and '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, start, end, id, sn)
  659. o := orm.NewOrm()
  660. exec, err := o.Raw(sqlStatement).Exec()
  661. if err != nil {
  662. fmt.Println(err.Error())
  663. }
  664. affected, _ := exec.RowsAffected()
  665. fmt.Printf("从%s~%s时间段删除了%d条数据", start, end, affected)
  666. }
  667. // UpdateTaskDataTemperatureAndHumidityRandom 随机更新
  668. func UpdateTaskDataTemperatureAndHumidityRandom(T_task_id, sn, id, startTime, endTime string, ttMax, ttMin, trhMax, trhMin int) {
  669. //sql语句
  670. sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t + FLOOR(%d + RAND() * (%d - %d + 1)) /100.0, t_rh = t_rh + FLOOR(%d + RAND() * (%d - %d + 1)) /100.0 where t_time BETWEEN '%s' AND '%s' and t_id = '%s' and t_sn = '%s'", T_task_id, ttMin, ttMax, ttMin, trhMin, trhMax, trhMin, startTime, endTime, id, sn)
  671. o := orm.NewOrm()
  672. exec, err := o.Raw(sqlStatement).Exec()
  673. if err != nil {
  674. fmt.Println("执行错误:", sqlStatement)
  675. }
  676. affected, _ := exec.RowsAffected()
  677. fmt.Println("执行更新行数:", affected)
  678. }
  679. // UpdateTaskDataTemperatureAndHumidity 更新设备探头数据温湿度
  680. func UpdateTaskDataTemperatureAndHumidity(T_task_id, sn, id, startTime, endTime string, temperature, humidity float64) {
  681. sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t + %f , t_rh = t_rh + %f where t_id = '%s' and t_sn = '%s' and t_time BETWEEN '%s' and '%s'", T_task_id, temperature, humidity, id, sn, startTime, endTime)
  682. o := orm.NewOrm()
  683. exec, err := o.Raw(sqlStatement).Exec()
  684. if err != nil {
  685. fmt.Println(err.Error())
  686. }
  687. affected, err := exec.RowsAffected()
  688. if err != nil {
  689. fmt.Println(err.Error())
  690. }
  691. fmt.Printf("影响了%d行\n", affected)
  692. }
  693. // UpdateTaskData 更新设备探头数据
  694. func UpdateTaskData(T_task_id, sn, id string, old, newO TaskData_) {
  695. sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = %f , t_rh = %f where t_sn = '%s' and t_id = '%s' and t_t = %f and t_rh = %f and t_time= '%s'", T_task_id, newO.T_t, newO.T_rh, sn, id, old.T_t, old.T_rh, old.T_time)
  696. fmt.Println("执行SQL:", sqlStatement)
  697. o := orm.NewOrm()
  698. exec, err := o.Raw(sqlStatement).Exec()
  699. if err != nil {
  700. fmt.Println("执行错误:", sqlStatement, err.Error())
  701. }
  702. affected, _ := exec.RowsAffected()
  703. fmt.Println("执行更新行数:", affected)
  704. }
  705. // 查询温度最小值 最大值
  706. func Read_TaskData_T_Min_Max(T_task_id string, SN string, T_id string, Time_start_ string, Time_end_ string) (minT, maxT float64) {
  707. o := orm.NewOrm()
  708. sql_condition := ""
  709. if len(Time_start_) > 1 {
  710. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  711. }
  712. if len(Time_end_) > 1 {
  713. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  714. }
  715. if len(T_id) > 0 {
  716. sql_condition += " AND t_id = '" + T_id + "'"
  717. }
  718. if len(SN) > 0 {
  719. sql_condition += " AND t_sn = '" + SN + "'"
  720. }
  721. if len(sql_condition) > 0 {
  722. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  723. }
  724. //fmt.Println("maps_z;",maps_z[0][0])
  725. sql := "SELECT MIN(t_t) AS min_t, MAX(t_t) AS max_t FROM z_task_data_" + T_task_id + sql_condition
  726. fmt.Println(sql)
  727. err := o.Raw(sql).QueryRow(&minT, &maxT)
  728. if err != nil {
  729. logs.Error(lib.FuncName(), err)
  730. }
  731. return
  732. }
  733. // UpdateTaskDataTemperatureAndHumidityByGeometric 更新设备探头数据温湿度 等比缩放
  734. func UpdateTaskDataTemperatureAndHumidityByGeometric(T_task_id, sn, id, startTime, endTime string, temperature, humidity float64) {
  735. sqlStatement := fmt.Sprintf("update z_task_data_%s set t_t = t_t * %f , t_rh = t_rh * %f where t_id = '%s' and t_sn = '%s' and t_time BETWEEN '%s' and '%s'", T_task_id, temperature, humidity, id, sn, startTime, endTime)
  736. o := orm.NewOrm()
  737. exec, err := o.Raw(sqlStatement).Exec()
  738. if err != nil {
  739. fmt.Println(err.Error())
  740. }
  741. affected, err := exec.RowsAffected()
  742. if err != nil {
  743. fmt.Println(err.Error())
  744. }
  745. fmt.Printf("影响了%d行\n", affected)
  746. }
  747. func Read_TaskData_T_Min_Max_Time_Min_Max(T_task_id string, SN []string, T_id []string, Time_start_ string, Time_end_ string) (minT, maxT float64, minTime, maxTime time.Time) {
  748. o := orm.NewOrm()
  749. sql_condition := ""
  750. if len(Time_start_) > 1 {
  751. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  752. }
  753. if len(Time_end_) > 1 {
  754. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  755. }
  756. if len(T_id) > 0 || len(SN) > 0 {
  757. sql_condition += " AND t_id in (" + strings.Join(T_id, ",") + ") OR t_sn in (" + strings.Join(SN, ",") + ")"
  758. }
  759. if len(sql_condition) > 0 {
  760. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  761. }
  762. //fmt.Println("maps_z;",maps_z[0][0])
  763. sql := "SELECT MIN(t_t) AS min_t, MAX(t_t) AS max_t,MIN(t_time) AS min_time, MAX(t_time) AS max_time FROM z_task_data_" + T_task_id + sql_condition
  764. fmt.Println(sql)
  765. err := o.Raw(sql).QueryRow(&minT, &maxT, &minTime, &maxTime)
  766. if err != nil {
  767. logs.Error(lib.FuncName(), err)
  768. }
  769. return
  770. }
  771. // 获取线上设备数据
  772. func Read_DeviceData_T_Min_Max_Time_Min_Max(T_sn, T_id string, Time_start_ string, Time_end_ string) (minT, maxT float64, minTime, maxTime time.Time) {
  773. o := orm2.NewOrmUsingDB(conf.Server_AliasName)
  774. sql_condition := ""
  775. if len(Time_start_) > 1 {
  776. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  777. }
  778. if len(Time_end_) > 1 {
  779. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  780. }
  781. sql_condition += " AND t_id = '" + T_id + "'"
  782. if len(sql_condition) > 0 {
  783. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  784. }
  785. //fmt.Println("maps_z;",maps_z[0][0])
  786. sql := "SELECT MIN(t_t) AS min_t, MAX(t_t) AS max_t,MIN(t_time) AS min_time, MAX(t_time) AS max_time FROM z_devicedata_" + T_sn + sql_condition
  787. fmt.Println(sql)
  788. err := o.Raw(sql).QueryRow(&minT, &maxT, &minTime, &maxTime)
  789. if err != nil {
  790. logs.Error(lib.FuncName(), err)
  791. }
  792. return
  793. }
  794. func Read_TaskData_ByIds_List(T_task_id string, SN []string, T_id []string, Time_start_ string, Time_end_ string) []TaskData {
  795. o := orm.NewOrm()
  796. var maps []TaskData
  797. sql_condition := ""
  798. if len(Time_start_) > 1 {
  799. sql_condition += " AND t_time >= '" + Time_start_ + "'"
  800. }
  801. if len(Time_end_) > 1 {
  802. sql_condition += " AND t_time <= '" + Time_end_ + "'"
  803. }
  804. if len(T_id) > 0 || len(SN) > 0 {
  805. sql_condition += " AND (t_id in (" + strings.Join(T_id, ",") + ") OR t_sn in (" + strings.Join(SN, ",") + "))"
  806. }
  807. if len(sql_condition) > 0 {
  808. sql_condition = " WHERE " + strings.TrimLeft(sql_condition, " AND ")
  809. }
  810. //fmt.Println("maps_z;",maps_z[0][0])
  811. sql := "SELECT ID,t_sn,t_id,t_t,t_rh,t_time FROM z_task_data_" + T_task_id + sql_condition + " ORDER BY t_time"
  812. fmt.Println(sql)
  813. _, err := o.Raw(sql).QueryRows(&maps)
  814. if err != nil {
  815. logs.Error(lib.FuncName(), err)
  816. }
  817. return maps
  818. }
  819. // 按照 t_id 分组数据
  820. func GroupDataByTID(data []TaskData) map[string][]TaskData {
  821. groupedData := make(map[string][]TaskData)
  822. for _, d := range data {
  823. groupedData[d.T_id] = append(groupedData[d.T_id], d)
  824. }
  825. return groupedData
  826. }
  827. // 删除的重复数据
  828. func DeleteDeduplicate(T_task_id string) (int64, error) {
  829. localOrm := orm2.NewOrmUsingDB(conf.Local_AliasName)
  830. tb_name := "z_task_data_" + T_task_id
  831. var cnt int64
  832. // 创建临时表
  833. sqlCreate := "CREATE TABLE `tmp_table` AS (SELECT MIN(`ID`) AS `min_id` FROM " + tb_name + " GROUP BY `t_sn`,`t_id`,`t_time`);"
  834. _, err := localOrm.Raw(sqlCreate).Exec()
  835. if err != nil {
  836. logs.Error(lib.FuncName(), err)
  837. return cnt, err
  838. }
  839. sqlDelete := "DELETE FROM " + tb_name + " WHERE `ID` NOT IN (SELECT `min_id` FROM `tmp_table`);"
  840. res, err := localOrm.Raw(sqlDelete).Exec()
  841. if err != nil {
  842. logs.Error(lib.FuncName(), err)
  843. return cnt, err
  844. }
  845. cnt, _ = res.RowsAffected()
  846. sqlDrop := "DROP TABLE `tmp_table`;"
  847. _, err = localOrm.Raw(sqlDrop).Exec()
  848. if err != nil {
  849. logs.Error(lib.FuncName(), err)
  850. return cnt, err
  851. }
  852. return cnt, nil
  853. }
  854. func CheckTableExist(alias_name, T_task_id string) bool {
  855. o := orm2.NewOrmUsingDB(alias_name)
  856. sql := "select count(ID) from z_task_data_" + T_task_id
  857. _, err := o.Raw(sql).Exec()
  858. if err != nil {
  859. // 数据库表不存在,则创建数据库
  860. logs.Error(lib.FuncName(), err)
  861. if err.(*mysql.MySQLError).Number == 1146 {
  862. return false
  863. }
  864. }
  865. return true
  866. }