userbase.go
4.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
)
type UserBaseService struct {
}
// 返回列表
func (userService *UserBaseService) SearchUser(listUserQuery *query.ListUserQuery) (interface{}, error) {
if err := listUserQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var count int64
var userBases []*domain.UserBase
userBaseRepository, _, _ := factory.FastPgUserBase(transactionContext, 0)
userRepository, _, _ := factory.FastPgUser(transactionContext, 0)
_, users, err := userRepository.Find(map[string]interface{}{
"limit": listUserQuery.Limit,
"offset": listUserQuery.Offset,
"orgName": listUserQuery.OrgName,
"userName": listUserQuery.UserName,
"distinctOnUserBaseId": true,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var result = make([]*dto.UserBaseDto, 0)
if len(users) > 0 {
var userBaseIds []int64
for i := range users {
userBaseIds = append(userBaseIds, users[i].UserBaseId)
}
listUserQuery.InUserBaseIds = userBaseIds
queryOptions := utils.ObjectToMap(listUserQuery)
count, userBases, err = userBaseRepository.Find(queryOptions)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range userBases {
u := userBases[i]
var relateUsers []*domain.User
if listUserQuery.FetchOrgBelong && len(u.RelatedUsers) > 0 {
for i := range u.RelatedUsers {
relateUser, _ := userRepository.FindOne(map[string]interface{}{"userId": u.RelatedUsers[i]})
if relateUser != nil {
relateUsers = append(relateUsers, relateUser)
}
}
}
userBaseDto := &dto.UserBaseDto{}
userBaseDto.LoadDto(u, relateUsers)
result = append(result, userBaseDto)
}
}
return map[string]interface{}{
"count": count,
"users": result,
}, nil
}
// 批量修改启用状态
func (userService *UserBaseService) BatchEnable(batchEnableCommand *command.BatchEnableCommand) (interface{}, error) {
if err := batchEnableCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
userRepository, _, err := factory.FastPgUserBase(transactionContext, 0)
if err != nil {
return nil, err
}
for i := 0; i < len(batchEnableCommand.UserBaseIds); i++ {
if user, err := userRepository.FindOne(map[string]interface{}{"userBaseId": batchEnableCommand.UserBaseIds[i]}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := user.SetEnableStatus(batchEnableCommand.EnableStatus); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if _, err := userRepository.Save(user); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
func NewUserBaseService(options map[string]interface{}) *UserBaseService {
newUserService := &UserBaseService{}
return newUserService
}