user.go
8.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package user
import (
"crypto/sha1"
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/user"
"strings"
"time"
)
type UserService struct {
}
func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *protocolx.CreateUserRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.CreateUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
defer func() {
transactionContext.RollbackTransaction()
}()
newUser := &domain.Users{
Name: request.Name,
Phone: request.Phone,
Roles: request.Roles,
Status: 1,
AdminType: request.AdminType,
CreateTime: time.Now(),
UpdateTime: time.Now(),
}
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
if request.Phone != "" {
if _, err = UserRepository.FindOne(map[string]interface{}{"phone": request.Phone}); err == nil {
err = protocol.NewCustomMessage(1, "手机号已存在")
return
}
}
if len(newUser.Passwd) == 0 {
newUser.Passwd = fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
}
if m, e := UserRepository.Save(newUser); e != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, e.Error())
return
} else {
rsp = m
}
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *protocolx.UpdateUserRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.UpdateUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
var user *domain.Users
if user, err = UserRepository.FindOne(map[string]interface{}{"id": request.Id}); err != nil {
err = protocol.NewCustomMessage(1, "用户不存在")
return
}
if request.Phone != "" && request.Phone != user.Phone {
if _, err = UserRepository.FindOne(map[string]interface{}{"phone": request.Phone}); err == nil {
err = protocol.NewCustomMessage(1, "手机号已存在")
return
}
}
// common.ObjectToMap(request)
if err = user.Update(utils.LoadCustomFieldToMap(request, header.BodyKeys...)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
if user, err = UserRepository.Save(user); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protocolx.GetUserRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
)
rsp = &protocolx.GetUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
defer func() {
transactionContext.RollbackTransaction()
}()
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
var user *domain.Users
if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
retMap := map[string]interface{}{"id": user.Id, "name": user.Name, "phone": user.Phone, "adminType": user.AdminType, "status": user.Status}
var roles []*domain.Role
for _, v := range user.Roles {
if role, e := RoleRepository.FindOne(map[string]interface{}{"id": v}); e == nil {
roles = append(roles, role)
}
}
retMap["roles"] = utils.LoadCustomField(roles, "Id", "RoleName")
rsp = map[string]interface{}{"user": retMap}
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *protocolx.DeleteUserRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.DeleteUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
defer func() {
transactionContext.RollbackTransaction()
}()
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
var user *domain.Users
if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
if user, err = UserRepository.Remove(user); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
rsp = user
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protocolx.ListUserRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
)
rsp = &protocolx.ListUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
getRoles := func(roleIds []int64) string {
if len(roleIds) == 0 {
return ""
}
var roles []string
var mapRoles = make(map[int64]*domain.Role)
for _, id := range roleIds {
if _, ok := mapRoles[id]; ok {
continue
}
if roleItem, _ := RoleRepository.FindOne(map[string]interface{}{"id": id}); roleItem != nil {
roles = append(roles, roleItem.RoleName)
} else {
mapRoles[id] = nil
}
continue
}
return strings.Join(roles, ",")
}
// TODO:可优化,每次只查询 user.id 列表 ,通过缓存查询user对象
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
var user []*domain.Users
var total int64
if total, user, err = UserRepository.Find(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
userList := make([]map[string]interface{}, 0)
for _, v := range user {
item := map[string]interface{}{"id": v.Id, "name": v.Name, "phone": v.Phone, "adminType": v.AdminType, "status": v.Status, "createTime": v.CreateTime.Local().Format("2006-01-02 15:04:05")}
item["roles"] = getRoles(v.Roles)
userList = append(userList, item)
}
rsp = map[string]interface{}{
"totalRow": total,
"pageNumber": request.PageNumber,
"lists": userList,
}
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
func NewUserService(options map[string]interface{}) *UserService {
svr := &UserService{}
return svr
}