DeviceClassList.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_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_remark) > 0 {
  220. sqlWhere += " AND t_remark like \"%" + T_remark + "%\""
  221. }
  222. sql := "SELECT COUNT(ID) FROM device_class_list WHERE " + sqlWhere
  223. logs.Println(sql)
  224. _, err := o.Raw(sql).ValuesList(&maps_z)
  225. if err != nil {
  226. return r, 0
  227. }
  228. if len(maps_z) == 0 {
  229. return r, 0
  230. }
  231. //logs.Println("maps_z;",maps_z[0][0])
  232. sql = "SELECT * FROM device_class_list WHERE " + sqlWhere + " ORDER BY t_id+0 "
  233. if page_z != 9999 {
  234. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  235. }
  236. logs.Println(sql)
  237. _, err = o.Raw(sql).QueryRows(&r)
  238. if err != nil {
  239. logs.Println(lib.FuncName(), err)
  240. return r, 0
  241. }
  242. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  243. key, _ := strconv.Atoi(maps_z[0][0].(string))
  244. return r, int64(key)
  245. }
  246. // 获取列表
  247. func Read_DeviceClassList_List_id(T_class_Id int) (r []DeviceClassList) {
  248. o := orm.NewOrm()
  249. // 也可以直接使用 Model 结构体作为表名
  250. qs := o.QueryTable(new(DeviceClassList))
  251. qs.Filter("T_class", T_class_Id).Filter("T_State", 1).All(&r)
  252. return r
  253. }