admin_user.go
8.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
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
259
260
261
262
263
264
265
266
267
268
269
270
package service
import (
"crypto/sha1"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//AdminUserService 管理员相关服务
type AdminUserService struct {
}
func NewAdminUserService(option map[string]interface{}) *AdminUserService {
newAdminUserService := new(AdminUserService)
return newAdminUserService
}
func (adminUserSrv AdminUserService) GetAdminUser(getAdminUserQuery *query.GetAdminUserQuery) (*domain.AdminUser, error) {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
adminuserRepository domain.AdminUserRepository
adminuser *domain.AdminUser
)
if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
adminuserRepository = value
}
adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{
AccountEqual: getAdminUserQuery.AdminAccount,
AdminUserId: getAdminUserQuery.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return adminuser, nil
}
func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdminUserCommand) (*domain.AdminUser, error) {
if err := saveUserCmd.ValidateCommand(); err != nil {
return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
}
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserRepository domain.AdminUserRepository
adminuser *domain.AdminUser
)
if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
adminuserRepository = value
}
//获取权限
var (
permissionRepository domain.AdminPermissionRepository
permissions []domain.AdminPermission
)
if value, err := factory.CreateAdminPermissionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
permissionRepository = value
}
permissions, err = permissionRepository.Find(map[string]interface{}{
"IdsIn": saveUserCmd.PermissionId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range permissions {
if permissions[i].Code == domain.PERMINSSION_ADMIN_USER {
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "操作异常")
}
}
permissionBases := []domain.AdminPermissionBase{}
for i := range permissions {
p := domain.AdminPermissionBase{
Id: permissions[i].Id, Code: permissions[i].Code,
}
permissionBases = append(permissionBases, p)
}
//账号是否有变更
var accountChange bool
if saveUserCmd.Id > 0 {
//更新数据
adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{
AdminUserId: saveUserCmd.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if adminuser.Account != saveUserCmd.Account {
accountChange = true
}
adminuser.Account = saveUserCmd.Account
adminuser.AdminName = saveUserCmd.Name
adminuser.IsUsable = saveUserCmd.IsUsable
if !adminuser.IsDefault {
adminuser.Permission = permissionBases
}
} else {
//添加新数据
accountChange = true
defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
adminuser = &domain.AdminUser{
Id: saveUserCmd.Id,
Account: saveUserCmd.Account,
Password: defaultPwd,
AdminName: saveUserCmd.Name,
IsUsable: saveUserCmd.IsUsable,
Permission: permissionBases,
}
}
if accountChange {
//检查账号是否已存在
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
ok, err := adminuserDao.AdminUserAccountExist(saveUserCmd.Account)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if ok {
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "账号已存在")
}
}
adminuser, err = adminuserRepository.Save(*adminuser)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return adminuser, nil
}
func (adminUserSrv AdminUserService) PageListAdminUser(listAdminUserQuery *query.ListAdminUserQuery) ([]domain.AdminUser, int, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
adminuserRepository domain.AdminUserRepository
adminusers []domain.AdminUser
cnt int
)
if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
adminuserRepository = value
}
adminusers, err = adminuserRepository.Find(domain.AdminUserFindQuery{
AccountLike: listAdminUserQuery.AdminAccountMatch,
Offset: listAdminUserQuery.Offset,
Limit: listAdminUserQuery.Limit,
})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
cnt, err = adminuserRepository.CountAll(domain.AdminUserFindQuery{
AccountLike: listAdminUserQuery.AdminAccountMatch,
})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return adminusers, cnt, nil
}
func (adminUserSrv AdminUserService) UpdateAdminPassword(updatecmd command.UpdateAdminUserPwdCommand) error {
if err := updatecmd.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
err = adminuserDao.UpdatePassword(updatecmd.Id, updatecmd.Password)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}
func (adminUserSrv AdminUserService) UpdateAdminIsUsable(uid int64, isUsable bool) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
if ok, err := adminuserDao.AdminUserIsDefault(uid); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "请勿禁用超级管理员")
}
err = adminuserDao.UpdateIsUsable(uid, isUsable)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}