Device.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 Device struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  18. T_id int `orm:"size(256);null"` // 标题
  19. T_t float32 `orm:"size(10);null"` // 温度
  20. T_rh float32 `orm:"size(10);null"` // 湿度
  21. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  22. T_MSISDN string `orm:"size(256);null"` // 物联网卡
  23. T_version int `orm:"size(2);1"` // 版本
  24. T_signal int `orm:"size(2);1"` // 信号强度
  25. T_electric int `orm:"size(2);1"` // 电量百分比
  26. T_up_time_interval int `orm:"size(2);1"` //上传时间间隔
  27. T_note_time_interval int `orm:"size(2);1"` //记录时间间隔
  28. T_note_file_num int `orm:"size(2);1"` //记录文件数量
  29. int `orm:"size(2);1"` // 电量百分比
  30. T_State int `orm:"size(2);1"` // 0 删除 1 正常
  31. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  32. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  33. }
  34. func (t *Device) TableName() string {
  35. return "Device" // 数据库名称 // ************** 替换 FormulaList **************
  36. }
  37. var redisCache_Device cache.Cache
  38. func init() {
  39. //注册模型
  40. orm.RegisterModel(new(Device))
  41. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  42. "redis_"+"Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  43. logs.Println(config)
  44. var err error
  45. redisCache_Device, err = cache.NewCache("redis", config)
  46. if err != nil || redisCache_Device == nil {
  47. errMsg := "failed to init redis"
  48. logs.Println(errMsg, err)
  49. }
  50. }
  51. // ---------------- Redis -------------------
  52. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  53. func Redis_Device_Set(key string, r Device) (err error) {
  54. //json序列化
  55. str, err := json.Marshal(r)
  56. if err != nil {
  57. logs.Println(err)
  58. return
  59. }
  60. err = redisCache_Device.Put(key, str, 24*time.Hour)
  61. if err != nil {
  62. logs.Println("set key:", key, ",value:", str, err)
  63. }
  64. return
  65. }
  66. //if r,is :=Redis_Get(T_sn);is{
  67. //return r,nil
  68. //}
  69. func Redis_Device_Get(key string) (r Device, is bool) {
  70. if redisCache_Device.IsExist(key) {
  71. logs.Println("找到key:", key)
  72. v := redisCache_Device.Get(key)
  73. json.Unmarshal(v.([]byte), &r)
  74. return r, true
  75. }
  76. logs.Println("没有 找到key:", key)
  77. return Device{}, false
  78. }
  79. func Redis_Device_DelK(key string) (err error) {
  80. err = redisCache_Device.Delete(key)
  81. return
  82. }
  83. // ---------------- 特殊方法 -------------------
  84. // 获取 ById
  85. func Read_Device_ById(id int) (r Device, is bool) {
  86. o := orm.NewOrm()
  87. r = Device{Id: id}
  88. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  89. if err != nil {
  90. logs.Println(err)
  91. return r, false
  92. }
  93. return r, true
  94. }
  95. // 获取 By
  96. func Read_Device(T_sn string) (r Device, is bool) {
  97. if r, is = Redis_Device_Get(T_sn); is == true {
  98. return r, true
  99. }
  100. o := orm.NewOrm()
  101. qs := o.QueryTable(new(Device))
  102. err := qs.Filter("T_sn", T_sn).One(&r)
  103. if err != nil {
  104. return r, false
  105. }
  106. Redis_Device_Set(T_sn, r)
  107. return r, true
  108. }
  109. // 添加
  110. func Add_Device(r Device) (id int64, is bool) {
  111. o := orm.NewOrm()
  112. id, err := o.Insert(&r)
  113. if err != nil {
  114. logs.Println(err)
  115. return 0, false
  116. }
  117. CREATE_DeviceData(r.T_sn)
  118. Redis_Device_Set(r.T_sn, r)
  119. return id, true
  120. }
  121. // 删除
  122. func Delete_Device(v Device) bool {
  123. o := orm.NewOrm()
  124. if num, err := o.Delete(&v); err == nil {
  125. logs.Println("Number of records deleted in database:", num)
  126. } else {
  127. return false
  128. }
  129. Redis_Device_DelK(v.T_sn)
  130. return true
  131. }
  132. // 删除
  133. func Delete_Device_(v Device) bool {
  134. o := orm.NewOrm()
  135. v.T_State = 0
  136. if num, err := o.Update(&v, "T_State"); err == nil {
  137. fmt.Println("Number of records updated in database:", num)
  138. } else {
  139. return false
  140. }
  141. Redis_Device_DelK(v.T_sn)
  142. return true
  143. }
  144. // 修改
  145. func Update_Device(m Device, cols ...string) bool {
  146. o := orm.NewOrm()
  147. if num, err := o.Update(&m, cols...); err == nil {
  148. fmt.Println("Number of records updated in database:", num)
  149. Redis_Device_Set(m.T_sn, m)
  150. return true
  151. }
  152. return false
  153. }
  154. // 获取列表
  155. func Read_Device_List(T_sn string, T_MSISDN string, page int, page_z int) (r []Device, cnt int64) {
  156. o := orm.NewOrm()
  157. // 也可以直接使用 Model 结构体作为表名
  158. qs := o.QueryTable(new(Device))
  159. var offset int64
  160. if page <= 1 {
  161. offset = 0
  162. } else {
  163. offset = int64((page - 1) * page_z)
  164. }
  165. cond := orm.NewCondition()
  166. cond1 := cond.And("T_MSISDN__icontains", T_MSISDN).And("T_sn__icontains", T_sn).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  167. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  168. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  169. return r, cnt
  170. }