DeviceClassList.go 5.4 KB

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