作者 郑周

Merge remote-tracking branch 'origin/test' into test

package command
type BatchRemove struct {
CompanyId int64 `json:"company_id"`
UserIds []int64 `json:"user_ids"`
DepartmentIds []int `json:"department_ids"`
}
... ...
... ... @@ -62,6 +62,13 @@ func (srv SyncDataUserService) FromBusinessAdmin(param *domain.MessageBody) erro
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
err = srv.importUser(&param4)
case "employee/batchRemove":
batchRemove := &command.BatchRemove{}
err = json.Unmarshal(param.Data, batchRemove)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
err = srv.BatchRemove(batchRemove)
default:
log.Logger.Error("action err:" + action)
}
... ... @@ -320,3 +327,38 @@ func (srv SyncDataUserService) importUser(param *command.ImportUserCommand) erro
return nil
}
// BatchRemove 调整部门
func (srv SyncDataUserService) BatchRemove(batchRemove *command.BatchRemove) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
if len(batchRemove.UserIds) > 0 {
for _, item := range batchRemove.UserIds {
user, err := userRepo.FindOne(map[string]interface{}{"id": item, "companyId": batchRemove.CompanyId})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
user.DepartmentId = batchRemove.DepartmentIds
user.UpdatedAt = time.Now()
_, err = userRepo.Update(user)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ...
... ... @@ -92,7 +92,7 @@ func (repo *DepartmentRepository) FindOne(queryOptions map[string]interface{}) (
func (repo *DepartmentRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Department, error) {
tx := repo.transactionContext.PgTx
dparmentModel := []models.Department{}
query := tx.Model(&dparmentModel).Where("delete_at isnull")
query := tx.Model(&dparmentModel)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
... ...
... ... @@ -21,16 +21,18 @@ func NewUserRepository(tx *pgTransaction.TransactionContext) *UserRepository {
func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
userModel := models.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdatedAt: user.UpdatedAt,
CreatedAt: user.CreatedAt,
DeletedAt: user.DeletedAt,
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
DepartmentId: user.DepartmentId,
PositionId: user.PositionId,
Name: user.Name,
Status: user.Status,
UpdatedAt: user.UpdatedAt,
CreatedAt: user.CreatedAt,
DeletedAt: user.DeletedAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&userModel).Insert()
... ... @@ -43,17 +45,19 @@ func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
userModel := models.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Email: user.Email,
Status: user.Status,
UpdatedAt: user.UpdatedAt,
CreatedAt: user.CreatedAt,
DeletedAt: user.DeletedAt,
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
DepartmentId: user.DepartmentId,
PositionId: user.PositionId,
Name: user.Name,
Email: user.Email,
Status: user.Status,
UpdatedAt: user.UpdatedAt,
CreatedAt: user.CreatedAt,
DeletedAt: user.DeletedAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&userModel).WherePK().Update()
... ...