12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package Account
- import (
- "Cold_mqtt/conf"
- "Cold_mqtt/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);"` // 上一级 ID
- T_name string `orm:"size(256);"` // 公司名称
- T_key string `orm:"size(256);index;"` // 公司密钥
- T_warning int `orm:"size(20);default(1)"` // 是否处理报警信息 1处理 2不处理
- T_money float32 `orm:"digits(12);decimals(2)"` // 余额
- T_Charging int `orm:"size(11);default(0)"` // 记账扣费 公司ID 默认为:0 (自己)
- T_State int `orm:"size(200);"` // 0删除 1 正常
- CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);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_User_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)
- }
- }
- // 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
- }
- // ---------------- 特殊方法 -------------------
- // 获取 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
- }
|