pg_create_user_account_service.go 1.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"
	"time"
)

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

// CreateUserAccount  创建账号服务
//
// registerAccount 注册账号
// password  密码
// userInfo  用户信息
func (ptr *PgCreateUserAccountService) CreateUserAccount(registerAccount string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error) {

	userBaseRepository, err := repository.NewUserBaseRepository(ptr.transactionContext)
	if err != nil {
		return nil, err
	}
	if len(password) == 0 {
		return nil, fmt.Errorf("密码不能为空")
	}
	userBase := &domain.UserBase{
		UserInfo:  userInfo,
		Account:   registerAccount,
		Password:  password,
		Im:        &domain.Im{},
		Status:    int(domain.UserStatusEnable),
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Favorite: &domain.Favorite{
			OrgItems: make([]int64, 0),
			Referer:  userInfo.Referer,
		},
	}
	if existsUserBase, e := userBaseRepository.FindOne(map[string]interface{}{"account": registerAccount}); e == nil && existsUserBase != nil {
		return existsUserBase, nil
	}
	if userBase, err = userBaseRepository.Save(userBase); err != nil {
		return nil, err
	}
	return userBase, nil
}

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