DeviceClassList.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package Device
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. "strconv"
  10. "github.com/astaxie/beego/cache"
  11. _ "github.com/astaxie/beego/cache/redis"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. _ "github.com/go-sql-driver/mysql"
  14. "time"
  15. )
  16. // 模板
  17. type DeviceClassList struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_class int `orm:"size(200);null"` // 分类
  20. T_id string `orm:"size(20);null"` // 设备id
  21. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  22. T_failure_time string `orm:"size(256);null"` // 失效时间
  23. T_pdf string `orm:"size(256);null"` // pdf链接
  24. T_Certificate_sn string `orm:"size(256);null"` // 证书编号
  25. T_remark string `orm:"size(1024);null"` // 备注
  26. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  27. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  28. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  29. }
  30. func (t *DeviceClassList) TableName() string {
  31. return "device_class_list" // 数据库名称 // ************** 替换 FormulaList **************
  32. }
  33. var redisCache_DeviceClassList cache.Cache
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(DeviceClassList))
  37. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  38. "redis_"+"DeviceClassList", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  39. logs.Println(config)
  40. var err error
  41. redisCache_DeviceClassList, err = cache.NewCache("redis", config)
  42. if err != nil || redisCache_DeviceClassList == nil {
  43. errMsg := "failed to init redis"
  44. logs.Println(errMsg, err)
  45. }
  46. }
  47. // ---------------- Redis -------------------
  48. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  49. func Redis_DeviceClassList_Set(key string, r DeviceClassList) (err error) {
  50. //json序列化
  51. str, err := json.Marshal(r)
  52. if err != nil {
  53. logs.Error(lib.FuncName(), err)
  54. return
  55. }
  56. err = redisCache_DeviceClassList.Put(key, str, 24*time.Hour)
  57. if err != nil {
  58. logs.Println("set key:", key, ",value:", str, err)
  59. }
  60. return
  61. }
  62. // if r,is :=Redis_Get(T_sn);is{
  63. // return r,nil
  64. // }
  65. func Redis_DeviceClassList_Get(key string) (r DeviceClassList, is bool) {
  66. if redisCache_DeviceClassList.IsExist(key) {
  67. logs.Println("找到key:", key)
  68. v := redisCache_DeviceClassList.Get(key)
  69. json.Unmarshal(v.([]byte), &r)
  70. return r, true
  71. }
  72. logs.Println("没有 找到key:", key)
  73. return DeviceClassList{}, false
  74. }
  75. func Redis_DeviceClassList_DelK(key string) (err error) {
  76. err = redisCache_DeviceClassList.Delete(key)
  77. return
  78. }
  79. // ---------------- 特殊方法 -------------------
  80. // 获取 ById
  81. func Read_DeviceClassList_ById(id int) (r DeviceClassList, is bool) {
  82. o := orm.NewOrm()
  83. r = DeviceClassList{Id: id}
  84. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  85. if err != nil {
  86. logs.Error(lib.FuncName(), err)
  87. return r, false
  88. }
  89. return r, true
  90. }
  91. // 获取 By
  92. func Read_DeviceClassList(T_sn string) (r DeviceClassList, is bool) {
  93. if r, is = Redis_DeviceClassList_Get(T_sn); is == true {
  94. return r, true
  95. }
  96. o := orm.NewOrm()
  97. qs := o.QueryTable(new(DeviceClassList))
  98. err := qs.Filter("T_sn", T_sn).One(&r)
  99. if err != nil {
  100. logs.Println(lib.FuncName(), err)
  101. return r, false
  102. }
  103. Redis_DeviceClassList_Set(T_sn, r)
  104. return r, true
  105. }
  106. // 添加
  107. func Read_DeviceClassList_T_class_T_sn(T_class int, T_sn string) (r DeviceClassList, is bool) {
  108. o := orm.NewOrm()
  109. qs := o.QueryTable(new(DeviceClassList))
  110. err := qs.Filter("T_sn", T_sn).Filter("T_class", T_class).Filter("T_State", 1).One(&r)
  111. if err != nil {
  112. logs.Println(lib.FuncName(), err)
  113. return r, false
  114. }
  115. return r, true
  116. }
  117. func Read_DeviceClassList_T_class_T_id(T_class int, T_id string) (r DeviceClassList, is bool) {
  118. o := orm.NewOrm()
  119. qs := o.QueryTable(new(DeviceClassList))
  120. err := qs.Filter("T_id", T_id).Filter("T_class", T_class).Filter("T_State", 1).One(&r)
  121. if err != nil {
  122. logs.Println(lib.FuncName(), err)
  123. return r, false
  124. }
  125. return r, true
  126. }
  127. // 添加
  128. func Add_DeviceClassList(r DeviceClassList) (id int64, is bool) {
  129. o := orm.NewOrm()
  130. //r.T_State = 1
  131. //err := o.Read(&r, "T_class", "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  132. //if err == nil {
  133. // if r.Id > 0 {
  134. // logs.Println("重复添加", r.T_class, r.T_sn)
  135. // return 0, false
  136. // }
  137. //}
  138. id, err := o.Insert(&r)
  139. if err != nil {
  140. logs.Error(lib.FuncName(), err)
  141. return 0, false
  142. }
  143. Redis_DeviceClassList_Set(r.T_sn, r)
  144. return id, true
  145. }
  146. // 删除
  147. func Delete_DeviceClassList(v DeviceClassList) bool {
  148. o := orm.NewOrm()
  149. if num, err := o.Delete(&v); err == nil {
  150. logs.Println("Number of records deleted in database:", num)
  151. } else {
  152. logs.Println(lib.FuncName(), err)
  153. return false
  154. }
  155. Redis_DeviceClassList_DelK(v.T_sn)
  156. return true
  157. }
  158. // 删除
  159. func Delete_DeviceClassList_(v DeviceClassList) bool {
  160. o := orm.NewOrm()
  161. v.T_State = 0
  162. if num, err := o.Update(&v, "T_State"); err == nil {
  163. logs.Println("Number of records updated in database:", num)
  164. } else {
  165. logs.Println(lib.FuncName(), err)
  166. return false
  167. }
  168. Redis_DeviceClassList_DelK(v.T_sn)
  169. return true
  170. }
  171. // 修改
  172. func Update_DeviceClassList(m DeviceClassList, cols ...string) bool {
  173. o := orm.NewOrm()
  174. if num, err := o.Update(&m, cols...); err == nil {
  175. logs.Println("Number of records updated in database:", num)
  176. Redis_DeviceClassList_Set(m.T_sn, m)
  177. return true
  178. } else {
  179. logs.Println(lib.FuncName(), err)
  180. }
  181. return false
  182. }
  183. // 获取列表
  184. func Read_DeviceClassList_List(T_class int, T_sn string, page int, page_z int) (r []DeviceClassList, cnt int64) {
  185. o := orm.NewOrm()
  186. // 也可以直接使用 Model 结构体作为表名
  187. qs := o.QueryTable(new(DeviceClassList))
  188. var offset int64
  189. if page <= 1 {
  190. offset = 0
  191. } else {
  192. offset = int64((page - 1) * page_z)
  193. }
  194. cond := orm.NewCondition()
  195. cond1 := cond.And("T_class", T_class).And("T_sn__icontains", T_sn).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  196. if page_z == 9999 {
  197. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_id").All(&r)
  198. } else {
  199. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_id").All(&r)
  200. }
  201. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  202. return r, cnt
  203. }
  204. func Read_DeviceClassList_OrderList(T_class int, T_sn, T_id, T_remark string, page int, page_z int) (r []DeviceClassList, cnt int64) {
  205. o := orm.NewOrm()
  206. var maps_z []orm2.ParamsList
  207. pagez := page_z
  208. var offset int
  209. if page <= 1 {
  210. offset = 0
  211. } else {
  212. page -= 1
  213. offset = page * pagez
  214. }
  215. sqlWhere := "t_class = " + strconv.Itoa(T_class) + " AND t__state = 1"
  216. if len(T_sn) > 1 {
  217. sqlWhere += " AND t_sn like \"%" + T_sn + "%\""
  218. }
  219. if len(T_id) > 1 {
  220. sqlWhere += " AND T_id like \"%" + T_id + "%\""
  221. }
  222. if len(T_remark) > 0 {
  223. sqlWhere += " AND t_remark like \"%" + T_remark + "%\""
  224. }
  225. sql := "SELECT COUNT(ID) FROM device_class_list WHERE " + sqlWhere
  226. logs.Println(sql)
  227. _, err := o.Raw(sql).ValuesList(&maps_z)
  228. if err != nil {
  229. return r, 0
  230. }
  231. if len(maps_z) == 0 {
  232. return r, 0
  233. }
  234. //logs.Println("maps_z;",maps_z[0][0])
  235. sql = "SELECT * FROM device_class_list WHERE " + sqlWhere + " ORDER BY t_id+0 "
  236. if page_z != 9999 {
  237. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  238. }
  239. logs.Println(sql)
  240. _, err = o.Raw(sql).QueryRows(&r)
  241. if err != nil {
  242. logs.Println(lib.FuncName(), err)
  243. return r, 0
  244. }
  245. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  246. key, _ := strconv.Atoi(maps_z[0][0].(string))
  247. return r, int64(key)
  248. }
  249. // 获取列表
  250. func Read_DeviceClassList_List_id(T_class_Id int) (r []DeviceClassList) {
  251. o := orm.NewOrm()
  252. // 也可以直接使用 Model 结构体作为表名
  253. qs := o.QueryTable(new(DeviceClassList))
  254. qs.Filter("T_class", T_class_Id).Filter("T_State", 1).All(&r)
  255. return r
  256. }
  257. // 删除重复设备列表
  258. func Del_DeviceClassList_Duplication(T_class int) error {
  259. o := orm.NewOrm()
  260. // 开始插入数据 UPDATE `cold_verify`.`Z_TaskData_d8qMyeXLzIxn` SET `t_t` = 20.2 WHERE `ID` = 69
  261. sql := "DELETE FROM device_class_list WHERE ID NOT IN (" +
  262. "SELECT * FROM (SELECT MIN(ID) FROM device_class_list WHERE t__state = 1 AND t_class = " + strconv.Itoa(T_class) +
  263. " GROUP BY t_class, t_id, t_sn) AS keep_ids) " +
  264. " AND t__state = 1 AND t_class = " + strconv.Itoa(T_class)
  265. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  266. logs.Debug(sql)
  267. res, err := o.Raw(sql).Exec()
  268. if err != nil {
  269. logs.Error(lib.FuncName(), err)
  270. return err
  271. }
  272. num, _ := res.RowsAffected()
  273. logs.Debug("删除重复设备列表: ", num)
  274. return nil
  275. }