package Company import ( "Cold_Api/conf" "Cold_Api/controllers/lib" "Cold_Api/models/Account" "encoding/json" "fmt" "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 CompanyClass struct { Id int `orm:"column(ID);size(11);auto;pk"` T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司 T_name string `orm:"size(256);null"` // 分类 T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常 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 保存时都会对时间自动更新 } type CompanyClass_R struct { Id int T_pid int // Account.Company 绑定公司 T_name string // 分类 } type CompanyClass_Company struct { Id int T_pid int // Account.Company 绑定公司 T_pid_name string // Account.Company 绑定公司名称 T_name string // 分类 } func (t *CompanyClass) TableName() string { return "company_class" // 数据库名称 // ************** 替换 DesignClass ************** } var redisCache_CompanyClass cache.Cache func init() { //注册模型 orm.RegisterModel(new(CompanyClass)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_CompanyClass", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_CompanyClass, err = cache.NewCache("redis", config) if err != nil || redisCache_CompanyClass == nil { errMsg := "failed to init redis" logs.Error(errMsg, err) panic(errMsg) } } // Redis_Set(m.T_sn,m) // Redis 更新缓存 func Redis_CompanyClass_Set(r CompanyClass) (err error) { key := strconv.Itoa(r.Id) //json序列化 str, err := json.Marshal(r) if err != nil { logs.Error(lib.FuncName(), err) return } err = redisCache_CompanyClass.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_CompanyClass_Get(key string) (CompanyClass, bool) { println("找到key:", key) if redisCache_CompanyClass.IsExist(key) { //println("找到key:",key) v := redisCache_CompanyClass.Get(key) var r CompanyClass err := json.Unmarshal(v.([]byte), &r) if err != nil { logs.Error(lib.FuncName(), err) return CompanyClass{}, false } return r, true } return CompanyClass{}, false } func Redis_CompanyClass_DelK(key string) (err error) { err = redisCache_CompanyClass.Delete(key) if err != nil { logs.Error(lib.FuncName(), err) return } return } // ---------------- 特殊方法 ------------------- func CompanyClassToCompanyClass_R(t CompanyClass) (r CompanyClass_R) { r.Id = t.Id r.T_pid = t.T_pid r.T_name = t.T_name return r } func CompanyClassToCompanyClass_Company(t CompanyClass) (r CompanyClass_Company) { r.Id = t.Id r.T_pid = t.T_pid r.T_pid_name = Account.Read_Company_Get(t.T_pid) r.T_name = t.T_name return r } // 获取 ById func Read_CompanyClass_ById(id int) (r CompanyClass, err error) { key := strconv.Itoa(id) if r, is := Redis_CompanyClass_Get(key); is { return r, nil } o := orm.NewOrm() r = CompanyClass{Id: id, T_State: 1} err = o.Read(&r, "Id", "T_State") if err != nil { logs.Error(lib.FuncName(), err) return r, err } Redis_CompanyClass_Set(r) return r, err } // 添加 func Add_CompanyClass(m CompanyClass) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) return } Redis_CompanyClass_Set(m) return } // 修改 func Update_CompanyClass(r CompanyClass, cols ...string) bool { o := orm.NewOrm() num, err := o.Update(&r, cols...) if err != nil { logs.Error(lib.FuncName(), err) return false } logs.Info("Number of records updated in database:", num) Redis_CompanyClass_Set(r) return true } // 删除 func Delete_CompanyClass_ById(id int) bool { o := orm.NewOrm() v := CompanyClass{Id: id} if err := o.Read(&v); err == nil { var num int64 v.T_State = 0 num, err = o.Update(&v, "T_State") if err != nil { logs.Error(lib.FuncName(), err) return false } logs.Info("Number of records updated in database:", num) key := strconv.Itoa(v.Id) Redis_CompanyClass_DelK(key) return true } return false } // 获取列表 func Read_CompanyClass_List(T_pid int, T_name string, page int, page_z int) (r []CompanyClass_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var map_r []CompanyClass qs := o.QueryTable(new(CompanyClass)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("T_State", 1) if T_pid > 0 { cond1 = cond1.And("T_pid", T_pid) } if len(T_name) > 0 { cond1 = cond1.And("T_name__icontains", T_name) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range map_r { r = append(r, CompanyClassToCompanyClass_R(v)) } return r, cnt } // 获取全部列表 func Read_CompanyClass_All(T_pid int, T_name string) (r []CompanyClass_R) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var map_r []CompanyClass qs := o.QueryTable(new(CompanyClass)) cond := orm.NewCondition() cond1 := cond.And("T_State", 1).And("T_pid", T_pid) if len(T_name) > 0 { cond1 = cond1.And("T_name__icontains", T_name) } _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range map_r { r = append(r, CompanyClassToCompanyClass_R(v)) } return r } func Read_CompanyClass_All_For_Company(T_pids []int, T_name string) (r []CompanyClass_Company) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var map_r []CompanyClass qs := o.QueryTable(new(CompanyClass)) cond := orm.NewCondition() cond1 := cond.And("T_State", 1).And("T_pid__in", T_pids) if len(T_name) > 0 { cond1 = cond1.And("T_name__icontains", T_name) } _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_pid").All(&map_r) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range map_r { r = append(r, CompanyClassToCompanyClass_Company(v)) } return r }