Company.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package Account
  2. import (
  3. "Cold_mqtt/conf"
  4. "Cold_mqtt/logs"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "time"
  13. )
  14. type Company struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_mid int `orm:"size(200);"` // 上一级 ID
  17. T_name string `orm:"size(256);"` // 公司名称
  18. T_key string `orm:"size(256);index;"` // 公司密钥
  19. T_warning int `orm:"size(20);default(1)"` // 是否处理报警信息 1处理 2不处理
  20. T_money float32 `orm:"digits(12);decimals(2)"` // 余额
  21. T_Charging int `orm:"size(11);default(0)"` // 记账扣费 公司ID 默认为:0 (自己)
  22. T_State int `orm:"size(200);"` // 0删除 1 正常
  23. CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  24. UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"` //auto_now_add 第一次保存时才设置时间
  25. }
  26. func (t *Company) TableName() string {
  27. return "company" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. var redisCache_Company cache.Cache
  30. func init() {
  31. //注册模型
  32. orm.RegisterModel(new(Company))
  33. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  34. "redis_User_Company", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  35. logs.Println(config)
  36. var err error
  37. redisCache_Company, err = cache.NewCache("redis", config)
  38. if err != nil || redisCache_Company == nil {
  39. errMsg := "failed to init redis"
  40. logs.Println(errMsg, err)
  41. }
  42. }
  43. // if r,is :=Redis_Get(T_sn);is{
  44. // return r,nil
  45. // }
  46. func Redis_Company_Get(key string) (r Company, is bool) {
  47. if redisCache_Company.IsExist(key) {
  48. //println("找到key:",key)
  49. v := redisCache_Company.Get(key)
  50. json.Unmarshal(v.([]byte), &r)
  51. return r, true
  52. }
  53. //println("没有 找到key:",key)
  54. return Company{}, false
  55. }
  56. func Redis_Company_DelK(key string) (err error) {
  57. err = redisCache_Company.Delete(key)
  58. return
  59. }
  60. // ---------------- 特殊方法 -------------------
  61. // 获取 ById
  62. func Read_Company_id(Id int) (e error, r Company) {
  63. if r, is := Redis_Company_Get(strconv.Itoa(Id)); is {
  64. //println("Redis_Get OK")
  65. return nil, r
  66. }
  67. o := orm.NewOrm()
  68. qs := o.QueryTable(new(Company))
  69. e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
  70. return e, r
  71. }