Device.go 7.5 KB

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