DeviceClass.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/models/Admin"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "time"
  13. )
  14. type DeviceClass struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_uuid string `orm:"size(256);null"` //
  17. T_name string `orm:"size(256);null"` // 分类
  18. T_Notice_wx string `orm:"type(text);null"` //w微信公众号 appid/名字|
  19. T_Notice_wx2 string `orm:"type(text);null"` //w微信公众号 appid/名字|
  20. T_Notice_phone string `orm:"type(text);null"` //p手机 1111111|
  21. T_Notice_message string `orm:"type(text);null"` //m短信 1111111|
  22. T_Notice_mailbox string `orm:"type(text);null"` //e邮箱 1111111|
  23. T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
  24. // 湿度超下限预警,处理,w启用,数量,上限,~|
  25. // 湿度超下限预警,0,0,0,0,0,0,0,0,0,0,0,0|
  26. T_State int `orm:"size(2);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 *DeviceClass) TableName() string {
  31. return "DeviceClass" // 数据库名称 // ************** 替换 DesignClass **************
  32. }
  33. var redisCache_DeviceClass cache.Cache
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(DeviceClass))
  37. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  38. "redis_DeviceClass", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  39. fmt.Println(config)
  40. var err error
  41. redisCache_DeviceClass, err = cache.NewCache("redis", config)
  42. if err != nil || redisCache_DeviceClass == nil {
  43. errMsg := "failed to init redis"
  44. fmt.Println(errMsg, err)
  45. panic(errMsg)
  46. }
  47. }
  48. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  49. func Redis_DeviceClass_Set(r DeviceClass) (err error) {
  50. key := strconv.Itoa(r.Id)
  51. //json序列化
  52. str, err := json.Marshal(r)
  53. if err != nil {
  54. fmt.Print(err)
  55. return
  56. }
  57. err = redisCache_DeviceClass.Put(key, str, 2*time.Hour)
  58. if err != nil {
  59. fmt.Println("set key:", key, ",value:", str, err)
  60. }
  61. return
  62. }
  63. //if r,is :=Redis_Get(T_sn);is{
  64. //return r,nil
  65. //}
  66. func Redis_DeviceClass_Get(key string) (DeviceClass, bool) {
  67. println("找到key:", key)
  68. if redisCache_DeviceClass.IsExist(key) {
  69. //println("找到key:",key)
  70. v := redisCache_DeviceClass.Get(key)
  71. var r DeviceClass
  72. json.Unmarshal(v.([]byte), &r)
  73. return r, true
  74. }
  75. return DeviceClass{}, false
  76. }
  77. func Redis_DeviceClass_DelK(key string) (err error) {
  78. err = redisCache_DeviceClass.Delete(key)
  79. return
  80. }
  81. // ---------------- 特殊方法 -------------------
  82. // 获取 ById
  83. func Read_Class_ById(id int) (r DeviceClass, err error) {
  84. key := strconv.Itoa(id)
  85. if r, is := Redis_DeviceClass_Get(key); is {
  86. //println("Redis_Get OK")
  87. return r, nil
  88. }
  89. o := orm.NewOrm()
  90. r = DeviceClass{Id: id}
  91. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  92. if err != nil {
  93. fmt.Println(err)
  94. return r, err
  95. }
  96. Redis_DeviceClass_Set(r)
  97. return r, err
  98. }
  99. // 添加
  100. func Add_Class(m DeviceClass) (id int64, err error) {
  101. o := orm.NewOrm()
  102. id, err = o.Insert(&m)
  103. if err != nil {
  104. fmt.Println(err)
  105. }
  106. Redis_DeviceClass_Set(m)
  107. return
  108. }
  109. // 修改
  110. func Update_Class_ById(m DeviceClass) (err error) {
  111. o := orm.NewOrm()
  112. v := DeviceClass{Id: m.Id}
  113. // ascertain id exists in the database
  114. if err = o.Read(&v); err == nil {
  115. var num int64
  116. if num, err = o.Update(&m, "T_name", "T_Notice_wx", "T_Notice_wx2", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism"); err == nil {
  117. fmt.Println("Number of records updated in database:", num)
  118. }
  119. }
  120. Redis_DeviceClass_Set(m)
  121. return
  122. }
  123. // 删除
  124. func Delete_Class_ById(id int) DeviceClass {
  125. o := orm.NewOrm()
  126. v := DeviceClass{Id: id}
  127. // ascertain id exists in the database
  128. if err := o.Read(&v); err == nil {
  129. var num int64
  130. v.T_State = 0
  131. if num, err = o.Update(&v, "T_State"); err == nil {
  132. fmt.Println("Number of records updated in database:", num)
  133. }
  134. key := strconv.Itoa(v.Id)
  135. Redis_DeviceClass_DelK(key)
  136. }
  137. return v
  138. }
  139. // 删除
  140. func Delete_Class_ByUuid_All(user_ Admin.Admin) {
  141. o := orm.NewOrm()
  142. qs := o.QueryTable(new(DeviceClass))
  143. var r []DeviceClass
  144. qs.Filter("T_State", 1).All(&r)
  145. for _, v := range r {
  146. v.T_State = 0
  147. o.Update(&v, "T_State")
  148. }
  149. }
  150. // 获取全部
  151. func Read_Class_All_1() (r []DeviceClass) {
  152. o := orm.NewOrm()
  153. qs := o.QueryTable(new(DeviceClass))
  154. qs.Filter("T_State", 1).All(&r)
  155. return r
  156. }
  157. // 获取列表
  158. func Read_DeviceClass_ALL_1(T_uuid string, page int, T_name string) (r []DeviceClass, cnt int64) {
  159. o := orm.NewOrm()
  160. // 也可以直接使用 Model 结构体作为表名
  161. qs := o.QueryTable(new(DeviceClass))
  162. var offset int64
  163. if page <= 1 {
  164. offset = 0
  165. } else {
  166. offset = int64((page - 1) * conf.Page_size)
  167. }
  168. qs.Limit(conf.Page_size, offset).Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).OrderBy("-Id").Filter("T_State", 1).All(&r)
  169. cnt, _ = qs.Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
  170. return r, cnt
  171. }
  172. // 获取列表
  173. func Read_DeviceClass_ALL_T_uuid_1(T_uuid string) (r []DeviceClass) {
  174. o := orm.NewOrm()
  175. // 也可以直接使用 Model 结构体作为表名
  176. qs := o.QueryTable(new(DeviceClass))
  177. qs.Filter("T_uuid", T_uuid).OrderBy("-Id").Filter("T_State", 1).All(&r)
  178. return r
  179. }