Admin.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package Account
  2. import (
  3. "ColdP_server/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type Admin struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_uuid string `orm:"size(256);null"` // 用户编号
  18. T_pid int `orm:"size(200);null"` // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  19. T_pids string `orm:"size(200);null"` // 绑定公司管理 Pid| 如 P1|P2
  20. T_power int `orm:"size(2);default(0)"` // 权限 (关联权限表)
  21. T_user string `orm:"size(256);null"` // 用户名 (唯一)
  22. T_pass string `orm:"size(256);null"` // MD5
  23. T_name string `orm:"size(256);null"` // 姓名
  24. T_phone string `orm:"size(256);null"` // 电话
  25. T_mail string `orm:"size(200);null"` // 邮箱
  26. T_wx string `orm:"size(256);null"` // 微信
  27. T_State int `orm:"size(2);default(1)"` // 0删除 1 正常
  28. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  29. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  30. }
  31. type Admin_R struct {
  32. T_uuid string // 用户编号
  33. T_pid int // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  34. T_pids string // 绑定公司管理 Pid| 如 P1|P2
  35. T_power int // 权限 (关联权限表)
  36. T_user string // 用户名 (唯一)
  37. T_name string // 姓名
  38. T_phone string // 电话
  39. T_mail string // 邮箱
  40. T_wx string // 微信
  41. }
  42. type Admin_ struct {
  43. T_name string // 姓名
  44. T_phone string // 电话
  45. }
  46. func AdminToAdmin_R(r Admin) (v Admin_R) {
  47. v.T_uuid = r.T_uuid
  48. v.T_pid = r.T_pid
  49. v.T_pids = r.T_pids
  50. v.T_power = r.T_power
  51. v.T_user = r.T_user
  52. v.T_name = r.T_name
  53. v.T_phone = r.T_phone
  54. v.T_mail = r.T_mail
  55. v.T_wx = r.T_wx
  56. return v
  57. }
  58. func AdminToAdmin_(r Admin) (v Admin_) {
  59. v.T_name = r.T_name
  60. v.T_phone = r.T_phone
  61. return v
  62. }
  63. func (t *Admin) TableName() string {
  64. return "admin" // 数据库名称 // ************** 替换 FormulaList **************
  65. }
  66. var redisCache_Admin cache.Cache
  67. func init() {
  68. //注册模型
  69. orm.RegisterModel(new(Admin))
  70. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  71. "redis_ColdP_User_Admin", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  72. fmt.Println(config)
  73. var err error
  74. redisCache_Admin, err = cache.NewCache("redis", config)
  75. if err != nil || redisCache_Admin == nil {
  76. errMsg := "failed to init redis"
  77. fmt.Println(errMsg, err)
  78. }
  79. }
  80. // ---------------- Redis -------------------
  81. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  82. func Redis_Admin_Set(r Admin) (err error) {
  83. //json序列化
  84. str, err := json.Marshal(r)
  85. if err != nil {
  86. fmt.Print(err)
  87. return
  88. }
  89. err = redisCache_Admin.Put(r.T_uuid, str, 24*time.Hour)
  90. if err != nil {
  91. fmt.Println("set key:", r.T_uuid, ",value:", str, err)
  92. }
  93. return
  94. }
  95. // if r,is :=Redis_Get(T_sn);is{
  96. // return r,nil
  97. // }
  98. func Redis_Admin_Get(key string) (r Admin, is bool) {
  99. if redisCache_Admin.IsExist(key) {
  100. //println("找到key:",key)
  101. v := redisCache_Admin.Get(key)
  102. json.Unmarshal(v.([]byte), &r)
  103. return r, true
  104. }
  105. //println("没有 找到key:",key)
  106. return Admin{}, false
  107. }
  108. func Redis_Admin_DelK(key string) (err error) {
  109. err = redisCache_Admin.Delete(key)
  110. return
  111. }
  112. // ---------------- 特殊方法 -------------------
  113. // 添加
  114. func Add_Admin(m Admin) (id int64, err error) {
  115. o := orm.NewOrm()
  116. id, err = o.Insert(&m)
  117. if err != nil {
  118. fmt.Println(err)
  119. }
  120. m.Id = int(id)
  121. Redis_Admin_Set(m)
  122. return id, err
  123. }
  124. //// 修改
  125. //func Update_Delete_Admin_ByT_uuid(T_uuid string) (err error) {
  126. // o := orm.NewOrm()
  127. // v := Admin{T_uuid: T_uuid}
  128. // // ascertain id exists in the database
  129. // if err = o.Read(&v, "T_uuid"); err == nil {
  130. // var num int64
  131. // v.T_State = 0
  132. //
  133. // if num, err = o.Update(&v, "T_State"); err == nil {
  134. // fmt.Println("Number of records updated in database:", num)
  135. // }
  136. //
  137. // Redis_Admin_Set(v)
  138. // }
  139. // return err
  140. //}
  141. func Read_Admin_T_user(T_user string) (Admin, error) {
  142. o := orm.NewOrm()
  143. r := Admin{T_user: T_user, T_State: 1}
  144. err := o.Read(&r, "T_user", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  145. if err != nil {
  146. fmt.Println(err)
  147. }
  148. return r, err
  149. }
  150. func Read_AdminLogin_verification(T_user string, T_pass string) (error, Admin) {
  151. o := orm.NewOrm()
  152. r := Admin{T_user: T_user, T_pass: T_pass, T_State: 1}
  153. err := o.Read(&r, "T_user", "T_pass", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  154. if err != nil {
  155. fmt.Println(err)
  156. }
  157. Redis_Admin_Set(r) // Redis 更新缓存
  158. return err, r
  159. }
  160. // 修改
  161. func Update_Admin(m Admin, cols ...string) bool {
  162. o := orm.NewOrm()
  163. if num, err := o.Update(&m, cols...); err == nil {
  164. fmt.Println("Number of records updated in database:", num)
  165. Redis_Admin_Set(m) // Redis 更新缓存
  166. return true
  167. }
  168. return false
  169. }
  170. // 删除
  171. func Delete_Admin(v Admin) bool {
  172. o := orm.NewOrm()
  173. v.T_State = 0
  174. if num, err := o.Update(&v, "T_State"); err == nil {
  175. fmt.Println("Number of records updated in database:", num)
  176. } else {
  177. return false
  178. }
  179. Redis_Admin_DelK(v.T_uuid) // Redis 更新缓存
  180. return true
  181. }
  182. // 获取 ById
  183. func Read_Admin_ById(Id int) (r Admin, err error) {
  184. o := orm.NewOrm()
  185. r = Admin{Id: Id, T_State: 1}
  186. err = o.Read(&r, "Id", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  187. if err != nil {
  188. fmt.Println(err)
  189. }
  190. return r, err
  191. }
  192. // 获取 ByT_uuid
  193. func Read_Admin_ByUuid(T_uuid string) (r Admin, err error) {
  194. if r, is := Redis_Admin_Get(T_uuid); is {
  195. //println("Redis_Get OK")
  196. return r, nil
  197. }
  198. o := orm.NewOrm()
  199. //redis 保存方式 uuid|公司pid
  200. arr := strings.Split(T_uuid, "|")
  201. r = Admin{T_uuid: arr[0], T_State: 1}
  202. err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  203. if err != nil {
  204. fmt.Println(err)
  205. }
  206. //设置为redis中存储的pid
  207. pid, _ := strconv.ParseInt(arr[1], 10, 64)
  208. r.T_pid = int(pid)
  209. return r, err
  210. }
  211. // 获取列表
  212. func Read_Admin_List(T_pid int, T_name string, page int, page_z int) (AdminList []Admin_R, cnt int64) {
  213. o := orm.NewOrm()
  214. // 也可以直接使用 Model 结构体作为表名
  215. qs := o.QueryTable(new(Admin))
  216. var maps []Admin
  217. var offset int64
  218. if page_z == 0 {
  219. page_z = conf.Page_size
  220. }
  221. if page <= 1 {
  222. offset = 0
  223. } else {
  224. offset = int64((page - 1) * page_z)
  225. }
  226. cond := orm.NewCondition()
  227. cond1 := cond.And("T_State", 1)
  228. if T_pid > 0 {
  229. cond1 = cond1.AndCond(cond.And("T_pid", T_pid))
  230. }
  231. if len(T_name) > 0 {
  232. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
  233. }
  234. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  235. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  236. for _, v := range maps {
  237. AdminList = append(AdminList, AdminToAdmin_R(v))
  238. }
  239. return AdminList, cnt
  240. }
  241. func Read_Admin_List_T_pids(T_pid int) (AdminList []Admin_) {
  242. o := orm.NewOrm()
  243. // 也可以直接使用 Model 结构体作为表名
  244. qs := o.QueryTable(new(Admin))
  245. var maps []Admin
  246. cond := orm.NewCondition()
  247. cond1 := cond.And("T_State", 1).And("T_pid", 0).And("T_pids__icontains", "P"+strconv.Itoa(T_pid)+"|")
  248. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  249. for _, v := range maps {
  250. AdminList = append(AdminList, AdminToAdmin_(v))
  251. }
  252. return AdminList
  253. }
  254. // 获取列表
  255. func Read_Admin_List_All() (AdminList []Admin_R) {
  256. o := orm.NewOrm()
  257. // 也可以直接使用 Model 结构体作为表名
  258. qs := o.QueryTable(new(Admin))
  259. var maps []Admin
  260. qs.All(&maps)
  261. for _, v := range maps {
  262. AdminList = append(AdminList, AdminToAdmin_R(v))
  263. }
  264. return AdminList
  265. }