pg_sign_up_person_service.go 2.5 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"
	"time"
)

// PgSignUpPersonService 个人注册服务
type PgSignUpPersonService struct {
	transactionContext *pgTransaction.TransactionContext
}

// SignUp  个人注册服务
//
// registerPhone 注册人手机号
// password      密码
// companyInfo 注册公司信息
// userInfo 用户信息
func (ptr *PgSignUpPersonService) SignUp(account string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error) {
	var err error
	// 前置验证
	if len(account) == 0 || len(password) == 0 {
		return nil, fmt.Errorf("账号密码不能为空")
	}

	var existsUser *domain.User
	var userBase *domain.UserBase
	createUserAccountService, _ := NewPgCreateUserAccountService(ptr.transactionContext)
	if userBase, err = createUserAccountService.CreateUserAccount(account, password, userInfo); err != nil {
		return nil, err
	}

	userBaseRepository, _ := repository.NewUserRepository(ptr.transactionContext)
	if existsUser, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBase.UserBaseId, "userType": domain.UserTypeVisitor}); err == nil && existsUser != nil {
		return nil, fmt.Errorf("账号已存在")
	}

	// 4.创建用户、分配角色、关联组织、账号
	var user *domain.User = &domain.User{
		CompanyId:       0,
		UserType:        domain.UserTypeVisitor,
		UserCode:        "",
		OrganizationId:  0,
		DepartmentId:    0,
		UserOrg:         []*domain.Org{},
		UserRole:        []*domain.Role{},
		FavoriteMenus:   []string{},
		CooperationInfo: &domain.CooperationInfo{},
		EnableStatus:    int(domain.UserStatusEnable),
		UserInfo:        userInfo,
		Ext: &domain.Ext{
			UserName: userInfo.UserName,
			Phone:    userInfo.Phone,
		},
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
	createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
	if user, err = createUserService.CreateUser(nil, user, password); err != nil {
		return nil, err
	}

	return userBase, nil
}

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