DeviceClassList.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package Device
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. "strconv"
  10. "github.com/astaxie/beego/cache"
  11. _ "github.com/astaxie/beego/cache/redis"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. _ "github.com/go-sql-driver/mysql"
  14. "time"
  15. )
  16. // 模板
  17. type DeviceClassList struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_class int `orm:"size(200);null"` // 分类
  20. T_id string `orm:"size(20);null"` // 设备id
  21. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  22. T_failure_time string `orm:"size(256);null"` // 失效时间
  23. T_pdf string `orm:"size(256);null"` // pdf链接
  24. T_remark string `orm:"size(1024);null"` // 备注
  25. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  26. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  27. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  28. }
  29. func (t *DeviceClassList) TableName() string {
  30. return "device_class_list" // 数据库名称 // ************** 替换 FormulaList **************
  31. }
  32. var redisCache_DeviceClassList cache.Cache
  33. func init() {
  34. //注册模型
  35. orm.RegisterModel(new(DeviceClassList))
  36. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  37. "redis_"+"DeviceClassList", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  38. logs.Println(config)
  39. var err error
  40. redisCache_DeviceClassList, err = cache.NewCache("redis", config)
  41. if err != nil || redisCache_DeviceClassList == nil {
  42. errMsg := "failed to init redis"
  43. logs.Println(errMsg, err)
  44. }
  45. }
  46. // ---------------- Redis -------------------
  47. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  48. func Redis_DeviceClassList_Set(key string, r DeviceClassList) (err error) {
  49. //json序列化
  50. str, err := json.Marshal(r)
  51. if err != nil {
  52. logs.Error(lib.FuncName(), err)
  53. return
  54. }
  55. err = redisCache_DeviceClassList.Put(key, str, 24*time.Hour)
  56. if err != nil {
  57. logs.Println("set key:", key, ",value:", str, err)
  58. }
  59. return
  60. }
  61. // if r,is :=Redis_Get(T_sn);is{
  62. // return r,nil
  63. // }
  64. func Redis_DeviceClassList_Get(key string) (r DeviceClassList, is bool) {
  65. if redisCache_DeviceClassList.IsExist(key) {
  66. logs.Println("找到key:", key)
  67. v := redisCache_DeviceClassList.Get(key)
  68. json.Unmarshal(v.([]byte), &r)
  69. return r, true
  70. }
  71. logs.Println("没有 找到key:", key)
  72. return DeviceClassList{}, false
  73. }
  74. func Redis_DeviceClassList_DelK(key string) (err error) {
  75. err = redisCache_DeviceClassList.Delete(key)
  76. return
  77. }
  78. // ---------------- 特殊方法 -------------------
  79. // 获取 ById
  80. func Read_DeviceClassList_ById(id int) (r DeviceClassList, is bool) {
  81. o := orm.NewOrm()
  82. r = DeviceClassList{Id: id}
  83. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  84. if err != nil {
  85. logs.Error(lib.FuncName(), err)
  86. return r, false
  87. }
  88. return r, true
  89. }
  90. // 获取 By
  91. func Read_DeviceClassList(T_sn string) (r DeviceClassList, is bool) {
  92. if r, is = Redis_DeviceClassList_Get(T_sn); is == true {
  93. return r, true
  94. }
  95. o := orm.NewOrm()
  96. qs := o.QueryTable(new(DeviceClassList))
  97. err := qs.Filter("T_sn", T_sn).One(&r)
  98. if err != nil {
  99. logs.Println(lib.FuncName(), err)
  100. return r, false
  101. }
  102. Redis_DeviceClassList_Set(T_sn, r)
  103. return r, true
  104. }
  105. // 添加
  106. func Read_DeviceClassList_T_class_T_sn(T_class int, T_sn string) (r DeviceClassList, is bool) {
  107. o := orm.NewOrm()
  108. qs := o.QueryTable(new(DeviceClassList))
  109. err := qs.Filter("T_sn", T_sn).Filter("T_class", T_class).Filter("T_State", 1).One(&r)
  110. if err != nil {
  111. logs.Println(lib.FuncName(), err)
  112. return r, false
  113. }
  114. return r, true
  115. }
  116. // 添加
  117. func Add_DeviceClassList(r DeviceClassList) (id int64, is bool) {
  118. o := orm.NewOrm()
  119. //r.T_State = 1
  120. //err := o.Read(&r, "T_class", "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  121. //if err == nil {
  122. // if r.Id > 0 {
  123. // logs.Println("重复添加", r.T_class, r.T_sn)
  124. // return 0, false
  125. // }
  126. //}
  127. id, err := o.Insert(&r)
  128. if err != nil {
  129. logs.Error(lib.FuncName(), err)
  130. return 0, false
  131. }
  132. Redis_DeviceClassList_Set(r.T_sn, r)
  133. return id, true
  134. }
  135. // 删除
  136. func Delete_DeviceClassList(v DeviceClassList) bool {
  137. o := orm.NewOrm()
  138. if num, err := o.Delete(&v); err == nil {
  139. logs.Println("Number of records deleted in database:", num)
  140. } else {
  141. logs.Println(lib.FuncName(), err)
  142. return false
  143. }
  144. Redis_DeviceClassList_DelK(v.T_sn)
  145. return true
  146. }
  147. // 删除
  148. func Delete_DeviceClassList_(v DeviceClassList) bool {
  149. o := orm.NewOrm()
  150. v.T_State = 0
  151. if num, err := o.Update(&v, "T_State"); err == nil {
  152. fmt.Println("Number of records updated in database:", num)
  153. } else {
  154. logs.Println(lib.FuncName(), err)
  155. return false
  156. }
  157. Redis_DeviceClassList_DelK(v.T_sn)
  158. return true
  159. }
  160. // 修改
  161. func Update_DeviceClassList(m DeviceClassList, cols ...string) bool {
  162. o := orm.NewOrm()
  163. if num, err := o.Update(&m, cols...); err == nil {
  164. fmt.Println("Number of records updated in database:", num)
  165. Redis_DeviceClassList_Set(m.T_sn, m)
  166. return true
  167. } else {
  168. logs.Println(lib.FuncName(), err)
  169. }
  170. return false
  171. }
  172. // 获取列表
  173. func Read_DeviceClassList_List(T_class int, T_sn string, page int, page_z int) (r []DeviceClassList, cnt int64) {
  174. o := orm.NewOrm()
  175. // 也可以直接使用 Model 结构体作为表名
  176. qs := o.QueryTable(new(DeviceClassList))
  177. var offset int64
  178. if page <= 1 {
  179. offset = 0
  180. } else {
  181. offset = int64((page - 1) * page_z)
  182. }
  183. cond := orm.NewCondition()
  184. cond1 := cond.And("T_class", T_class).And("T_sn__icontains", T_sn).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  185. if page_z == 9999 {
  186. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_id").All(&r)
  187. } else {
  188. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_id").All(&r)
  189. }
  190. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  191. return r, cnt
  192. }
  193. func Read_DeviceClassList_OrderList(T_class int, T_sn string, page int, page_z int) (r []DeviceClassList, cnt int64) {
  194. o := orm.NewOrm()
  195. var maps_z []orm2.ParamsList
  196. pagez := page_z
  197. var offset int
  198. if page <= 1 {
  199. offset = 0
  200. } else {
  201. page -= 1
  202. offset = page * pagez
  203. }
  204. sqlWhere := "t_class = " + strconv.Itoa(T_class) + " AND t__state = 1"
  205. if len(T_sn) > 1 {
  206. sqlWhere += " AND t_sn like \"%" + T_sn + "%\""
  207. }
  208. sql := "SELECT COUNT(ID) FROM device_class_list WHERE " + sqlWhere
  209. fmt.Println(sql)
  210. _, err := o.Raw(sql).ValuesList(&maps_z)
  211. if err != nil {
  212. return r, 0
  213. }
  214. if len(maps_z) == 0 {
  215. return r, 0
  216. }
  217. //fmt.Println("maps_z;",maps_z[0][0])
  218. sql = "SELECT * FROM device_class_list WHERE " + sqlWhere + " ORDER BY t_id+0 "
  219. if page_z != 9999 {
  220. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  221. }
  222. fmt.Println(sql)
  223. _, err = o.Raw(sql).QueryRows(&r)
  224. if err != nil {
  225. logs.Println(lib.FuncName(), err)
  226. return r, 0
  227. }
  228. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  229. key, _ := strconv.Atoi(maps_z[0][0].(string))
  230. return r, int64(key)
  231. }
  232. // 获取列表
  233. func Read_DeviceClassList_List_id(T_class_Id int) (r []DeviceClassList) {
  234. o := orm.NewOrm()
  235. // 也可以直接使用 Model 结构体作为表名
  236. qs := o.QueryTable(new(DeviceClassList))
  237. qs.Filter("T_class", T_class_Id).Filter("T_State", 1).All(&r)
  238. return r
  239. }