package Function import ( "Cold_GoodsOrder/conf" "encoding/json" "fmt" "github.com/astaxie/beego/cache" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" _ "github.com/go-sql-driver/mysql" "strconv" "time" ) // Couriers Order 模板 type Couriers struct { Id int `json:"Id" orm:"column(ID);size(11);auto;pk"` Name string `json:"T_name" orm:"size(256);null"` // 配送员 Phone string `orm:"size(256);null"` // 电话 UserId int `orm:"index;size(256);null"` // 用户ID pid CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` // 创建时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` // 更新时间 } type CouriersR struct { Id int `json:"Id"` // ID Name string `json:"T_name"` // 配送员 Phone string // 电话 CreateTime string // 创建时间 UpdateTime string // 更新时间 } func CouriersToCouriersR(t Couriers) (r CouriersR) { r.Id = t.Id r.Name = t.Name r.Phone = t.Phone r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05") r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05") return } func (t *Couriers) TableName() string { return "couriers" // 数据库名称 } var redisCache_Couriers cache.Cache func init() { //注册模型 orm.RegisterModel(new(Couriers)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_Couriers", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_Couriers, err = cache.NewCache("redis", config) if err != nil || redisCache_Couriers == nil { errMsg := "failed to init redis" fmt.Println(errMsg, err) } } // ---------------- Redis ------------------- func Redis_Couriers_Set(r Couriers) (err error) { //json序列化 str, err := json.Marshal(r) if err != nil { fmt.Print(err) return } err = redisCache_Couriers.Put(strconv.Itoa(r.Id), str, 24*time.Hour) if err != nil { fmt.Println("set key:", strconv.Itoa(r.Id), ",value:", str, err) } return } func Redis_Couriers_Get(key string) (r Couriers, is bool) { if redisCache_Couriers.IsExist(key) { //println("找到key:",key) v := redisCache_Couriers.Get(key) json.Unmarshal(v.([]byte), &r) return r, true } //println("没有 找到key:",key) return Couriers{}, false } func Redis_Couriers_DelK(key string) (err error) { err = redisCache_Couriers.Delete(key) return } // ---------------- 特殊方法 ------------------- // 获取 ById func Read_Couriers_ById(id int) (r Couriers) { if r, is := Redis_Couriers_Get(strconv.Itoa(id)); is { return r } o := orm.NewOrm() r = Couriers{Id: id} err := o.Read(&r) if err != nil { fmt.Println(err) } return r } // 添加 func Add_Couriers(m Couriers) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { fmt.Println(err) } Redis_Couriers_Set(m) return id, err } // 修改 func Update_Couriers(r Couriers, 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_Couriers_Set(r) return true } return false } // 删除 func Delete_Couriers(m Couriers) bool { o := orm.NewOrm() if num, err := o.Delete(&m); err == nil { fmt.Println("Number of records deleted in database:", num) Redis_Couriers_DelK(strconv.Itoa(m.Id)) return true } return false } // 获取列表 func Read_Couriers_List(userId int, page int, page_z int, name string) (t []CouriersR, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Couriers)) var offset int64 if page_z == 0 { page_z = conf.Page_size } var r []Couriers if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("UserId", userId) if len(name) > 0 { cond1 = cond1.And("name__icontains", name) } qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count() for _, v := range r { t = append(t, CouriersToCouriersR(v)) } return t, cnt }