employee.go
11.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package service
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/astaxie/beego/logs"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
// UserDepartData 用户的部门数据
type UserDepartData struct {
Id int64 `json:"id"`
CompanyId int64 `json:"company_id"`
DepartmentId int64 `json:"department_id"`
UserId int64 `json:"user_id"`
}
//UserPositionData 用户的职位数据
type UserPositionData struct {
Id int64 `json:"id"`
CompanyId int64 `json:"company_id"`
PositionId int64 `json:"position_id"`
UserId int64 `json:"user_id"`
}
//EmployeeData 从平台平台接收的数据
type EmployeeData struct {
Id int64 `json:"id"` //用户的id,对应本地的user_company_id
OpenId int64 `json:"open_id"` //同一用户中心的用户id
Phone string `json:"phone"`
Name string `json:"name"`
Sex int8 `json:"sex"`
JobNum string `json:"job_num"`
PrivatePhone string `json:"private_phone"` //私人手机
CompanyId int64 `json:"company_id"` //总后台的company_id
Email string `json:"email"`
ExtensionNum string `json:"extension_num"` //分机号
EntryTime string `json:"entry_time"` //入职时间
WorkSpace string `json:"workspace"`
IsBusiness int8 `json:"is_business"` //
Status int8 `json:"status"`
Avatar string `json:"avatar"`
ExtraText string `json:"extra_text"`
Remarks string `json:"remarks"`
AdminType int8 `json:"admin_type"`
ChargeStatus int8 `json:"charge_status"`
UserDepartments []UserDepartData `json:"user_departments"`
UserPositions []UserPositionData `json:"user_positions"`
}
//DeleteUserData 批量删除用户
type DeleteUserData struct {
CompanyId int64 `json:"companyId"`
Ids []int64 `json:"ids"`
}
//ForbidAllowUserData 禁用启用用户
type ForbidAllowUserData struct {
CompanyId int64 `json:"companyId"`
Ids []int64 `json:"ids"`
Status int8 `json:"status"`
}
//ImportEmployeeData 批量导入用户
type ImportEmployeeData struct {
Add []EmployeeData `json:"add"`
Edit []EmployeeData `json:"edit"`
}
//SyncEmployeeService 同步用户数据
type SyncEmployeeService struct{}
func NewSyncEmployeeService(option map[string]interface{}) *SyncEmployeeService {
syncEmployee := new(SyncEmployeeService)
return syncEmployee
}
var _ SyncAction = (*SyncEmployeeService)(nil)
func (service SyncEmployeeService) DoAction(action string, byteData []byte) error {
switch action {
case "add":
//添加
var (
data EmployeeData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err = service.addEmployeeData([]EmployeeData{data})
return err
case "edit":
//编辑更新
var (
data EmployeeData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err = service.updateEmployeeData([]EmployeeData{data})
return err
case "batchDelete":
//批量删除
var (
err error
data DeleteUserData
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.deleteEmployeeData(data)
case "batchForbid":
//启用禁用
var (
err error
data ForbidAllowUserData
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateUsersStatus(data)
// case "batchRemove":
// //移动通讯录用户部门 暂时不需要
// return nil
case "import":
//批量导入
var (
data ImportEmployeeData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err1 := service.addEmployeeData(data.Add)
err2 := service.updateEmployeeData(data.Edit)
if err1 != nil || err2 != nil {
return fmt.Errorf("添加用户错误:%s,更新用户发生错误:%s", err1, err2)
}
return nil
default:
return errors.New("action not found")
}
}
//addEmployeeData 添加用户
func (service SyncEmployeeService) addEmployeeData(datas []EmployeeData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range datas {
data := datas[i]
newUser := domain.Users{
Id: data.Id,
CompanyId: data.CompanyId,
OpenId: data.OpenId,
Name: data.Name,
Sex: data.Sex,
JobNum: data.JobNum,
Phone: data.Phone,
PrivatePhone: data.PrivatePhone,
Email: data.Email,
ExtensionNum: data.ExtensionNum,
Workspace: data.WorkSpace,
Status: data.Status,
Avatar: data.Avatar,
Remarks: data.Remarks,
ChargeStatus: data.ChargeStatus,
Permission: []domain.AdminPermissionBase{}, //初始化权限
AccessPartners: []domain.Partner{}, //默认初始化
AdminType: data.AdminType,
IsSenior: 2,
}
newUser.EntryTime, _ = time.Parse("2006-01-02", data.EntryTime)
if err = usersRepository.Add(&newUser); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
}
err = transactionContext.CommitTransaction()
return err
}
// updateEmployeeData 更新用户
func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range datas {
data := datas[i]
var oldUser domain.Users
oldUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{
Id: data.Id,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
entryTime, _ := time.Parse("2006-01-02", data.EntryTime)
_ = oldUser.Update(map[string]interface{}{
"CompanyId": data.CompanyId,
"Name": data.Name,
"Sex": data.Sex,
"JobNum": data.JobNum,
"Phone": data.Phone,
"PrivatePhone": data.PrivatePhone,
"Email": data.Email,
"ExtensionNum": data.ExtensionNum,
"Workspace": data.WorkSpace,
"Status": data.Status,
"Avatar": data.Avatar,
"Remarks": data.Remarks,
"ChargeStatus": data.ChargeStatus,
"EntryTime": entryTime,
"AdminType": data.AdminType,
})
if err = usersRepository.Edit(&oldUser); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
}
err = transactionContext.CommitTransaction()
return err
}
//deleteEmployeeData 删除用户
func (service SyncEmployeeService) deleteEmployeeData(data DeleteUserData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = usersRepository.Remove(data.Ids); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return err
}
//updateUsersStatus 批量更新用户的状态
func (service SyncEmployeeService) updateUsersStatus(data ForbidAllowUserData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var uDao *dao.UsersDao
if uDao, err = factory.CreateUsersDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = uDao.UpdateUserStatus(data.Ids, data.Status); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return err
}
//ChangeAdmin 变更公司负责人,超级管理员
func (service SyncEmployeeService) ChangeSuperAdmin(cmd command.ChanceSuperAdminCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var (
oldSuperUser domain.Users
newSuperUser domain.Users
userList []domain.Users
)
_, userList, err = usersRepository.Find(domain.UsersFindQuery{
CompanyId: cmd.CompanyId,
AdminType: domain.UserIsAdmin,
})
if err != nil {
e := fmt.Sprintf("获取用户(admin_type=%d;company_id=%d)数据失败:%s",
domain.UserIsAdmin, cmd.CompanyId, err.Error())
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if len(userList) == 0 {
logs.Error(fmt.Sprintf("没有获得公司主管数据(admin_type=%d;company_id=%d)",
domain.UserIsAdmin, cmd.CompanyId))
}
if len(userList) > 1 {
logs.Error(fmt.Sprintf("存在复数公司主管数据(admin_type=%d;company_id=%d)",
domain.UserIsAdmin, cmd.CompanyId))
}
newSuperUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{
CompanyId: cmd.CompanyId,
Phone: cmd.Phone,
})
if err != nil {
e := fmt.Sprintf("获取公司用户数据(phone=%s;company_id=%d)",
cmd.Phone, cmd.CompanyId)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
for i := range userList {
err = userList[i].Update(map[string]interface{}{
"AdminType": domain.UserIsNotAdmin,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = usersRepository.Edit(&userList[i])
if err != nil {
e := fmt.Sprintf("更新公司主管user数据(id=%d)失败:%s",
oldSuperUser.Id, err)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
}
err = newSuperUser.Update(map[string]interface{}{
"AdminType": domain.UserIsAdmin,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = usersRepository.Edit(&newSuperUser)
if err != nil {
e := fmt.Sprintf("更新公司主管user数据(id=%d)失败:%s",
newSuperUser.Id, err)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
err = transactionContext.CommitTransaction()
return nil
}