user.go
7.4 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
package user
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/adapter"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type UserService struct{}
func (us *UserService) ListUsers(listUserQuery *query.ListUserQuery) (interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
count, list, err := userRepo.Find(map[string]interface{}{
"companyId": listUserQuery.CompanyId,
"name": listUserQuery.Name,
"status": domain.UserStatusEnable,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return tool_funs.SimpleWrapGridMap(int64(count), list), nil
}
func (us *UserService) ListByDepartment(in *query.ListByDepartmentQuery) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
positionRepo := factory.CreatePositionRepository(map[string]interface{}{"transactionContext": transactionContext})
inMap := tool_funs.SimpleStructToMap(in)
if in.DepartmentId == 0 {
delete(inMap, "departmentId") // 删除部门ID字段
}
count, list, err := userRepo.Find(inMap)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
parentIds := make([]int64, 0) // 上级ID
positionIds := make([]int, 0) // 职位ID
for i := range list {
if list[i].ParentId > 0 {
parentIds = append(parentIds, list[i].ParentId)
}
pList := list[i].PositionId
for i2 := range pList {
if pList[i2] > 0 {
positionIds = append(positionIds, pList[i2])
}
}
}
// 获取上级
parentMap := map[int64]*domain.User{}
if len(parentIds) > 0 {
_, parentList, err := userRepo.Find(map[string]interface{}{"ids": parentIds})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range parentList {
parentMap[parentList[i].Id] = parentList[i]
}
}
// 获取职位
positionMap := map[int64]*domain.Position{}
if len(positionIds) > 0 {
_, positionList, err := positionRepo.Find(map[string]interface{}{"ids": positionIds})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range positionList {
positionMap[positionList[i].Id] = positionList[i]
}
}
adapters := make([]*adapter.UserResp, 0)
for i := range list {
adp := &adapter.UserResp{
User: list[i],
}
// 上级名称
if v, ok := parentMap[list[i].ParentId]; ok {
adp.ParentName = v.Name
}
// 职位名称数组
adp.PositionNames = make([]string, 0)
pList := list[i].PositionId
for i2 := range pList {
if v2, ok := positionMap[int64(pList[i2])]; ok {
adp.PositionNames = append(adp.PositionNames, v2.Name)
}
}
adapters = append(adapters, adp)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return tool_funs.SimpleWrapGridMap(int64(count), adapters), nil
}
// EditParentUser 保存上级
func (us *UserService) EditParentUser(in *command.EditParentCommand) error {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
hrbp, err := service.GetHrBp(transactionContext, in.CompanyId, in.OperatorId)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if hrbp != domain.RoleTypeSystem {
return application.ThrowError(application.BUSINESS_ERROR, "HRBP权限的员工才能操作")
}
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
user, err := userRepo.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if in.ParentId != 0 {
// 上级ID是否存在
_, err = userRepo.FindOne(map[string]interface{}{"id": in.ParentId})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
user.ParentId = in.ParentId
if _, err = userRepo.Update(user); err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
func (us *UserService) ImportParentUser(in *command.ImportParentUserCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
hrbp, err := service.GetHrBp(transactionContext, in.CompanyId, in.OperatorId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if hrbp != domain.RoleTypeSystem {
return nil, application.ThrowError(application.BUSINESS_ERROR, "HRBP权限的员工才能操作")
}
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
userMap := map[string]*domain.User{}
accounts := make([]string, 0) // 全部账号数组(查询一次)
for i := range in.Data {
v := in.Data[i]
if len(v.Phone) == 0 || len(v.ParentPhone) == 0 {
continue
}
accounts = append(accounts, v.Phone)
accounts = append(accounts, v.ParentPhone)
}
if len(accounts) > 0 {
if _, users, err := userRepo.Find(map[string]interface{}{"accounts": accounts, "companyId": in.CompanyId}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
for i := range users {
userMap[users[i].Account] = users[i]
}
}
}
for i := range in.Data {
v := in.Data[i]
if len(v.Phone) == 0 || len(v.ParentPhone) == 0 {
continue
}
if vCurrentUser, ok := userMap[v.Phone]; ok {
if vParentUser, ok := userMap[v.ParentPhone]; ok {
vCurrentUser.ParentId = vParentUser.Id
// 更新上级数据
if _, err := userRepo.Update(vCurrentUser); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}