pg_pg_update_user_service.go
3.4 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
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"
"strings"
"time"
)
// PgUpdateUserService 用户更新服务
type PgUpdateUserService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PgUpdateUserService) UpdateUser(optUser *domain.CheckOptions, user *domain.User, userInfo *domain.UserInfo, enableStatus int) (*domain.User, error) {
var err error
roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
//1.更新所属组织、部门
var org, dep *domain.Org
orgRepository, _ := repository.NewOrgRepository(ptr.transactionContext)
if user.OrganizationId > 0 {
org, err = orgRepository.FindOne(map[string]interface{}{"orgId": user.OrganizationId})
if err != nil {
return nil, err
}
}
if user.DepartmentId > 0 {
dep, err = orgRepository.FindOne(map[string]interface{}{"orgId": user.DepartmentId})
if err != nil {
return nil, err
}
}
//2.更新关联角色,组织
var userOrg = make([]*domain.Org, 0)
for i := range user.UserOrg {
var tmpOrg *domain.Org
tmpOrg, err = orgRepository.FindOne(map[string]interface{}{"orgId": user.UserOrg[i].OrgId})
if err != nil {
return nil, err
}
userOrg = append(userOrg, tmpOrg.CloneSample())
}
var userRole = make([]*domain.Role, 0)
for i := range user.UserRole {
var tmpRole *domain.Role
tmpRole, err = roleRepository.FindOne(map[string]interface{}{"roleId": user.UserRole[i].RoleId})
if err != nil {
return nil, err
}
userRole = append(userRole, tmpRole.CloneSample())
}
//3.更新用户信息
userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
var userBase *domain.UserBase
if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": user.UserBaseId}); err != nil {
return nil, err
}
if userBase.Account != strings.TrimSpace(userInfo.Phone) && len(userInfo.Phone) > 0 { // 修改了手机号
if _, err = userBaseRepository.FindOne(map[string]interface{}{"account": strings.TrimSpace(userInfo.Phone)}); err == nil {
return nil, fmt.Errorf("手机号已存在")
}
if err = userBase.ResetPhone(userBase.Account, userInfo.Phone); err != nil {
return nil, err
}
}
if err = userBase.UpdateUserInfo(userInfo); err != nil {
return nil, err
}
if userBase, err = userBaseRepository.Save(userBase); err != nil {
return nil, err
}
//4.更新用户、冗余信息
userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
user.UserRole = userRole
user.UserOrg = userOrg
if org != nil {
user.Ext.OrgName = org.OrgName
}
if dep != nil {
user.Ext.DepName = dep.OrgName
}
user.Ext.Phone = userBase.UserInfo.Phone
user.Ext.UserName = userBase.UserInfo.UserName
user.UpdatedAt = time.Now()
if err = user.SetEnableStatus(enableStatus); err != nil {
return nil, err
}
if user, err = userRepository.Save(user); err != nil {
return nil, err
}
return user, nil
}
func NewPgUpdateUserService(transactionContext *pgTransaction.TransactionContext) (*PgUpdateUserService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgUpdateUserService{
transactionContext: transactionContext,
}, nil
}
}