pg_sign_up_company_service.go 3.3 KB
package domainService

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"time"
)

// PgSignUpCompanyService 公司注册服务
type PgSignUpCompanyService struct {
	transactionContext *pgTransaction.TransactionContext
}

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

	// 1.创建企业
	var company *domain.Company
	createCompanyService, _ := NewPgCreateCompanyService(ptr.transactionContext)
	if company, err = createCompanyService.CreateCompany(nil, registerPhone, companyInfo); err != nil {
		return nil, err
	}

	// 2.创建企业顶级组织
	var org *domain.Org = &domain.Org{
		CompanyId:  company.CompanyId,
		OrgCode:    domain.DefaultOrgCodeCompany,
		OrgName:    company.CompanyInfo.CompanyName,
		IsOrg:      domain.IsOrgFlag,
		Ext:        &domain.Ext{},
		OrgStatus:  domain.OrgStatusEnable,
		ParentId:   0,
		ParentPath: "",
		CreatedAt:  time.Now(),
		UpdatedAt:  time.Now(),
	}
	createOrgService, _ := NewPgCreateOrgService(ptr.transactionContext)
	if org, err = createOrgService.CreateOrg(nil, org); err != nil {
		return nil, err
	}

	// 3.创建企业管理员角色
	var role *domain.Role = &domain.Role{
		CompanyId: company.CompanyId,
		OrgId:     org.OrgId,
		RoleType:  domain.RoleTypeAdmin,
		RoleName:  domain.DefaultAdminRoleName,
		Ext: &domain.Ext{
			OrgName: org.OrgName,
		},
		Desc:        domain.DefaultAdminRoleName,
		AccessMenus: []int64{},
		CreatedAt:   time.Now(),
		UpdatedAt:   time.Now(),
	}
	createRoleService, _ := NewPgCreateRoleService(ptr.transactionContext)
	if role, err = createRoleService.CreateRole(nil, role); err != nil {
		return nil, err
	}

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

	return user, nil
}

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