Admin.go 5.2 KB

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