作者 yangfu

批量添加用户修改

... ... @@ -18,35 +18,35 @@ type UserService struct {
}
// 批量添加用户
func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
if err := batchAddCommand.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()
}()
batchAddUserService, _ := factory.CreateBatchAddUserService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 批量添加用户
//func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
// if err := batchAddCommand.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()
// }()
//
// batchAddUserService, _ := factory.CreateBatchAddUserService(map[string]interface{}{
// "transactionContext": transactionContext,
// })
// if err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
//
// if err := transactionContext.CommitTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// return nil, nil
//}
// BatchAdd2 批量添加用户
func (userService *UserService) BatchAdd2(batchAddCommand *command.BatchAdd2Command) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ...
... ... @@ -13,7 +13,7 @@ type PgBatchAddUserService struct {
transactionContext *pgTransaction.TransactionContext
}
// CreateUser 批量添加用户服务
// BatchAddUser 批量添加用户服务
//
// optUser 操作用户
// users 待添加用户列表数据
... ... @@ -107,7 +107,6 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
user.FailReason = err.Error()
failRows = append(failRows, user)
continue
//return err
}
var org, dep *domain.Org
var ok bool
... ... @@ -115,26 +114,22 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
user.FailReason = "导入的组织机构不存在:" + user.Org
failRows = append(failRows, user)
continue
//return fmt.Errorf("导入的组织机构不存在:" + user.Org)
}
if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
user.FailReason = "导入的所属部门不存在:" + user.Department
failRows = append(failRows, user)
continue
//return fmt.Errorf("导入的所属部门不存在:" + user.Department)
}
newUser := &domain.User{
CompanyId: user.CompanyId,
UserType: user.UserType,
UserCode: user.UserCode,
OrganizationId: org.OrgId,
//DepartmentId: dep.OrgId,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{
CooperationCompany: user.CooperationCompany,
//CooperationDeadline: user.CooperationDeadline,
},
UserInfo: &domain.UserInfo{
UserName: user.UserName,
... ... @@ -147,7 +142,6 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
Phone: user.Phone,
UserName: user.UserName,
OrgName: org.OrgName,
//DepName: dep.OrgName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
... ... @@ -164,11 +158,13 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
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 fmt.Errorf("%v %v", user.UserName, err.Error())
}
}
return failRows, nil
... ... @@ -206,12 +202,6 @@ func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error
if len(user.Phone) == 0 || len(user.Phone) != 11 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
//if len(user.Org) == 0 {
// return fmt.Errorf("导入的组织机构不存在")
//}
//if len(user.Department) == 0 && user.UserType == domain.UserTypeEmployee {
// return fmt.Errorf("导入的所属部门不存在")
//}
return nil
}
... ...
... ... @@ -84,14 +84,14 @@ func (controller *UserController) GetUserProfile() {
controller.Response(data, err)
}
func (controller *UserController) BatchAdd() {
userService := service.NewUserService(nil)
batchAddCommand := &command.BatchAddCommand{}
Must(controller.Unmarshal(batchAddCommand))
batchAddCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
data, err := userService.BatchAdd(batchAddCommand)
controller.Response(data, err)
}
//func (controller *UserController) BatchAdd() {
// userService := service.NewUserService(nil)
// batchAddCommand := &command.BatchAddCommand{}
// Must(controller.Unmarshal(batchAddCommand))
// batchAddCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
// data, err := userService.BatchAdd(batchAddCommand)
// controller.Response(data, err)
//}
func (controller *UserController) BatchAdd2() {
userService := service.NewUserService(nil)
... ...
... ... @@ -13,7 +13,7 @@ func init() {
web.Router("/user/search", &controllers.UserController{}, "Post:SearchUser")
web.Router("/user/:userId/access-menus", &controllers.UserController{}, "Get:GetUserAccessMenus")
web.Router("/user/:userId/profile", &controllers.UserController{}, "Get:GetUserProfile")
web.Router("/user/batch-add", &controllers.UserController{}, "Post:BatchAdd")
//web.Router("/user/batch-add", &controllers.UserController{}, "Post:BatchAdd")
web.Router("/user/batch-add2", &controllers.UserController{}, "Post:BatchAdd2")
web.Router("/user/batch-enable", &controllers.UserController{}, "Post:BatchEnable")
web.Router("/user/batch-reset-password", &controllers.UserController{}, "Post:BatchResetPassword")
... ...