Admin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package Account
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "time"
  8. )
  9. type Admin struct {
  10. Id int `orm:"column(ID);size(11);auto;pk"`
  11. T_uuid string `orm:"size(256);null"` //
  12. T_power int `orm:"size(2);;default(0)"` // 权限 0
  13. T_name string `orm:"size(256);null"` // 姓名
  14. T_user string `orm:"size(256);null"` // 用户名
  15. T_pass string `orm:"size(256);null"` // 密码
  16. T_State int `orm:"size(200);default(1)"` // 0删除 1 正常
  17. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  18. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  19. }
  20. type Admin_R struct {
  21. T_uuid string `orm:"column(t_uuid)"`
  22. T_power int `orm:"column(t_power)"` // 权限
  23. T_power_name string `orm:"column(t_power_name)"` // 权限
  24. T_name string `orm:"column(t_name)"` // 姓名
  25. T_user string `orm:"column(t_user)"` // 用户名
  26. }
  27. func (t *Admin) TableName() string {
  28. return "admin" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. func init() {
  31. //注册模型
  32. orm.RegisterModel(new(Admin))
  33. }
  34. func AdminToAdmin_R(T Admin, powerMap map[int]string) (T_r Admin_R) {
  35. T_r.T_uuid = T.T_uuid
  36. T_r.T_power = T.T_power
  37. T_r.T_name = T.T_name
  38. T_r.T_user = T.T_user
  39. T_r.T_power_name = powerMap[T.T_power]
  40. return T_r
  41. }
  42. // 获取 ByT_user
  43. func Read_Admin_ByT_user(T_user string) (e error, r Admin) {
  44. o := orm.NewOrm()
  45. qs := o.QueryTable(new(Admin))
  46. e = qs.Filter("T_user", T_user).Filter("T_State", 1).One(&r)
  47. return e, r
  48. }
  49. // 获取 ByT_uuid
  50. func Read_Admin_ByT_uuid(T_uuid string) (e error, r Admin) {
  51. o := orm.NewOrm()
  52. qs := o.QueryTable(new(Admin))
  53. e = qs.Filter("T_uuid", T_uuid).Filter("T_State", 1).One(&r)
  54. return e, r
  55. }
  56. // 添加
  57. func Add_Admin(m Admin) (id int64, err error) {
  58. o := orm.NewOrm()
  59. id, err = o.Insert(&m)
  60. if err != nil {
  61. logs.Error(lib.FuncName(), err)
  62. }
  63. return id, err
  64. }
  65. func Read_Admin_verification(T_user string, T_pass string) (error, Admin) {
  66. o := orm.NewOrm()
  67. r := Admin{T_user: T_user, T_pass: T_pass, T_State: 1}
  68. err := o.Read(&r, "T_user", "T_pass", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  69. if err != nil {
  70. logs.Error(lib.FuncName(), err)
  71. }
  72. return err, r
  73. }
  74. // 获取列表
  75. func Read_Admin_List(T_name string, page int, page_z int, powerMap map[int]string) ([]Admin_R, int) {
  76. o := orm.NewOrm()
  77. var offset int
  78. if page <= 1 {
  79. offset = 0
  80. } else {
  81. offset = (page - 1) * page_z
  82. }
  83. qs := o.QueryTable(new(Admin))
  84. var r []Admin
  85. cond := orm.NewCondition()
  86. cond1 := cond.And("T_State", 1).AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
  87. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  88. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  89. // 转换
  90. var Admin_r []Admin_R
  91. for _, v := range r {
  92. Admin_r = append(Admin_r, AdminToAdmin_R(v, powerMap))
  93. }
  94. return Admin_r, int(cnt)
  95. }
  96. // 修改
  97. func Update_Admin(m Admin, cols ...string) bool {
  98. o := orm.NewOrm()
  99. if num, err := o.Update(&m, cols...); err == nil {
  100. logs.Println("Number of records updated in database:", num)
  101. return true
  102. }
  103. return false
  104. }
  105. // 删除
  106. func Delete_Admin(v Admin) bool {
  107. o := orm.NewOrm()
  108. if num, err := o.Delete(&v); err == nil {
  109. logs.Println("Number of records deleted in database:", num)
  110. } else {
  111. return false
  112. }
  113. return true
  114. }
  115. // 删除
  116. func Delete_Admin_(v Admin) bool {
  117. o := orm.NewOrm()
  118. v.T_State = 0
  119. if num, err := o.Update(&v, "T_State"); err == nil {
  120. logs.Println("Number of records updated in database:", num)
  121. } else {
  122. return false
  123. }
  124. return true
  125. }
  126. // 获取全部列表
  127. func Read_Admin_List_ALL() (maps []Admin) {
  128. o := orm.NewOrm()
  129. qs := o.QueryTable(new(Admin))
  130. qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
  131. return maps
  132. }
  133. // 获取全部列表-包括软删除数据
  134. func Read_Admin_List_ALL_1() (maps []Admin) {
  135. o := orm.NewOrm()
  136. qs := o.QueryTable(new(Admin))
  137. qs.OrderBy("Id").All(&maps)
  138. return maps
  139. }
  140. // 获取全部列表
  141. func Read_Admin_List_ALL_Power(PowerMaps map[int]string) (maps []AdminPower_R) {
  142. o := orm.NewOrm()
  143. var r []Admin
  144. qs := o.QueryTable(new(Admin))
  145. qs.Filter("T_State", 1).OrderBy("Id").All(&r)
  146. // 转换
  147. var Admin_Power_r []AdminPower_R
  148. for _, v := range r {
  149. Admin_Power_r = append(Admin_Power_r, AdminToAdminPower_R(v, PowerMaps))
  150. }
  151. return Admin_Power_r
  152. }
  153. // {"uuid":"name"}
  154. func AdminListToMap(A []Admin) map[string]string {
  155. maps := make(map[string]string, len(A))
  156. for _, v := range A {
  157. maps[v.T_uuid] = v.T_name
  158. }
  159. return maps
  160. }
  161. type AdminPower_R struct {
  162. T_uuid string `orm:"column(t_uuid)"`
  163. T_name string `orm:"column(t_name)"` // 姓名
  164. T_power_name string `orm:"column(t_power_name)"` // 权限
  165. }
  166. // PowerMaps {1:'管理员'}
  167. func AdminToAdminPower_R(T Admin, PowerMaps map[int]string) (T_r AdminPower_R) {
  168. T_power_name, ok := PowerMaps[T.T_power]
  169. if !ok {
  170. T_power_name = ""
  171. }
  172. T_r.T_uuid = T.T_uuid
  173. T_r.T_name = T.T_name
  174. T_r.T_power_name = T_power_name
  175. return T_r
  176. }