jingcai-php/app/Service/CustomerWalletService.php

759 lines
31 KiB
PHP
Executable File

<?php
/**
* @createtime 2023/4/23
* @author wild
* @copyright PhpStorm
*/
namespace App\Service;
use App\Enums\BillType;
use App\Enums\BoolEnum;
use App\Enums\FreezeType;
use App\Enums\OrderType;
use App\Enums\PayState;
use App\Enums\WithdrawState;
use App\Enums\WithdrawType;
use App\Exceptions\JingCaiException;
use App\Http\RequestValidators\BankValidator;
use App\Model\Config;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerBill;
use App\Model\Customer\CustomerFreeze;
use App\Model\Customer\CustomerRecharge;
use App\Model\Customer\CustomerWithdraw;
use App\Model\Order;
use App\Model\Seller\Seller;
use App\Model\Seller\Shop;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CustomerWalletService
{
/**
* 退回合买保底所需金额
* @param Order $order
* @param Float $backMoney
*/
public static function backUnionKeepMoney(Order $order, $backMoney)
{
$orderId = $order->id;
/** @var Customer $beforeCustomer */
$beforeCustomer = Customer::find($order->customer_id);
$money = $backMoney;
Customer::balanceIncr($order->customer_id, 0, $money);
$endCustomer = Customer::find($order->customer_id);
// 账单记录
$bill = new CustomerBill();
$bill->type = BillType::UNION_KEEP_BACK;
$bill->title = CustomerBill::getBillTitle(BillType::UNION_KEEP_BACK);
$bill->customer_id = $order->customer_id;
$bill->recharge_id = 0;
$bill->order_id = $orderId;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $money;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 购买
* @param Order $order
*/
public static function buyLottery(Order $order)
{
$orderId = $order->id;
if ($order->type == OrderType::UNION) {
$unionOrder = Order::find($order->pid);
$orderId = $unionOrder->id;
}
/** @var Customer $beforeCustomer */
$beforeCustomer = Customer::find($order->customer_id);
$money = $order->money;
if ($order->type == OrderType::UNION) {
$money = $order->union_money;
}
Customer::balanceReduce($order->customer_id, $money);
$endCustomer = Customer::find($order->customer_id);
// 账单记录
$bill = new CustomerBill();
$bill->type = BillType::BETTING;
$bill->title = CustomerBill::getBillTitle(BillType::BETTING);
$bill->customer_id = $order->customer_id;
$bill->recharge_id = 0;
$bill->order_id = $orderId;
$bill->ie = '-';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $money;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 用户充值
* @param CustomerRecharge $recharge
*/
public static function recharge(CustomerRecharge $recharge)
{
ThrowException::isTrue(!$recharge->isCompleted(), '充值单未成功');
$beforeCustomer = Customer::find($recharge->customer_id);
// 增加余额
Customer::balanceIncr($recharge->customer_id, 0, $recharge->pay_money);
$endCustomer = Customer::find($recharge->customer_id);
// 账单记录
$bill = new CustomerBill();
$bill->type = BillType::RECHARGE;
$bill->title = CustomerBill::getBillTitle(BillType::RECHARGE);
$bill->customer_id = $recharge->customer_id;
$bill->recharge_id = $recharge->id;
$bill->order_id = $recharge->order_id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $recharge->pay_money;
$bill->created_date = date('Ymd');
$bill->save();
}
public static function sellerRecharge(Seller $seller, $data)
{
$customerId = Arr::get($data, 'customer_id');
ThrowException::isTrue($customerId < 1, '请选择用户');
$beforeCustomer = Customer::where('shop_id', $seller->shop_id)->find($customerId);
ThrowException::isTrue(!$beforeCustomer, '用户不存在');
$money = floatval(Arr::get($data, 'money'));
ThrowException::isTrue($money <= 0, '充值金额需要大于0');
$remark = strval(Arr::get($data, 'remark'));
$remark_img_path = strval(Arr::get($data, 'remark_img_path'));
DB::beginTransaction();
try {
// 增加余额
Customer::balanceIncr($customerId, 0, $money);
$endCustomer = Customer::find($customerId);
// 账单记录
$bill = new CustomerBill();
$bill->type = BillType::RECHARGE;
$bill->title = CustomerBill::getBillTitle(BillType::RECHARGE);
$bill->customer_id = $customerId;
$bill->recharge_id = 0;
$bill->order_id = 0;
$bill->seller_id = $seller->id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->remark = $remark;
$bill->remark_img_path = $remark_img_path;
$bill->money = $money;
$bill->created_date = date('Ymd');
$bill->save();
DB::commit();
} catch (JingCaiException $exception) {
DB::rollBack();
throw $exception;
} catch (\Exception $exception) {
DB::rollBack();
throw $exception;
}
}
/**
* 代理用户提取的佣金
* @param CustomerRecharge $recharge
*/
public static function agentBrokerage(Order $order, $customerId, $sellerId, $shopBillId, $agentBrokerage)
{
$beforeCustomer = Customer::find($customerId);
// 增加余额
Customer::balanceIncr($customerId, $agentBrokerage, 0);
$endCustomer = Customer::find($customerId);
// 账单记录
$bill = new CustomerBill();
$bill->type = BillType::AGENT;
$bill->title = CustomerBill::getBillTitle(BillType::AGENT);
$bill->customer_id = $customerId;
$bill->recharge_id = 0;
$bill->order_id = $order->id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $agentBrokerage;
$bill->shop_bill_id = $shopBillId;
$bill->seller_id = $sellerId;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 订单撤销,退款
* @param Order $order
* @param Seller $seller
*/
public static function refundForRevokeOrder(Order $order, Seller $seller)
{
if ($order->type == OrderType::UNION) {
self::refundForRevokeUnionOrder($order, $seller);
return;
}
$betBill = CustomerBill::where('order_id', $order->id)
->where('type', BillType::BETTING)
->where('customer_id', $order->customer_id)
->first();
ThrowException::isTrue(!$betBill, '无购买记录,无法撤销');
$moneyWithdraw = $betBill->begin_balance_withdraw - $betBill->end_balance_withdraw;
$moneyCash = $betBill->begin_balance_cash - $betBill->end_balance_cash;
if (!bc_equal($moneyWithdraw + $moneyCash,$order->money)) {
Log::error('撤单失败', [
'order_id' => $order->id,
'bill_id' => $betBill->id,
'$moneyWithdraw' => $moneyWithdraw,
'$moneyCash' => $moneyCash,
'union_money' => $order->union_money
]);
ThrowException::run('金额有误,无法撤销');
}
$beforeCustomer = Customer::find($order->customer_id);
// 增加余额
Customer::balanceIncr($order->customer_id, $moneyWithdraw, $moneyCash);
$endCustomer = Customer::find($order->customer_id);
$bill = new CustomerBill();
$bill->type = BillType::REFUND;
$bill->title = CustomerBill::getBillTitle(BillType::REFUND);
$bill->customer_id = $order->customer_id;
$bill->recharge_id = 0;
$bill->order_id = $order->id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $order->money;
$bill->seller_id = $seller->id;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 合买订单撤销退款
* @param Order $unionOrder
* @param Seller $seller
*/
private static function refundForRevokeUnionOrder(Order $unionOrder, Seller $seller)
{
$orders = Order::where('pid', $unionOrder->id)->where('pay_state', PayState::SUCCESS)->get();
foreach ($orders as $order) {
// $betBill = CustomerBill::where('order_id', $unionOrder->id)
// ->where('type', BillType::BETTING)
// ->where('customer_id', $order->customer_id)
// ->first();
// if (!$betBill) {
// Log::error('合买撤单失败', [
// 'union_order_id' => $unionOrder->id,
// 'order_id' => $order->id,
// 'error' => '无购买记录'
// ]);
// ThrowException::run('无购买记录,无法撤销');
// }
$moneyWithdraw = 0;
$moneyCash = $order->union_money;
// if (!bc_equal(Helps::floatFormat($moneyWithdraw + $moneyCash),Helps::floatFormat($order->union_money))) {
// Log::error('合买撤单失败', [
// 'union_order_id' => $unionOrder->id,
// 'order_id' => $order->id,
// 'bill_id' => $betBill->id,
// '$moneyWithdraw' => $moneyWithdraw,
// '$moneyCash' => $moneyCash,
// 'union_money' => $order->union_money
// ]);
// ThrowException::run('金额有误,无法撤销');
// }
$beforeCustomer = Customer::find($order->customer_id);
// 增加余额
Customer::balanceIncr($order->customer_id, $moneyWithdraw, $moneyCash);
$endCustomer = Customer::find($order->customer_id);
$bill = new CustomerBill();
$bill->type = BillType::REFUND;
$bill->title = CustomerBill::getBillTitle(BillType::REFUND);
$bill->customer_id = $order->customer_id;
$bill->recharge_id = 0;
$bill->order_id = $unionOrder->id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $order->union_money;
$bill->seller_id = $seller->id;
$bill->created_date = date('Ymd');
$bill->save();
}
}
/**
* 用户中奖
* @param Order $order
* @param $sendPrize
*/
public static function winPrize(Order $order, $sendPrize)
{
$orderId = $order->id;
// 合买参与者
if ($order->type == OrderType::UNION && $order->pid != $order->id) {
$unionOrder = Order::find($order->pid);
$orderId = $unionOrder->id;
}
$beforeCustomer = Customer::find($order->customer_id);
// 增加余额
Customer::balanceIncr($order->customer_id, $sendPrize, 0);
$endCustomer = Customer::find($order->customer_id);
$bill = new CustomerBill();
$bill->type = BillType::PRIZE;
$bill->title = CustomerBill::getBillTitle(BillType::PRIZE);
$bill->customer_id = $order->customer_id;
$bill->recharge_id = 0;
$bill->order_id = $orderId;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $sendPrize;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 跟单中奖,需要扣除佣金
* @param Seller $seller
* @param $customerId
* @param $money
* @param $remark
*/
public static function gendanReduceBrokerage(Order $order, $remark='')
{
$customerId = $order->customer_id;
$beforeCustomer = Customer::find($customerId);
$allBrokerage = $order->lottery_gendan_brokerage_all;
// 减少余额
Customer::balanceReduce($customerId, $allBrokerage);
$endCustomer = Customer::find($customerId);
$bill = new CustomerBill();
$bill->type = BillType::GENDAN_BROKERAGE;
$bill->title = CustomerBill::getBillTitle(BillType::GENDAN_BROKERAGE);
$bill->remark = strval($remark);
$bill->customer_id = $customerId;
$bill->recharge_id = 0;
$bill->order_id = $order->id;
$bill->ie = '-';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $allBrokerage;
$bill->seller_id = 0;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 店铺给用户加款
* @param Seller $seller
* @param $customerId
* @param $money
* @param $remark
*/
public static function sellerIncrBalance(Seller $seller, $customerId, $money, $remark)
{
$beforeCustomer = Customer::find($customerId);
// 增加余额
Customer::balanceIncr($customerId, 0, $money);
$endCustomer = Customer::find($customerId);
$bill = new CustomerBill();
$bill->type = BillType::SELLER_INCR;
$bill->title = CustomerBill::getBillTitle(BillType::SELLER_INCR);
$bill->remark = strval($remark);
$bill->customer_id = $customerId;
$bill->recharge_id = 0;
$bill->order_id = 0;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $money;
$bill->seller_id = $seller->id;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 合买佣金
* @param Order $order
* @param $unionBrokerage
*/
public static function unionBrokerage(Order $order, $unionBrokerage)
{
$beforeCustomer = Customer::find($order->customer_id);
// 增加余额
Customer::balanceIncr($order->customer_id, 0, $unionBrokerage);
$endCustomer = Customer::find($order->customer_id);
$bill = new CustomerBill();
$bill->type = BillType::UNION_BROKERAGE;
$bill->title = CustomerBill::getBillTitle(BillType::UNION_BROKERAGE);
$bill->customer_id = $order->customer_id;
$bill->recharge_id = 0;
$bill->order_id = $order->id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $unionBrokerage;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 发单人提取的佣金
*/
public static function fadanBrokerage(Order $order, $customerId, $sendPrize)
{
$allFadanBrokerage = $order->lottery_gendan_brokerage_all;
$fadanBrokerage = $allFadanBrokerage * Config::fadanUserFeeLv();
if ($fadanBrokerage <= 0) {
return;
}
$beforeCustomer = Customer::find($customerId);
// 增加余额
Customer::balanceIncr($customerId, 0, $fadanBrokerage);
$endCustomer = Customer::find($customerId);
$bill = new CustomerBill();
$bill->type = BillType::FADAN_BROKERAGE;
$bill->title = CustomerBill::getBillTitle(BillType::FADAN_BROKERAGE);
$bill->customer_id = $customerId;
$bill->recharge_id = 0;
$bill->order_id = $order->id;
$bill->ie = '+';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $fadanBrokerage;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 店铺给用户扣款
* @param Seller $seller
* @param $customerId
* @param $money
* @param $remark
*/
public static function sellerReduceBalance(Seller $seller, $customerId, $money, $remark)
{
$beforeCustomer = Customer::find($customerId);
// 减少余额
Customer::balanceReduce($customerId, $money);
$endCustomer = Customer::find($customerId);
$bill = new CustomerBill();
$bill->type = BillType::SELLER_REDUCE;
$bill->title = CustomerBill::getBillTitle(BillType::SELLER_REDUCE);
$bill->customer_id = $customerId;
$bill->remark = strval($remark);
$bill->recharge_id = 0;
$bill->order_id = 0;
$bill->ie = '-';
$bill->begin_balance = $beforeCustomer->balance;
$bill->begin_balance_withdraw = $beforeCustomer->balance_withdraw;
$bill->begin_balance_cash = $beforeCustomer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $beforeCustomer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $money;
$bill->seller_id = $seller->id;
$bill->created_date = date('Ymd');
$bill->save();
}
/**
* 菜名申请提现
* @param Customer $customer
* @param $data
* @throws \Exception
*/
public function withdrawMoney(Customer $customer, $data)
{
$type = Arr::get($data, 'type', 0);
$money = Arr::get($data, 'money', 0);
$ali_account = Arr::get($data, 'ali_account', '');
$bank_no = Arr::get($data, 'bank_no', '');
$bank_area = Arr::get($data, 'bank_area', '');
$bank_master = Arr::get($data, 'bank_master', '');
$bank_branch = Arr::get($data, 'bank_branch', '');
$password_pay = Arr::get($data, 'password_pay', '');
ThrowException::isTrue(!WithdrawType::hasValue($type, false), '不支持该提现方式');
ThrowException::isTrue(!$password_pay, '请填写支付密码');
$shop = Shop::find($customer->shop_id);
ThrowException::isTrue(!$shop->withdraw_type, '该店铺不支持提现');
ThrowException::isTrue(!in_array($type, $shop->withdraw_type), '该店铺不支持该提现方式');
if ($type == WithdrawType::ALI) {
ThrowException::isTrue(!$ali_account, '请输入支付宝账号');
}
if ($type == WithdrawType::BANK) {
$errors = BankValidator::hasErrors($data);
ThrowException::isTrue($errors, $errors);
}
DB::beginTransaction();
try {
$customer = Customer::lockForUpdate()->find($customer->id);
ThrowException::isTrue(!Customer::checkPassword($password_pay, $customer->password_pay), '支付密码错误');
ThrowException::isTrue($customer->balance_withdraw < $money, '可提现金额不足');
$customer->balance_withdraw -= $money;
$customer->balance_freeze += $money;
$customer->save();
$freeze = new CustomerFreeze();
$freeze->customer_id = $customer->id;
$freeze->type = FreezeType::WITHDRAW;
$freeze->money = $money;
$freeze->state = BoolEnum::YES;
$freeze->description = '';
$freeze->save();
$withdraw = new CustomerWithdraw();
$withdraw->shop_id = $customer->shop_id;
$withdraw->customer_id = $customer->id;
$withdraw->freeze_id = $freeze->id;
$withdraw->type = $type;
$withdraw->state = WithdrawState::PENDING;
$withdraw->money = $money;
$withdraw->ali_account = $ali_account;
$withdraw->bank_no = $bank_no;
$withdraw->bank_area = $bank_area;
$withdraw->bank_master = $bank_master;
$withdraw->bank_branch = $bank_branch;
$withdraw->save();
DB::commit();
} catch (JingCaiException $exception) {
DB::rollBack();
throw $exception;
} catch (\Exception $exception) {
DB::rollBack();
throw $exception;
}
}
/**
* 提现被审核
* @param Seller $seller
* @param $data
* @throws \Exception
*/
public function withdrawAudit(Seller $seller, $data)
{
$id = Arr::get($data, 'id');
$state = Arr::get($data, 'state');
$remark = Arr::get($data, 'remark');
ThrowException::isTrue($id < 1, '参数错误');
ThrowException::isTrue(!in_array($state, [WithdrawState::SUCCESS, WithdrawState::FAIL]), '状态错误');
DB::beginTransaction();
try {
$withdraw = CustomerWithdraw::lockForUpdate()->find($id);
ThrowException::isTrue(!$withdraw, '数据不存在');
ThrowException::isTrue($withdraw->state != WithdrawState::PENDING, '数据状态异常,不可操作');
$freeze = CustomerFreeze::lockForUpdate()->find($withdraw->freeze_id);
ThrowException::isTrue($freeze->state == BoolEnum::NO, '数据已被处理');
$customer = Customer::lockForUpdate()->find($withdraw->customer_id);
ThrowException::isTrue($customer->balance_freeze < $withdraw->money, '冻结金额不足');
$freeze->state = BoolEnum::NO;
$freeze->save();
if ($state == WithdrawState::SUCCESS) {
$customer->balance_freeze -= $withdraw->money;
$customer->save();
$endCustomer = Customer::find($customer->id);
$bill = new CustomerBill();
$bill->type = BillType::WITHDRAW;
$bill->title = CustomerBill::getBillTitle(BillType::WITHDRAW);
$bill->customer_id = $customer->id;
$bill->recharge_id = 0;
$bill->order_id = 0;
$bill->ie = '-';
$bill->begin_balance = $customer->balance;
$bill->begin_balance_withdraw = $customer->balance_withdraw;
$bill->begin_balance_cash = $customer->balance_cash;
$bill->end_balance = $endCustomer->balance;
$bill->end_balance_withdraw = $endCustomer->balance_withdraw;
$bill->end_balance_cash = $endCustomer->balance_cash;
$bill->begin_balance_freeze = $customer->balance_freeze;
$bill->end_balance_freeze = $endCustomer->balance_freeze;
$bill->money = $withdraw->money;
$bill->seller_id = $seller->id;
$bill->created_date = date('Ymd');
$bill->save();
} else if ($state == WithdrawState::FAIL) {
$customer->balance_freeze -= $withdraw->money;
$customer->balance_withdraw += $withdraw->money;
$customer->save();
}
$withdraw->state = $state;
$withdraw->seller_id = $seller->id;
$withdraw->seller_remark = strval($remark);
$withdraw->remark_at = date('Y-m-d H:i:s');
$withdraw->save();
DB::commit();
} catch (JingCaiException $exception) {
DB::rollBack();
throw $exception;
} catch (\Exception $exception) {
DB::rollBack();
throw $exception;
}
}
}