pg_batch_add_user_service.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
231
232
233
234
235
236
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
"time"
)
// PgBatchAddUserService 批量创建用户服务
type PgBatchAddUserService struct {
transactionContext *pgTransaction.TransactionContext
}
// BatchAddUser 批量添加用户服务
//
// optUser 操作用户
// users 待添加用户列表数据
// password 密码
//func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error {
// var (
// err error
// )
// orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
// if err != nil {
// return err
// }
// _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})
// if err != nil {
// return err
// }
// var mapOrg = make(map[int64]*domain.Org)
// for i := range orgs {
// mapOrg[orgs[i].OrgId] = orgs[i]
// }
//
// createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
// for i := range users {
// user := users[i]
// if err = ptr.preCheck(user); err != nil {
// return err
// }
// newUser := &domain.User{
// CompanyId: user.CompanyId,
// UserType: user.UserType,
// UserCode: user.UserCode,
// OrganizationId: user.OrganizationId,
// DepartmentId: user.DepartmentId,
// UserOrg: []*domain.Org{},
// UserRole: []*domain.Role{},
// FavoriteMenus: []string{},
// CooperationInfo: user.CooperationInfo,
// UserInfo: user.UserInfo,
// EnableStatus: int(domain.UserStatusEnable),
// Ext: &domain.Ext{
// Phone: user.UserInfo.Phone,
// UserName: user.UserInfo.UserName,
// },
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// }
// if user.OrganizationId > 0 {
// if v, ok := mapOrg[user.OrganizationId]; ok && v.CompanyId == user.CompanyId {
// newUser.Ext.OrgName = v.OrgName
// } else {
// return fmt.Errorf("导入的组织机构不存在")
// }
// }
// if user.DepartmentId > 0 {
// if v, ok := mapOrg[user.DepartmentId]; ok && v.CompanyId == user.CompanyId {
// newUser.Ext.DepName = v.OrgName
// } else {
// return fmt.Errorf("导入的所属部门不存在")
// }
// }
//
// if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
// return err
// }
// }
// return nil
//}
func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) {
var (
err error
)
var failRows = make([]*domain.BatchAddUserItem, 0)
orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
if err != nil {
return failRows, err
}
_, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId, "limit": 10000})
if err != nil {
return failRows, err
}
var mapOrg = make(map[string]*domain.Org)
for i := range orgs {
mapOrg[orgs[i].OrgCode] = orgs[i]
}
optUserOrg, err := orgRepository.FindOne(map[string]interface{}{"orgId": optUser.OrgId})
if err != nil {
return failRows, err
}
createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
for i := range users {
user := users[i]
if err = ptr.preCheck2(user); err != nil {
user.FailReason = err.Error()
failRows = append(failRows, user)
continue
}
var org = optUserOrg
var dep *domain.Org
var ok bool
// 使用导入用户的组织作为默认组织
//if org, ok = mapOrg[user.Org]; !ok {
// user.FailReason = "导入的组织机构不存在:" + user.Org
// failRows = append(failRows, user)
// continue
//}
if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
user.FailReason = "导入的所属部门不存在:" + user.Department
failRows = append(failRows, user)
continue
}
//TODO:子部门判断
//if dep != nil && !dep.IsChild(optUser.OrgId) {
// user.FailReason = fmt.Sprintf("导入的所属部门不是当前登录组织(%v)的子部门:%v", optUserOrg.OrgName, user.Department)
// failRows = append(failRows, user)
// continue
//}
newUser := &domain.User{
CompanyId: user.CompanyId,
UserType: user.UserType,
UserCode: user.UserCode,
OrganizationId: optUser.OrgId,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{
CooperationCompany: user.CooperationCompany,
},
UserInfo: &domain.UserInfo{
UserName: user.UserName,
Phone: user.Phone,
Avatar: "",
Email: user.Email,
},
EnableStatus: user.Status(),
Ext: &domain.Ext{
Phone: user.Phone,
UserName: user.UserName,
OrgName: org.OrgName,
EmployeeType: user.GetEmployeeType(),
IcCardNumber: user.IcCardNumber,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if dep != nil {
newUser.DepartmentId = dep.OrgId
newUser.Ext.DepName = dep.OrgName
}
if len(user.CooperationDeadline) > 0 {
newUser.CooperationInfo.CooperationDeadline, err = time.Parse("2006-01-02", user.CooperationDeadline)
if err != nil {
user.FailReason = fmt.Sprintf("共创到期时间格式有误 (例如:2001-01-01)")
failRows = append(failRows, user)
continue
}
}
if user.UserType == domain.UserTypeEmployee {
newUser.UserOrg = append(newUser.UserOrg, org.CloneSample())
}
if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
user.FailReason = err.Error()
failRows = append(failRows, user)
continue
}
}
return failRows, nil
}
//func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
// if len(user.UserCode) == 0 {
// return fmt.Errorf("导入的用户编码为空值")
// }
// if len(user.UserInfo.UserName) == 0 {
// return fmt.Errorf("导入的用户姓名为空值")
// }
// if len(user.UserInfo.Phone) == 0 || len(user.UserInfo.Phone) != 11 {
// return fmt.Errorf("导入的手机号不是有效手机号")
// }
// if user.OrganizationId == 0 {
// return fmt.Errorf("导入的组织机构不存在")
// }
// if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee {
// return fmt.Errorf("导入的所属部门不存在")
// }
// return nil
//}
func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error {
if len(user.UserCode) == 0 {
return fmt.Errorf("导入的用户编码为空值")
}
if !(user.EnableStatus == "启用" || user.EnableStatus == "禁用") {
return fmt.Errorf("导入的用户状态有误")
}
if len(user.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
}
if len(user.Phone) == 0 || len(user.Phone) != 11 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
if len(user.EmployeeType) == 0 {
return fmt.Errorf("导入的员工类型为空值")
}
if !(user.EmployeeType == "固定" || user.EmployeeType == "派遣" || user.EmployeeType == "临时") {
return fmt.Errorf("导入的员工类型有误")
}
return nil
}
func NewPgBatchAddUserService(transactionContext *pgTransaction.TransactionContext) (*PgBatchAddUserService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgBatchAddUserService{
transactionContext: transactionContext,
}, nil
}
}