TaskCopy.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package Task
  2. import (
  3. "ColdVerify_local/conf"
  4. "ColdVerify_local/lib"
  5. "ColdVerify_local/logs"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "time"
  9. )
  10. // 创建数据库 Device.CREATE_TaskData("")
  11. func CREATE_TaskDataCopy(alias_name, T_copy_id string) bool {
  12. o := orm2.NewOrmUsingDB(alias_name)
  13. sql := "DROP TABLE IF EXISTS `z_task_data_copy_" + T_copy_id + "`"
  14. o.Raw(sql).Exec()
  15. sql = "CREATE TABLE IF NOT EXISTS `z_task_data_copy_" + T_copy_id + "` ( " +
  16. " `ID` int(11) NOT NULL AUTO_INCREMENT," +
  17. " `t_sn` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL," +
  18. " `t_id` varchar(256) DEFAULT NULL," +
  19. " `t_t` float(6, 1) NULL DEFAULT NULL," +
  20. " `t_rh` float(6, 1) NULL DEFAULT NULL," +
  21. " `t_time` datetime(0) NULL DEFAULT NULL," +
  22. " PRIMARY KEY (`ID`) USING BTREE," +
  23. " KEY `t_sn` (`t_sn`)," +
  24. " KEY `t_id` (`t_id`)," +
  25. " KEY `t_time` (`t_time`)" +
  26. ") ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"
  27. _, err := o.Raw(sql).Exec()
  28. if err != nil {
  29. return false
  30. }
  31. //sqlIndex := "ALTER TABLE `z_task_data_" + T_task_id + "` add unique index(`t_sn`,`t_id`,`t_time`);"
  32. //logs.Debug(sqlIndex)
  33. //o.Raw(sqlIndex).Exec()
  34. return true
  35. }
  36. type TaskCopy struct {
  37. Id int `orm:"column(ID);size(11);auto;pk"`
  38. T_task_id string `orm:"size(256);null"` // 任务ID
  39. T_copy_id string `orm:"size(256);null"` // 复制id
  40. T_time string `orm:"size(256);null"` // 存档时间
  41. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  42. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  43. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  44. }
  45. func (t *TaskCopy) TableName() string {
  46. return "task_copy" // 数据库名称 // ************** 替换 FormulaList **************
  47. }
  48. func init() {
  49. //注册模型
  50. orm.RegisterModel(new(TaskCopy))
  51. }
  52. // 获取 By
  53. func Read_TaskCopy(T_copy_id string) (r TaskCopy, err error) {
  54. o := orm.NewOrm()
  55. qs := o.QueryTable(new(TaskCopy))
  56. err = qs.Filter("T_copy_id", T_copy_id).Filter("T_State", 1).One(&r)
  57. if err != nil {
  58. return r, err
  59. }
  60. return r, nil
  61. }
  62. // 添加
  63. func Add_TaskCopy(r TaskCopy) (string, bool) {
  64. o := orm.NewOrm()
  65. // 生成编号
  66. rand_x := 0
  67. for true {
  68. r.T_copy_id = lib.GetRandstring(12, "abcdefghijklmnopqrstuvwxyz0123456789", int64(rand_x)) // 1,336,336
  69. err := o.Read(&r, "T_copy_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  70. if err != nil {
  71. break
  72. }
  73. rand_x += 1
  74. }
  75. _, err := o.Insert(&r)
  76. if err != nil {
  77. logs.Error(lib.FuncName(), err)
  78. return "", false
  79. }
  80. return r.T_copy_id, true
  81. }
  82. // 删除
  83. func Delete_TaskCopy(v TaskCopy) bool {
  84. o := orm.NewOrm()
  85. v.T_State = 0
  86. num, err := o.Update(&v, "T_State")
  87. if err != nil {
  88. logs.Error(lib.FuncName(), err)
  89. return false
  90. }
  91. sql := "DROP TABLE IF EXISTS `z_task_data_copy_" + v.T_copy_id + "`"
  92. o.Raw(sql).Exec()
  93. logs.Println("Number of records deleted in database:", num)
  94. return true
  95. }
  96. func Read_TaskCopy_List(T_task_id string, page int, page_z int) ([]TaskCopy, int64) {
  97. o := orm.NewOrm()
  98. qs := o.QueryTable(new(TaskCopy))
  99. var r []TaskCopy
  100. var offset int64
  101. if page <= 1 {
  102. offset = 0
  103. } else {
  104. offset = int64((page - 1) * page_z)
  105. }
  106. cond := orm.NewCondition()
  107. cond1 := cond.And("T_task_id", T_task_id).And("T_State", 1)
  108. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_time").All(&r)
  109. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  110. return r, cnt
  111. }
  112. // 数据存档
  113. func TaskData_Arhiving(T_task_id, T_copy_id string) bool {
  114. o := orm.NewOrm()
  115. sql := "INSERT INTO `z_task_data_copy_" + T_copy_id + "`(`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`)" +
  116. " SELECT `t_sn`, `t_id`, `t_t`, `t_rh`, `t_time` FROM `z_task_data_" + T_task_id + "`"
  117. _, err := o.Raw(sql).Exec()
  118. if err != nil {
  119. logs.Error(lib.FuncName(), err)
  120. return false
  121. }
  122. return true
  123. }
  124. // 数据恢复
  125. func TaskData_Recover(T_task_id, T_copy_id string) bool {
  126. o := orm.NewOrm()
  127. Truncate_TaskData(conf.Local_AliasName, T_task_id)
  128. sql := "INSERT INTO `z_task_data_" + T_task_id + "`(`t_sn`, `t_id`, `t_t`, `t_rh`, `t_time`)" +
  129. " SELECT `t_sn`, `t_id`, `t_t`, `t_rh`, `t_time` FROM z_task_data_copy_" + T_copy_id
  130. _, err := o.Raw(sql).Exec()
  131. if err != nil {
  132. logs.Error(lib.FuncName(), err)
  133. return false
  134. }
  135. return true
  136. }