pg_create_user_service.go 3.8 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"
)

// PgCreateUserService 创建用户服务
type PgCreateUserService struct {
	transactionContext *pgTransaction.TransactionContext
}

// CreateUser  创建用户服务
//
// optUser 操作用户
// newUser 新用户数据
// password 密码
func (ptr *PgCreateUserService) CreateUser(optUser *domain.User, newUser *domain.User, password string) (*domain.User, error) {
	var (
		user     *domain.User
		userBase *domain.UserBase
		err      error
	)

	userRepository, err := repository.NewUserRepository(ptr.transactionContext)
	if err != nil {
		return nil, err
	}
	userBaseRepository, err := repository.NewUserBaseRepository(ptr.transactionContext)
	if err != nil {
		return nil, err
	}
	orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
	if err != nil {
		return nil, err
	}
	// 0.冗余扩展数据值
	if newUser.Ext != nil {
		if len(newUser.Ext.OrgName) == 0 && newUser.OrganizationId > 0 {
			if org, err := orgRepository.FindOne(map[string]interface{}{"orgId": newUser.OrganizationId}); err != nil {
				return nil, err
			} else {
				newUser.Ext.OrgName = org.OrgName
			}
		}
		if len(newUser.Ext.DepName) == 0 && newUser.DepartmentId > 0 {
			if org, err := orgRepository.FindOne(map[string]interface{}{"orgId": newUser.DepartmentId}); err != nil {
				return nil, err
			} else {
				newUser.Ext.DepName = org.OrgName
			}
		}
	}

	// 普通账号需要验证
	// 1.用户编号唯一验证      用户编号在该企业内已存在,请重新输入
	// 2.当前企业内手机号唯一  手机号在该企业内已存在,请重新输入
	if newUser.UserType&(domain.UserTypeCompanyAdmin|domain.UserTypeVisitor|domain.UserTypeOperationAdmin) == 0 {
		if user, err = userRepository.FindOne(map[string]interface{}{"companyId": newUser.CompanyId, "organizationId": newUser.OrganizationId, "userCode": newUser.UserCode}); err == nil && user != nil {
			return nil, fmt.Errorf("用户编号在该企业内已存在,请重新输入")
		}
		if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"account": newUser.UserInfo.Phone}); err == nil && userBase != nil {
			if user, err = userRepository.FindOne(map[string]interface{}{"userBaseId": userBase.UserBaseId, "companyId": newUser.CompanyId}); err == nil && user != nil {
				return nil, fmt.Errorf("手机号在该企业内已存在,请重新输入")
			}
		}
	}

	// 判断企业内IC卡号唯一
	if len(newUser.Ext.IcCardNumber) > 0 {
		if u, e := userRepository.FindOne(map[string]interface{}{"companyId": newUser.CompanyId, "icCardNumber": newUser.Ext.IcCardNumber}); e == nil && u != nil {
			return nil, fmt.Errorf("IC卡号在该企业内已存在,请重新输入")
		}
	}

	// 3.建账号
	if userBase == nil {
		createUserAccountService, _ := NewPgCreateUserAccountService(ptr.transactionContext)
		if userBase, err = createUserAccountService.CreateUserAccount(newUser.UserInfo.Phone, password, newUser.UserInfo); err != nil {
			return nil, err
		}
	}

	// 4.建用户
	newUser.UserBaseId = userBase.UserBaseId
	newUser.Ext.UserName = userBase.UserInfo.UserName
	if user, err = userRepository.Save(newUser); err != nil {
		return nil, err
	}

	// 5.更新绑定用户到userBase
	if userBase.AddRelatedUser(user.UserId) {
		if userBase, err = userBaseRepository.Save(userBase); err != nil {
			return nil, err
		}
	}
	return user, nil
}

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