PanelImg.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package Panel
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/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. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "time"
  13. )
  14. // 模板
  15. type ConfigImg struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_img0 string `orm:"size(256);null"` // 图片
  18. T_img1 string `orm:"size(256);null"` // 图片
  19. T_img2 string `orm:"size(256);null"` // 图片
  20. T_img3 string `orm:"size(256);null"` // 图片
  21. T_img4 string `orm:"size(256);null"` // 图片
  22. T_img5 string `orm:"size(256);null"` // 图片
  23. T_str0 string `orm:"size(256);null"` // 提示文本
  24. T_str1 string `orm:"size(256);null"` // 提示文本
  25. T_str2 string `orm:"size(256);null"` // 提示文本
  26. T_str3 string `orm:"size(256);null"` // 提示文本
  27. T_str4 string `orm:"size(256);null"` // 提示文本
  28. T_str5 string `orm:"size(256);null"` // 提示文本
  29. }
  30. //INSERT INTO `ProductConfigImg` VALUES (24,NULL,'2022-02-25 14:02:59',NULL,'https://standardoss.baozhida.cn/ConfigImg/c1dc79540b042f71e5416a3789b184c3.png','https://standardoss.baozhida.cn/ConfigImg/7bbaad0dc661ba4268a71536a9f15e21.png','https://standardoss.baozhida.cn/ConfigImg/2ea1606f9891137d2054c416618bc70d.png','','',''),(25,NULL,'2022-02-25 14:03:26',NULL,'https://standardoss.baozhida.cn/ConfigImg/f55b91cd3044a0294d6137c3f3ecbc38.png','https://standardoss.baozhida.cn/ConfigImg/f495dcadfae1eced6a0ca43f3e1618a6.png','https://standardoss.baozhida.cn/ConfigImg/1bad9d66625ea51dff8447897f49db23.png','','',''),(26,NULL,'2022-02-25 14:03:42',NULL,'https://standardoss.baozhida.cn/ConfigImg/48fc784ae77fc126114ffe1b9f8184b2.png','https://standardoss.baozhida.cn/ConfigImg/904df97113f0b4d53bd81c1fc4bd73db.png','https://standardoss.baozhida.cn/ConfigImg/ff9beaa73eca8eab3b6430d4cb68ef19.png','','',''),(27,NULL,'2022-02-25 14:03:52',NULL,'https://standardoss.baozhida.cn/ConfigImg/bd9f7e8e754cc010d2d9dc0b991fc14e.png','https://standardoss.baozhida.cn/ConfigImg/83a5dee3cd0bd95597701ba9f68dc7c6.png','https://standardoss.baozhida.cn/ConfigImg/3b4b5df0ef9821bd24a7151ed9432399.png','','',''),(28,NULL,'2022-02-25 14:04:05',NULL,'https://standardoss.baozhida.cn/ConfigImg/8255f092d497fc5e9418d7474b711e4f.png','https://standardoss.baozhida.cn/ConfigImg/a4b5622d7be160607d42f0e28a853c57.png','https://standardoss.baozhida.cn/ConfigImg/f1cb2b0b3ab403c6b6291c0876f597c9.png','','',''),(29,NULL,'2022-02-25 14:04:24',NULL,'https://standardoss.baozhida.cn/ConfigImg/fd0512ebdc4f5291c1df41eaaadeeb52.png','https://standardoss.baozhida.cn/ConfigImg/f68d6259f10299d34d18cfe186af73b9.png','https://standardoss.baozhida.cn/ConfigImg/04894a064ba803cc4f1f260260a239bc.png','','',''),(30,NULL,'2022-02-25 14:04:43',NULL,'https://standardoss.baozhida.cn/ConfigImg/5201691226c58bfef93b7bfed2f16d52.png','https://standardoss.baozhida.cn/ConfigImg/c2d379d6b1c0c6a618d2e13f22a4d7f2.png','https://standardoss.baozhida.cn/ConfigImg/35a4c2e0f46f9f9ac848007d6fc96892.png','','',''),(31,NULL,'2022-02-25 14:05:04',NULL,'https://standardoss.baozhida.cn/ConfigImg/7379573b58871748150d28e42d311d4f.png','https://standardoss.baozhida.cn/ConfigImg/9f944f8ea4e4b3dd2b01e75279e00e6f.png','https://standardoss.baozhida.cn/ConfigImg/bc850ec94724a06386958898e63e686c.png','','',''),(32,NULL,'2022-02-25 14:05:15',NULL,'https://standardoss.baozhida.cn/ConfigImg/e6591afdcb2787afca86ff9960ee3429.png','https://standardoss.baozhida.cn/ConfigImg/98d5cca6ead7ef62798cf947a54b1e83.png','https://standardoss.baozhida.cn/ConfigImg/3c69f8ae5b80b91f02498ac13c611a8b.png','','',''),(33,NULL,'2022-02-25 14:05:36',NULL,'https://standardoss.baozhida.cn/ConfigImg/15d0bad6f07d133301aef897b0484762.png','https://standardoss.baozhida.cn/ConfigImg/4a607bc3abaea243ac3a1da4b3d8422a.png','https://standardoss.baozhida.cn/ConfigImg/8040f817850797a4b1c8f1d6770ecab3.png','','',''),(34,NULL,'2022-02-25 14:06:20',NULL,'https://standardoss.baozhida.cn/ConfigImg/eb9481fc92dc78f36463723da66d36bb.png','https://standardoss.baozhida.cn/ConfigImg/76992e069ac26f530ee2c3ff58f68e14.png','https://standardoss.baozhida.cn/ConfigImg/feb793592b619b4b0ce9718e1d411a85.png','','',''),(35,NULL,'2022-02-25 14:06:34',NULL,'https://standardoss.baozhida.cn/ConfigImg/34d5b40206473c6ef49892b5684c5823.png','https://standardoss.baozhida.cn/ConfigImg/3a00508f0208a72c4329c68f6d183dfc.png','https://standardoss.baozhida.cn/ConfigImg/1cd2da29d463cf39114c3dfa247208f9.png','','',''),(36,NULL,'2022-02-25 14:06:53',NULL,'https://standardoss.baozhida.cn/ConfigImg/ce1a1b1871f76f2cb7377a01553e1752.png','https://standardoss.baozhida.cn/ConfigImg/38eeb5d80894683ef20d82d9a7d60557.png','https://standardoss.baozhida.cn/ConfigImg/3ee36a04824209504584f5d941b75a08.png','','',''),(37,NULL,'2022-02-25 14:07:15',NULL,'https://standardoss.baozhida.cn/ConfigImg/a5f643c58aeeb3acf305c2e3ebc6435f.png','https://standardoss.baozhida.cn/ConfigImg/1028836d862a96710c77e266a7d6e323.png','https://standardoss.baozhida.cn/ConfigImg/6d80b06d35763ec84b987a5466e33715.png','','',''),(38,NULL,'2022-02-25 14:07:30',NULL,'https://standardoss.baozhida.cn/ConfigImg/e913af1ef9a70da88de3ba061ff8b3f6.png','https://standardoss.baozhida.cn/ConfigImg/833a06591b7565bf0ce89509eff8fec6.png','https://standardoss.baozhida.cn/ConfigImg/ea9855988e2947f1c476d94d0525555d.png','','',''),(39,NULL,'2022-02-25 14:07:49',NULL,'https://standardoss.baozhida.cn/ConfigImg/ba9d17552fe7e1ead8dbcf7e18ad3733.png','https://standardoss.baozhida.cn/ConfigImg/130be01a6ec111381664121605189b30.png','https://standardoss.baozhida.cn/ConfigImg/387aa67f2c324266cd236901d2b57059.png','','',''),(40,NULL,'2022-02-25 14:08:28',NULL,'https://standardoss.baozhida.cn/ConfigImg/3b8e34d532ca2dadf7ac62b94c2788c3.png','https://standardoss.baozhida.cn/ConfigImg/618509f6ff64a9c39e9934dc785c595e.png','https://standardoss.baozhida.cn/ConfigImg/94aa48db3f7ff87676c734a376c8e2eb.png','','',''),(41,NULL,'2022-02-25 14:12:33',NULL,'https://standardoss.baozhida.cn/ConfigImg/a59b83758221607750042793409d9362.png','https://standardoss.baozhida.cn/ConfigImg/8e921514d3d91d59ad8de835cca76e2b.png','https://standardoss.baozhida.cn/ConfigImg/7cab99390abe94575dfb6b336a718c4d.png','','','');
  31. func (t *ConfigImg) TableName() string {
  32. return "ConfigImg" // 数据库名称 // ************** 替换 FormulaList **************
  33. }
  34. var redisCache_ConfigImg cache.Cache
  35. func init() {
  36. //注册模型
  37. orm.RegisterModel(new(ConfigImg))
  38. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  39. "redis_"+"ConfigImg", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  40. logs.Println(config)
  41. var err error
  42. redisCache_ConfigImg, err = cache.NewCache("redis", config)
  43. if err != nil || redisCache_ConfigImg == nil {
  44. errMsg := "failed to init redis"
  45. logs.Println(errMsg, err)
  46. }
  47. }
  48. // ---------------- Redis -------------------
  49. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  50. func Redis_ConfigImg_Set(key string, r ConfigImg) (err error) {
  51. //json序列化
  52. str, err := json.Marshal(r)
  53. if err != nil {
  54. logs.Println(err)
  55. return
  56. }
  57. err = redisCache_ConfigImg.Put(key, str, 24*time.Hour)
  58. if err != nil {
  59. logs.Println("set key:", key, ",value:", str, err)
  60. }
  61. return
  62. }
  63. //if r,is :=Redis_Get(T_sn);is{
  64. //return r,nil
  65. //}
  66. func Redis_ConfigImg_Get(key string) (r ConfigImg, is bool) {
  67. if !redisCache_ConfigImg.IsExist(key) {
  68. return r, false
  69. }
  70. v := redisCache_ConfigImg.Get(key)
  71. json.Unmarshal(v.([]byte), &r)
  72. return r, true
  73. }
  74. func Redis_ConfigImg_DelK(key string) (err error) {
  75. err = redisCache_ConfigImg.Delete(key)
  76. return
  77. }
  78. // ---------------- 特殊方法 -------------------
  79. // 获取 ById
  80. func Read_ConfigImg_ById(id int) (r ConfigImg, is bool) {
  81. if r, is = Redis_ConfigImg_Get(strconv.Itoa(id)); is == true {
  82. return r, true
  83. }
  84. o := orm.NewOrm()
  85. r = ConfigImg{Id: id}
  86. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  87. if err != nil {
  88. logs.Println(err)
  89. return r, false
  90. }
  91. Redis_ConfigImg_Set(strconv.Itoa(id), r)
  92. return r, true
  93. }
  94. // 添加
  95. func Add_ConfigImg(v ConfigImg) (id int64, is bool) {
  96. o := orm.NewOrm()
  97. id, err := o.Insert(&v)
  98. if err != nil {
  99. logs.Println(err)
  100. return 0, false
  101. }
  102. Redis_ConfigImg_Set(strconv.Itoa(v.Id), v)
  103. return id, true
  104. }
  105. // 删除
  106. func Delete_ConfigImg_ById(v ConfigImg) bool {
  107. o := orm.NewOrm()
  108. if num, err := o.Delete(&v); err == nil {
  109. logs.Println("Number of records deleted in database:", num)
  110. } else {
  111. return false
  112. }
  113. Redis_ConfigImg_DelK(strconv.Itoa(v.Id))
  114. return true
  115. }
  116. // 修改
  117. func Update_ConfigImg_ById(m ConfigImg) bool {
  118. o := orm.NewOrm()
  119. v := ConfigImg{Id: m.Id}
  120. if err := o.Read(&v); err == nil {
  121. var num int64
  122. v.T_img0 = m.T_img0
  123. v.T_img1 = m.T_img1
  124. v.T_img2 = m.T_img2
  125. v.T_img3 = m.T_img3
  126. v.T_img4 = m.T_img4
  127. v.T_img5 = m.T_img5
  128. v.T_str0 = m.T_str0
  129. v.T_str1 = m.T_str1
  130. v.T_str2 = m.T_str2
  131. v.T_str3 = m.T_str3
  132. v.T_str4 = m.T_str4
  133. v.T_str5 = m.T_str5
  134. if num, err = o.Update(&m, "T_img0", "T_img1", "T_img2", "T_img3", "T_img4", "T_img5", "T_str0", "T_str1", "T_str2", "T_str3", "T_str4", "T_str5"); err == nil {
  135. fmt.Println("Number of records updated in database:", num)
  136. }
  137. Redis_ConfigImg_Set(strconv.Itoa(v.Id), v)
  138. return true
  139. }
  140. return false
  141. }
  142. // 获取列表
  143. func Read_ConfigImg_1(page int, page_z int) (r []ConfigImg, cnt int64) {
  144. o := orm.NewOrm()
  145. // 也可以直接使用 Model 结构体作为表名
  146. qs := o.QueryTable(new(ConfigImg))
  147. var offset int64
  148. if page <= 1 {
  149. offset = 0
  150. } else {
  151. offset = int64((page - 1) * page_z)
  152. }
  153. qs.Limit(page_z, offset).OrderBy("-Id").All(&r)
  154. cnt, _ = qs.Count()
  155. return r, cnt
  156. }