DeviceClass.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package Device
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "time"
  13. )
  14. // 模板
  15. type DeviceClass struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_uuid string `orm:"size(256);null"` // 管理员 UUID
  18. T_name string `orm:"size(256);null"` // 标题
  19. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  20. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  21. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  22. }
  23. func (t *DeviceClass) TableName() string {
  24. return "device_class" // 数据库名称 // ************** 替换 FormulaList **************
  25. }
  26. var redisCache_DeviceClass cache.Cache
  27. func init() {
  28. //注册模型
  29. orm.RegisterModel(new(DeviceClass))
  30. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  31. "redis_"+"DeviceClass", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  32. logs.Println(config)
  33. var err error
  34. redisCache_DeviceClass, err = cache.NewCache("redis", config)
  35. if err != nil || redisCache_DeviceClass == nil {
  36. errMsg := "failed to init redis"
  37. logs.Println(errMsg, err)
  38. }
  39. }
  40. // ---------------- Redis -------------------
  41. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  42. func Redis_DeviceClass_Set(key string, r DeviceClass) (err error) {
  43. //json序列化
  44. str, err := json.Marshal(r)
  45. if err != nil {
  46. logs.Error(lib.FuncName(), err)
  47. return
  48. }
  49. err = redisCache_DeviceClass.Put(key, str, 24*time.Hour)
  50. if err != nil {
  51. logs.Println("set key:", key, ",value:", str, err)
  52. }
  53. return
  54. }
  55. // if r,is :=Redis_Get(T_sn);is{
  56. // return r,nil
  57. // }
  58. func Redis_DeviceClass_Get(key string) (r DeviceClass, is bool) {
  59. if redisCache_DeviceClass.IsExist(key) {
  60. logs.Println("找到key:", key)
  61. v := redisCache_DeviceClass.Get(key)
  62. json.Unmarshal(v.([]byte), &r)
  63. return r, true
  64. }
  65. logs.Println("没有 找到key:", key)
  66. return DeviceClass{}, false
  67. }
  68. func Redis_DeviceClass_DelK(key string) (err error) {
  69. err = redisCache_DeviceClass.Delete(key)
  70. return
  71. }
  72. // ---------------- 特殊方法 -------------------
  73. // 获取 ById
  74. func Read_DeviceClass_ById(id int) (r DeviceClass, is bool) {
  75. o := orm.NewOrm()
  76. r = DeviceClass{Id: id}
  77. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  78. if err != nil {
  79. logs.Error(lib.FuncName(), err)
  80. return r, false
  81. }
  82. return r, true
  83. }
  84. // 添加
  85. func Add_DeviceClass(r DeviceClass) (id int64, is bool) {
  86. o := orm.NewOrm()
  87. id, err := o.Insert(&r)
  88. if err != nil {
  89. logs.Error(lib.FuncName(), err)
  90. return 0, false
  91. }
  92. return id, true
  93. }
  94. // 删除
  95. func Delete_DeviceClass(v DeviceClass) bool {
  96. o := orm.NewOrm()
  97. if num, err := o.Delete(&v); err == nil {
  98. logs.Println("Number of records deleted in database:", num)
  99. } else {
  100. logs.Println(lib.FuncName(), err)
  101. return false
  102. }
  103. return true
  104. }
  105. // 删除
  106. func Delete_DeviceClass_(v DeviceClass) bool {
  107. o := orm.NewOrm()
  108. v.T_State = 0
  109. if num, err := o.Update(&v, "T_State"); err == nil {
  110. logs.Println("Number of records updated in database:", num)
  111. } else {
  112. logs.Println(lib.FuncName(), err)
  113. return false
  114. }
  115. return true
  116. }
  117. // 修改
  118. func Update_DeviceClass(m DeviceClass, cols ...string) bool {
  119. o := orm.NewOrm()
  120. if num, err := o.Update(&m, cols...); err == nil {
  121. logs.Println("Number of records updated in database:", num)
  122. return true
  123. } else {
  124. logs.Println(lib.FuncName(), err)
  125. }
  126. return false
  127. }
  128. // 获取列表
  129. func Read_DeviceClass_List(T_uuid string, T_name string, page int, page_z int) (r []DeviceClass, cnt int64) {
  130. o := orm.NewOrm()
  131. // 也可以直接使用 Model 结构体作为表名
  132. qs := o.QueryTable(new(DeviceClass))
  133. var offset int64
  134. if page <= 1 {
  135. offset = 0
  136. } else {
  137. offset = int64((page - 1) * page_z)
  138. }
  139. cond := orm.NewCondition()
  140. cond1 := cond.And("T_uuid", T_uuid).And("T_name__icontains", T_name).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  141. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  142. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  143. return r, cnt
  144. }