UserWalletTrait.php
4.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
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* trait :用户相关资金账户操作 trait
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2023年5月8日14:36:47
* @copyright 2022-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Traits;
use App\Models\User\Commission;
use App\Models\User\Wallet;
use App\Models\User\WalletRecord;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Trait UserWalletTrait
*
* @package App\Models\Traits
*/
trait UserWalletTrait
{
/**
* 现金余额账户 入账
*
* @param $recordable
* @param int $amount
* @param int $event
* @param int $rate
* @return mixed
* @throws Exception
*/
public function walletEntry($recordable, $amount = 0, $event = 0, $rate = 0)
{
$this->accountEntryOrCost(
WalletRecord::INCREASE,
$recordable,
$amount,
$event,
$rate
);
}
/**
* 现金余额账户 消费
*
* @param $recordable
* @param int $amount
* @param int $event
* @param int $rate
* @return mixed
* @throws Exception
*/
public function walletCost($recordable, $amount = 0, $event = 0, $rate = 0)
{
$this->accountEntryOrCost(
WalletRecord::DECREASE,
$recordable,
$amount,
$event,
$rate
);
}
/**
* 冻结账户
*
* @param $account
* @param int $amount
* @return mixed
*/
public function frozenAmount($account, $amount = 0)
{
$account->frozen += $amount;
$account->balance -= $amount;
return $account->save();
}
/**
* 钱包账户
*
* @return mixed
*/
public function getWallet()
{
//总的钱包数据
$account = $this->wallet()->lockForUpdate()->first();
if (!$account) {
$account = $this->wallet()->create([]);
}
return $account;
}
/**
* 资金入账
*
* @param $type
* @param $recordable
* @param int $amount
* @param int $event
* @param int $rate
* @return mixed
* @throws Exception
*/
public function accountEntryOrCost($type, $recordable, $amount = 0, $event = 0, $rate = 0)
{
if ($amount > 0) {
$account = $this->getWallet();
switch ($type) {
case WalletRecord::INCREASE:
$account->income += $amount;
$account->balance += $amount;
break;
case WalletRecord::DECREASE:
// 如果是扣减需要判断账号余额是否足够
$balance = $this->balance;
if ($balance < $amount) {
throw new Exception("操作失败,账户余额不足。");
}
$account->balance -= $amount;
break;
}
$account->save();
// 刷新用户模型
// $this->refresh();
// 用户创建分销记录
$record = [
'user_id' => $this->id,
'amount' => $amount,
'type' => $type,
'event' => $event,
'ratio' => $rate
];
$recordable->walletRecords()->create($record);
}
}
/**
* 释放冻结资金
*
* @param int $amount
* @param string $event
* @return mixed
*/
public function releaseFrozenAmount($amount = 0, $event = 'pass')
{
$account = $this->getWallet();
// 通过
$account->frozen -= $amount;
if ($event === 'refuse') {
// 拒绝
$account->balance += $amount;
} else {
// 通过
$account->withdrawn += $amount;
}
return $account->save();
}
}