面包店
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

123 linhas
3.6 KiB

  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. }