ProductUpgrade.go 5.1 KB

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