ProductProt.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package Product
  2. import (
  3. "Yunlot/conf"
  4. "Yunlot/logs"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  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 ProductProt struct {
  16. Id int `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
  17. T_name string `orm:"size(256);" json:"T_name" form:"T_name"` // 协议名称
  18. T_mode int `orm:"size(1);default(0)" json:"T_Mode" form:"T_mode"` //接入方式 0:Mqtt 1:http 2:tcp 3:CoAP 4:websocket
  19. //T_prot int `orm:"size(1);default(0)" json:"T_Prot" form:"T_prot"` //接入协议ID 0:统一协议
  20. T_lang int `orm:"size(1);default(1)" json:"T_lang" form:"T_lang"` //编程语言 0: C 1: go
  21. T_analysis string `orm:"size(100);default(yunlot-pxxxx-v1.19-01.so)" json:"T_analysis" form:"T_analysis"` //数据解析
  22. T_text string `orm:"type(text);default('')" json:"T_text" form:"T_text"` // 代码内容
  23. T_describe string `orm:"type(text);default('')" json:"T_describe" form:"T_describe"` // 描述内容
  24. //T_reply string `orm:"size(100);default(_reply)" json:"T_reply" form:"T_reply"` //返回后缀 _reply
  25. }
  26. func (t *ProductProt) TableName() string {
  27. return "ProductProt" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. var redis_ProductProt cache.Cache
  30. func init() {
  31. //注册模型
  32. orm.RegisterModel(new(ProductProt))
  33. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  34. "ProductProt", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  35. var err error
  36. redis_ProductProt, err = cache.NewCache("redis", config)
  37. if err != nil || redis_ProductProt == nil {
  38. logs.Println(config)
  39. println(err)
  40. panic(any(err))
  41. }
  42. }
  43. // ---------------- Redis -------------------
  44. func (t *ProductProt) redis_Set() (err error) {
  45. //json序列化
  46. str, err := json.Marshal(t)
  47. if err != nil {
  48. logs.PrintlnError("Redis_Set", err)
  49. return
  50. }
  51. err = redis_ProductProt.Put(fmt.Sprintf("%d", t.Id), str, 24*time.Hour)
  52. if err != nil {
  53. logs.Println("set key:", t.Id, ",value:", str, err)
  54. }
  55. return
  56. }
  57. func (t *ProductProt) redis_Get(key string) (is bool) {
  58. if redis_ProductProt.IsExist(key) {
  59. //println("找到key:",key)
  60. v := redis_ProductProt.Get(key)
  61. if v == nil {
  62. return false
  63. }
  64. json.Unmarshal(v.([]byte), t)
  65. return true
  66. }
  67. //println("没有 找到key:",key)
  68. return false
  69. }
  70. func (t *ProductProt) redis_DelK() (err error) {
  71. err = redis_ProductProt.Delete(fmt.Sprintf("%d", t.Id))
  72. return
  73. }
  74. // ---------------- 方法 -------------------
  75. // 添加
  76. func (t *ProductProt) Add() (is bool) {
  77. o := orm.NewOrm()
  78. id, err := o.Insert(t)
  79. if err != nil {
  80. return false
  81. }
  82. println(id)
  83. t.redis_Set()
  84. return true
  85. }
  86. // 获取
  87. func (t *ProductProt) Read() (bool bool) {
  88. if t.redis_Get(fmt.Sprintf("%d", t.Id)) {
  89. return true
  90. }
  91. o := orm.NewOrm()
  92. err := o.Read(&t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  93. if err != nil {
  94. return false
  95. }
  96. t.redis_Set() // Redis 更新缓存
  97. return true
  98. }
  99. // 修改
  100. func (t *ProductProt) Update(cols ...string) bool {
  101. o := orm.NewOrm()
  102. if num, err := o.Update(t, cols...); err == nil {
  103. logs.Println("Number of records updated in database:", num)
  104. t.redis_Set() // Redis 更新缓存
  105. return true
  106. }
  107. return false
  108. }
  109. // 删除
  110. func (t *ProductProt) Delete() bool {
  111. o := orm.NewOrm()
  112. if num, err := o.Delete(t); err == nil {
  113. logs.Println("Number of records deleted in database:", num)
  114. } else {
  115. return false
  116. }
  117. t.redis_DelK()
  118. return true
  119. }
  120. // 获取列表
  121. func (t *ProductProt) Lists(PageIndex int, PageSize int) (r []ProductProt, Total int64) {
  122. o := orm.NewOrm()
  123. // 也可以直接使用 Model 结构体作为表名
  124. qs := o.QueryTable(new(ProductProt))
  125. var offset int64
  126. if PageIndex <= 1 {
  127. offset = 0
  128. } else {
  129. offset = int64((PageIndex - 1) * PageSize)
  130. }
  131. // 筛选参数
  132. cond := orm.NewCondition()
  133. cond = cond.And("T_mode", t.T_mode)
  134. if len(t.T_name) > 0 {
  135. cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  136. }
  137. // 执行
  138. qs.Limit(PageSize, offset).SetCond((*orm2.Condition)(cond)).All(&r)
  139. Total, _ = qs.SetCond((*orm2.Condition)(cond)).Count()
  140. return r, Total
  141. }
  142. // 获取列表
  143. func (t *ProductProt) Lists_All() (r []ProductProt) {
  144. o := orm.NewOrm()
  145. // 也可以直接使用 Model 结构体作为表名
  146. qs := o.QueryTable(new(ProductProt))
  147. // 筛选参数
  148. cond := orm.NewCondition()
  149. if t.T_mode > 0 {
  150. cond = cond.And("T_mode", t.T_mode) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  151. }
  152. if len(t.T_name) > 0 {
  153. cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  154. }
  155. // 执行
  156. qs.SetCond((*orm2.Condition)(cond)).All(&r)
  157. return r
  158. }