广告平台(站长下代理使用)
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.
 
 
 
 
 
 

116 lines
3.2 KiB

  1. package implement
  2. import (
  3. "applet/app/db/dao"
  4. "applet/app/db/model"
  5. "applet/app/utils/logx"
  6. "reflect"
  7. "xorm.io/xorm"
  8. )
  9. func NewAdminDb(engine *xorm.Engine) dao.AdminDao {
  10. return &AdminDb{Db: engine}
  11. }
  12. type AdminDb struct {
  13. Db *xorm.Engine
  14. }
  15. func (a AdminDb) CreateAdminId() (admId int, err error) {
  16. m := new(model.Admin)
  17. has, err := a.Db.Desc("adm_id").Get(m)
  18. if err != nil {
  19. return 0, logx.Error(err)
  20. }
  21. if has == false {
  22. return 0, nil
  23. }
  24. admId = m.AdmId + 1
  25. return admId, nil
  26. }
  27. func (a AdminDb) AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error) {
  28. if reflect.TypeOf(admId).Kind() == reflect.Slice {
  29. return session.In("adm_id", admId).Delete(model.Admin{})
  30. } else {
  31. return session.Where("id = ?", admId).Delete(model.Admin{})
  32. }
  33. }
  34. func (a AdminDb) GetAdmin(id int) (m *model.Admin, err error) {
  35. m = new(model.Admin)
  36. has, err := a.Db.Where("adm_id =?", id).Get(m)
  37. if err != nil {
  38. return nil, logx.Error(err)
  39. }
  40. if has == false {
  41. return nil, nil
  42. }
  43. return m, nil
  44. }
  45. func (a AdminDb) GetAdminByUserName(userName string) (m *model.Admin, err error) {
  46. m = new(model.Admin)
  47. has, err := a.Db.Where("username =?", userName).Get(m)
  48. if err != nil {
  49. return nil, logx.Error(err)
  50. }
  51. if has == false {
  52. return nil, nil
  53. }
  54. return m, nil
  55. }
  56. func (a AdminDb) GetAdminRolePermission(admId int) (list []*dao.AdminRolePermission, total int64, err error) {
  57. total, err = a.Db.Where("admin.adm_id =?", admId).
  58. Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
  59. Join("LEFT", "role", "admin_role.role_id = role.id").
  60. Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
  61. Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
  62. Join("LEFT", "permission_group_permission", "permission_group.id = permission_group_permission.group_id").
  63. Join("LEFT", "permission", "permission_group_permission.permission_id = permission.id").
  64. FindAndCount(&list)
  65. return
  66. }
  67. func (a AdminDb) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) {
  68. sess := a.Db.Desc("adm_id").Limit(limit, (page-1)*limit)
  69. if username != "" {
  70. sess.And("username like ?", "%"+username+"%")
  71. }
  72. if state != 0 {
  73. sess.And("state = ?", state)
  74. }
  75. total, err = sess.FindAndCount(&list)
  76. if err != nil {
  77. return nil, 0, err
  78. }
  79. return
  80. }
  81. func (a AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) {
  82. affected, err := a.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m)
  83. if err != nil {
  84. return 0, err
  85. }
  86. return affected, nil
  87. }
  88. func (a AdminDb) AdminInsert(m *model.Admin) (int64, error) {
  89. insertAffected, err := a.Db.InsertOne(m)
  90. if err != nil {
  91. return 0, err
  92. }
  93. return insertAffected, nil
  94. }
  95. func (a AdminDb) FindAdminRolePermissionGroup(admId int) (list []*dao.AdminRolePermissionGroup, total int64, err error) {
  96. total, err = a.Db.Where("admin.adm_id =?", admId).
  97. Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
  98. Join("LEFT", "role", "admin_role.role_id = role.id").
  99. //Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
  100. //Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
  101. FindAndCount(&list)
  102. return
  103. }