123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package Company
- import (
- "ColdP_server/conf"
- "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/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 // 分类
- }
- func (t *CompanyClass) TableName() string {
- return "company_class" // 数据库名称 // ************** 替换 DesignClass **************
- }
- var redisCache_CompanyClass cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(CompanyClass))
- }
- // 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 {
- fmt.Print(err)
- return
- }
- err = redisCache_CompanyClass.Put(key, str, 2*time.Hour)
- if err != nil {
- fmt.Println("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
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- return CompanyClass{}, false
- }
- func Redis_CompanyClass_DelK(key string) (err error) {
- err = redisCache_CompanyClass.Delete(key)
- 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
- }
- // 获取 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_pid", "T_State")
- if err != nil {
- fmt.Println(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 {
- fmt.Println(err)
- }
- Redis_CompanyClass_Set(m)
- return
- }
- // 修改
- func Update_CompanyClass(r CompanyClass, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&r, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- Redis_CompanyClass_Set(r)
- return true
- }
- return false
- }
- // 删除
- func Delete_CompanyClass_ById(id int) bool {
- o := orm.NewOrm()
- v := CompanyClass{Id: id}
- // ascertain id exists in the database
- if err := o.Read(&v); err == nil {
- var num int64
- v.T_State = 0
- if num, err = o.Update(&v, "T_State"); err == nil {
- fmt.Println("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_z == 0 {
- page_z = conf.Page_size
- }
- 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", T_name)
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- 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", T_name)
- }
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
- for _, v := range map_r {
- r = append(r, CompanyClassToCompanyClass_R(v))
- }
- return r
- }
|