ProductProt.go 5.2 KB

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