pg_create_user_service.go
3.5 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
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("手机号在该企业内已存在,请重新输入")
}
}
}
// 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
}
}