PaneView.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "time"
  12. )
  13. // 模板
  14. type PaneView struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_viewid string `orm:"size(256);null"` //
  17. T_mode int `orm:"size(200);0"` // 模式
  18. T_width int `orm:"size(200);null"` // 宽度
  19. T_height int `orm:"size(200);null"` // 高度
  20. T_css string `orm:"type(text);null"` // 样式
  21. T_text string `orm:"type(text);null"` // 详情
  22. T_data string `orm:"type(text);null"` // 数据来源
  23. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  24. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  25. }
  26. func (t *PaneView) TableName() string {
  27. return "PaneView" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. var redisCache_PaneView cache.Cache
  30. func init() {
  31. //注册模型
  32. orm.RegisterModel(new(PaneView))
  33. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  34. "redis_"+"PaneView", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  35. logs.Println(config)
  36. var err error
  37. redisCache_PaneView, err = cache.NewCache("redis", config)
  38. if err != nil || redisCache_PaneView == nil {
  39. errMsg := "failed to init redis"
  40. logs.Println(errMsg, err)
  41. }
  42. }
  43. // ---------------- Redis -------------------
  44. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  45. func Redis_PaneView_Set(key string, r PaneView) (err error) {
  46. //json序列化
  47. str, err := json.Marshal(r)
  48. if err != nil {
  49. logs.Println(err)
  50. return
  51. }
  52. err = redisCache_PaneView.Put(key, str, 24*time.Hour)
  53. if err != nil {
  54. logs.Println("set key:", key, ",value:", str, err)
  55. }
  56. return
  57. }
  58. //if r,is :=Redis_Get(T_sn);is{
  59. //return r,nil
  60. //}
  61. func Redis_PaneView_Get(key string) (r PaneView, is bool) {
  62. if redisCache_PaneView.IsExist(key) {
  63. logs.Println("找到key:", key)
  64. v := redisCache_PaneView.Get(key)
  65. json.Unmarshal(v.([]byte), &r)
  66. return r, true
  67. }
  68. logs.Println("没有 找到key:", key)
  69. return PaneView{}, false
  70. }
  71. func Redis_PaneView_DelK(key string) (err error) {
  72. err = redisCache_PaneView.Delete(key)
  73. return
  74. }
  75. // ---------------- 特殊方法 -------------------
  76. // 获取 ById
  77. func Read_PaneView_ByT_viewid(T_viewid string) (r PaneView, is bool) {
  78. o := orm.NewOrm()
  79. r = PaneView{T_viewid: T_viewid}
  80. err := o.Read(&r, "T_viewid") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  81. if err != nil {
  82. logs.Println(err)
  83. return r, false
  84. }
  85. return r, true
  86. }
  87. // 获取 By
  88. func Read_PaneView_ByT_name(T_viewid string) (r PaneView, is bool) {
  89. //if r, is = Redis_PaneView_Get(strconv.Itoa(Id)); is == true {
  90. // return r, true
  91. //}
  92. o := orm.NewOrm()
  93. qs := o.QueryTable(new(PaneView))
  94. err := qs.Filter("T_viewid", T_viewid).One(&r)
  95. if err != nil {
  96. return r, false
  97. }
  98. Redis_PaneView_Set(T_viewid, r)
  99. return r, true
  100. }
  101. // 添加
  102. func Add_PaneView(r PaneView) (id int64, is bool) {
  103. o := orm.NewOrm()
  104. id, err := o.Insert(&r)
  105. if err != nil {
  106. logs.Println(err)
  107. return 0, false
  108. }
  109. Redis_PaneView_Set(r.T_viewid, r)
  110. return id, true
  111. }
  112. // 修改
  113. func Update_PaneView(m PaneView) bool {
  114. o := orm.NewOrm()
  115. if num, err := o.Update(&m, "T_text","T_mode","T_width","T_height","T_css","T_data"); err == nil {
  116. fmt.Println("Number of records updated in database:", num)
  117. Redis_PaneView_Set(m.T_viewid, m)
  118. return true
  119. }
  120. return false
  121. }
  122. // 获取列表
  123. func Read_PaneView_T_uuid(T_uuid string) (r []PaneView) {
  124. o := orm.NewOrm()
  125. // 也可以直接使用 Model 结构体作为表名
  126. qs := o.QueryTable(new(PaneView))
  127. qs.Limit(1, 0).Filter("T_uuid",T_uuid).OrderBy("Id").All(&r)
  128. return r
  129. }