Device.go 5.2 KB

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