MusicianWithdrawService.php
6.08 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
<?php
namespace App\Services;
use App\Helper\ErrorCode;
use App\Helper\RedisClient;
use App\Helper\Response;
use App\Models\Legal\StakeholderBalance;
use App\Models\Legal\StakeholderBalanceByPayer;
use App\Models\Musician\CashOutOrder;
use Illuminate\Support\Facades\DB;
/**
* Class MusicianAgreementService
* @package App\Services
*/
class MusicianWithdrawService extends Service
{
/**
* 冻结资金
* @return \Illuminate\Http\JsonResponse
*/
public function prepare()
{
$order_id = $this->request->input('order_id');
if (!RedisClient::instance()->set(str_replace('#order#', $order_id, config('cachekey.withdraw.prepare')), true, "nx", "ex", 3)) {
return Response::error(ErrorCode::REPEAT_SUBMIT);
}
$order = CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_DEFAULT, 'status'=>CashOutOrder::STATUS_WAIT])->first();
//订单非法
if (empty($order)) return Response::error(ErrorCode::ORDER_ILLEGAl);
if (empty($order->balance_detail)) return Response::error(ErrorCode::ORDER_WITHDRAW_ERROR);
if (empty($order->cash_out_money)) return Response::error(ErrorCode::ORDER_MONEY_ERROR);
Db::beginTransaction();
try {
//付款方集合
foreach ($order->balance_detail['detail'] as $item) {
//付款方账户
StakeholderBalanceByPayer::query()->where(['stakeholder_id'=>$item['stakeholderId'], 'subject_no'=>$item['no']])
->where('balance', '>=', $item['balance'])
->update([
'balance'=>Db::raw("balance - {$item['balance']}"),
'freeze'=>Db::raw("freeze + {$item['balance']}"),
]);
//总账户
StakeholderBalance::query()->where(['stakeholder_id'=>$item['stakeholderId']])->where('balance', '>=', $item['balance'])
->update([
'balance'=>Db::raw("balance - {$item['balance']}"),
'freeze'=>Db::raw("freeze + {$item['balance']}"),
]);
}
CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_DEFAULT, 'status'=>CashOutOrder::STATUS_WAIT])->update([
'withdraw_state' => CashOutOrder::WITHDRAW_STATE_WAIT,
]);
DB::commit();
return Response::success(['order_id'=>$order->order_id]);
} catch (\Exception $e) {
DB::rollBack();
return Response::error();
}
}
/**
* 后台提现
* @return \Illuminate\Http\JsonResponse
*/
public function withdraw()
{
$order_id = $this->request->input('order_id');
if (!RedisClient::instance()->set(str_replace('#order#', $order_id, config('cachekey.withdraw.audit')), true, "nx", "ex", 3)) {
return Response::error(ErrorCode::REPEAT_SUBMIT);
}
$order = CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_WAIT, 'status'=>$this->request->input('status')])->first();
//订单非法
if (empty($order)) return Response::error(ErrorCode::ORDER_ILLEGAl);
if (empty($order->balance_detail)) return Response::error(ErrorCode::ORDER_WITHDRAW_ERROR);
Db::beginTransaction();
try {
switch (intval($this->request->input('status'))) {
case 1:
//审核通过
$this->withdrawAuditSuccess($order);
break;
case 2:
//审核失败
$this->withdrawAuditFail($order);
break;
}
CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_WAIT, 'status'=>$this->request->input('status')])->update([
'withdraw_state'=>$this->request->input('status')
]);
Db::commit();
return Response::success();
} catch (\Exception $e) {
Db::rollBack();
return Response::error(ErrorCode::WITHDRAW_HANDLE_ERROR);
}
}
/**
* 提现审核成功
* @param CashOutOrder $order
*/
private function withdrawAuditSuccess(CashOutOrder $order)
{
foreach ($order->balance_detail['detail'] as $item) {
//付款方维度
StakeholderBalanceByPayer::query()->where(['stakeholder_id'=>$item['stakeholderId'], 'subject_no'=>$item['no']])
->where('freeze', '>=', $item['balance'])
->update([
'freeze'=>Db::raw("freeze - {$item['balance']}"),
'pay_out'=>Db::raw("pay_out + {$item['balance']}"),
]);
//修改总的资金账户
StakeholderBalance::query()->where(['stakeholder_id'=>$item['stakeholderId']])->where('freeze', '>=', $item['balance'])
->update([
'freeze'=>Db::raw("freeze - {$item['balance']}"),
'pay_out'=>Db::raw("pay_out + {$item['balance']}"),
]);
}
}
/**
* 提现审核失败
* @param CashOutOrder $order
*/
private function withdrawAuditFail(CashOutOrder $order)
{
foreach ($order->balance_detail['detail'] as $item) {
StakeholderBalanceByPayer::query()->where(['stakeholder_id'=>$item['stakeholderId'], 'subject_no'=>$item['no']])
->where('freeze', '>=', $item['balance'])
->update([
'freeze'=>Db::raw("freeze - {$item['balance']}"),
'balance'=>Db::raw("balance + {$item['balance']}"),
]);
StakeholderBalance::query()->where(['stakeholder_id'=>$item['stakeholderId']])->where('freeze', '>=', $item['balance'])
->update([
'freeze'=>Db::raw("freeze - {$item['balance']}"),
'balance'=>Db::raw("balance + {$item['balance']}"),
]);
}
}
}