Admin.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. orm2 "github.com/beego/beego/v2/client/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "time"
  13. )
  14. type Admin struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_uuid string `orm:"size(256);null"` // 用户编号
  17. T_pid int `orm:"size(200);null"` // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  18. T_pids string `orm:"size(256);null"` // 绑定公司管理 Pid| 如:P1|P2|
  19. T_power int `orm:"size(2);0"` // 权限 (关联权限表)
  20. T_user string `orm:"size(256);null"` // 用户名 (唯一)
  21. T_pass string `orm:"size(256);null"` // MD5
  22. T_name string `orm:"size(256);null"` // 姓名
  23. T_phone string `orm:"size(256);null"` // 电话
  24. T_mail string `orm:"size(200);null"` // 邮箱
  25. T_wx string `orm:"size(256);null"` // 微信
  26. T_State int `orm:"size(200);1"` // 0删除 1 正常
  27. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  28. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  29. }
  30. func (t *Admin) TableName() string {
  31. return "admin" // 数据库名称 // ************** 替换 FormulaList **************
  32. }
  33. var redisCache_Admin cache.Cache
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(Admin))
  37. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  38. "redis_Cold_User_Admin", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  39. fmt.Println(config)
  40. var err error
  41. redisCache_Admin, err = cache.NewCache("redis", config)
  42. if err != nil || redisCache_Admin == nil {
  43. errMsg := "failed to init redis"
  44. fmt.Println(errMsg, err)
  45. }
  46. }
  47. // if r,is :=Redis_Get(T_sn);is{
  48. // return r,nil
  49. // }
  50. func Redis_Admin_Get(key string) (r Admin, is bool) {
  51. if redisCache_Admin.IsExist(key) {
  52. //println("找到key:",key)
  53. v := redisCache_Admin.Get(key)
  54. json.Unmarshal(v.([]byte), &r)
  55. return r, true
  56. }
  57. //println("没有 找到key:",key)
  58. return Admin{}, false
  59. }
  60. // 获取 ById
  61. func Read_Admin_Byid(Id int) (r Admin, err error) {
  62. o := orm.NewOrm()
  63. r = Admin{Id: Id, T_State: 1}
  64. err = o.Read(&r, "Id", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  65. if err != nil {
  66. logs.Println("Read_Admin_Byid", err)
  67. }
  68. return r, err
  69. }
  70. // 获取列表
  71. func Read_Admin_Tpids_List(CompanyID int) (maps []Admin) {
  72. o := orm.NewOrm()
  73. // 也可以直接使用 Model 结构体作为表名
  74. qs := o.QueryTable(new(Admin))
  75. cond := orm.NewCondition()
  76. cond1 := cond.And("T_State", 1).And("T_pids__icontains", CompanyID)
  77. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  78. return maps
  79. }
  80. // 获取列表
  81. func Read_Admin_Tpid_List(CompanyID int) (maps []Admin) {
  82. o := orm.NewOrm()
  83. // 也可以直接使用 Model 结构体作为表名
  84. qs := o.QueryTable(new(Admin))
  85. cond := orm.NewCondition()
  86. cond1 := cond.And("T_State", 1).And("T_pid", CompanyID)
  87. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  88. return maps
  89. }
  90. // 获取 ByT_uuid
  91. func Read_Admin_ByUuid(T_uuid string) (r Admin, err error) {
  92. if r, is := Redis_Admin_Get(T_uuid); is {
  93. //println("Redis_Get OK")
  94. return r, nil
  95. }
  96. o := orm.NewOrm()
  97. r = Admin{T_uuid: T_uuid, T_State: 1}
  98. err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  99. if err != nil {
  100. fmt.Println(err)
  101. }
  102. return r, err
  103. }