-
Notifications
You must be signed in to change notification settings - Fork 3
/
fd_account_bill.go
422 lines (359 loc) · 12.9 KB
/
fd_account_bill.go
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package financial
import (
"context"
"fmt"
"github.com/SupenBysz/gf-admin-community/sys_service"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_do"
"github.com/kysion/base-library/utility/kconv"
"github.com/SupenBysz/gf-admin-company-modules/co_interface"
"github.com/SupenBysz/gf-admin-company-modules/co_model"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_dao"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_enum"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_hook"
"github.com/kysion/base-library/base_hook"
"github.com/kysion/base-library/base_model"
"github.com/kysion/base-library/utility/daoctl"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/yitter/idgenerator-go/idgen"
)
// 财务账单
type sFdAccountBill[
ITCompanyRes co_model.ICompanyRes,
ITEmployeeRes co_model.IEmployeeRes,
ITTeamRes co_model.ITeamRes,
ITFdAccountRes co_model.IFdAccountRes,
TR co_model.IFdAccountBillRes,
ITFdBankCardRes co_model.IFdBankCardRes,
ITFdCurrencyRes co_model.IFdCurrencyRes,
ITFdInvoiceRes co_model.IFdInvoiceRes,
ITFdInvoiceDetailRes co_model.IFdInvoiceDetailRes,
] struct {
base_hook.ResponseFactoryHook[TR]
modules co_interface.IModules[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]
dao *co_dao.XDao
hookArr base_hook.BaseHook[co_hook.AccountBillHookFilter, co_hook.AccountBillHookFunc]
}
func NewFdAccountBill[
ITCompanyRes co_model.ICompanyRes,
ITEmployeeRes co_model.IEmployeeRes,
ITTeamRes co_model.ITeamRes,
ITFdAccountRes co_model.IFdAccountRes,
TR co_model.IFdAccountBillRes,
ITFdBankCardRes co_model.IFdBankCardRes,
ITFdCurrencyRes co_model.IFdCurrencyRes,
ITFdInvoiceRes co_model.IFdInvoiceRes,
ITFdInvoiceDetailRes co_model.IFdInvoiceDetailRes,
](modules co_interface.IModules[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) co_interface.IFdAccountBill[TR] {
result := &sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]{
modules: modules,
dao: modules.Dao(),
}
result.ResponseFactoryHook.RegisterResponseFactory(result.FactoryMakeResponseInstance)
return result
}
// InstallTradeHook 订阅Hook
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) InstallTradeHook(hookKey co_hook.AccountBillHookFilter, hookFunc co_hook.AccountBillHookFunc) {
s.hookArr.InstallHook(hookKey, hookFunc)
}
// GetTradeHook 获取Hook
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) GetTradeHook() base_hook.BaseHook[co_hook.AccountBillHookFilter, co_hook.AccountBillHookFunc] {
return s.hookArr
}
// FactoryMakeResponseInstance 响应实例工厂方法
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) FactoryMakeResponseInstance() TR {
var ret co_model.IFdAccountBillRes
ret = &co_model.FdAccountBillRes{}
return ret.(TR)
}
// CreateAccountBill 创建财务账单
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) CreateAccountBill(ctx context.Context, info co_model.AccountBillRegister) (bool, error) {
// 判断交易时间是否大于当前系统时间
now := gtime.Now()
if now.Format("Y-m-d H:i:s") < info.TradeAt.Format("Y-m-d H:i:s") { // 系统时间是否在交易时间之后
fmt.Println(now.Format("Y-m-d H:i:s"))
fmt.Println(info.TradeAt.Format("Y-m-d H:i:s"))
return false, gerror.New(s.modules.T(ctx, "error_Legal_Operation"))
}
// 交易金额是否为负数
if info.Amount < 0 {
return false, gerror.New(s.modules.T(ctx, "error_AccountAmount_NonNegative"))
}
var success bool
var err error
// 判读收支类型 收入/支出
if info.InOutType == 1 {
success, err = s.income(ctx, info)
} else if info.InOutType == 2 {
success, err = s.spending(ctx, info)
}
if success == false {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_Transaction_Failed"), s.dao.FdAccountBill.Table())
}
return success == true, err
}
// 修改支付状态
// 接收微信支付宝的支付通知 (账单id,微信的交易id,支付结果)
// 接收微信支付宝的握手
// 待支付:
// HOOK (Hook)
// income 收入
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) income(ctx context.Context, info co_model.AccountBillRegister) (bool, error) {
//sessionUser := sys_service.SysSession().Get(ctx).JwtClaimsUser
// 判断接受者是否存在
toUser, err := sys_service.SysUser().GetSysUserById(ctx, info.ToUserId)
if err != nil || toUser == nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#error_Transaction_Failed}{#error_Transaction_ToUser_NoExist}"), s.dao.FdAccountBill.Table())
}
// 先通过财务账号id查询账号出来
account, err := s.modules.Account().GetAccountById(ctx, info.FdAccountId)
// 判断需要收款的用户是否存在
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#error_Transaction_Failed}{#error_ToUserAccount_NoExist}"), s.dao.FdAccountBill.Table())
}
bill := s.FactoryMakeResponseInstance()
// 使用乐观锁校验余额,和更新余额
err = s.dao.FdAccountBill.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 版本
version := account.Data().Version
// 余额 = 之前的余额 + 本次交易的金额
afterBalance := account.Data().Balance + info.Amount
// 1. 添加一条收入财务账单流水
info.BeforeBalance = account.Data().Balance
info.AfterBalance = afterBalance
gconv.Struct(info, bill.Data())
bill.Data().Id = idgen.NextId()
bill.Data().CreatedAt = gtime.Now()
//bill.Data().CreatedBy = sessionUser.Id
bill.Data().CreatedBy = info.ToUserId
data := kconv.Struct(bill.Data(), &co_do.FdAccountBill{})
// 重载Do模型
doData, err := info.OverrideDo.DoFactory(*data)
if err != nil {
return err
}
result, err := s.dao.FdAccountBill.Ctx(ctx).Insert(doData)
if result == nil || err != nil {
return sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#AccountBill}{#error_Create_Failed}"), s.dao.FdAccountBill.Table())
}
// 2.修改财务账号的余额
// 参数:上下文, 财务账号id, 需要修改的钱数目, 查询到的版本, 收支类型
affected, err := s.modules.Account().UpdateAccountBalance(ctx, account.Data().Id, info.Amount, version, info.InOutType,info.FromUserId )
if affected == 0 || err != nil {
return sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_AccountBalance_Update_Failed"), s.dao.FdAccountBill.Table())
}
// 3.修改财务账号金额明细统计
increment, err := s.modules.Account().Increment(ctx, account.Data().Id, gconv.Int(info.Amount))
if increment == false || err != nil {
return err
}
s.hookArr.Iterator(func(key co_hook.AccountBillHookFilter, value co_hook.AccountBillHookFunc) {
if key.InTransaction && key.InOutType == co_enum.Financial.InOutType.In {
if key.TradeType.Code()&info.TradeType == info.TradeType {
value(ctx, key, bill)
}
}
})
return nil
})
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_Transaction_Failed"), s.dao.FdAccountBill.Table())
}
s.hookArr.Iterator(func(key co_hook.AccountBillHookFilter, value co_hook.AccountBillHookFunc) {
if !key.InTransaction && key.InOutType == co_enum.Financial.InOutType.In {
if key.TradeType.Code()&info.TradeType == info.TradeType {
value(ctx, key, bill)
}
}
})
return true, nil
}
// spending 支出
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) spending(ctx context.Context, info co_model.AccountBillRegister) (bool, error) {
//sessionUser := sys_service.SysSession().Get(ctx).JwtClaimsUser
// 先通过财务账号id查询账号出来
account, err := s.modules.Account().GetAccountById(ctx, info.FdAccountId)
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#error_Transaction_Failed}{#error_ToUserAccount_NoExist}"), s.dao.FdAccountBill.Table())
}
bill := s.FactoryMakeResponseInstance()
// 使用乐观锁校验余额,和更新余额
err = s.dao.FdAccountBill.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 版本
version := account.Data().Version
// 余额 = 之前的余额 - 本次交易的金额
afterBalance := account.Data().Balance - info.Amount
// 判断余额是否足够
// 检验机制:余额足够 || 账号类型是系统账户&&场景不限&&允许存在负数余额 || 允许存在负数余额 (相当于只校验了Allow参数,条件2直接忽略了)
if account.Data().Balance >= info.Amount ||
(((account.Data().AccountType & co_enum.Financial.AccountType.System.Code()) == account.Data().AccountType&co_enum.Financial.AccountType.System.Code()) &&
(account.Data().SceneType&co_enum.Financial.SceneType.UnLimit.Code()) == co_enum.Financial.SceneType.UnLimit.Code() && account.Data().AllowExceed == co_enum.Financial.AllowExceed.Allow.Code()) ||
account.Data().AllowExceed == co_enum.Financial.AllowExceed.Allow.Code() {
// 1. 添加一条财务账单流水
info.BeforeBalance = account.Data().Balance
info.AfterBalance = afterBalance
gconv.Struct(info, bill.Data())
bill.Data().Id = idgen.NextId()
bill.Data().CreatedAt = gtime.Now()
//bill.Data().CreatedBy = sessionUser.Id // TODO 后续解开
bill.Data().CreatedBy = info.FromUserId
result, err := s.dao.FdAccountBill.Ctx(ctx).Insert(bill)
if result == nil || err != nil {
return sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_AccountBalance_Update_Failed"), s.dao.FdAccountBill.Table())
}
// 2.修改财务账号的余额
// 参数:上下文, 财务账号id, 需要修改的钱数目, 查询到的版本, 收支类型
affected, err := s.modules.Account().UpdateAccountBalance(ctx, account.Data().Id, info.Amount, version, info.InOutType,info.FromUserId)
if affected == 0 || err != nil {
return sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_AccountBalance_Update_Failed"), s.dao.FdAccountBill.Table())
}
// 3.修改财务账号金额明细统计
decrement, err := s.modules.Account().Decrement(ctx, account.Data().Id, gconv.Int(info.Amount))
if decrement == false || err != nil {
return err
}
s.hookArr.Iterator(func(key co_hook.AccountBillHookFilter, value co_hook.AccountBillHookFunc) {
if key.InTransaction && key.InOutType == co_enum.Financial.InOutType.Out {
if key.TradeType.Code()&info.TradeType == info.TradeType {
value(ctx, key, bill)
}
}
})
} else {
return gerror.New(s.modules.T(ctx, "error_Transaction_FromUser_InsufficientBalance"))
}
return nil
})
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_Transaction_Failed"), s.dao.FdAccountBill.Table())
}
s.hookArr.Iterator(func(key co_hook.AccountBillHookFilter, value co_hook.AccountBillHookFunc) {
if !key.InTransaction && key.InOutType == co_enum.Financial.InOutType.Out {
if key.TradeType.Code()&info.TradeType == info.TradeType {
value(ctx, key, bill)
}
}
})
return true, nil
}
// GetAccountBillByAccountId 根据财务账号id获取账单
func (s *sFdAccountBill[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
TR,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
ITFdInvoiceDetailRes,
]) GetAccountBillByAccountId(ctx context.Context, accountId int64, searchParams *base_model.SearchParams) (*base_model.CollectRes[TR], error) {
if accountId == 0 {
return nil, gerror.New(s.modules.T(ctx, "error_AccountId_NonZero"))
}
//if pagination == nil {
// pagination = &base_model.Pagination{
// PageNum: 1,
// PageSize: 20,
// }
//}
result, err := daoctl.Query[TR](s.dao.FdAccountBill.Ctx(ctx).
Where(s.dao.FdAccountBill.Columns().FdAccountId, accountId).
OrderAsc(s.dao.FdAccountBill.Columns().CreatedAt), searchParams, false)
if err != nil {
return nil, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#AccountBill}{#error_Data_Get_Failed}"), s.dao.FdAccountBill.Table())
}
return result, nil
}