2021-06-07 17:22:07 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"context"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"mayfly-go/pkg/contextx"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"mayfly-go/pkg/gormx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
"strings"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"time"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type Role interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
SaveRole(role *entity.Role) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
DeleteRole(id uint64) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
GetRoleResourceIds(roleId uint64) []uint64
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetRoleResources(roleId uint64, toEntity any)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 保存角色资源关联记录
|
2023-07-27 16:47:05 +08:00
|
|
|
|
SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 删除角色资源关联记录
|
2021-06-07 17:22:07 +08:00
|
|
|
|
DeleteRoleResource(roleId uint64, resourceId uint64)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 获取账号角色id列表
|
2021-06-07 17:22:07 +08:00
|
|
|
|
GetAccountRoleIds(accountId uint64) []uint64
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 保存账号角色关联信息
|
2023-07-27 16:47:05 +08:00
|
|
|
|
SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
DeleteAccountRole(accountId, roleId uint64)
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetAccountRoles(accountId uint64, toEntity any)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func newRoleApp(roleRepo repository.Role) Role {
|
|
|
|
|
|
return &roleAppImpl{
|
|
|
|
|
|
roleRepo: roleRepo,
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type roleAppImpl struct {
|
|
|
|
|
|
roleRepo repository.Role
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *roleAppImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
return m.roleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *roleAppImpl) SaveRole(role *entity.Role) error {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
role.Code = strings.ToUpper(role.Code)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
if role.Id != 0 {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// code不可更改,防止误传
|
|
|
|
|
|
role.Code = ""
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.UpdateById(role)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
role.Status = 1
|
|
|
|
|
|
return gormx.Insert(role)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *roleAppImpl) DeleteRole(id uint64) error {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 删除角色与资源的关联关系
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.Tx(
|
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
return m.roleRepo.DeleteByIdWithDb(db, id)
|
|
|
|
|
|
},
|
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
return gormx.DeleteByWithDb(db, &entity.RoleResource{RoleId: id})
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *roleAppImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
return m.roleRepo.GetRoleResourceIds(roleId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (m *roleAppImpl) GetRoleResources(roleId uint64, toEntity any) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
m.roleRepo.GetRoleResources(roleId, toEntity)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-27 16:47:05 +08:00
|
|
|
|
func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) {
|
|
|
|
|
|
oIds := m.GetRoleResourceIds(roleId)
|
|
|
|
|
|
|
|
|
|
|
|
addIds, delIds, _ := collx.ArrayCompare(resourceIds, oIds, func(i1, i2 uint64) bool {
|
|
|
|
|
|
return i1 == i2
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
la := contextx.GetLoginAccount(ctx)
|
|
|
|
|
|
createTime := time.Now()
|
|
|
|
|
|
creator := la.Username
|
|
|
|
|
|
creatorId := la.Id
|
|
|
|
|
|
undeleted := model.ModelUndeleted
|
|
|
|
|
|
|
|
|
|
|
|
addVals := make([]*entity.RoleResource, 0)
|
|
|
|
|
|
for _, v := range addIds {
|
|
|
|
|
|
rr := &entity.RoleResource{RoleId: roleId, ResourceId: v, CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
|
|
|
|
|
|
rr.IsDeleted = undeleted
|
|
|
|
|
|
addVals = append(addVals, rr)
|
|
|
|
|
|
}
|
|
|
|
|
|
m.roleRepo.SaveRoleResource(addVals)
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range delIds {
|
|
|
|
|
|
m.DeleteRoleResource(roleId, v)
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *roleAppImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
m.roleRepo.DeleteRoleResource(roleId, resourceId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *roleAppImpl) GetAccountRoleIds(accountId uint64) []uint64 {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
return m.roleRepo.GetAccountRoleIds(accountId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-27 16:47:05 +08:00
|
|
|
|
// 保存账号角色关联信息
|
|
|
|
|
|
func (m *roleAppImpl) SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64) {
|
|
|
|
|
|
oIds := m.GetAccountRoleIds(accountId)
|
|
|
|
|
|
|
|
|
|
|
|
addIds, delIds, _ := collx.ArrayCompare(roleIds, oIds, func(i1, i2 uint64) bool {
|
|
|
|
|
|
return i1 == i2
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
la := contextx.GetLoginAccount(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
createTime := time.Now()
|
|
|
|
|
|
creator := la.Username
|
|
|
|
|
|
creatorId := la.Id
|
|
|
|
|
|
for _, v := range addIds {
|
|
|
|
|
|
rr := &entity.AccountRole{AccountId: accountId, RoleId: v, CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
|
|
|
|
|
|
m.roleRepo.SaveAccountRole(rr)
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, v := range delIds {
|
|
|
|
|
|
m.DeleteAccountRole(accountId, v)
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *roleAppImpl) DeleteAccountRole(accountId, roleId uint64) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
m.roleRepo.DeleteAccountRole(accountId, roleId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (m *roleAppImpl) GetAccountRoles(accountId uint64, toEntity any) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
m.roleRepo.GetAccountRoles(accountId, toEntity)
|
|
|
|
|
|
}
|