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.

227 lines
8.7 KiB
PHP

<?php
namespace App\Http\Controllers\API\Admin\YeWu;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TransactionController
{
public function GetList(){
$type=request('type');
$page =request('page');
$pageSize =request('pageSize');
$searchInfo =request('searchInfo');
$list = DB::table('transactions')
->leftJoin('members','transactions.member_id','=','members.id')
->select('transactions.*','members.name','members.tel','members.bank_name','members.card_number')
->where('transactions.type',$type)
->where('transactions.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('transactions.id','desc')
->limit($pageSize)->skip(($page - 1) * $pageSize)->take($pageSize)
->get();
return \Yz::Return(true,'获取成功',['list'=>$list,'count'=>$count]);
}
//充值
public function Recharge(Request $request)
{
$dayCutCheck = \Yz::DayCutCheck();
if(!$dayCutCheck['status']){
return \Yz::echoError1($dayCutCheck['msg']);
}
$userid = $request->get('userid');//中间件产生的参数
$memberId = request('memberId');
$amount = request('amount');
$Note = request('note');
if (empty($amount)){
return \Yz::echoError1('请输入充值金额');
}
if (!is_numeric($amount) || $amount <= 0){
return \Yz::echoError1('充值金额格式错误');
}
try {
$result = DB::transaction(function () use ($userid, $memberId, $amount, $Note) {
$memberInfo=DB::table('members')->where('id',$memberId)->where('is_del',0)->lockForUpdate()->first();
if (empty($memberInfo)){
throw new \Exception('用户不存在');
}
if ($memberInfo->status==0){
throw new \Exception('用户已被禁用');
}
$amount = (string)$amount;
$balance = (string)$memberInfo->balance;
$new_balance = bcadd($balance, $amount, 2);
//插入流水表
$up_trans= DB::table('transactions')->insert([
'member_id' => $memberId,
'type' => 1, // 充值
'amount' => $amount,
'balance_after' => $new_balance,
'status' => 3, // 已处理
'note' => $Note,
'source' => 'admin',
'operator_id' => $userid,
'verify_userid' => $userid,
'verify_time' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s'),
]);
// 4. 更新用户余额
$up_member=false;
if(!!$up_trans){
$up_member= DB::table('members')
->where('id', $memberId)
->update([
'balance' => $new_balance,
'updated_at' => date('Y-m-d H:i:s'),
]);
}
return $up_member;
});
if(!!$result){
return \Yz::Return(true,'充值成功');
}else{
return \Yz::echoError1('充值失败');
}
}catch (\Exception $e){
return \Yz::echoError1($e->getMessage());
}
}
// 审核
public function Verify(Request $request)
{
$dayCutCheck = \Yz::DayCutCheck();
if(!$dayCutCheck['status']){
return \Yz::echoError1($dayCutCheck['msg']);
}
$userid = $request->get('userid');//中间件产生的参数
$id = request('id');
$TransactionsType = request('TransactionsType');
$DoType = request('DoType');
$Note = request('Note');
if ($DoType === '通过') {
$status = 3;
} elseif ($DoType === '拒绝') {
$status = 2;
} else {
return \Yz::Return(false, '无效的操作类型');
}
try {
$result = DB::transaction(function () use ($id, $TransactionsType, $status,$Note,$userid) {
// 1. 获取交易记录(加锁)
$transaction = DB::table('transactions')
->where('id', $id)
->where('type', $TransactionsType)
->where('is_del', 0)
->lockForUpdate()
->first();
if (empty($transaction)) {
throw new \Exception('交易记录不存在');
}
if ($transaction->status !== 1) {
throw new \Exception('当前状态不可审核');
}
// 2. 获取用户信息(加锁)
$memberInfo = DB::table('members')
->where('id', $transaction->member_id)
->lockForUpdate()
->first();
if (empty($memberInfo)) {
throw new \Exception('用户不存在');
}
$yesterday = Carbon::yesterday();
if(!empty($memberInfo->day_cut_at) && $memberInfo->day_cut_at != $yesterday->toDateString()){
throw new \Exception('该用户昨日日切异常,无法审核');
}
if(empty($memberInfo->day_cut_at) && $memberInfo->created_at < date('Y-m-d')." 00:00:00"){
throw new \Exception('该用户昨日日切异常,无法审核');
}
// 3. 处理审核结果
if ($status === 3) {
// 审核通过
$amount = (string)$transaction->amount;
$balance = (string)$memberInfo->balance;
if (!is_numeric($amount) || !is_numeric($balance)) {
throw new \Exception('余额或交易金额格式错误');
}
if ($TransactionsType === 3) {
// 提现:余额减少
$new_balance = bcsub($balance, $amount, 2);
// 可选:检查余额是否足够
if (bccomp($new_balance, '0', 2) < 0) {
throw new \Exception('用户余额不足');
}
//累积提现金额
$withdraw_amount = (string)$memberInfo->withdraw_amount;
$new_withdraw_amount = bcadd($withdraw_amount, $amount, 2);
$up_member_date = [
'withdraw_amount' => $new_withdraw_amount,
'balance' => $new_balance,
];
} else {
// 充值/利息:余额增加
$new_balance = bcadd($balance, $amount, 2);
$up_member_date = [
'balance' => $new_balance,
];
}
// 更新用户余额
DB::table('members')
->where('id', $transaction->member_id)
->update($up_member_date);
$up_transactions_data = [
'status' => $status,
'verify_time' => now(),
'verify_userid' => $userid,
'balance_after' => $new_balance,
'note' => $Note,
];
} else {
// 拒绝:不改余额
$up_transactions_data = [
'status' => $status,
'verify_time' => now(),
'verify_userid' => $userid,
'note' => $Note,
];
}
// 更新交易状态
DB::table('transactions')
->where('id', $id)
->update($up_transactions_data);
return true; // 表示成功
});
if($result){
return \Yz::Return(true, '审核成功',['transactions'=>$id]);
}else{
return \Yz::Return(false, '审核失败');
}
} catch (\Exception $e) {
return \Yz::Return(false, $e->getMessage());
}
}
}