Admin.go 3.6 KB

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