WithdrawalObserver.php
3.9 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
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* 观察者层:UserWalletWithdrawal 处理业务逻辑
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Observers\User
* @package App\Observers\User
* @author Richer <yangzi1028@163.com>
* @date 2022年5月25日11:16:12
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Observers\User;
use App\Jobs\User\WithdrawalAuditPassedJob;
use App\Models\User\UserWalletRecord;
use App\Models\User\UserWalletWithdrawal as Model;
/**
* Class WithdrawalObserver
*
* @category App\Observers\User
* @package App\Observers\User
* @author Richer <yangzi1028@163.com>
* @date 2022年5月25日11:16:12
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class WithdrawalObserver
{
/**
* 监听数据即将创建的事件。
*
* @param Model $model
* @return void
*/
public function creating(Model $model)
{
//
}
/**
* 监听数据创建后的事件。
*
* @param Model $model
* @return void
*/
public function created(Model $model)
{
// 创建成功后,更新用户的钱包信息
$wallet = $model->wallet;
$user = $wallet->user;
// 更新钱包的冻结余额
$amount = $model->amount;
$wallet->balance -= $amount;
$wallet->frozen_balance += $amount;
$wallet->save();
$user->balance -= $amount;// 余额
$user->save();
}
/**
* 监听数据即将更新的事件。
*
* @param Model $model
* @return void
*/
public function updated(Model $model)
{
// 如果更新了,判断是审核还是用户进行提交
$audited_status = $model->getAttribute('audited_status');
$audited_status_original = $model->getOriginal('audited_status');
if ($audited_status_original == config('constants.UNAUDITED')) {
$amount = $model->amount;
// 更新用户的钱包
$wallet = $model->wallet;
// 如果是审核 通过
if ($audited_status == config('constants.AUDIT_PASSED')) {
$wallet->frozen_balance -= $amount;
$wallet->withdrawn_amount += $amount;
$wallet->save();
// 同时向流水中增加提现的流水 TODO
$wallet->records()->create([
'user_id' => $wallet->user_id,
'amount' => $amount,
'recordable_type' => Model::OBJ_NAME,
'recordable_id' => $model->id,
'type' => UserWalletRecord::DECREASE,
'event' => UserWalletRecord::WITHDRAW,
]);
// TODO 需要唤起微信的零钱转账功能
WithdrawalAuditPassedJob::dispatch($model);
} elseif ($audited_status == config('constants.AUDIT_NOT_PASSED')) {
$wallet->frozen_balance -= $amount;
$wallet->balance += $amount;
$wallet->save();
$user = $wallet->user;
$user->balance += $amount;
$user->save();
}
}
}
/**
* 监听数据即将保存的事件。
*
* @param Model $model
* @return void
*/
public function saving(Model $model)
{
//
}
/**
* 监听数据保存后的事件。
*
* @param Model $model
* @return void
*/
public function saved(Model $model)
{
//
}
}