DeviceClass.go 4.1 KB

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