fast_domain_repository.go
9.8 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package factory
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
)
/***** 1.快速模块 *****/
// FastPgUser 快速返回领域用户
//
// transactionContext 事务
// userId 用户ID
func FastPgUser(transactionContext application.TransactionContext, userId int64, options ...option) (domain.UserRepository, *domain.User, error) {
var rep domain.UserRepository
var mod *domain.User
var err error
if value, err := CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if userId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"userId": userId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该用户不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
return nil, nil, err
}
return rep, mod, err
}
// FastPgUser 快速返回领域用户基础
//
// transactionContext 事务
// userBaseId 用户基础ID
func FastPgUserBase(transactionContext application.TransactionContext, userBaseId int64) (domain.UserBaseRepository, *domain.UserBase, error) {
var rep domain.UserBaseRepository
var mod *domain.UserBase
var err error
if value, err := CreateUserBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if userBaseId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"userBaseId": userBaseId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "用户基础不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// FastPgRole 快速返回领域角色
//
// transactionContext 事务
// roleId 角色Id
func FastPgRole(transactionContext application.TransactionContext, roleId int64, options ...option) (domain.RoleRepository, *domain.Role, error) {
var rep domain.RoleRepository
var mod *domain.Role
var err error
if value, err := CreateRoleRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if roleId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"roleId": roleId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该角色不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
return nil, nil, err
}
return rep, mod, err
}
// FastPgRole 快速返回领域组织
//
// transactionContext 事务
// orgId 组织Id
func FastPgOrg(transactionContext application.TransactionContext, orgId int64) (domain.OrgRepository, *domain.Org, error) {
var rep domain.OrgRepository
var mod *domain.Org
var err error
if value, err := CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if orgId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"orgId": orgId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该组织不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// FastPgCompany 快速返回领域公司
//
// transactionContext 事务
// companyId 公司Id
func FastPgCompany(transactionContext application.TransactionContext, companyId int64) (domain.CompanyRepository, *domain.Company, error) {
var rep domain.CompanyRepository
var mod *domain.Company
var err error
if value, err := CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if companyId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"companyId": companyId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该企业不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// FastPgMenu 快速返回领域菜单
//
// transactionContext 事务
// menuId 菜单Id
func FastPgMenu(transactionContext application.TransactionContext, menuId int64) (domain.MenuRepository, *domain.Menu, error) {
var rep domain.MenuRepository
var mod *domain.Menu
var err error
if value, err := CreateMenuRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if menuId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"menuId": menuId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该菜单不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// FastPgUpChain 快速返回区块链
//
// transactionContext 事务
// upChain upChainId
func FastPgUpChain(transactionContext application.TransactionContext, upChainId int64) (domain.UpChainRepository, *domain.UpChain, error) {
var rep domain.UpChainRepository
var mod *domain.UpChain
var err error
if value, err := CreateUpChainRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if upChainId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"upChainId": upChainId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该组织不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// FastPgCustomizeMenu 快速返回领域自定义菜单
//
// transactionContext 事务
// customizeMenuId 自定义菜单Id
func FastPgCustomizeMenu(transactionContext application.TransactionContext, customizeMenuId int64) (domain.CustomizeMenuRepository, *domain.CustomizeMenu, error) {
var rep domain.CustomizeMenuRepository
var mod *domain.CustomizeMenu
var err error
if value, err := CreateCustomizeMenuRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if customizeMenuId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"customizeMenuId": customizeMenuId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "自定义菜单不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// FastPgAccountDestroyRecord 快速返回领域账号注销记录
//
// transactionContext 事务
// accountDestroyRecordId 账号注销记录Id
func FastPgAccountDestroyRecord(transactionContext application.TransactionContext, accountDestroyRecordId int64) (domain.AccountDestroyRecordRepository, *domain.AccountDestroyRecord, error) {
var rep domain.AccountDestroyRecordRepository
var mod *domain.AccountDestroyRecord
var err error
if value, err := CreateAccountDestroyRecordRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if accountDestroyRecordId > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"accountDestroyRecord": accountDestroyRecordId}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "账号注销记录不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
// fastPgDataAuth 快速数据权限验证
//
// data 待认证的数据
// options 配置项
func fastPgDataAuth(transactionContext application.TransactionContext, data domain.AuthedData, options ...option) error {
option := NewFastOptions(options...)
if option.DataAuthRequired && data != nil {
if data.BelongOrg() != option.OperateInfo.OrgId {
return application.ThrowError(application.BUSINESS_ERROR, "当前登录的组织机构与操作数据组织机构不一致")
}
}
return nil
}
/***** 2.配置 *****/
type FastOptions struct {
DataAuthRequired bool
OperateInfo *domain.OperateInfo
}
func NewFastOptions(options ...option) *FastOptions {
o := &FastOptions{
DataAuthRequired: false,
}
for i := 0; i < len(options); i++ {
options[i](o)
}
return o
}
type option func(options *FastOptions)
// 需要数据权限
func WithDataAuthRequired() option {
return func(options *FastOptions) {
options.DataAuthRequired = true
}
}
// WithOperator 操作人
func WithOperator(op *domain.OperateInfo) option {
return func(options *FastOptions) {
options.OperateInfo = op
}
}