unified_user_center.go
9.9 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
305
306
307
308
package service
import (
"encoding/json"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/unifiedUserCenter/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
"time"
)
// 统一用户中心适配服务
type UnifiedUserCenterService struct {
}
// 同步企业员工回调
func (unifiedUserCenterService *UnifiedUserCenterService) SyncEmployeeCallback(syncEmployeeCallbackCommand *command.SyncEmployeeCallbackCommand) (interface{}, error) {
if err := syncEmployeeCallbackCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.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()
}()
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cashPoolDao = value
}
var companyId int64
var uid int64
var employeeName string
var employeeAccount string
var oldPhone string
var newPhone string
var employeeAvatarUrl string
var isPrincipal bool
var status int
var uids []int64
var addEmployees []map[string]interface{}
var editEmployees []map[string]interface{}
var data map[string]interface{}
if err := json.Unmarshal([]byte(syncEmployeeCallbackCommand.Data), &data); err != nil {
return false, err
}
if value, ok := data["company_id"]; ok {
companyId = int64(value.(float64))
}
if value, ok := data["id"]; ok {
uid = int64(value.(float64))
}
if value, ok := data["name"]; ok {
employeeName = value.(string)
}
if value, ok := data["phone"]; ok {
employeeAccount = value.(string)
}
if value, ok := data["avatar"]; ok {
employeeAvatarUrl = value.(string)
}
if value, ok := data["oldPhone"]; ok {
oldPhone = value.(string)
}
if value, ok := data["newPhone"]; ok {
newPhone = value.(string)
}
if value, ok := data["admin_type"]; ok {
if int(value.(float64)) == 2 {
isPrincipal = true
} else {
isPrincipal = false
}
}
if value, ok := data["status"]; ok {
status = int(value.(float64))
}
if value, ok := data["ids"]; ok {
for _, uid := range value.([]interface{}) {
uids = append(uids, int64(uid.(float64)))
}
}
if value, ok := data["add"]; ok { // 添加员工
for _, addEmployee := range value.([]interface{}) {
addEmployees = append(addEmployees, addEmployee.(map[string]interface{}))
}
}
if value, ok := data["edit"]; ok { // 编辑员工
for _, editEmployee := range value.([]interface{}) {
editEmployees = append(editEmployees, editEmployee.(map[string]interface{}))
}
}
if syncEmployeeCallbackCommand.Module == "employee" {
switch syncEmployeeCallbackCommand.Action {
case "import":
var companyId int64
var uid int64
var employeeName string
var employeeAccount string
var employeeAvatarUrl string
var isPrincipal bool
var status int
for _, addEmployee := range addEmployees {
if value, ok := addEmployee["company_id"]; ok {
companyId = int64(value.(float64))
}
if value, ok := addEmployee["id"]; ok {
uid = int64(value.(float64))
}
if value, ok := addEmployee["name"]; ok {
employeeName = value.(string)
}
if value, ok := addEmployee["phone"]; ok {
employeeAccount = value.(string)
}
if value, ok := addEmployee["avatar"]; ok {
employeeAvatarUrl = value.(string)
}
if value, ok := addEmployee["admin_type"]; ok {
if int(value.(float64)) == 2 {
isPrincipal = true
} else {
isPrincipal = false
}
}
if value, ok := addEmployee["status"]; ok {
status = int(value.(float64))
}
employee := &domain.Employee{
CompanyId: companyId,
EmployeeInfo: &domain.EmployeeInfo{
Uid: uid,
EmployeeName: employeeName,
EmployeeAccount: employeeAccount,
EmployeeAvatarUrl: employeeAvatarUrl,
IsPrincipal: isPrincipal,
},
Status: status,
SuMoney: 0,
CreateTime: time.Now(),
}
if _, err := employeeRepository.Save(employee); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
for _, editEmployee := range editEmployees {
if value, ok := editEmployee["company_id"]; ok {
companyId = int64(value.(float64))
}
if value, ok := editEmployee["id"]; ok {
uid = int64(value.(float64))
}
if value, ok := editEmployee["name"]; ok {
employeeName = value.(string)
}
if value, ok := editEmployee["phone"]; ok {
employeeAccount = value.(string)
}
if value, ok := editEmployee["avatar"]; ok {
employeeAvatarUrl = value.(string)
}
if value, ok := editEmployee["admin_type"]; ok {
if int(value.(float64)) == 2 {
isPrincipal = true
} else {
isPrincipal = false
}
}
if value, ok := editEmployee["status"]; ok {
status = int(value.(float64))
}
employee, err := employeeRepository.FindOne(map[string]interface{}{"uid": uid})
if err == nil && employee != nil {
updateData := make(map[string]interface{})
updateData["employeeName"] = employeeName
updateData["employeeAccount"] = employeeAccount
updateData["employeeAvatarUrl"] = employeeAvatarUrl
updateData["isPrincipal"] = isPrincipal
updateData["status"] = status
if err := employee.Update(updateData); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if _, err := employeeRepository.Save(employee); err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
}
break
case "add":
employee := &domain.Employee{
CompanyId: companyId,
EmployeeInfo: &domain.EmployeeInfo{
Uid: uid,
EmployeeName: employeeName,
EmployeeAccount: employeeAccount,
EmployeeAvatarUrl: employeeAvatarUrl,
IsPrincipal: isPrincipal,
},
Status: status,
SuMoney: 0,
CreateTime: time.Now(),
}
if _, err := employeeRepository.Save(employee); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
break
case "edit":
employee, err := employeeRepository.FindOne(map[string]interface{}{"uid": uid})
if err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if employee == nil {
return false, nil
}
updateData := make(map[string]interface{})
updateData["employeeName"] = employeeName
updateData["employeeAccount"] = employeeAccount
updateData["employeeAvatarUrl"] = employeeAvatarUrl
updateData["isPrincipal"] = isPrincipal
updateData["status"] = status
if err := employee.Update(updateData); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if _, err := employeeRepository.Save(employee); err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
break
case "batchDelete":
err := employeeDao.BatchRemove(uids)
if err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
break
case "batchForbid":
err := employeeDao.BatchSetStatus(uids, status)
if err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
break
case "changePhone": // TODO 修改手机号
// 修改用户数据
err := employeeDao.ChangePhone(oldPhone, newPhone)
if err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
// 修改业务数据-素币兑换清单
errUpdateExchangeCashPersonList := cashPoolDao.UpdateExchangeCashPersonListUserInfo(oldPhone, newPhone)
if errUpdateExchangeCashPersonList != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, errUpdateExchangeCashPersonList.Error())
}
break
default:
return false, nil
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return true, nil
} else if syncEmployeeCallbackCommand.Module == "company" {
switch syncEmployeeCallbackCommand.Action {
case "changeAdmin":
err := employeeDao.ChangePrincipal(companyId, employeeAccount)
if err != nil {
return false, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
break
default:
return false, nil
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return true, nil
} else {
return false, nil
}
}
func NewUnifiedUserCenterService(options map[string]interface{}) *UnifiedUserCenterService {
newUnifiedUserCenterService := &UnifiedUserCenterService{}
return newUnifiedUserCenterService
}