system_update_role_logic.go
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package role
import (
"context"
"fmt"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemUpdateRoleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUpdateRoleLogic {
return &SystemUpdateRoleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUpdateRoleLogic) SystemUpdateRole(req *types.RoleUpdateRequest) (resp *types.RoleUpdateResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
role *domain.Role
beforeUserIds []int64
afterUserIds []int64
userRemoveRoleList []*domain.User
userAddRoleList []*domain.User
userRoles []*domain.UserRole
)
if role, err = l.svcCtx.RoleRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("角色不存在", err)
}
for _, authId := range req.Role.AuthIds {
if auth := role.GetAuth(authId); auth == nil {
return nil, xerr.NewErrMsg(fmt.Sprintf("unknown auth %d ", authId))
}
}
_, userRoles, _ = l.svcCtx.UserRoleRepository.Find(l.ctx, conn, domain.IndexCompanyId(role.CompanyId)().WithKV("roleId", role.Id).WithFindOnly())
beforeUserIds = domain.Values(userRoles, func(item *domain.UserRole) int64 {
return item.UserId
})
tempRole := NewDomainRole(l.ctx, req.Role)
lo.ForEach(req.Role.Users, func(item types.RoleUser, index int) {
afterUserIds = append(afterUserIds, item.Id)
})
removeList, addList := lo.Difference(beforeUserIds, afterUserIds)
for _, id := range removeList {
var user *domain.User
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, id); err != nil {
return nil, xerr.NewErrMsgErr("更新角色失败", err)
}
userRemoveRoleList = append(userRemoveRoleList, user)
}
for _, id := range addList {
var user *domain.User
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, id); err != nil {
return nil, xerr.NewErrMsgErr("更新角色失败", err)
}
userAddRoleList = append(userAddRoleList, user)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// 更新角色
role.Name = tempRole.Name
role.Auths = tempRole.Auths
role.Remark = tempRole.Remark
if role, err = l.svcCtx.RoleRepository.UpdateWithVersion(ctx, conn, role); err != nil {
return err
}
// 移除角色用户
for _, user := range userRemoveRoleList {
user.RemoveRole(role.Id)
if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
return err
}
if err = l.svcCtx.UserRoleRepository.DeleteByUserAndRole(ctx, conn, user.Id, role.Id); err != nil {
return err
}
}
// 新增角色用户
for _, user := range userAddRoleList {
user.AddRole(role.Id)
if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
return err
}
if _, err = l.svcCtx.UserRoleRepository.Insert(l.ctx, conn, &domain.UserRole{
CompanyId: user.CompanyId,
UserId: user.Id,
RoleId: role.Id,
}); err != nil {
return err
}
}
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("更新角色失败", err)
}
resp = &types.RoleUpdateResponse{}
return
}