智慧食堂
Você não pode selecionar mais de 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.

115 linhas
3.3 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) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) {
  76. sess := adminDb.Db.Desc("adm_id").Limit(limit, (page-1)*limit)
  77. if username != "" {
  78. sess.And("username like ?", "%"+username+"%")
  79. }
  80. if state != 0 {
  81. sess.And("state = ?", state)
  82. }
  83. total, err = sess.FindAndCount(&list)
  84. if err != nil {
  85. return nil, 0, err
  86. }
  87. return
  88. }
  89. func (adminDb *AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) {
  90. affected, err := adminDb.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m)
  91. if err != nil {
  92. return 0, err
  93. }
  94. return affected, nil
  95. }
  96. func (adminDb *AdminDb) AdminInsert(m *model.Admin) (int64, error) {
  97. insertAffected, err := adminDb.Db.InsertOne(m)
  98. if err != nil {
  99. return 0, err
  100. }
  101. return insertAffected, nil
  102. }