roles.go
7.5 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
package service
import (
"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.CompanyId,
roleAddCommand.Operator.OrgId,
roleAddCommand.Operator.UserId)
result, err := creationUserGateway.RoleCreate(allied_creation_user.ReqRoleCreate{
RoleName: roleAddCommand.RoleName,
Desc: roleAddCommand.Desc,
})
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.CompanyId,
roleUpdateCommand.Operator.OrgId,
roleUpdateCommand.Operator.UserId)
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.CompanyId,
roleGetQuery.Operator.OrgId,
roleGetQuery.Operator.UserId)
roleId, _ := strconv.Atoi(roleGetQuery.RoleId)
roleData, err := creationUserGateway.RoleGet(allied_creation_user.ReqRoleGet{
RoleId: int64(roleId),
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//TODO 补充字段 :权限数据,关联用户数据
result := map[string]interface{}{
"role": roleData.Role,
}
return result, nil
}
// 返回role列表
func (rolesService *RolesService) RoleList(roleListQuery *query.RoleListQuery) (int64, interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleListQuery.Operator.CompanyId,
roleListQuery.Operator.OrgId,
roleListQuery.Operator.UserId)
roleList, err := creationUserGateway.RoleSearch(allied_creation_user.ReqRoleSearch{
Offset: (roleListQuery.PageNumber - 1) * roleListQuery.PageSize,
Limit: roleListQuery.PageSize,
OrgName: roleListQuery.OrgName,
RoleName: roleListQuery.RoleName,
})
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := []dto.RoleItem{}
for _, v := range roleList.Roles {
result = append(result, dto.RoleItem{
RoleId: strconv.Itoa(v.RoleID),
OrgId: strconv.Itoa(v.OrgID),
RoleName: v.RoleName,
Describe: v.Desc,
OrgName: v.Ext.OrgName,
RoleType: v.RoleType,
})
}
var cnt int64 = roleList.Count
return cnt, result, nil
}
// 编辑角色关联权限菜单的前置准备数据
func (rolesService *RolesService) RoleMenuBeforeEdit(roleMenuBeforeEditQuery *query.RoleMenuBeforeEditQuery) (interface{}, error) {
return nil, nil
}
// 编辑角色关联用户的前置准备数据
func (rolesService *RolesService) RoleUserBeforeEdit(roleUserBeforeEditQuery *query.RoleUserBeforeEditQuery) (interface{}, error) {
//TODO
return nil, nil
}
// 角色编辑关联菜单权限
func (rolesService *RolesService) RoleMenuEdit(roleMenuEditCommand *command.RoleMenuEditCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleMenuEditCommand.Operator.CompanyId,
roleMenuEditCommand.Operator.OrgId,
roleMenuEditCommand.Operator.UserId)
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.CompanyId,
roleRemoveCommand.Operator.OrgId,
roleRemoveCommand.Operator.UserId)
var roleIds []int64
for _, v := range roleRemoveCommand.RoleId {
id, err := strconv.Atoi(v)
if err == nil {
roleIds = append(roleIds, int64(id))
}
}
_, err := creationUserGateway.RoleRemove(allied_creation_user.ReqRoleRemove{
//TODO 修改 为 切片类型
})
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.CompanyId,
roleUserAddCommand.Operator.OrgId,
roleUserAddCommand.Operator.UserId)
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.TRANSACTION_ERROR, err.Error())
}
return roleUserAddCommand, err
}
// 角色添加关联用户
func (rolesService *RolesService) RoleUserDelete(roleUserDeleteCommand *command.RoleUserDeleteCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
roleUserDeleteCommand.Operator.CompanyId,
roleUserDeleteCommand.Operator.OrgId,
roleUserDeleteCommand.Operator.UserId)
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.TRANSACTION_ERROR, err.Error())
}
return roleUserDeleteCommand, err
}
// 角色下关联用户的数据
func (rolesService *RolesService) RoleUserInfo(roleUserInfoQuery *query.RoleUserInfoQuery) (interface{}, error) {
//TODO
return nil, nil
}
func NewRolesService(options map[string]interface{}) *RolesService {
newRolesService := &RolesService{}
return newRolesService
}