pg_update_user_service.go 3.7 KB
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"
	"strings"
	"time"
)

// PgUpdateUserService 用户更新服务
type PgUpdateUserService struct {
	transactionContext *pgTransaction.TransactionContext
}

func (ptr *PgUpdateUserService) UpdateUser(optUser *domain.OperateInfo, user *domain.User, userInfo *domain.UserInfo, enableStatus int) (*domain.User, error) {
	var err error
	roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
	//1.更新所属组织、部门
	var org, dep *domain.Org
	orgRepository, _ := repository.NewOrgRepository(ptr.transactionContext)
	if user.OrganizationId > 0 {
		org, err = orgRepository.FindOne(map[string]interface{}{"orgId": user.OrganizationId})
		if err != nil {
			return nil, err
		}
	}
	if user.DepartmentId > 0 {
		dep, err = orgRepository.FindOne(map[string]interface{}{"orgId": user.DepartmentId})
		if err != nil {
			return nil, err
		}
	}

	//2.更新关联角色,组织
	var userOrg = make([]*domain.Org, 0)
	for i := range user.UserOrg {
		var tmpOrg *domain.Org
		tmpOrg, err = orgRepository.FindOne(map[string]interface{}{"orgId": user.UserOrg[i].OrgId})
		if err != nil {
			return nil, err
		}
		userOrg = append(userOrg, tmpOrg.CloneSample())
	}
	var userRole = make([]*domain.Role, 0)
	for i := range user.UserRole {
		var tmpRole *domain.Role
		tmpRole, err = roleRepository.FindOne(map[string]interface{}{"roleId": user.UserRole[i].RoleId})
		if err != nil {
			return nil, err
		}
		userRole = append(userRole, tmpRole.CloneSample())
	}

	//3.更新用户信息
	userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
	var userBase *domain.UserBase
	if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": user.UserBaseId}); err != nil {
		return nil, err
	}
	if userBase.Account != strings.TrimSpace(userInfo.Phone) && len(userInfo.Phone) > 0 { // 修改了手机号
		if _, err = userBaseRepository.FindOne(map[string]interface{}{"account": strings.TrimSpace(userInfo.Phone)}); err == nil {
			return nil, fmt.Errorf("手机号已存在")
		}
		if err = userBase.ResetPhone(userBase.Account, userInfo.Phone); err != nil {
			return nil, err
		}
	}
	if err = userBase.UpdateUserInfo(userInfo); err != nil {
		return nil, err
	}
	if userBase, err = userBaseRepository.Save(userBase); err != nil {
		return nil, err
	}

	//4.更新用户、冗余信息
	userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
	if _, err = userRepository.FindOne(map[string]interface{}{"companyId": user.CompanyId, "organizationId": user.OrganizationId, "userCode": user.UserCode, "notEqualUserId": user.UserId}); err == nil {
		return nil, fmt.Errorf("用户编号在该企业内已存在,请重新输入")
	}

	user.UserRole = userRole
	user.UserOrg = userOrg
	if org != nil {
		user.Ext.OrgName = org.OrgName
	}
	if dep != nil {
		user.Ext.DepName = dep.OrgName
	} else {
		user.Ext.DepName = userInfo.DepartmentName
	}
	user.Ext.Phone = userBase.UserInfo.Phone
	user.Ext.UserName = userBase.UserInfo.UserName
	user.UpdatedAt = time.Now()
	if err = user.SetEnableStatus(enableStatus); err != nil {
		return nil, err
	}
	if user, err = userRepository.Save(user); err != nil {
		return nil, err
	}
	return user, nil
}

func NewPgUpdateUserService(transactionContext *pgTransaction.TransactionContext) (*PgUpdateUserService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &PgUpdateUserService{
			transactionContext: transactionContext,
		}, nil
	}
}