Device.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package Device
  2. import (
  3. "Yunlot/conf"
  4. "Yunlot/lib"
  5. "Yunlot/logs"
  6. "Yunlot/models"
  7. "Yunlot/models/Product"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/astaxie/beego/cache"
  11. _ "github.com/astaxie/beego/cache/redis"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. orm2 "github.com/beego/beego/v2/client/orm"
  14. _ "github.com/go-sql-driver/mysql"
  15. "time"
  16. )
  17. // 设备
  18. type Device struct {
  19. T_sn string `orm:"size(256);pk" json:"T_sn" form:"T_sn"` // Sn
  20. T_password string `orm:"size(256);" json:"T_password" form:"T_password"` // 密码
  21. T_online int `orm:"size(1);index;default(0)" json:"T_online" form:"T_online"` // 在线状态 0 离线 1 在线 3 无效
  22. T_ProductID string `orm:"size(8);index" json:"T_ProductID" form:"T_ProductID"` // 产品类型
  23. T_ProductJson Product.ProductType `orm:"-" json:"T_ProductJson"` // 产品类型 json
  24. T_data string `orm:"column(t_data);type(text);default('')" json:"T_data" ` // 设备数据
  25. T_dataJson map[string]interface{} `orm:"-" json:"T_dataJson"` // 设备数据
  26. T_state int `orm:"size(2);default(0);index" json:"T_state" form:"T_state"` // 0 未激活 1 正常 2 禁用\删除
  27. CreateTime models.Time `orm:"column(create_time);type(timestamp);auto_now_add"`
  28. UpdateTime models.Time `orm:"column(update_time);type(timestamp);auto_now"`
  29. }
  30. func (t *Device) TableName() string {
  31. return "Device" // 数据库名称 // ************** 替换 FormulaList **************
  32. }
  33. var redis_Device cache.Cache
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(Device))
  37. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  38. "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  39. var err error
  40. redis_Device, err = cache.NewCache("redis", config)
  41. if err != nil || redis_Device == nil {
  42. logs.Println(config)
  43. panic(any(err))
  44. }
  45. }
  46. // ---------------- Redis -------------------
  47. func (t *Device) redis_Set() (err error) {
  48. if len(t.T_data) > 5 {
  49. var T_datajson map[string]interface{}
  50. json.Unmarshal([]byte(t.T_data), &T_datajson)
  51. t.T_dataJson = T_datajson
  52. }
  53. if len(t.T_ProductID) > 0 {
  54. t.T_ProductJson.T_ProductID = t.T_ProductID
  55. t.T_ProductJson.Read()
  56. }
  57. //json序列化
  58. str, err := json.Marshal(t)
  59. if err != nil {
  60. logs.PrintlnError("Redis_Set", err)
  61. return
  62. }
  63. err = redis_Device.Put(t.T_sn, str, 24*time.Hour)
  64. if err != nil {
  65. logs.Println("set key:", t.T_sn, ",value:", str, err)
  66. }
  67. return
  68. }
  69. func (t *Device) redis_Get(key string) (is bool) {
  70. if redis_Device.IsExist(key) {
  71. //println("找到key:",key)
  72. v := redis_Device.Get(key)
  73. if v == nil {
  74. return false
  75. }
  76. json.Unmarshal(v.([]byte), t)
  77. if t.T_state == 3 { // 3 无效
  78. return false
  79. }
  80. return true
  81. }
  82. //println("没有 找到key:",key)
  83. return false
  84. }
  85. func (t *Device) redis_DelK() (err error) {
  86. err = redis_Device.Delete(t.T_sn)
  87. return
  88. }
  89. // ---------------- 方法 -------------------
  90. // 获取
  91. func (t *Device) Read() (is bool) {
  92. o := orm.NewOrm()
  93. err := o.Read(t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  94. if err != nil {
  95. return false
  96. }
  97. t.redis_Set()
  98. return true
  99. }
  100. // 获取-高效的
  101. func (t *Device) Read_Tidy() (bool bool) {
  102. if t.redis_Get(t.T_sn) {
  103. return true
  104. }
  105. o := orm.NewOrm()
  106. err := o.Read(t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  107. if err != nil {
  108. t.T_state = 3 // 3 无效
  109. t.redis_Set() // Redis 更新缓存
  110. return false
  111. }
  112. return true
  113. }
  114. func (Device_r *Device) CreateSn(GetRandstringI int) bool {
  115. GetRandstringI += GetRandstringI * 100
  116. if len(Device_r.T_sn) < 10 {
  117. for true {
  118. Device_r.T_sn = lib.WeekByDate() + lib.GetRandstring(10, "0123456789123456789", int64(GetRandstringI))
  119. Device_is := Device{T_sn: Device_r.T_sn}
  120. if !Device_is.Read() {
  121. break
  122. }
  123. GetRandstringI += 1
  124. }
  125. } else {
  126. Device_is := Device{T_sn: Device_r.T_sn}
  127. if Device_is.Read() {
  128. return false
  129. }
  130. Device_r.Add()
  131. }
  132. GetRandstringI += 1
  133. // mysql 秘钥
  134. Device_r.T_password = lib.GetRandstring(16, "", int64(GetRandstringI))
  135. //println(Device_r.T_password)
  136. //Device_r.T_password = lib.Sha256(Device_r.T_password)
  137. Device_r.Add()
  138. return true
  139. }
  140. // 添加
  141. func (t *Device) Add() (is bool) {
  142. o := orm.NewOrm()
  143. id, err := o.Insert(t)
  144. if err != nil {
  145. return false
  146. }
  147. println(id)
  148. t.redis_Set()
  149. return true
  150. }
  151. // 修改
  152. func (t *Device) Update(cols ...string) bool {
  153. data, _ := json.Marshal(t.T_dataJson)
  154. t.T_data = string(data)
  155. o := orm.NewOrm()
  156. if num, err := o.Update(t, cols...); err == nil {
  157. logs.Println("Number of records updated in database:", num)
  158. t.redis_Set() // Redis 更新缓存
  159. return true
  160. }
  161. return false
  162. }
  163. // 删除
  164. func (t *Device) Delete() bool {
  165. o := orm.NewOrm()
  166. if num, err := o.Delete(t); err == nil {
  167. logs.Println("Number of records deleted in database:", num)
  168. } else {
  169. return false
  170. }
  171. t.redis_DelK()
  172. return true
  173. }
  174. // 获取列表
  175. func (t *Device) Lists(PageIndex int, PageSize int) (r []Device, Total int64) {
  176. o := orm.NewOrm()
  177. // 也可以直接使用 Model 结构体作为表名
  178. qs := o.QueryTable(new(Device))
  179. var offset int64
  180. if PageIndex <= 1 {
  181. offset = 0
  182. } else {
  183. offset = int64((PageIndex - 1) * PageSize)
  184. }
  185. // 筛选参数
  186. cond := orm.NewCondition()
  187. if len(t.T_ProductID) > 0 {
  188. cond = cond.And("T_ProductID", t.T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  189. }
  190. // 执行
  191. qs.Limit(PageSize, offset).SetCond((*orm2.Condition)(cond)).All(&r)
  192. Total, _ = qs.SetCond((*orm2.Condition)(cond)).Count()
  193. for i, _ := range r {
  194. if len(r[i].T_ProductID) > 0 {
  195. r[i].T_ProductJson.T_ProductID = r[i].T_ProductID
  196. r[i].T_ProductJson.Read()
  197. }
  198. if len(t.T_data) > 5 {
  199. var T_datajson map[string]interface{}
  200. json.Unmarshal([]byte(t.T_data), &T_datajson)
  201. t.T_dataJson = T_datajson
  202. }
  203. }
  204. return r, Total
  205. }
  206. // 获取列表
  207. func (t *Device) Lists_All() (r []Device) {
  208. o := orm.NewOrm()
  209. // 也可以直接使用 Model 结构体作为表名
  210. qs := o.QueryTable(new(Device))
  211. // 筛选参数
  212. cond := orm.NewCondition()
  213. if len(t.T_ProductID) > 0 {
  214. cond = cond.And("T_ProductID", t.T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  215. }
  216. // 执行
  217. qs.SetCond((*orm2.Condition)(cond)).All(&r)
  218. for i, _ := range r {
  219. if len(r[i].T_ProductID) > 0 {
  220. r[i].T_ProductJson.T_ProductID = r[i].T_ProductID
  221. r[i].T_ProductJson.Read()
  222. }
  223. if len(t.T_data) > 5 {
  224. var T_datajson map[string]interface{}
  225. json.Unmarshal([]byte(t.T_data), &T_datajson)
  226. t.T_dataJson = T_datajson
  227. }
  228. }
  229. return r
  230. }