作者 yangfu

用户批量添加

... ... @@ -86,3 +86,11 @@ func CreatePgImService(options map[string]interface{}) (service.PgImService, err
}
return domainService.NewPgImService(transactionContext)
}
func CreateBatchAddUserService(options map[string]interface{}) (service.PgBatchAddUserService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgBatchAddUserService(transactionContext)
}
... ...
... ... @@ -12,9 +12,9 @@ import (
type BatchAddCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UserType int `cname:"用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)" json:"userType" valid:"Required"`
UserType int `cname:"用户类型" json:"userType" valid:"Required"`
// 用户列表
User []*domain.User `cname:"用户列表" json:"user,omitempty"`
Users []*domain.User `cname:"用户列表" json:"users,omitempty"`
// 密码
Password string `cname:"密码" json:"password" valid:"Required"`
}
... ...
... ... @@ -15,6 +15,8 @@ type UpdateFavoriteMenusCommand struct {
FavoriteMenus []string `cname:"菜单编码列表" json:"favoriteMenus,omitempty"`
// 用户Id 用户唯一标识
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"`
// 用户Id 用户唯一标识
Action int64 `cname:"操作类型 1:添加菜单 2:移除菜单 3.全量更新" json:"action" valid:"Required"`
}
func (updateFavoriteMenusCommand *UpdateFavoriteMenusCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -19,7 +19,7 @@ type UserDto struct {
// 用户关联的角色
UserRole []*domain.Role `json:"userRole"`
// 收藏的菜单(工作台)(菜单编码列表)
FavoriteMenus []string `json:"favoriteMenus,omitempty"`
FavoriteMenus []string `json:"favoriteMenus"`
// 共创信息 (共创用户有效)
CooperationInfo *domain.CooperationInfo `json:"cooperationInfo,omitempty"`
// 状态(1:启用 2:禁用 3:注销)
... ...
... ... @@ -16,7 +16,7 @@ import (
type UserService struct {
}
// TODO:批量添加
// 批量添加用户
func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -31,6 +31,14 @@ func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddComman
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())
}
... ... @@ -598,7 +606,16 @@ func (userService *UserService) UpdateFavoriteMenus(updateFavoriteMenusCommand *
if err != nil {
return nil, err
}
user.FavoriteMenus = updateFavoriteMenusCommand.FavoriteMenus
switch updateFavoriteMenusCommand.Action {
case 1:
user.AddFavoriteMenu(updateFavoriteMenusCommand.FavoriteMenus...)
case 2:
user.RemoveFavoriteMenu(updateFavoriteMenusCommand.FavoriteMenus...)
case 3:
user.FavoriteMenus = updateFavoriteMenusCommand.FavoriteMenus
default:
return nil, application.ThrowError(application.TRANSACTION_ERROR, fmt.Sprintf("action: %v not support", updateFavoriteMenusCommand.Action))
}
if _, err = userRepository.Save(user); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgBatchAddUserService 批量添加用户服务
type PgBatchAddUserService interface {
BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error
}
... ...
... ... @@ -229,6 +229,45 @@ func (user *User) RemoveUserRole(roleId int64) bool {
return res
}
// AddFavoriteMenu 菜单收藏
//
// codes 菜单列表
func (user *User) AddFavoriteMenu(codes ...string) error {
for i := 0; i < len(codes); i++ {
var menuExisted = false
for j := 0; j < len(user.FavoriteMenus); j++ {
if user.FavoriteMenus[j] == codes[i] {
menuExisted = true
}
}
if !menuExisted {
user.FavoriteMenus = append(user.FavoriteMenus, codes[i])
}
}
return nil
}
// AddFavoriteMenu 菜单收藏移除
//
// codes 菜单列表
func (user *User) RemoveFavoriteMenu(codes ...string) error {
for i := 0; i < len(codes); i++ {
var menuExisted = false
var newMenus = make([]string, 0)
for j := 0; j < len(user.FavoriteMenus); j++ {
if user.FavoriteMenus[j] == codes[i] {
menuExisted = true
} else {
newMenus = append(newMenus, user.FavoriteMenus[j])
}
}
if menuExisted {
user.FavoriteMenus = newMenus
}
}
return nil
}
/***** 2.缓存模块 *****/
func (user *User) CacheKeyFunc() string {
... ...
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
}
// CreateUser 批量添加用户服务
//
// 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) preCheck(user *domain.User) error {
if len(user.UserInfo.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
}
if len(user.UserInfo.Phone) == 0 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
if user.OrganizationId == 0 {
return fmt.Errorf("导入的组织机构不存在")
}
if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee {
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
}
}
... ...