system_save_role_logic.go
3.1 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
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/contextdata"
"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 SystemSaveRoleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemSaveRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemSaveRoleLogic {
return &SystemSaveRoleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemSaveRoleLogic) SystemSaveRole(req *types.RoleSaveRequest) (resp *types.RoleSaveResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
userAddRoleList []*domain.User
)
role := NewDomainRole(l.ctx, req.Role)
for _, authId := range req.Role.AuthIds {
if auth := role.GetAuth(authId); auth == nil {
return nil, xerr.NewErrMsg(fmt.Sprintf("unknown auth %d ", authId))
}
}
for _, item := range req.Role.Users {
var user *domain.User
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, item.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 {
if role, err = l.svcCtx.RoleRepository.Insert(ctx, conn, role); 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
}
}
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("添加角色失败", err)
}
resp = &types.RoleSaveResponse{}
return
}
func NewDomainRole(ctx context.Context, item types.RoleItem) *domain.Role {
var (
role *domain.Role
users []domain.User = make([]domain.User, 0)
userToken = contextdata.GetUserTokenFromCtx(ctx)
)
lo.ForEach(item.Users, func(item types.RoleUser, index int) {
users = append(users, domain.User{
Id: item.Id,
Name: item.Name,
})
})
role = &domain.Role{
CompanyId: userToken.CompanyId,
Name: item.Name,
Auths: item.AuthIds,
Remark: item.Remark,
Users: users,
}
return role
}
func NewTypesRole(item *domain.Role) types.RoleItem {
var (
role types.RoleItem
users []types.RoleUser = make([]types.RoleUser, 0)
)
lo.ForEach(item.Users, func(item domain.User, index int) {
users = append(users, types.RoleUser{
Id: item.Id,
Name: item.Name,
})
})
role = types.RoleItem{
Id: item.Id,
CompanyId: item.CompanyId,
Name: item.Name,
AuthIds: item.Auths,
Remark: item.Remark,
Users: users,
UpdatedAt: item.UpdatedAt,
}
return role
}