roles.go
10.3 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package service
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"strconv"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/roles/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/roles/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/roles/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
)
// roles
type RolesService struct {
}
// 创建role
func (rolesService *RolesService) RoleAdd(roleAddCommand *command.RoleAddCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleAddCommand.Operator)
result, err := creationUserGateway.RoleCreate(allied_creation_user.ReqRoleCreate{
RoleName: roleAddCommand.RoleName,
Desc: roleAddCommand.Desc,
OrgId: roleAddCommand.OrgId,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
data := struct {
RoleId string `json:"roleId"`
command.RoleAddCommand
}{
RoleId: strconv.Itoa(int(result.RoleID)),
RoleAddCommand: *roleAddCommand,
}
return data, err
}
// 编辑role
func (rolesService *RolesService) RoleEdit(roleUpdateCommand *command.RoleUpdateCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleUpdateCommand.Operator)
roleId, _ := strconv.Atoi(roleUpdateCommand.RoleId)
_, err := creationUserGateway.RoleUpdate(allied_creation_user.ReqRoleUpdate{
RoleId: int64(roleId),
RoleName: roleUpdateCommand.RoleName,
Desc: roleUpdateCommand.Desc,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return roleUpdateCommand, err
}
// 返单个角色
func (rolesService *RolesService) RoleGet(roleGetQuery *query.RoleGetQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleGetQuery.Operator)
roleId, _ := strconv.Atoi(roleGetQuery.RoleId)
roleData, err := creationUserGateway.RoleGet(allied_creation_user.ReqRoleGet{
RoleId: int64(roleId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
accessMenus, err := creationUserGateway.RoleGetAccessMenus(allied_creation_user.ReqRoleGetAccessMenus{
RoleId: int64(roleId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
relatedUser, err := creationUserGateway.RoleRelatedUser(allied_creation_user.ReqRoleGetRelatedUser{
RoleId: int64(roleId),
OrgIds: roleGetQuery.Operator.OrgIds,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
result := map[string]interface{}{
"role": roleData,
"menus": accessMenus.Menus,
"users": relatedUser.RoleUser,
}
return result, nil
}
// 返回role列表
func (rolesService *RolesService) RoleList(roleListQuery *query.RoleListQuery) (int64, interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleListQuery.Operator)
req := allied_creation_user.ReqRoleSearch{
Offset: (roleListQuery.PageNumber - 1) * roleListQuery.PageSize,
Limit: roleListQuery.PageSize,
OrgName: roleListQuery.OrgName,
MatchRoleName: roleListQuery.RoleName,
InOrgIds: roleListQuery.Operator.OrgIds,
RoleType: roleListQuery.RoleType,
}
if len(roleListQuery.OrgIds) > 0 {
req.InOrgIds = roleListQuery.OrgIds
}
roleList, err := creationUserGateway.RoleSearch(req)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := []dto.RoleItem{}
for _, v := range roleList.Roles {
item := dto.RoleItem{
RoleId: strconv.Itoa(int(v.RoleID)),
OrgId: strconv.Itoa(int(v.OrgID)),
RoleName: v.RoleName,
Describe: v.Desc,
OrgName: v.Ext.OrgName,
RoleType: v.RoleType,
Ext: v.Ext,
}
if int64(v.OrgID) == roleListQuery.Operator.OrgId {
item.AuthFlag = true
}
if (item.RoleType & domain.UserTypeCompanyAdmin) > 0 {
item.AuthFlag = false
}
result = append(result, item)
}
var cnt int64 = roleList.Count
return cnt, result, nil
}
// 编辑角色关联权限菜单的前置准备数据
func (rolesService *RolesService) RoleMenuBeforeEdit(roleMenuBeforeEditQuery *query.RoleMenuBeforeEditQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleMenuBeforeEditQuery.Operator)
roleId, _ := strconv.Atoi(roleMenuBeforeEditQuery.RoleId)
roles, err := creationUserGateway.RoleSearch(allied_creation_user.ReqRoleSearch{
OrgId: roleMenuBeforeEditQuery.Operator.OrgId,
Limit: 999,
RoleType: 1,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
menus, err := creationUserGateway.RoleGetAccessMenus(allied_creation_user.ReqRoleGetAccessMenus{
RoleId: int64(roleId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
result := map[string]interface{}{
"roles": roles.Roles,
"menus": menus.Menus,
}
return result, nil
}
// 编辑角色关联用户的前置准备数据
func (rolesService *RolesService) RoleUserBeforeEdit(roleUserBeforeEditQuery *query.RoleUserBeforeEditQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleUserBeforeEditQuery.Operator)
roleId, _ := strconv.Atoi(roleUserBeforeEditQuery.RoleId)
roles, err := creationUserGateway.RoleSearch(allied_creation_user.ReqRoleSearch{
OrgId: roleUserBeforeEditQuery.Operator.OrgId,
Limit: 999,
RoleType: 1,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
orgs, err := creationUserGateway.OrgGetSubDepartment(allied_creation_user.ReqOrgGetSubDepartment{
OrgId: roleUserBeforeEditQuery.Operator.OrgId,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
result := map[string]interface{}{
"orgs": orgs.Orgs,
"roles": roles.Roles,
}
// 有传入角色ID才返回关联角色用户数据
if roleId > 0 {
relatedUser, err := creationUserGateway.RoleGetRelatedUser(allied_creation_user.ReqRoleGetRelatedUser{
RoleId: int64(roleId),
OrgId: roleUserBeforeEditQuery.Operator.OrgId,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
result["notInRoleUser"] = relatedUser.NotInRoleUser
result["roleUser"] = relatedUser.RoleUser
}
return result, nil
}
// 角色编辑关联菜单权限
func (rolesService *RolesService) RoleMenuEdit(roleMenuEditCommand *command.RoleMenuEditCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleMenuEditCommand.Operator)
roleId, _ := strconv.Atoi(roleMenuEditCommand.RoleId)
var menuIds []int64
for _, v := range roleMenuEditCommand.MenuId {
id, err := strconv.Atoi(v)
if err == nil {
menuIds = append(menuIds, int64(id))
}
}
_, err := creationUserGateway.RoleSetAccessMenus(allied_creation_user.ReqRoleSetAccessMenus{
RoleId: int64(roleId),
AccessMenus: menuIds,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return roleMenuEditCommand, err
}
// 移除role
func (rolesService *RolesService) RoleRemove(roleRemoveCommand *command.RoleRemoveCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleRemoveCommand.Operator)
var roleIds []int64
for _, v := range roleRemoveCommand.RoleIds {
id, err := strconv.Atoi(v)
if err == nil {
roleIds = append(roleIds, int64(id))
}
}
_, err := creationUserGateway.RoleBatchRemove(allied_creation_user.ReqRoleBatchRemove{
RoleIds: roleIds,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return roleRemoveCommand, err
}
// 角色添加关联用户
func (rolesService *RolesService) RoleUserAdd(roleUserAddCommand *command.RoleUserAddCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleUserAddCommand.Operator)
roleId, _ := strconv.Atoi(roleUserAddCommand.RoleId)
var userIds []int64
for _, v := range roleUserAddCommand.UserId {
id, err := strconv.Atoi(v)
if err == nil {
userIds = append(userIds, int64(id))
}
}
_, err := creationUserGateway.RoleAssign(allied_creation_user.ReqRoleAssign{
RoleId: int64(roleId),
UserIds: userIds,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return roleUserAddCommand, err
}
// 角色添加关联用户
func (rolesService *RolesService) RoleUserDelete(roleUserDeleteCommand *command.RoleUserDeleteCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleUserDeleteCommand.Operator)
roleId, _ := strconv.Atoi(roleUserDeleteCommand.RoleId)
var userIds []int64
for _, v := range roleUserDeleteCommand.UserId {
id, err := strconv.Atoi(v)
if err == nil {
userIds = append(userIds, int64(id))
}
}
_, err := creationUserGateway.RoleUnassign(allied_creation_user.ReqRoleUnassign{
RoleId: int64(roleId),
UserIds: userIds,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return roleUserDeleteCommand, err
}
// 角色下关联用户的数据
func (rolesService *RolesService) RoleUserInfo(roleUserInfoQuery *query.RoleUserInfoQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleUserInfoQuery.Operator)
relatedUser, err := creationUserGateway.RoleGetRelatedUser(allied_creation_user.ReqRoleGetRelatedUser{
RoleId: roleUserInfoQuery.RoleId,
OrgId: roleUserInfoQuery.Operator.OrgId,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var result = make(map[string]interface{})
result["notInRoleUser"] = relatedUser.NotInRoleUser
result["roleUser"] = relatedUser.RoleUser
return result, nil
}
func NewRolesService(options map[string]interface{}) *RolesService {
newRolesService := &RolesService{}
return newRolesService
}