WithdrawService.php
5.02 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
<?php
namespace App\Services;
use App\ApiInterface\WithdrawInterface;
use App\Helper\ErrorCode;
use App\Helper\Response;
use App\Models\Legal\StakeholderBalanceByPayer;
use App\Models\Legal\StakeholderIncomeSyncApp;
use App\Models\Legal\StakeholderWithdrawAppLogs;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Class MusicianAgreementService
* @package App\Services
*/
class WithdrawService extends Service implements WithdrawInterface
{
/**
* 提现申请
* @param StakeholderIncomeSyncApp $app
*/
public function advance(StakeholderIncomeSyncApp $app)
{
$condition = $this->compose($app);
DB::beginTransaction();
try {
//修改状态-》提现中
StakeholderIncomeSyncApp::query()->where(['serial_no'=>$app->serial_no, 'identifier'=>$this->request->get('identifier')->identifier, 'sync_status'=>1, 'withdraw_status'=>1])->update([
'withdraw_status'=>2
]);
//修改资金账户
StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->where('balance', '>=', $app->total_money)->update([
'freeze' => DB::raw("freeze + {$app->total_money}"),
'balance'=> DB::raw("balance - {$app->total_money}"),
]);
//提现申请日志
StakeholderWithdrawAppLogs::query()->create([
'serial_no' => $app->serial_no,
'identifier' => $this->request->get('identifier')->identifier,
'type' => __FUNCTION__,
'withdraw_money' => $app->total_money,
]);
DB::commit();
return Response::success();
} catch (\Exception $e) {
DB::rollBack();
Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]);
return Response::error(ErrorCode::WITHDRAW_APPLY_FAIL);
}
}
/**
* 提现成功
* @param StakeholderIncomeSyncApp $app
*/
public function success(StakeholderIncomeSyncApp $app)
{
$condition = $this->compose($app);
DB::beginTransaction();
try {
//修改状态-》提现中 -> 提现完成
StakeholderIncomeSyncApp::query()->where(['serial_no'=>$app->serial_no, 'identifier'=>$this->request->get('identifier')->identifier, 'sync_status'=>1, 'withdraw_status'=>2])->update([
'withdraw_status'=>3
]);
//修改资金账户
StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->where('freeze', '>=', $app->total_money)->update([
'freeze' => DB::raw("freeze - {$app->total_money}"),
'pay_out'=> DB::raw("pay_out + {$app->total_money}"),
]);
//提现申请日志
StakeholderWithdrawAppLogs::query()->create([
'serial_no' => $app->serial_no,
'identifier' => $this->request->get('identifier')->identifier,
'type' => __FUNCTION__,
'withdraw_money' => $app->total_money,
]);
DB::commit();
return Response::success();
} catch (\Exception $e) {
DB::rollBack();
Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]);
return Response::error(ErrorCode::WITHDRAW_APPLY_FAIL);
}
}
/**
* 提现失败
* @param StakeholderIncomeSyncApp $app
*/
public function fail(StakeholderIncomeSyncApp $app)
{
$condition = $this->compose($app);
DB::beginTransaction();
try {
//修改状态-》提现中 -》 已确认
StakeholderIncomeSyncApp::query()->where(['serial_no'=>$app->serial_no, 'identifier'=>$this->request->get('identifier')->identifier, 'sync_status'=>1, 'withdraw_status'=>2])->update([
'withdraw_status'=>1
]);
//修改资金账户
StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->where('freeze', '>=', $app->total_money)->update([
'freeze' => DB::raw("freeze - {$app->total_money}"),
'balance'=> DB::raw("balance + {$app->total_money}"),
]);
//提现申请日志
StakeholderWithdrawAppLogs::query()->create([
'serial_no' => $app->serial_no,
'identifier' => $this->request->get('identifier')->identifier,
'type' => __FUNCTION__,
'withdraw_money' => $app->total_money,
]);
DB::commit();
return Response::success();
} catch (\Exception $e) {
DB::rollBack();
Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]);
return Response::error();
}
}
/**
* @param StakeholderIncomeSyncApp $app
* @return string
*/
private function compose(StakeholderIncomeSyncApp $app)
{
return $app->stakeholder_id .'|'. $app->subject_no;
}
}