erp/app/api/controller/user/UserWithdrawController.php
mkm 358dfe078b 1. 更新 UserWithdrawController.php 文件,优化用户提现逻辑。
2. 更新 MerchantLogic.php 文件,优化商户逻辑
2024-05-23 11:57:11 +08:00

136 lines
5.0 KiB
PHP

<?php
namespace app\api\controller\user;
use app\admin\validate\withdraw\MerchantWithdrawValidate;
use app\api\controller\BaseApiController;
use app\api\lists\withdraw\MerchantWithdrawLists;
use app\common\model\bank\Bank;
use app\common\model\merchant\Merchant;
use app\common\model\merchant\MerchantBank;
use app\common\model\supplier\Supplier;
use app\common\model\withdraw\MerchantWithdraw;
class UserWithdrawController extends BaseApiController
{
//获取用户余额和绑定银行账户信息
public function amount_account()
{
$supplier = $this->request->userInfo['supplier'];
if (!$supplier) {
return $this->fail('当前用户非供应商');
}
// $merchant = Merchant::where('uid',$uid)->findOrEmpty();
$supplier = Supplier::where('id', $supplier['id'])->findOrEmpty();
// if($merchant->isEmpty() && $supplier->isEmpty()){
// return $this->fail('当前用户非商户或供应商');
// }
// if(!$merchant->isEmpty()){
// $bank_list = MerchantBank::where('mer_id',$merchant['mer_id'])->where('is_check',1)->select()->each(function($data){
// $bank_info = Bank::where('id',$data['bank_id'])->findOrEmpty();
// $data['bank_name'] = !$bank_info->isEmpty() ? $bank_info['name'] : '';
// $data['bank_image'] = !$bank_info->isEmpty() ? $bank_info['image'] : '';
// return $data;
// })->toArray();
// return $this->success('请求成功',[
// 'balance' => $merchant['mer_money'],
// 'bank_list' => $bank_list
// ]);
// } elseif (!$supplier->isEmpty()){
$bank_list = MerchantBank::where('supplier_id', $supplier['id'])->where('is_check', 1)->select()->each(function ($data) {
$bank_info = Bank::where('id', $data['bank_id'])->findOrEmpty();
$data['bank_name'] = !$bank_info->isEmpty() ? $bank_info['name'] : '';
$data['bank_image'] = !$bank_info->isEmpty() ? $bank_info['image'] : '';
return $data;
})->toArray();
return $this->success('请求成功', [
'balance' => $supplier['mer_money'],
'bank_list' => $bank_list
]);
// }else{
// return $this->fail('未找到商户或供应商信息');
// }
}
//用户提现操作
public function withdraw()
{
$params = (new MerchantWithdrawValidate())->post()->goCheck('withdraw');
$uid = $this->request->userId;
// $merchant = Merchant::where('uid',$uid)->findOrEmpty();
$supplier = Supplier::where('uid',$uid)->findOrEmpty();
if (!$supplier) {
return $this->fail('当前用户非供应商');
}
if ($supplier->isEmpty()) {
return $this->fail('当前用户非供应商');
}
// if(!$merchant->isEmpty()){
// if($params['amount'] > $merchant['mer_money']){
// return $this->fail('提现余额不足');
// }
// $save_data = [
// 'mer_id' => $merchant['mer_id'],
// 'merchant_bank_id' => $params['merchant_bank_id'],
// 'amount' => $params['amount'],
// 'is_check' => 0,
// 'is_arrival' => 0,
// 'admin_id' => 0,
// 'create_time' => time(),
// ];
// (new MerchantWithdraw) -> save($save_data);
// return $this->success('提现成功,等待管理员审核');
// } elseif (!$supplier->isEmpty()){
if ($params['amount'] > $supplier['mer_money']) {
return $this->fail('提现余额不足');
}
$save_data = [
'supplier_id' => $supplier['id'],
'merchant_bank_id' => $params['merchant_bank_id'],
'amount' => $params['amount'],
'is_check' => 0,
'is_arrival' => 0,
'admin_id' => 0,
'create_time' => time(),
];
$supplier->mer_money =bcsub($supplier->mer_money, $params['amount'], 2);
$supplier->save();
(new MerchantWithdraw)->save($save_data);
return $this->success('提现成功,等待管理员审核');
// }else{
// return $this->fail('未找到商户或供应商信息');
// }
}
public function index()
{
// $uid = $this->request->userInfo['user_id'];
// $merchant = Merchant::where('uid',$uid)->findOrEmpty();
// $supplier = Supplier::where('uid',$uid)->findOrEmpty();
$supplier = $this->request->userInfo['supplier'];
if (!$supplier) {
return $this->fail('当前用户非供应商');
}
if ($supplier->isEmpty()) {
return $this->fail('当前用户非供应商');
}
// if(!$merchant->isEmpty()){
// $total_amount = MerchantWithdraw::where('mer_id',$merchant['mer_id'])->where('is_check',1)->where('is_arrival',1)->sum('amount');
// $count = MerchantWithdraw::where('mer_id',$merchant['mer_id'])->where('is_check',1)->where('is_arrival',1)->count();
// return $this->success('请求成功',['total'=>$total_amount,'count'=>$count]);
// } elseif (!$supplier->isEmpty()){
$total_amount = MerchantWithdraw::where('supplier_id', $supplier['id'])->where('is_check', 1)->where('is_arrival', 1)->sum('amount');
$count = MerchantWithdraw::where('supplier_id', $supplier['id'])->where('is_check', 1)->where('is_arrival', 1)->count();
return $this->success('请求成功', ['total' => $total_amount, 'count' => $count]);
// }else{
// return $this->fail('未找到商户或供应商信息');
// }
}
//提现列表
public function lists()
{
return $this->dataLists(new MerchantWithdrawLists());
}
}