package Product import ( "Cold_Api/controllers/lib" "encoding/json" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" _ "github.com/go-sql-driver/mysql" "strconv" "time" ) // 产品类型 type ProductUpgrade struct { Id int `orm:"column(ID);size(11);auto;pk"` T_model string `orm:"size(256);null"` // 产品型号 T_version string `orm:"size(256);null"` // 版本 T_file string `orm:"size(256);null"` // 文件 T_remarks string `orm:"size(256);null"` // 备注 T_carryout int `orm:"size(1);null"` // 全量发布 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 } type ProductUpgrade_T struct { T_sn string `orm:"size(256);null"` // 设备编号 T_version string `orm:"size(256);null"` // 版本 T_file string `orm:"size(256);null"` // 文件 } type ProductUpgrade_R struct { Id int T_model string T_version string T_file string T_remarks string T_carryout int CreateTime string } func (t *ProductUpgrade) TableName() string { return "product_upgrade" // 数据库名称 // ************** 替换 FormulaList ************** } var redisCache_ProductUpgrade cache.Cache func init() { //注册模型 orm.RegisterModel(new(ProductUpgrade)) //config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, // "redis_ProductUpgrade", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) //fmt.Println(config) //var err error //redisCache_ProductUpgrade, err = cache.NewCache("redis", config) //if err != nil || redisCache_ProductUpgrade == nil { // errMsg := "failed to init redis" // panic(errMsg) //} } // Redis_Set(m.T_sn,m) // Redis 更新缓存 func Redis_ProductUpgrade_Set(r ProductUpgrade) (err error) { key := strconv.Itoa(r.Id) //json序列化 str, err := json.Marshal(r) if err != nil { logs.Error(lib.FuncName(), err) return } err = redisCache_ProductUpgrade.Put(key, str, 2*time.Hour) if err != nil { logs.Error("set key:", key, ",value:", str, err) } return } // if r,is :=Redis_Get(T_sn);is{ // return r,nil // } func Redis_ProductUpgrade_Get(key string) (ProductUpgrade, bool) { println("找到key:", key) if redisCache_ProductUpgrade.IsExist(key) { //println("找到key:",key) v := redisCache_ProductUpgrade.Get(key) var r ProductUpgrade err := json.Unmarshal(v.([]byte), &r) if err != nil { logs.Error(lib.FuncName(), err) return ProductUpgrade{}, false } return r, true } return ProductUpgrade{}, false } func Redis_ProductUpgrade_DelK(key string) (err error) { err = redisCache_ProductUpgrade.Delete(key) if err != nil { logs.Error(lib.FuncName(), err) } return } // ---------------- 特殊方法 ------------------- func ProductUpgradeToProductUpgrade_R(t ProductUpgrade) (r ProductUpgrade_R) { r.Id = t.Id r.T_model = t.T_model r.T_version = t.T_version r.T_file = t.T_file r.T_remarks = t.T_remarks r.T_carryout = t.T_carryout r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05") return r } // 获取 ById func Read_ProductUpgrade_ById(id int) (r ProductUpgrade, err error) { //key := strconv.Itoa(id) //if r, is := Redis_ProductUpgrade_Get(key); is { // return r, nil //} o := orm.NewOrm() r = ProductUpgrade{Id: id} err = o.Read(&r) if err != nil { logs.Error(lib.FuncName(), err) return r, err } //Redis_ProductUpgrade_Set(r) return r, err } // 添加 func Add_ProductUpgrade(m ProductUpgrade) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) return } //Redis_ProductUpgrade_Set(m) return } // 删除 func Delete_ProductUpgrade_ById(id int) bool { o := orm.NewOrm() v := ProductUpgrade{Id: id} _, err := o.Delete(&v) if err != nil { logs.Error(lib.FuncName(), err) return false } return true } // 获取列表 func Read_ProductUpgrade_List(T_model string, page int, page_z int) (r []ProductUpgrade_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var map_r []ProductUpgrade qs := o.QueryTable(new(ProductUpgrade)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() if len(T_model) > 0 { cond = cond.And("T_model", T_model) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range map_r { r = append(r, ProductUpgradeToProductUpgrade_R(v)) } return r, cnt } // 获取列表 func Read_ProductUpgrade_T_model() (lists orm2.ParamsList) { o := orm.NewOrm() var pl_lists orm2.ParamsList _, err := o.Raw("SELECT DISTINCT t_model FROM product_upgrade LIMIT 0,1000").ValuesFlat(&pl_lists) if err != nil { logs.Error(lib.FuncName(), err) } return pl_lists }