DeviceClass.go 6.7 KB

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