123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package Account
- import (
- "Cold_DeductionNotice/conf"
- "Cold_DeductionNotice/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"
- "strconv"
- "time"
- )
- type Company struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_mid int `orm:"size(200);null"` // 上一级 ID
- T_name string `orm:"size(256);null"` // 公司名称
- T_key string `orm:"size(256);index;null"` // 公司密钥
- T_money float32 `orm:"digits(12);decimals(2)"` // 余额
- T_State int `orm:"size(200);"` // 0删除 1 正常
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
- }
- func (t *Company) TableName() string {
- return "company" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_Company cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(Company))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_Company", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redisCache_Company, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Company == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_Company_Set(r Company) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- fmt.Print(err)
- return
- }
- err = redisCache_Company.Put(strconv.Itoa(r.Id), str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", strconv.Itoa(r.Id), ",value:", str, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_Company_Get(key string) (r Company, is bool) {
- if redisCache_Company.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Company.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- //println("没有 找到key:",key)
- return Company{}, false
- }
- func Redis_Company_DelK(key string) (err error) {
- err = redisCache_Company.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 修改
- func Update_Company(m Company, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Println("Number of records updated in database:", num)
- Redis_Company_Set(m) // Redis 更新缓存
- return true
- }
- return false
- }
- // 获取全部
- func Read_Company_All() (r_l []Company) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- qs.Filter("T_State", 1).All(&r_l)
- return
- }
- // 获取 ById
- func Read_Company_id(Id int) (e error, r Company) {
- if r, is := Redis_Company_Get(strconv.Itoa(Id)); is {
- //println("Redis_Get OK")
- return nil, r
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
- return e, r
- }
|