DeviceClassList.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package Device
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego/cache"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. "github.com/beego/beego/v2/adapter/orm"
  12. _ "github.com/go-sql-driver/mysql"
  13. "time"
  14. )
  15. // 模板
  16. type DeviceClassList struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_class int `orm:"size(200);null"` // 分类
  19. T_id int `orm:"size(20);null"` // 设备id
  20. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  21. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  22. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  23. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  24. }
  25. func (t *DeviceClassList) TableName() string {
  26. return "device_class_list" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. var redisCache_DeviceClassList cache.Cache
  29. func init() {
  30. //注册模型
  31. orm.RegisterModel(new(DeviceClassList))
  32. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  33. "redis_"+"DeviceClassList", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  34. logs.Println(config)
  35. var err error
  36. redisCache_DeviceClassList, err = cache.NewCache("redis", config)
  37. if err != nil || redisCache_DeviceClassList == nil {
  38. errMsg := "failed to init redis"
  39. logs.Println(errMsg, err)
  40. }
  41. }
  42. // ---------------- Redis -------------------
  43. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  44. func Redis_DeviceClassList_Set(key string, r DeviceClassList) (err error) {
  45. //json序列化
  46. str, err := json.Marshal(r)
  47. if err != nil {
  48. logs.Error(lib.FuncName(), err)
  49. return
  50. }
  51. err = redisCache_DeviceClassList.Put(key, str, 24*time.Hour)
  52. if err != nil {
  53. logs.Println("set key:", key, ",value:", str, err)
  54. }
  55. return
  56. }
  57. // if r,is :=Redis_Get(T_sn);is{
  58. // return r,nil
  59. // }
  60. func Redis_DeviceClassList_Get(key string) (r DeviceClassList, is bool) {
  61. if redisCache_DeviceClassList.IsExist(key) {
  62. logs.Println("找到key:", key)
  63. v := redisCache_DeviceClassList.Get(key)
  64. json.Unmarshal(v.([]byte), &r)
  65. return r, true
  66. }
  67. logs.Println("没有 找到key:", key)
  68. return DeviceClassList{}, false
  69. }
  70. func Redis_DeviceClassList_DelK(key string) (err error) {
  71. err = redisCache_DeviceClassList.Delete(key)
  72. return
  73. }
  74. // ---------------- 特殊方法 -------------------
  75. // 获取 ById
  76. func Read_DeviceClassList_ById(id int) (r DeviceClassList, is bool) {
  77. o := orm.NewOrm()
  78. r = DeviceClassList{Id: id}
  79. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  80. if err != nil {
  81. logs.Error(lib.FuncName(), err)
  82. return r, false
  83. }
  84. return r, true
  85. }
  86. // 获取 By
  87. func Read_DeviceClassList(T_sn string) (r DeviceClassList, is bool) {
  88. if r, is = Redis_DeviceClassList_Get(T_sn); is == true {
  89. return r, true
  90. }
  91. o := orm.NewOrm()
  92. qs := o.QueryTable(new(DeviceClassList))
  93. err := qs.Filter("T_sn", T_sn).One(&r)
  94. if err != nil {
  95. logs.Println(lib.FuncName(), err)
  96. return r, false
  97. }
  98. Redis_DeviceClassList_Set(T_sn, r)
  99. return r, true
  100. }
  101. // 添加
  102. func Read_DeviceClassList_T_class_T_sn(T_class int, T_sn string) (r DeviceClassList, is bool) {
  103. o := orm.NewOrm()
  104. qs := o.QueryTable(new(DeviceClassList))
  105. err := qs.Filter("T_sn", T_sn).Filter("T_class", T_class).Filter("T_State", 1).One(&r)
  106. if err != nil {
  107. logs.Println(lib.FuncName(), err)
  108. return r, false
  109. }
  110. return r, true
  111. }
  112. // 添加
  113. func Add_DeviceClassList(r DeviceClassList) (id int64, is bool) {
  114. o := orm.NewOrm()
  115. //r.T_State = 1
  116. //err := o.Read(&r, "T_class", "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  117. //if err == nil {
  118. // if r.Id > 0 {
  119. // logs.Println("重复添加", r.T_class, r.T_sn)
  120. // return 0, false
  121. // }
  122. //}
  123. id, err := o.Insert(&r)
  124. if err != nil {
  125. logs.Error(lib.FuncName(), err)
  126. return 0, false
  127. }
  128. Redis_DeviceClassList_Set(r.T_sn, r)
  129. return id, true
  130. }
  131. // 删除
  132. func Delete_DeviceClassList(v DeviceClassList) bool {
  133. o := orm.NewOrm()
  134. if num, err := o.Delete(&v); err == nil {
  135. logs.Println("Number of records deleted in database:", num)
  136. } else {
  137. logs.Println(lib.FuncName(), err)
  138. return false
  139. }
  140. Redis_DeviceClassList_DelK(v.T_sn)
  141. return true
  142. }
  143. // 删除
  144. func Delete_DeviceClassList_(v DeviceClassList) bool {
  145. o := orm.NewOrm()
  146. v.T_State = 0
  147. if num, err := o.Update(&v, "T_State"); err == nil {
  148. fmt.Println("Number of records updated in database:", num)
  149. } else {
  150. logs.Println(lib.FuncName(), err)
  151. return false
  152. }
  153. Redis_DeviceClassList_DelK(v.T_sn)
  154. return true
  155. }
  156. // 修改
  157. func Update_DeviceClassList(m DeviceClassList, cols ...string) bool {
  158. o := orm.NewOrm()
  159. if num, err := o.Update(&m, cols...); err == nil {
  160. fmt.Println("Number of records updated in database:", num)
  161. Redis_DeviceClassList_Set(m.T_sn, m)
  162. return true
  163. } else {
  164. logs.Println(lib.FuncName(), err)
  165. }
  166. return false
  167. }
  168. // 获取列表
  169. func Read_DeviceClassList_List(T_class int, T_sn string, page int, page_z int) (r []DeviceClassList, cnt int64) {
  170. o := orm.NewOrm()
  171. // 也可以直接使用 Model 结构体作为表名
  172. qs := o.QueryTable(new(DeviceClassList))
  173. var offset int64
  174. if page <= 1 {
  175. offset = 0
  176. } else {
  177. offset = int64((page - 1) * page_z)
  178. }
  179. cond := orm.NewCondition()
  180. 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)
  181. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_id").All(&r)
  182. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  183. return r, cnt
  184. }
  185. // 获取列表
  186. func Read_DeviceClassList_List_id(T_class_Id int) (r []DeviceClassList) {
  187. o := orm.NewOrm()
  188. // 也可以直接使用 Model 结构体作为表名
  189. qs := o.QueryTable(new(DeviceClassList))
  190. qs.Filter("T_class", T_class_Id).All(&r)
  191. return r
  192. }