Template.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package Template
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  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 Template struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_name string `orm:"size(256);null"` // 标题
  18. T_class int `orm:"size(200);null"` // 分类
  19. T_img string `orm:"size(256);null"` // 图片
  20. T_int int `orm:"size(200);null"` // 数量
  21. T_text string `orm:"type(text);null"` // 详情
  22. T_Visit int `orm:"size(200);default(0)"` // 浏览量
  23. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  25. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  26. }
  27. type Template_R struct {
  28. T_name string `orm:"size(256);null"` // 标题
  29. T_class int `orm:"size(200);null"` // 分类
  30. T_img string `orm:"size(256);null"` // 图片
  31. T_int int `orm:"size(200);null"` // 数量
  32. T_text string `orm:"type(text);null"` // 详情
  33. }
  34. func (t *Template) TableName() string {
  35. return "template" // 数据库名称 // ************** 替换 FormulaList **************
  36. }
  37. var redisCache_Template cache.Cache
  38. func init() {
  39. //注册模型
  40. orm.RegisterModel(new(Template))
  41. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  42. "redis_"+"Template", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  43. logs.Println(config)
  44. var err error
  45. redisCache_Template, err = cache.NewCache("redis", config)
  46. if err != nil || redisCache_Template == nil {
  47. errMsg := "failed to init redis"
  48. logs.Println(errMsg, err)
  49. }
  50. }
  51. // -------------------------------------------------------------
  52. func TemplateToTemplate_R(T Template) (T_r Template_R) {
  53. T_r.T_name = T.T_name
  54. //......
  55. return T_r
  56. }
  57. // ---------------- Redis -------------------
  58. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  59. func Redis_Template_Set(key string, r Template) (err error) {
  60. //json序列化
  61. str, err := json.Marshal(r)
  62. if err != nil {
  63. logs.Error(lib.FuncName(), err)
  64. return
  65. }
  66. err = redisCache_Template.Put(key, str, 24*time.Hour)
  67. if err != nil {
  68. logs.Println("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_Template_Get(key string) (r Template, is bool) {
  76. if redisCache_Template.IsExist(key) {
  77. logs.Println("找到key:", key)
  78. v := redisCache_Template.Get(key)
  79. json.Unmarshal(v.([]byte), &r)
  80. return r, true
  81. }
  82. logs.Println("没有 找到key:", key)
  83. return Template{}, false
  84. }
  85. func Redis_Template_DelK(key string) (err error) {
  86. err = redisCache_Template.Delete(key)
  87. return
  88. }
  89. // ---------------- 特殊方法 -------------------
  90. // 获取 ById
  91. func Read_Template_ById(id int) (r Template, is bool) {
  92. o := orm.NewOrm()
  93. r = Template{Id: id}
  94. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  95. if err != nil {
  96. logs.Error(lib.FuncName(), err)
  97. return r, false
  98. }
  99. return r, true
  100. }
  101. // 获取 By
  102. func Read_Template(T_name string) (r Template, is bool) {
  103. if r, is = Redis_Template_Get(T_name); is == true {
  104. return r, true
  105. }
  106. o := orm.NewOrm()
  107. qs := o.QueryTable(new(Template))
  108. err := qs.Filter("T_name", T_name).One(&r)
  109. if err != nil {
  110. return r, false
  111. }
  112. Redis_Template_Set(T_name, r)
  113. return r, true
  114. }
  115. // 添加
  116. func Add_Template(r Template) (id int64, is bool) {
  117. o := orm.NewOrm()
  118. // 生成编号
  119. //rand_x := 0
  120. //for true {
  121. // r.T_Product_id = lib.GetRandstring(4, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)) // 1,336,336
  122. // err := o.Read(&r, "T_Product_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  123. // if err != nil {
  124. // break
  125. // }
  126. // rand_x += 1
  127. //}
  128. id, err := o.Insert(&r)
  129. if err != nil {
  130. logs.Error(lib.FuncName(), err)
  131. return 0, false
  132. }
  133. Redis_Template_Set(r.T_name, r)
  134. return id, true
  135. }
  136. // 删除
  137. func Delete_Template(v Template) bool {
  138. o := orm.NewOrm()
  139. if num, err := o.Delete(&v); err == nil {
  140. logs.Println("Number of records deleted in database:", num)
  141. } else {
  142. return false
  143. }
  144. Redis_Template_DelK(v.T_name)
  145. return true
  146. }
  147. // 删除
  148. func Delete_Template_(v Template) bool {
  149. o := orm.NewOrm()
  150. v.T_State = 0
  151. if num, err := o.Update(&v, "T_State"); err == nil {
  152. logs.Println("Number of records updated in database:", num)
  153. } else {
  154. return false
  155. }
  156. Redis_Template_DelK(v.T_name)
  157. return true
  158. }
  159. // 修改
  160. func Update_Template(m Template, cols ...string) bool {
  161. o := orm.NewOrm()
  162. if num, err := o.Update(&m, cols...); err == nil {
  163. logs.Println("Number of records updated in database:", num)
  164. Redis_Template_Set(m.T_name, m)
  165. return true
  166. }
  167. return false
  168. }
  169. // 获取列表
  170. func Read_Template_List(T_name string, T_class int, page int, page_z int) ([]Template_R, int64) {
  171. o := orm.NewOrm()
  172. // 也可以直接使用 Model 结构体作为表名
  173. var r []Template
  174. qs := o.QueryTable(new(Template))
  175. var offset int64
  176. if page <= 1 {
  177. offset = 0
  178. } else {
  179. offset = int64((page - 1) * page_z)
  180. }
  181. cond := orm.NewCondition()
  182. cond1 := cond.And("T_name__icontains", T_name).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  183. if T_class > 0 {
  184. //cond1.AndCond(cond.And("T_class", class))
  185. cond1 = cond.AndCond(cond1).OrCond(cond.And("T_class", T_class))
  186. }
  187. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  188. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  189. // 转换
  190. var Template_r []Template_R
  191. for _, v := range r {
  192. Template_r = append(Template_r, TemplateToTemplate_R(v))
  193. }
  194. return Template_r, cnt
  195. }
  196. // 获取列表 /时间筛选 Time_start_ Time_end_ 2021-12-30,所以下面要加后缀 时间
  197. func Read_Template_List_Time(page int, T_sn string, T_Title string, Time_start_ string, Time_end_ string) ([]Template_R, int64) {
  198. o := orm.NewOrm()
  199. // 也可以直接使用 Model 结构体作为表名
  200. var r []Template
  201. var cnt int64
  202. qs := o.QueryTable(new(Template))
  203. var offset int64
  204. if page <= 1 {
  205. offset = 0
  206. } else {
  207. offset = int64((page - 1) * conf.Page_size)
  208. }
  209. if len(Time_start_) > 1 && len(Time_end_) > 1 {
  210. logs.Println(Time_start_, Time_end_)
  211. qs.Limit(conf.Page_size, offset).Filter("T_Ut__lte", Time_end_+" 23:59:59").Filter("T_Ut__gte", Time_start_+" 00:00:00").Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).OrderBy("-Id").Filter("T_State", 1).All(&r)
  212. cnt, _ = qs.Filter("T_Ut__lte", Time_end_+" 23:59:59").Filter("T_Ut__gte", Time_start_+" 00:00:00").Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).Count()
  213. return []Template_R{}, cnt
  214. }
  215. if len(Time_start_) > 1 {
  216. qs.Limit(conf.Page_size, offset).Filter("T_Ut__gte", Time_start_+" 00:00:00").Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).OrderBy("-Id").Filter("T_State", 1).All(&r)
  217. cnt, _ = qs.Filter("T_Ut__gte", Time_start_+" 00:00:00").Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).Count()
  218. return []Template_R{}, cnt
  219. } else if len(Time_end_) > 1 {
  220. qs.Limit(conf.Page_size, offset).Filter("T_Ut__lte", Time_end_+" 23:59:59").Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).OrderBy("-Id").Filter("T_State", 1).All(&r)
  221. cnt, _ = qs.Filter("T_Ut__lte", Time_end_+" 23:59:59").Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).Count()
  222. return []Template_R{}, cnt
  223. }
  224. qs.Limit(conf.Page_size, offset).Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).OrderBy("-Id").Filter("T_State", 1).All(&r)
  225. cnt, _ = qs.Filter("T_Title__icontains", T_Title).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).Count()
  226. var Template_r []Template_R
  227. for _, v := range r {
  228. Template_r = append(Template_r, TemplateToTemplate_R(v))
  229. }
  230. return Template_r, cnt
  231. }