pg_sign_up_company_service.go
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
}
}