Company.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package Account
  2. import (
  3. "Cold_DeductionNotice/conf"
  4. "Cold_DeductionNotice/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);null"` // 上一级 ID
  17. T_name string `orm:"size(256);null"` // 公司名称
  18. T_key string `orm:"size(256);index;null"` // 公司密钥
  19. T_money float32 `orm:"digits(12);decimals(2)"` // 余额
  20. T_State int `orm:"size(200);"` // 0删除 1 正常
  21. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  22. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  23. }
  24. func (t *Company) TableName() string {
  25. return "company" // 数据库名称 // ************** 替换 FormulaList **************
  26. }
  27. var redisCache_Company cache.Cache
  28. func init() {
  29. //注册模型
  30. orm.RegisterModel(new(Company))
  31. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  32. "redis_Company", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  33. logs.Println(config)
  34. var err error
  35. redisCache_Company, err = cache.NewCache("redis", config)
  36. if err != nil || redisCache_Company == nil {
  37. errMsg := "failed to init redis"
  38. logs.Println(errMsg, err)
  39. }
  40. }
  41. // ---------------- Redis -------------------
  42. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  43. func Redis_Company_Set(r Company) (err error) {
  44. //json序列化
  45. str, err := json.Marshal(r)
  46. if err != nil {
  47. fmt.Print(err)
  48. return
  49. }
  50. err = redisCache_Company.Put(strconv.Itoa(r.Id), str, 24*time.Hour)
  51. if err != nil {
  52. logs.Println("set key:", strconv.Itoa(r.Id), ",value:", str, err)
  53. }
  54. return
  55. }
  56. // if r,is :=Redis_Get(T_sn);is{
  57. // return r,nil
  58. // }
  59. func Redis_Company_Get(key string) (r Company, is bool) {
  60. if redisCache_Company.IsExist(key) {
  61. //println("找到key:",key)
  62. v := redisCache_Company.Get(key)
  63. json.Unmarshal(v.([]byte), &r)
  64. return r, true
  65. }
  66. //println("没有 找到key:",key)
  67. return Company{}, false
  68. }
  69. func Redis_Company_DelK(key string) (err error) {
  70. err = redisCache_Company.Delete(key)
  71. return
  72. }
  73. // ---------------- 特殊方法 -------------------
  74. // 修改
  75. func Update_Company(m Company, cols ...string) bool {
  76. o := orm.NewOrm()
  77. if num, err := o.Update(&m, cols...); err == nil {
  78. logs.Println("Number of records updated in database:", num)
  79. Redis_Company_Set(m) // Redis 更新缓存
  80. return true
  81. }
  82. return false
  83. }
  84. // 获取全部
  85. func Read_Company_All() (r_l []Company) {
  86. o := orm.NewOrm()
  87. qs := o.QueryTable(new(Company))
  88. qs.Filter("T_State", 1).All(&r_l)
  89. return
  90. }
  91. // 获取 ById
  92. func Read_Company_id(Id int) (e error, r Company) {
  93. if r, is := Redis_Company_Get(strconv.Itoa(Id)); is {
  94. //println("Redis_Get OK")
  95. return nil, r
  96. }
  97. o := orm.NewOrm()
  98. qs := o.QueryTable(new(Company))
  99. e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
  100. return e, r
  101. }