123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package Panel
- import (
- "Cold_Api/conf"
- "Cold_Api/logs"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 模板
- type PaneView struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_viewid string `orm:"size(256);null"` //
- T_mode int `orm:"size(200);0"` // 模式
- T_width int `orm:"size(200);null"` // 宽度
- T_height int `orm:"size(200);null"` // 高度
- T_css string `orm:"type(text);null"` // 样式
- T_text string `orm:"type(text);null"` // 详情
- T_data string `orm:"type(text);null"` // 数据来源
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- func (t *PaneView) TableName() string {
- return "PaneView" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_PaneView cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(PaneView))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_"+"PaneView", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redisCache_PaneView, err = cache.NewCache("redis", config)
- if err != nil || redisCache_PaneView == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- //Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_PaneView_Set(key string, r PaneView) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Println(err)
- return
- }
- err = redisCache_PaneView.Put(key, str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- //if r,is :=Redis_Get(T_sn);is{
- //return r,nil
- //}
- func Redis_PaneView_Get(key string) (r PaneView, is bool) {
- if redisCache_PaneView.IsExist(key) {
- logs.Println("找到key:", key)
- v := redisCache_PaneView.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- logs.Println("没有 找到key:", key)
- return PaneView{}, false
- }
- func Redis_PaneView_DelK(key string) (err error) {
- err = redisCache_PaneView.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_PaneView_ByT_viewid(T_viewid string) (r PaneView, is bool) {
- o := orm.NewOrm()
- r = PaneView{T_viewid: T_viewid}
- err := o.Read(&r, "T_viewid") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Println(err)
- return r, false
- }
- return r, true
- }
- // 获取 By
- func Read_PaneView_ByT_name(T_viewid string) (r PaneView, is bool) {
- //if r, is = Redis_PaneView_Get(strconv.Itoa(Id)); is == true {
- // return r, true
- //}
- o := orm.NewOrm()
- qs := o.QueryTable(new(PaneView))
- err := qs.Filter("T_viewid", T_viewid).One(&r)
- if err != nil {
- return r, false
- }
- Redis_PaneView_Set(T_viewid, r)
- return r, true
- }
- // 添加
- func Add_PaneView(r PaneView) (id int64, is bool) {
- o := orm.NewOrm()
- id, err := o.Insert(&r)
- if err != nil {
- logs.Println(err)
- return 0, false
- }
- Redis_PaneView_Set(r.T_viewid, r)
- return id, true
- }
- // 修改
- func Update_PaneView(m PaneView) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, "T_text","T_mode","T_width","T_height","T_css","T_data"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- Redis_PaneView_Set(m.T_viewid, m)
- return true
- }
- return false
- }
- // 获取列表
- func Read_PaneView_T_uuid(T_uuid string) (r []PaneView) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(PaneView))
- qs.Limit(1, 0).Filter("T_uuid",T_uuid).OrderBy("Id").All(&r)
- return r
- }
|