mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-02-06 12:55:36 +08:00
refactor: 精简base.repo与base.app等
This commit is contained in:
@@ -5,17 +5,17 @@ import (
|
||||
"mayfly-go/internal/sys/consts"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/contextx"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Role interface {
|
||||
base.App[*entity.Role]
|
||||
|
||||
GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
|
||||
ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error)
|
||||
@@ -29,7 +29,7 @@ type Role interface {
|
||||
GetRoleResources(roleId uint64, toEntity any)
|
||||
|
||||
// 保存角色资源关联记录
|
||||
SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64)
|
||||
SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) error
|
||||
|
||||
// 关联账号角色
|
||||
RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error
|
||||
@@ -42,53 +42,55 @@ type Role interface {
|
||||
}
|
||||
|
||||
type roleAppImpl struct {
|
||||
roleRepo repository.Role `inject:"RoleRepo"`
|
||||
accountRoleRepo repository.AccountRole `inject:"AccountRoleRepo"`
|
||||
base.AppImpl[*entity.Role, repository.Role]
|
||||
|
||||
accountRoleRepo repository.AccountRole `inject:"AccountRoleRepo"`
|
||||
roleResourceRepo repository.RoleResource `inject:"RoleResourceRepo"`
|
||||
}
|
||||
|
||||
func (r *roleAppImpl) InjectRoleRepo(repo repository.Role) {
|
||||
r.Repo = repo
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
return m.roleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
|
||||
return m.GetRepo().GetPageList(condition, pageParam, toEntity, orderBy...)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) {
|
||||
return m.roleRepo.ListByQuery(condition)
|
||||
return m.GetRepo().ListByQuery(condition)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error {
|
||||
if role.Id != 0 {
|
||||
// code不可更改,防止误传
|
||||
role.Code = ""
|
||||
return gormx.UpdateById(role)
|
||||
return m.UpdateById(ctx, role)
|
||||
}
|
||||
|
||||
role.Status = 1
|
||||
return m.roleRepo.Insert(ctx, role)
|
||||
return m.Insert(ctx, role)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error {
|
||||
// 删除角色与资源账号的关联关系
|
||||
return gormx.Tx(
|
||||
func(db *gorm.DB) error {
|
||||
return m.roleRepo.DeleteByIdWithDb(ctx, db, id)
|
||||
},
|
||||
func(db *gorm.DB) error {
|
||||
return gormx.DeleteByWithDb(db, &entity.RoleResource{RoleId: id})
|
||||
},
|
||||
func(db *gorm.DB) error {
|
||||
return gormx.DeleteByWithDb(db, &entity.AccountRole{RoleId: id})
|
||||
},
|
||||
)
|
||||
return m.Tx(ctx, func(ctx context.Context) error {
|
||||
return m.DeleteById(ctx, id)
|
||||
}, func(ctx context.Context) error {
|
||||
return m.roleResourceRepo.DeleteByCond(ctx, &entity.RoleResource{RoleId: id})
|
||||
}, func(ctx context.Context) error {
|
||||
return m.accountRoleRepo.DeleteByCond(ctx, &entity.AccountRole{RoleId: id})
|
||||
})
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
||||
return m.roleRepo.GetRoleResourceIds(roleId)
|
||||
return m.roleResourceRepo.GetRoleResourceIds(roleId)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetRoleResources(roleId uint64, toEntity any) {
|
||||
m.roleRepo.GetRoleResources(roleId, toEntity)
|
||||
m.roleResourceRepo.GetRoleResources(roleId, toEntity)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) {
|
||||
func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) error {
|
||||
oIds := m.GetRoleResourceIds(roleId)
|
||||
|
||||
addIds, delIds, _ := collx.ArrayCompare(resourceIds, oIds)
|
||||
@@ -105,11 +107,18 @@ func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resou
|
||||
rr.IsDeleted = undeleted
|
||||
addVals = append(addVals, rr)
|
||||
}
|
||||
m.roleRepo.SaveRoleResource(addVals)
|
||||
|
||||
for _, v := range delIds {
|
||||
m.roleRepo.DeleteRoleResource(roleId, v)
|
||||
}
|
||||
return m.Tx(ctx, func(ctx context.Context) error {
|
||||
if len(addVals) > 0 {
|
||||
return m.roleResourceRepo.BatchInsert(ctx, addVals)
|
||||
}
|
||||
return nil
|
||||
}, func(ctx context.Context) error {
|
||||
if len(delIds) > 0 {
|
||||
return m.roleResourceRepo.DeleteByCond(ctx, model.NewCond().In("resource_id", delIds))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error {
|
||||
@@ -118,7 +127,7 @@ func (m *roleAppImpl) RelateAccountRole(ctx context.Context, accountId, roleId u
|
||||
return m.accountRoleRepo.DeleteByCond(ctx, accountRole)
|
||||
}
|
||||
|
||||
err := m.accountRoleRepo.GetBy(accountRole)
|
||||
err := m.accountRoleRepo.GetByCond(accountRole)
|
||||
if err == nil {
|
||||
return errorx.NewBiz("该用户已拥有该权限")
|
||||
}
|
||||
@@ -133,7 +142,7 @@ func (m *roleAppImpl) RelateAccountRole(ctx context.Context, accountId, roleId u
|
||||
|
||||
func (m *roleAppImpl) GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error) {
|
||||
var res []*entity.AccountRole
|
||||
err := m.accountRoleRepo.ListByCond(&entity.AccountRole{AccountId: accountId}, &res)
|
||||
err := m.accountRoleRepo.SelectByCond(&entity.AccountRole{AccountId: accountId}, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user