You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
4.6 KiB
PHP
122 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API\Admin\YeWu;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class LoanController
|
|
{
|
|
public function GetList()
|
|
{
|
|
$page = request('page');
|
|
$pageSize = request('pageSize');
|
|
$searchInfo = request('searchInfo');
|
|
$list = DB::table('loans')
|
|
->leftJoin('members', 'loans.member_id', '=', 'members.id')
|
|
->select('loans.*', 'members.name', 'members.tel', 'members.bank_name', 'members.card_number')
|
|
->where('loans.is_del', 0);
|
|
if (!empty($searchInfo['info'])) {
|
|
$list->where('members.name', 'like', '%' . $searchInfo['info'] . '%')
|
|
->orWhere('members.tel', $searchInfo['info']);
|
|
}
|
|
$count = $list->count();
|
|
$list = $list
|
|
->orderBy('loans.id', 'desc')
|
|
->limit($pageSize)->skip(($page - 1) * $pageSize)->take($pageSize)
|
|
->get();
|
|
|
|
return \Yz::Return(true, '获取成功', ['list' => $list, 'count' => $count]);
|
|
}
|
|
|
|
// 审核
|
|
public function Verify(Request $request)
|
|
{
|
|
$dayCutCheck = \Yz::DayCutCheck();
|
|
if(!$dayCutCheck['status']){
|
|
return \Yz::echoError1($dayCutCheck['msg']);
|
|
}
|
|
$userid = $request->get('userid');//中间件产生的参数
|
|
$id = request('id');
|
|
$ChangeStatus = request('ChangeStatus');
|
|
$Note = request('Note');
|
|
if(empty($ChangeStatus)){
|
|
return \Yz::Return(false, '操作类型不能为空');
|
|
}
|
|
try {
|
|
$result = DB::transaction(function () use ($id, $ChangeStatus, $Note, $userid) {
|
|
// 1. 获取借款记录(加锁)
|
|
$loan = DB::table('loans')
|
|
->where('id', $id)
|
|
->where('is_del', 0)
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
if (empty($loan)) {
|
|
throw new \Exception('借款记录不存在');
|
|
}
|
|
// 2. 获取用户信息(加锁)
|
|
$memberInfo = DB::table('members')
|
|
->where('id', $loan->member_id)
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
if (empty($memberInfo)) {
|
|
throw new \Exception('用户不存在');
|
|
}
|
|
$amount = (string)$loan->amount;
|
|
$outstanding_loan_amount= (string)$memberInfo->outstanding_loan_amount;
|
|
$up_loan_data_temp=[];
|
|
if($ChangeStatus == 3){ //放款
|
|
$new_outstanding_loan_amount=bcadd($outstanding_loan_amount, $amount, 2); // 新的未还金额 = 未还金额 + 放款金额
|
|
$up_member=DB::table('members')
|
|
->where('id', $loan->member_id)
|
|
->update([
|
|
'outstanding_loan_amount' => $new_outstanding_loan_amount,
|
|
]);
|
|
$up_loan_data_temp=['disbursed_time'=>date('Y-m-d H:i:s')];
|
|
}
|
|
if($ChangeStatus == 4){ //已还款
|
|
$new_outstanding_loan_amount=bcsub($outstanding_loan_amount, $amount, 2); // 新的未还金额 = 未还金额 - 还款金额
|
|
if ($new_outstanding_loan_amount < 0) { // 未还金额不能小于0
|
|
throw new \Exception('还款金额不能大于未还金额');
|
|
}
|
|
$up_member=DB::table('members')
|
|
->where('id', $loan->member_id)
|
|
->update([
|
|
'outstanding_loan_amount' => $new_outstanding_loan_amount,
|
|
]);
|
|
$up_loan_data_temp=['repayment_time'=>date('Y-m-d H:i:s')];
|
|
}
|
|
|
|
$up_loan_data2 = [
|
|
'status' => $ChangeStatus,
|
|
'verify_userid' => $userid,
|
|
'verify_time' => date('Y-m-d H:i:s'),
|
|
'note' => $Note,
|
|
];
|
|
$up_loan_data=array_merge($up_loan_data2,$up_loan_data_temp);
|
|
$up_loan=DB::table('loans')
|
|
->where('id', $id)
|
|
->update($up_loan_data);
|
|
|
|
if($up_loan){
|
|
return true;
|
|
}else{
|
|
throw new \Exception('审核失败');
|
|
}
|
|
|
|
});
|
|
if($result){
|
|
return \Yz::Return(true, '审核成功',['loan'=>$id]);
|
|
}else{
|
|
return \Yz::Return(false, '审核失败');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return \Yz::Return(false, $e->getMessage());
|
|
}
|
|
|
|
|
|
}
|
|
}
|