UserDevice.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. "github.com/beego/beego/v2/core/logs"
  10. "time"
  11. )
  12. type UserDevice struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_sn string `orm:"size(256);null"` // 设备序列号
  15. T_uuid string `orm:"index;size(256);null"` // Account.Company 绑定用户
  16. }
  17. var redisCache_UserDevice cache.Cache
  18. func init() {
  19. //注册模型
  20. orm.RegisterModel(new(UserDevice))
  21. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  22. "redis_UserDevice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  23. fmt.Println(config)
  24. var err error
  25. redisCache_UserDevice, err = cache.NewCache("redis", config)
  26. if err != nil || redisCache_UserDevice == nil {
  27. errMsg := "failed to init redis"
  28. logs.Error(errMsg, err)
  29. panic(errMsg)
  30. }
  31. }
  32. // ---------------- Redis -------------------
  33. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  34. func Redis_UserDevice_Set(T_uuid string, deviceList []string) (err error) {
  35. //json序列化
  36. str, err := json.Marshal(deviceList)
  37. if err != nil {
  38. logs.Error(lib.FuncName(), err)
  39. return
  40. }
  41. err = redisCache_UserDevice.Put(T_uuid, str, 24*time.Hour)
  42. if err != nil {
  43. logs.Error("Redis_UserDevice_Set", "set key:", T_uuid, ",value:", deviceList, err)
  44. }
  45. return
  46. }
  47. // if r,is :=Redis_Get(T_sn);is{
  48. // return r,nil
  49. // }
  50. func Redis_UserDevice_Get(key string) (r []string, is bool) {
  51. if redisCache_UserDevice.IsExist(key) {
  52. //println("找到key:",key)
  53. v := redisCache_UserDevice.Get(key)
  54. err := json.Unmarshal(v.([]byte), &r)
  55. if err != nil {
  56. logs.Error(lib.FuncName(), err)
  57. return []string{}, false
  58. }
  59. return r, true
  60. }
  61. //println("没有 找到key:",key)
  62. return []string{}, false
  63. }
  64. func Redis_UserDevice_DelK(key string) (err error) {
  65. err = redisCache_UserDevice.Delete(key)
  66. if err != nil {
  67. logs.Error(lib.FuncName(), err)
  68. }
  69. return
  70. }
  71. // ---------------- 特殊方法 -------------------
  72. // 添加
  73. func Add_UserDevice(m UserDevice) (id int64, err error) {
  74. o := orm.NewOrm()
  75. id, err = o.Insert(&m)
  76. if err != nil {
  77. logs.Error(lib.FuncName(), err)
  78. return id, err
  79. }
  80. Redis_UserDevice_DelK(m.T_uuid)
  81. return id, err
  82. }
  83. // 批量添加主机
  84. func Add_UserDevice_Multi(T_uuid string, m []UserDevice) (id int64, err error) {
  85. o := orm.NewOrm()
  86. id, err = o.InsertMulti(len(m), &m)
  87. if err != nil {
  88. logs.Error(lib.FuncName(), err)
  89. return id, err
  90. }
  91. Redis_UserDevice_DelK(T_uuid)
  92. return id, err
  93. }
  94. func Delete_UserDevice(v UserDevice) error {
  95. o := orm.NewOrm()
  96. _, err := o.Delete(&v)
  97. if err != nil {
  98. logs.Error(lib.FuncName(), err)
  99. return err
  100. }
  101. Redis_UserDevice_DelK(v.T_uuid)
  102. return nil
  103. }
  104. func Read_UserDevice_List(admin Admin) (UserDeviceList []string, err error) {
  105. if admin.T_pid == 0 {
  106. return
  107. }
  108. T_uuid := admin.T_uuid
  109. if r, is := Redis_UserDevice_Get(T_uuid); is {
  110. //println("Redis_Get OK")
  111. return r, nil
  112. }
  113. o := orm.NewOrm()
  114. // 也可以直接使用 Model 结构体作为表名
  115. qs := o.QueryTable(new(UserDevice))
  116. var maps []UserDevice
  117. _, err = qs.Filter("T_uuid", T_uuid).All(&maps)
  118. if err != nil {
  119. logs.Error(lib.FuncName(), err)
  120. return
  121. }
  122. deviceList := []string{}
  123. for _, device := range maps {
  124. deviceList = append(deviceList, device.T_sn)
  125. }
  126. Redis_UserDevice_Set(T_uuid, deviceList)
  127. return deviceList, nil
  128. }
  129. func Read_UserDevice_ByT_uuid_T_sn(T_uuid, sn string) (r UserDevice, err error) {
  130. o := orm.NewOrm()
  131. // 也可以直接使用 Model 结构体作为表名
  132. qs := o.QueryTable(new(UserDevice))
  133. err = qs.Filter("T_uuid", T_uuid).Filter("T_sn", sn).One(&r)
  134. if err != nil {
  135. logs.Error(lib.FuncName(), err)
  136. return
  137. }
  138. return
  139. }