...
|
...
|
@@ -18,71 +18,71 @@ type PgBatchAddUserService struct { |
|
|
// 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.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) BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) {
|
|
|
func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
...
|
...
|
@@ -99,6 +99,10 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use |
|
|
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 {
|
...
|
...
|
@@ -110,13 +114,19 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use |
|
|
}
|
|
|
var org, dep *domain.Org
|
|
|
var ok bool
|
|
|
if org, ok = mapOrg[user.Org]; !ok {
|
|
|
user.FailReason = "导入的组织机构不存在:" + user.Org
|
|
|
// 使用导入用户的组织作为默认组织
|
|
|
//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
|
|
|
}
|
|
|
if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
|
|
|
user.FailReason = "导入的所属部门不存在:" + user.Department
|
|
|
if dep != nil && !dep.IsChild(optUser.OrgId) {
|
|
|
user.FailReason = fmt.Sprintf("导入的所属部门不是当前登录组织(%v)的子部门:%v", optUserOrg.OrgName, user.Department)
|
|
|
failRows = append(failRows, user)
|
|
|
continue
|
|
|
}
|
...
|
...
|
@@ -124,7 +134,7 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use |
|
|
CompanyId: user.CompanyId,
|
|
|
UserType: user.UserType,
|
|
|
UserCode: user.UserCode,
|
|
|
OrganizationId: org.OrgId,
|
|
|
OrganizationId: optUser.OrgId,
|
|
|
UserOrg: []*domain.Org{},
|
|
|
UserRole: []*domain.Role{},
|
|
|
FavoriteMenus: []string{},
|
...
|
...
|
@@ -172,24 +182,24 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use |
|
|
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) 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 {
|
...
|
...
|
|