正在显示
8 个修改的文件
包含
188 行增加
和
5 行删除
| @@ -86,3 +86,11 @@ func CreatePgImService(options map[string]interface{}) (service.PgImService, err | @@ -86,3 +86,11 @@ func CreatePgImService(options map[string]interface{}) (service.PgImService, err | ||
| 86 | } | 86 | } |
| 87 | return domainService.NewPgImService(transactionContext) | 87 | return domainService.NewPgImService(transactionContext) |
| 88 | } | 88 | } |
| 89 | + | ||
| 90 | +func CreateBatchAddUserService(options map[string]interface{}) (service.PgBatchAddUserService, error) { | ||
| 91 | + var transactionContext *pgTransaction.TransactionContext | ||
| 92 | + if value, ok := options["transactionContext"]; ok { | ||
| 93 | + transactionContext = value.(*pgTransaction.TransactionContext) | ||
| 94 | + } | ||
| 95 | + return domainService.NewPgBatchAddUserService(transactionContext) | ||
| 96 | +} |
| @@ -12,9 +12,9 @@ import ( | @@ -12,9 +12,9 @@ import ( | ||
| 12 | type BatchAddCommand struct { | 12 | type BatchAddCommand struct { |
| 13 | OperateInfo *domain.OperateInfo `json:"-"` | 13 | OperateInfo *domain.OperateInfo `json:"-"` |
| 14 | // 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加) | 14 | // 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加) |
| 15 | - UserType int `cname:"用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)" json:"userType" valid:"Required"` | 15 | + UserType int `cname:"用户类型" json:"userType" valid:"Required"` |
| 16 | // 用户列表 | 16 | // 用户列表 |
| 17 | - User []*domain.User `cname:"用户列表" json:"user,omitempty"` | 17 | + Users []*domain.User `cname:"用户列表" json:"users,omitempty"` |
| 18 | // 密码 | 18 | // 密码 |
| 19 | Password string `cname:"密码" json:"password" valid:"Required"` | 19 | Password string `cname:"密码" json:"password" valid:"Required"` |
| 20 | } | 20 | } |
| @@ -15,6 +15,8 @@ type UpdateFavoriteMenusCommand struct { | @@ -15,6 +15,8 @@ type UpdateFavoriteMenusCommand struct { | ||
| 15 | FavoriteMenus []string `cname:"菜单编码列表" json:"favoriteMenus,omitempty"` | 15 | FavoriteMenus []string `cname:"菜单编码列表" json:"favoriteMenus,omitempty"` |
| 16 | // 用户Id 用户唯一标识 | 16 | // 用户Id 用户唯一标识 |
| 17 | UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"` | 17 | UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"` |
| 18 | + // 用户Id 用户唯一标识 | ||
| 19 | + Action int64 `cname:"操作类型 1:添加菜单 2:移除菜单 3.全量更新" json:"action" valid:"Required"` | ||
| 18 | } | 20 | } |
| 19 | 21 | ||
| 20 | func (updateFavoriteMenusCommand *UpdateFavoriteMenusCommand) Valid(validation *validation.Validation) { | 22 | func (updateFavoriteMenusCommand *UpdateFavoriteMenusCommand) Valid(validation *validation.Validation) { |
| @@ -19,7 +19,7 @@ type UserDto struct { | @@ -19,7 +19,7 @@ type UserDto struct { | ||
| 19 | // 用户关联的角色 | 19 | // 用户关联的角色 |
| 20 | UserRole []*domain.Role `json:"userRole"` | 20 | UserRole []*domain.Role `json:"userRole"` |
| 21 | // 收藏的菜单(工作台)(菜单编码列表) | 21 | // 收藏的菜单(工作台)(菜单编码列表) |
| 22 | - FavoriteMenus []string `json:"favoriteMenus,omitempty"` | 22 | + FavoriteMenus []string `json:"favoriteMenus"` |
| 23 | // 共创信息 (共创用户有效) | 23 | // 共创信息 (共创用户有效) |
| 24 | CooperationInfo *domain.CooperationInfo `json:"cooperationInfo,omitempty"` | 24 | CooperationInfo *domain.CooperationInfo `json:"cooperationInfo,omitempty"` |
| 25 | // 状态(1:启用 2:禁用 3:注销) | 25 | // 状态(1:启用 2:禁用 3:注销) |
| @@ -16,7 +16,7 @@ import ( | @@ -16,7 +16,7 @@ import ( | ||
| 16 | type UserService struct { | 16 | type UserService struct { |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | -// TODO:批量添加 | 19 | +// 批量添加用户 |
| 20 | func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) { | 20 | func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) { |
| 21 | if err := batchAddCommand.ValidateCommand(); err != nil { | 21 | if err := batchAddCommand.ValidateCommand(); err != nil { |
| 22 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 22 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
| @@ -31,6 +31,14 @@ func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddComman | @@ -31,6 +31,14 @@ func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddComman | ||
| 31 | defer func() { | 31 | defer func() { |
| 32 | transactionContext.RollbackTransaction() | 32 | transactionContext.RollbackTransaction() |
| 33 | }() | 33 | }() |
| 34 | + | ||
| 35 | + batchAddUserService, _ := factory.CreateBatchAddUserService(map[string]interface{}{ | ||
| 36 | + "transactionContext": transactionContext, | ||
| 37 | + }) | ||
| 38 | + if err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil { | ||
| 39 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 40 | + } | ||
| 41 | + | ||
| 34 | if err := transactionContext.CommitTransaction(); err != nil { | 42 | if err := transactionContext.CommitTransaction(); err != nil { |
| 35 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 43 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
| 36 | } | 44 | } |
| @@ -598,7 +606,16 @@ func (userService *UserService) UpdateFavoriteMenus(updateFavoriteMenusCommand * | @@ -598,7 +606,16 @@ func (userService *UserService) UpdateFavoriteMenus(updateFavoriteMenusCommand * | ||
| 598 | if err != nil { | 606 | if err != nil { |
| 599 | return nil, err | 607 | return nil, err |
| 600 | } | 608 | } |
| 601 | - user.FavoriteMenus = updateFavoriteMenusCommand.FavoriteMenus | 609 | + switch updateFavoriteMenusCommand.Action { |
| 610 | + case 1: | ||
| 611 | + user.AddFavoriteMenu(updateFavoriteMenusCommand.FavoriteMenus...) | ||
| 612 | + case 2: | ||
| 613 | + user.RemoveFavoriteMenu(updateFavoriteMenusCommand.FavoriteMenus...) | ||
| 614 | + case 3: | ||
| 615 | + user.FavoriteMenus = updateFavoriteMenusCommand.FavoriteMenus | ||
| 616 | + default: | ||
| 617 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, fmt.Sprintf("action: %v not support", updateFavoriteMenusCommand.Action)) | ||
| 618 | + } | ||
| 602 | if _, err = userRepository.Save(user); err != nil { | 619 | if _, err = userRepository.Save(user); err != nil { |
| 603 | return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | 620 | return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) |
| 604 | } | 621 | } |
| @@ -229,6 +229,45 @@ func (user *User) RemoveUserRole(roleId int64) bool { | @@ -229,6 +229,45 @@ func (user *User) RemoveUserRole(roleId int64) bool { | ||
| 229 | return res | 229 | return res |
| 230 | } | 230 | } |
| 231 | 231 | ||
| 232 | +// AddFavoriteMenu 菜单收藏 | ||
| 233 | +// | ||
| 234 | +// codes 菜单列表 | ||
| 235 | +func (user *User) AddFavoriteMenu(codes ...string) error { | ||
| 236 | + for i := 0; i < len(codes); i++ { | ||
| 237 | + var menuExisted = false | ||
| 238 | + for j := 0; j < len(user.FavoriteMenus); j++ { | ||
| 239 | + if user.FavoriteMenus[j] == codes[i] { | ||
| 240 | + menuExisted = true | ||
| 241 | + } | ||
| 242 | + } | ||
| 243 | + if !menuExisted { | ||
| 244 | + user.FavoriteMenus = append(user.FavoriteMenus, codes[i]) | ||
| 245 | + } | ||
| 246 | + } | ||
| 247 | + return nil | ||
| 248 | +} | ||
| 249 | + | ||
| 250 | +// AddFavoriteMenu 菜单收藏移除 | ||
| 251 | +// | ||
| 252 | +// codes 菜单列表 | ||
| 253 | +func (user *User) RemoveFavoriteMenu(codes ...string) error { | ||
| 254 | + for i := 0; i < len(codes); i++ { | ||
| 255 | + var menuExisted = false | ||
| 256 | + var newMenus = make([]string, 0) | ||
| 257 | + for j := 0; j < len(user.FavoriteMenus); j++ { | ||
| 258 | + if user.FavoriteMenus[j] == codes[i] { | ||
| 259 | + menuExisted = true | ||
| 260 | + } else { | ||
| 261 | + newMenus = append(newMenus, user.FavoriteMenus[j]) | ||
| 262 | + } | ||
| 263 | + } | ||
| 264 | + if menuExisted { | ||
| 265 | + user.FavoriteMenus = newMenus | ||
| 266 | + } | ||
| 267 | + } | ||
| 268 | + return nil | ||
| 269 | +} | ||
| 270 | + | ||
| 232 | /***** 2.缓存模块 *****/ | 271 | /***** 2.缓存模块 *****/ |
| 233 | 272 | ||
| 234 | func (user *User) CacheKeyFunc() string { | 273 | func (user *User) CacheKeyFunc() string { |
| 1 | +package domainService | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
| 7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository" | ||
| 8 | + "time" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +// PgBatchAddUserService 批量创建用户服务 | ||
| 12 | +type PgBatchAddUserService struct { | ||
| 13 | + transactionContext *pgTransaction.TransactionContext | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +// CreateUser 批量添加用户服务 | ||
| 17 | +// | ||
| 18 | +// optUser 操作用户 | ||
| 19 | +// users 待添加用户列表数据 | ||
| 20 | +// password 密码 | ||
| 21 | +func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error { | ||
| 22 | + var ( | ||
| 23 | + err error | ||
| 24 | + ) | ||
| 25 | + orgRepository, err := repository.NewOrgRepository(ptr.transactionContext) | ||
| 26 | + if err != nil { | ||
| 27 | + return err | ||
| 28 | + } | ||
| 29 | + _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId}) | ||
| 30 | + if err != nil { | ||
| 31 | + return err | ||
| 32 | + } | ||
| 33 | + var mapOrg = make(map[int64]*domain.Org) | ||
| 34 | + for i := range orgs { | ||
| 35 | + mapOrg[orgs[i].OrgId] = orgs[i] | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + createUserService, _ := NewPgCreateUserService(ptr.transactionContext) | ||
| 39 | + for i := range users { | ||
| 40 | + user := users[i] | ||
| 41 | + if err = ptr.preCheck(user); err != nil { | ||
| 42 | + return err | ||
| 43 | + } | ||
| 44 | + newUser := &domain.User{ | ||
| 45 | + CompanyId: user.CompanyId, | ||
| 46 | + UserType: user.UserType, | ||
| 47 | + UserCode: user.UserCode, | ||
| 48 | + OrganizationId: user.OrganizationId, | ||
| 49 | + DepartmentId: user.DepartmentId, | ||
| 50 | + UserOrg: []*domain.Org{}, | ||
| 51 | + UserRole: []*domain.Role{}, | ||
| 52 | + FavoriteMenus: []string{}, | ||
| 53 | + CooperationInfo: user.CooperationInfo, | ||
| 54 | + UserInfo: user.UserInfo, | ||
| 55 | + EnableStatus: int(domain.UserStatusEnable), | ||
| 56 | + Ext: &domain.Ext{ | ||
| 57 | + Phone: user.UserInfo.Phone, | ||
| 58 | + UserName: user.UserInfo.UserName, | ||
| 59 | + }, | ||
| 60 | + CreatedAt: time.Now(), | ||
| 61 | + UpdatedAt: time.Now(), | ||
| 62 | + } | ||
| 63 | + if user.OrganizationId > 0 { | ||
| 64 | + if v, ok := mapOrg[user.OrganizationId]; ok && v.CompanyId == user.CompanyId { | ||
| 65 | + newUser.Ext.OrgName = v.OrgName | ||
| 66 | + } else { | ||
| 67 | + return fmt.Errorf("导入的组织机构不存在") | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + if user.DepartmentId > 0 { | ||
| 71 | + if v, ok := mapOrg[user.DepartmentId]; ok && v.CompanyId == user.CompanyId { | ||
| 72 | + newUser.Ext.DepName = v.OrgName | ||
| 73 | + } else { | ||
| 74 | + return fmt.Errorf("导入的所属部门不存在") | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil { | ||
| 79 | + return err | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + return nil | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error { | ||
| 86 | + if len(user.UserInfo.UserName) == 0 { | ||
| 87 | + return fmt.Errorf("导入的用户姓名为空值") | ||
| 88 | + } | ||
| 89 | + if len(user.UserInfo.Phone) == 0 { | ||
| 90 | + return fmt.Errorf("导入的手机号不是有效手机号") | ||
| 91 | + } | ||
| 92 | + if user.OrganizationId == 0 { | ||
| 93 | + return fmt.Errorf("导入的组织机构不存在") | ||
| 94 | + } | ||
| 95 | + if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee { | ||
| 96 | + return fmt.Errorf("导入的所属部门不存在") | ||
| 97 | + } | ||
| 98 | + return nil | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +func NewPgBatchAddUserService(transactionContext *pgTransaction.TransactionContext) (*PgBatchAddUserService, error) { | ||
| 102 | + if transactionContext == nil { | ||
| 103 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
| 104 | + } else { | ||
| 105 | + return &PgBatchAddUserService{ | ||
| 106 | + transactionContext: transactionContext, | ||
| 107 | + }, nil | ||
| 108 | + } | ||
| 109 | +} |
-
请 注册 或 登录 后发表评论