ProductUpgrade.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package Product
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "encoding/json"
  5. "github.com/astaxie/beego/cache"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. "github.com/beego/beego/v2/core/logs"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "time"
  13. )
  14. // 产品类型
  15. type ProductUpgrade struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_model string `orm:"size(256);null"` // 产品型号
  18. T_version string `orm:"size(256);null"` // 版本
  19. T_file string `orm:"size(256);null"` // 文件
  20. T_remarks string `orm:"size(256);null"` // 备注
  21. T_carryout int `orm:"size(1);null"` // 全量发布
  22. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  23. }
  24. type ProductUpgrade_T struct {
  25. T_sn string `orm:"size(256);null"` // 设备编号
  26. T_version string `orm:"size(256);null"` // 版本
  27. T_file string `orm:"size(256);null"` // 文件
  28. }
  29. type ProductUpgrade_R struct {
  30. Id int
  31. T_model string
  32. T_version string
  33. T_file string
  34. T_remarks string
  35. T_carryout int
  36. CreateTime string
  37. }
  38. func (t *ProductUpgrade) TableName() string {
  39. return "product_upgrade" // 数据库名称 // ************** 替换 FormulaList **************
  40. }
  41. var redisCache_ProductUpgrade cache.Cache
  42. func init() {
  43. //注册模型
  44. orm.RegisterModel(new(ProductUpgrade))
  45. //config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  46. // "redis_ProductUpgrade", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  47. //fmt.Println(config)
  48. //var err error
  49. //redisCache_ProductUpgrade, err = cache.NewCache("redis", config)
  50. //if err != nil || redisCache_ProductUpgrade == nil {
  51. // errMsg := "failed to init redis"
  52. // panic(errMsg)
  53. //}
  54. }
  55. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  56. func Redis_ProductUpgrade_Set(r ProductUpgrade) (err error) {
  57. key := strconv.Itoa(r.Id)
  58. //json序列化
  59. str, err := json.Marshal(r)
  60. if err != nil {
  61. logs.Error(lib.FuncName(), err)
  62. return
  63. }
  64. err = redisCache_ProductUpgrade.Put(key, str, 2*time.Hour)
  65. if err != nil {
  66. logs.Error("set key:", key, ",value:", str, err)
  67. }
  68. return
  69. }
  70. // if r,is :=Redis_Get(T_sn);is{
  71. // return r,nil
  72. // }
  73. func Redis_ProductUpgrade_Get(key string) (ProductUpgrade, bool) {
  74. println("找到key:", key)
  75. if redisCache_ProductUpgrade.IsExist(key) {
  76. //println("找到key:",key)
  77. v := redisCache_ProductUpgrade.Get(key)
  78. var r ProductUpgrade
  79. err := json.Unmarshal(v.([]byte), &r)
  80. if err != nil {
  81. logs.Error(lib.FuncName(), err)
  82. return ProductUpgrade{}, false
  83. }
  84. return r, true
  85. }
  86. return ProductUpgrade{}, false
  87. }
  88. func Redis_ProductUpgrade_DelK(key string) (err error) {
  89. err = redisCache_ProductUpgrade.Delete(key)
  90. if err != nil {
  91. logs.Error(lib.FuncName(), err)
  92. }
  93. return
  94. }
  95. // ---------------- 特殊方法 -------------------
  96. func ProductUpgradeToProductUpgrade_R(t ProductUpgrade) (r ProductUpgrade_R) {
  97. r.Id = t.Id
  98. r.T_model = t.T_model
  99. r.T_version = t.T_version
  100. r.T_file = t.T_file
  101. r.T_remarks = t.T_remarks
  102. r.T_carryout = t.T_carryout
  103. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  104. return r
  105. }
  106. // 获取 ById
  107. func Read_ProductUpgrade_ById(id int) (r ProductUpgrade, err error) {
  108. //key := strconv.Itoa(id)
  109. //if r, is := Redis_ProductUpgrade_Get(key); is {
  110. // return r, nil
  111. //}
  112. o := orm.NewOrm()
  113. r = ProductUpgrade{Id: id}
  114. err = o.Read(&r)
  115. if err != nil {
  116. logs.Error(lib.FuncName(), err)
  117. return r, err
  118. }
  119. //Redis_ProductUpgrade_Set(r)
  120. return r, err
  121. }
  122. // 添加
  123. func Add_ProductUpgrade(m ProductUpgrade) (id int64, err error) {
  124. o := orm.NewOrm()
  125. id, err = o.Insert(&m)
  126. if err != nil {
  127. logs.Error(lib.FuncName(), err)
  128. return
  129. }
  130. //Redis_ProductUpgrade_Set(m)
  131. return
  132. }
  133. // 删除
  134. func Delete_ProductUpgrade_ById(id int) bool {
  135. o := orm.NewOrm()
  136. v := ProductUpgrade{Id: id}
  137. _, err := o.Delete(&v)
  138. if err != nil {
  139. logs.Error(lib.FuncName(), err)
  140. return false
  141. }
  142. return true
  143. }
  144. // 获取列表
  145. func Read_ProductUpgrade_List(T_model string, page int, page_z int) (r []ProductUpgrade_R, cnt int64) {
  146. o := orm.NewOrm()
  147. // 也可以直接使用 Model 结构体作为表名
  148. var map_r []ProductUpgrade
  149. qs := o.QueryTable(new(ProductUpgrade))
  150. var offset int64
  151. if page <= 1 {
  152. offset = 0
  153. } else {
  154. offset = int64((page - 1) * page_z)
  155. }
  156. cond := orm.NewCondition()
  157. if len(T_model) > 0 {
  158. cond = cond.And("T_model", T_model)
  159. }
  160. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  161. if err != nil {
  162. logs.Error(lib.FuncName(), err)
  163. return
  164. }
  165. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  166. if err != nil {
  167. logs.Error(lib.FuncName(), err)
  168. return
  169. }
  170. for _, v := range map_r {
  171. r = append(r, ProductUpgradeToProductUpgrade_R(v))
  172. }
  173. return r, cnt
  174. }
  175. // 获取列表
  176. func Read_ProductUpgrade_T_model() (lists orm2.ParamsList) {
  177. o := orm.NewOrm()
  178. var pl_lists orm2.ParamsList
  179. _, err := o.Raw("SELECT DISTINCT t_model FROM product_upgrade LIMIT 0,1000").ValuesFlat(&pl_lists)
  180. if err != nil {
  181. logs.Error(lib.FuncName(), err)
  182. }
  183. return pl_lists
  184. }