ProductProt.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if !(t.Id > 0) {
  92. logs.Println("ProductProt.Id:", t.Id)
  93. return false
  94. }
  95. o := orm.NewOrm()
  96. err := o.Read(t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  97. if err != nil {
  98. return false
  99. }
  100. t.redis_Set() // Redis 更新缓存
  101. return true
  102. }
  103. // 修改
  104. func (t *ProductProt) Update(cols ...string) bool {
  105. o := orm.NewOrm()
  106. if num, err := o.Update(t, cols...); err == nil {
  107. logs.Println("Number of records updated in database:", num)
  108. t.redis_Set() // Redis 更新缓存
  109. return true
  110. }
  111. return false
  112. }
  113. // 删除
  114. func (t *ProductProt) Delete() bool {
  115. o := orm.NewOrm()
  116. if num, err := o.Delete(t); err == nil {
  117. logs.Println("Number of records deleted in database:", num)
  118. } else {
  119. return false
  120. }
  121. t.redis_DelK()
  122. return true
  123. }
  124. // 获取列表
  125. func (t *ProductProt) Lists(PageIndex int, PageSize int) (r []ProductProt, Total int64) {
  126. o := orm.NewOrm()
  127. // 也可以直接使用 Model 结构体作为表名
  128. qs := o.QueryTable(new(ProductProt))
  129. var offset int64
  130. if PageIndex <= 1 {
  131. offset = 0
  132. } else {
  133. offset = int64((PageIndex - 1) * PageSize)
  134. }
  135. // 筛选参数
  136. cond := orm.NewCondition()
  137. cond = cond.And("T_mode", t.T_mode)
  138. if len(t.T_name) > 0 {
  139. cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  140. }
  141. // 执行
  142. qs.Limit(PageSize, offset).SetCond((*orm2.Condition)(cond)).All(&r)
  143. Total, _ = qs.SetCond((*orm2.Condition)(cond)).Count()
  144. return r, Total
  145. }
  146. // 获取列表
  147. func (t *ProductProt) Lists_All() (r []ProductProt) {
  148. o := orm.NewOrm()
  149. // 也可以直接使用 Model 结构体作为表名
  150. qs := o.QueryTable(new(ProductProt))
  151. // 筛选参数
  152. cond := orm.NewCondition()
  153. if t.T_mode > 0 {
  154. cond = cond.And("T_mode", t.T_mode) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  155. }
  156. if len(t.T_name) > 0 {
  157. cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  158. }
  159. // 执行
  160. qs.SetCond((*orm2.Condition)(cond)).All(&r)
  161. return r
  162. }