DeviceClassList.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. err := o.Read(&r,"T_class","T_sn") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  103. if err == nil {
  104. if r.Id > 0{
  105. logs.Println("重复添加",r.T_class,r.T_sn)
  106. return 0, false
  107. }
  108. }
  109. id, err = o.Insert(&r)
  110. if err != nil {
  111. logs.Println(err)
  112. return 0, false
  113. }
  114. Redis_DeviceClassList_Set(r.T_sn, r)
  115. return id, true
  116. }
  117. // 删除
  118. func Delete_DeviceClassList(v DeviceClassList) bool {
  119. o := orm.NewOrm()
  120. if num, err := o.Delete(&v); err == nil {
  121. logs.Println("Number of records deleted in database:", num)
  122. } else {
  123. return false
  124. }
  125. Redis_DeviceClassList_DelK(v.T_sn)
  126. return true
  127. }
  128. // 删除
  129. func Delete_DeviceClassList_(v DeviceClassList) bool {
  130. o := orm.NewOrm()
  131. v.T_State = 0
  132. if num, err := o.Update(&v, "T_State"); err == nil {
  133. fmt.Println("Number of records updated in database:", num)
  134. } else {
  135. return false
  136. }
  137. Redis_DeviceClassList_DelK(v.T_sn)
  138. return true
  139. }
  140. // 修改
  141. func Update_DeviceClassList(m DeviceClassList,cols ...string) bool {
  142. o := orm.NewOrm()
  143. if num, err := o.Update(&m, cols...); err == nil {
  144. fmt.Println("Number of records updated in database:", num)
  145. Redis_DeviceClassList_Set(m.T_sn, m)
  146. return true
  147. }
  148. return false
  149. }
  150. // 获取列表
  151. func Read_DeviceClassList_List(T_class int, T_sn string, page int, page_z int) (r []DeviceClassList, cnt int64) {
  152. o := orm.NewOrm()
  153. // 也可以直接使用 Model 结构体作为表名
  154. qs := o.QueryTable(new(DeviceClassList))
  155. var offset int64
  156. if page <= 1 {
  157. offset = 0
  158. } else {
  159. offset = int64((page - 1) * page_z)
  160. }
  161. cond := orm.NewCondition()
  162. 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)
  163. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_id").All(&r)
  164. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  165. return r, cnt
  166. }
  167. // 获取列表
  168. func Read_DeviceClassList_List_id(T_class_Id int ) (r []DeviceClassList) {
  169. o := orm.NewOrm()
  170. // 也可以直接使用 Model 结构体作为表名
  171. qs := o.QueryTable(new(DeviceClassList))
  172. qs.Filter("T_class", T_class_Id).All(&r)
  173. return r
  174. }