Device.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "time"
  13. )
  14. // 模板
  15. type Device struct {
  16. T_sn string `orm:"pk;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  17. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  18. T_MSISDN string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  19. T_devName string `orm:"size(256);null"` // 设备名称
  20. T_protocol int `orm:"size(2);default(1)"` // 冷链通讯协议 1 :1.0协议 2 :2.0协议 3 :3.0协议
  21. T_VerifyTime time.Time `orm:"type(timestamp);null"` // 验证时间
  22. T_CalibrationTime time.Time `orm:"type(timestamp);null"` // 校准时间
  23. T_ist int `orm:"size(2);default(1)"` // 温度 1开启 2关闭
  24. T_ish int `orm:"size(2);default(1)"` // 湿度 1开启 2关闭
  25. // 设备同步参数
  26. T_Dattery int `orm:"size(4);null"` // 电量
  27. T_Site string `orm:"size(200);null"` // GPS
  28. T_type int `orm:"index;size(4);null"` // Device.DeviceType 1库房 2移动
  29. T_give int `orm:"index;size(2);default(1)"` // 屏蔽状态 0 屏蔽 1 正常
  30. T_monitor int `orm:"index;size(2);null"` // 监控状态 0 未监控 1 监控
  31. T_online int `orm:"index;size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线
  32. T_online_s int `orm:"index;size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线
  33. T_State int `orm:"index;size(2);default(1)"` // 0 删除 1 正常
  34. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  35. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  36. }
  37. type Device_R struct {
  38. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  39. T_MSISDN string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  40. T_devName string // 设备名称
  41. T_protocol int // 1 1.0协议 2 2.0协议
  42. T_VerifyTime string // 验证时间
  43. T_CalibrationTime string // 校准时间
  44. T_ist int // 温度 1开启 2关闭
  45. T_ish int // 湿度 1开启 2关闭
  46. T_Dattery int // 电量
  47. T_Site string // GPS
  48. T_type int // 1库房 2移动
  49. T_give int // 屏蔽状态 0 屏蔽 1 正常
  50. T_monitor int // 监控状态 0 未监控 1 监控
  51. T_online int // 在线状态 0 未启用 1 在线 2 离线
  52. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  53. CreateTime string //auto_now_add 第一次保存时才设置时间
  54. UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新
  55. }
  56. type Device_task struct {
  57. T_sn string
  58. T_task string
  59. }
  60. func (t *Device) TableName() string {
  61. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  62. }
  63. var redisCache_Device cache.Cache
  64. func init() {
  65. //注册模型
  66. orm.RegisterModel(new(Device))
  67. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  68. "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  69. fmt.Println(config)
  70. var err error
  71. redisCache_Device, err = cache.NewCache("redis", config)
  72. if err != nil || redisCache_Device == nil {
  73. errMsg := "failed to init redis"
  74. fmt.Println(errMsg, err)
  75. }
  76. }
  77. func DeviceToDevice_R(r Device) (t Device_R) {
  78. t.T_sn = r.T_sn
  79. t.T_MSISDN = r.T_MSISDN
  80. t.T_devName = r.T_devName
  81. t.T_protocol = r.T_protocol
  82. t.T_VerifyTime = r.T_VerifyTime.Format("2006-01-02 15:04:05")
  83. t.T_CalibrationTime = r.T_CalibrationTime.Format("2006-01-02 15:04:05")
  84. t.T_ist = r.T_ist
  85. t.T_ish = r.T_ish
  86. t.T_Dattery = r.T_Dattery
  87. t.T_Site = r.T_Site
  88. t.T_type = r.T_type
  89. t.T_give = r.T_give
  90. t.T_monitor = r.T_monitor
  91. t.T_online = r.T_online
  92. t.T_online_s = r.T_online_s
  93. t.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  94. t.UpdateTime = r.UpdateTime.Format("2006-01-02 15:04:05")
  95. return
  96. }
  97. // ---------------- Redis -------------------
  98. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  99. func Redis_Set(r Device) (err error) {
  100. //json序列化
  101. str, err := json.Marshal(r)
  102. if err != nil {
  103. fmt.Print(err)
  104. return
  105. }
  106. err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour)
  107. if err != nil {
  108. fmt.Println("set key:", r.T_sn, ",value:", str, err)
  109. }
  110. return
  111. }
  112. // if r,is :=Redis_Get(T_sn);is{
  113. // return r,nil
  114. // }
  115. func Redis_Get(key string) (r Device, is bool) {
  116. if redisCache_Device.IsExist(key) {
  117. //println("找到key:",key)
  118. v := redisCache_Device.Get(key)
  119. json.Unmarshal(v.([]byte), &r)
  120. return r, true
  121. }
  122. //println("没有 找到key:",key)
  123. return Device{}, false
  124. }
  125. func Redis_DelK(key string) (err error) {
  126. err = redisCache_Device.Delete(key)
  127. return
  128. }
  129. // ---------------- 特殊方法 -------------------
  130. // 获取 ById
  131. func Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  132. if r, is := Redis_Get(T_sn); is {
  133. //println("Redis_Get OK")
  134. return r, nil
  135. }
  136. //println("没有 Redis_Get SN")
  137. o := orm.NewOrm()
  138. r = Device{T_sn: T_sn, T_State: 1}
  139. err = o.Read(&r, "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  140. if err != nil {
  141. fmt.Println(err)
  142. } else {
  143. Redis_Set(r) // Redis 更新缓存
  144. }
  145. return r, err
  146. }
  147. // 添加
  148. func Add_Device(m Device) (err error) {
  149. o := orm.NewOrm()
  150. _, err = o.Insert(&m)
  151. if err != nil {
  152. fmt.Println(err)
  153. }
  154. Redis_Set(m) // Redis 更新缓存
  155. return err
  156. }
  157. // 删除
  158. func Delete_Device(m Device) (err error) {
  159. o := orm.NewOrm()
  160. if num, err := o.Delete(&m); err == nil {
  161. fmt.Println("Number of records deleted in database:", num)
  162. }
  163. Redis_DelK(m.T_sn)
  164. return
  165. }
  166. // 修改
  167. func Update_Device(r Device, cols ...string) bool {
  168. o := orm.NewOrm()
  169. if num, err := o.Update(&r, cols...); err == nil {
  170. fmt.Println("Number of records updated in database:", num)
  171. Redis_Set(r) // Redis 更新缓存
  172. return true
  173. }
  174. return false
  175. }
  176. // 获取列表
  177. func Read_Device_List(T_pid int, T_name string, T_monitor string, T_online string, page int, page_z int) (r []Device_R, cnt int64) {
  178. o := orm.NewOrm()
  179. // 也可以直接使用 Model 结构体作为表名
  180. qs := o.QueryTable(new(Device))
  181. var offset int64
  182. if page_z == 0 {
  183. page_z = conf.Page_size
  184. }
  185. if page <= 1 {
  186. offset = 0
  187. } else {
  188. offset = int64((page - 1) * page_z)
  189. }
  190. cond := orm.NewCondition()
  191. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  192. if len(T_name) > 0 {
  193. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name).Or("T_MSISDN__icontains", T_name))
  194. }
  195. if len(T_monitor) > 0 {
  196. T_monitor_int, err := strconv.Atoi(T_monitor)
  197. if err == nil {
  198. cond1 = cond1.AndCond(cond.And("T_monitor", T_monitor_int))
  199. }
  200. }
  201. if T_online == "1" {
  202. cond1 = cond1.AndCond(cond.And("T_online", 1))
  203. } else if T_online == "2" {
  204. cond1 = cond1.AndCond(cond.And("T_online", 2))
  205. } else if T_online == "0" {
  206. cond1 = cond1.AndCond(cond.And("T_online", 0))
  207. }
  208. var rx []Device
  209. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").OrderBy("-T_give").All(&rx)
  210. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  211. for _, v := range rx {
  212. r = append(r, DeviceToDevice_R(v))
  213. }
  214. return r, cnt
  215. }
  216. func Read_Device_ALL_T_Type_Count(T_pid int, T_type int) (cnt int64) {
  217. o := orm.NewOrm()
  218. qs := o.QueryTable(new(Device))
  219. cnt, _ = qs.Filter("T_pid", T_pid).Filter("T_type", T_type).Filter("T_State", 1).Count()
  220. return cnt
  221. }