面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_admin.go 4.1 KiB

9 months ago
9 months ago
9 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type AdminDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (adminDb *AdminDb) Set() { // set方法
  12. adminDb.Db = Db
  13. }
  14. func (adminDb *AdminDb) CreateAdminId() (admId int, err error) {
  15. m := new(model.Admin)
  16. has, err := adminDb.Db.Desc("adm_id").Get(m)
  17. if err != nil {
  18. return 0, logx.Error(err)
  19. }
  20. if has == false {
  21. return 0, nil
  22. }
  23. admId = m.AdmId + 1
  24. return admId, nil
  25. }
  26. func (adminDb *AdminDb) AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error) {
  27. if reflect.TypeOf(admId).Kind() == reflect.Slice {
  28. return session.In("adm_id", admId).Delete(model.Admin{})
  29. } else {
  30. return session.Where("id = ?", admId).Delete(model.Admin{})
  31. }
  32. }
  33. func (adminDb *AdminDb) GetAdmin(id int) (m *model.Admin, err error) {
  34. m = new(model.Admin)
  35. has, err := adminDb.Db.Where("adm_id =?", id).Get(m)
  36. if err != nil {
  37. return nil, logx.Error(err)
  38. }
  39. if has == false {
  40. return nil, nil
  41. }
  42. return m, nil
  43. }
  44. func (adminDb *AdminDb) GetAdminByUserName(userName string) (m *model.Admin, err error) {
  45. m = new(model.Admin)
  46. has, err := adminDb.Db.Where("username =?", userName).Get(m)
  47. if err != nil {
  48. return nil, logx.Error(err)
  49. }
  50. if has == false {
  51. return nil, nil
  52. }
  53. return m, nil
  54. }
  55. func (adminDb *AdminDb) GetAdminRolePermission(admId int) (list []*AdminRolePermission, total int64, err error) {
  56. total, err = adminDb.Db.Where("admin.adm_id =?", admId).
  57. Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
  58. Join("LEFT", "role", "admin_role.role_id = role.id").
  59. Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
  60. Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
  61. Join("LEFT", "permission_group_permission", "permission_group.id = permission_group_permission.group_id").
  62. Join("LEFT", "permission", "permission_group_permission.permission_id = permission.id").
  63. FindAndCount(&list)
  64. return
  65. }
  66. func (adminDb *AdminDb) FindAdminRolePermissionGroup(admId int) (list []*AdminRolePermissionGroup, total int64, err error) {
  67. total, err = adminDb.Db.Where("admin.adm_id =?", admId).
  68. Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
  69. Join("LEFT", "role", "admin_role.role_id = role.id").
  70. //Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
  71. //Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
  72. FindAndCount(&list)
  73. return
  74. }
  75. func (adminDb *AdminDb) FindAdminWithEnterprise(admId int) (list []*AdminEnterprise, total int64, err error) {
  76. total, err = adminDb.Db.Where("admin.adm_id =?", admId).
  77. Join("LEFT", "admin_with_enterprise", "admin.adm_id = admin_with_enterprise.adm_id").
  78. Join("LEFT", "enterprise", "admin_with_enterprise.enterprise_id = enterprise.id").
  79. FindAndCount(&list)
  80. return
  81. }
  82. func (adminDb *AdminDb) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) {
  83. sess := adminDb.Db.Desc("adm_id").Limit(limit, (page-1)*limit)
  84. if username != "" {
  85. sess.And("username like ?", "%"+username+"%")
  86. }
  87. if state != 0 {
  88. sess.And("state = ?", state)
  89. }
  90. total, err = sess.FindAndCount(&list)
  91. if err != nil {
  92. return nil, 0, err
  93. }
  94. return
  95. }
  96. func (adminDb *AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) {
  97. affected, err := adminDb.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m)
  98. if err != nil {
  99. return 0, err
  100. }
  101. return affected, nil
  102. }
  103. func (adminDb *AdminDb) AdminInsert(m *model.Admin) (int64, error) {
  104. insertAffected, err := adminDb.Db.InsertOne(m)
  105. if err != nil {
  106. return 0, err
  107. }
  108. return insertAffected, nil
  109. }
  110. func GetAllAdmin(eg *xorm.Engine) *[]model.Admin {
  111. var data []model.Admin
  112. err := eg.Where("is_super_administrator=?", 0).Find(&data)
  113. if err != nil {
  114. return nil
  115. }
  116. return &data
  117. }
  118. func GetAdminMore(eg *xorm.Engine, ids []int) map[int]model.Admin {
  119. data := make(map[int]model.Admin)
  120. if len(ids) > 0 {
  121. list := make([]model.Admin, 0)
  122. eg.In("adm_id", ids).Find(&list)
  123. for _, v := range list {
  124. data[v.AdmId] = v
  125. }
  126. }
  127. return data
  128. }