订单、纠纷

This commit is contained in:
cano
2024-03-11 02:21:52 +08:00
parent 6d242ae973
commit 1161f06aeb
15 changed files with 855 additions and 53 deletions

View File

@ -1,27 +0,0 @@
<?php
namespace App\Models\Api\Wallet;
use App\Models\Api\Base\ApiBaseModel;
use Spatie\FlareClient\Api;
class CustomerPostOrderWalletCheckbookModel extends ApiBaseModel
{
protected $table = 'customer_post_order_wallet_checkbook';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'post_order_id',
'type',
'status',
'pay_uid',
'receive_uid',
'amount',
'currency_id',
'currency_code',
'pay_time',
'created_at',
'updated_at',
'exec_time',
'remark',
];
}

View File

@ -30,6 +30,7 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
'remark',
'target_uid',
'target_post_order_id',
'target_post_order_dispute_id',
'sign',
'callback_time',
'delay_payment_time',
@ -52,6 +53,8 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
const TYPE_ADMIN_SUB = 13;
const TYPE_TRANSFER_TO_USER_DEC = 14;
const TYPE_TRANSFER_RECEIVE_ADD = 15;
const TYPE_ORDER_DISPUTE_FEE_DEC = 16;
const TYPE_ORDER_DISPUTE_FEE_ADD = 17;
const TYPE = [
self::TYPE_RECHARGE_ADD => '充值',
self::TYPE_WITHDRAW_DEC => '提现',
@ -68,6 +71,8 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
self::TYPE_ADMIN_SUB => '管理员扣款',
self::TYPE_TRANSFER_TO_USER_DEC => '转账给用户',
self::TYPE_TRANSFER_RECEIVE_ADD => '转账收款',
self::TYPE_ORDER_DISPUTE_FEE_DEC => '纠纷仲裁费用扣除',
self::TYPE_ORDER_DISPUTE_FEE_ADD => '纠纷仲裁费用退还',
];
const STATUS_WAIT = 1;
@ -90,6 +95,117 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
self::SOURCE_PARAMS_SECURITY => '保证金',
];
function getDelayPayList($datetime, $col = ['*']): \Illuminate\Database\Eloquent\Collection|array
{
return $this->newQuery()->where('status', self::STATUS_DELAY_PAYMENT)->where('delay_payment_time', '<=', $datetime)->limit(100)->get($col);
}
//纠纷仲裁费用退还
function typeOrderDisputeFeeAdd($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setType(self::TYPE_ORDER_DISPUTE_FEE_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('order dispute fee add fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
return false;
}
}
//纠纷仲裁费用扣除
function typeOrderDisputeFeeDec($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //账变负数
$bean->setType(self::TYPE_ORDER_DISPUTE_FEE_DEC);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) throw new ModelException('balance not enough');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('order dispute fee dec fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
return false;
}
}
//处理延期账变
function dealDelayPaymentTransactionCmd(): void
{
$datetimeNow = Times::getNowDateTime();
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
while (true) {
$resList = $this->getDelayPayList($datetimeNow, ['id']);
if ($resList->isEmpty()) {
break;
}
foreach ($resList as $itemId) {
try {
DB::beginTransaction();
$item = $this->findItem($itemId->id);
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findItem($item->wallet_id);
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$updateItem = [
'id' => $item->id,
'status' => self::STATUS_SUCCESS,
'updated_at' => Times::getNowDateTime(),
];
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('updateItem fail');
$res = $oCustomerWalletBalanceModel->unFrozenAmount($item->wallet_id, $item->amount);
if (!$res) throw new ModelException('unFrozenAmount fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
}
}
}
}
//转账给用户
function typeTransferToUser($uid, $target_uid, $currency_code, $amount, $remark = null): bool
{
@ -310,7 +426,7 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
}
//仲裁判罚扣除
function typeDisputeSub($uid, $currency_code, $amount, $remark = null): bool
function typeDisputeSub($uid, $currency_code, $amount, $target_post_order_dispute_id, $remark = null): bool|int
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
@ -319,6 +435,7 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
$bean->setType(self::TYPE_DISPUTE_SUB);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
$bean->setTargetPostOrderDisputeId($target_post_order_dispute_id);
try {
DB::beginTransaction();
//查询用户余额
@ -330,8 +447,8 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
$resModel = $this->addTransaction($bean);
if (!$resModel) throw new ModelException('addTransaction fail');
//变更余额
//检查余额是否足够
if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) { //从余额中扣除
@ -346,7 +463,7 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
if (!$res) throw new ModelException('dispute sub fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
return $resModel->id;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
@ -356,7 +473,7 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
}
//仲裁判罚增加
function typeDisputeAdd($uid, $currency_code, $amount, $remark = null): bool
function typeDisputeAdd($uid, $currency_code, $amount, $target_post_order_dispute_id, $remark = null): bool|int
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
@ -365,6 +482,7 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
$bean->setType(self::TYPE_DISPUTE_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
$bean->setTargetPostOrderDisputeId($target_post_order_dispute_id);
try {
DB::beginTransaction();
//查询用户余额
@ -375,15 +493,15 @@ class CustomerWalletBalanceTransactionModel extends ApiBaseModel
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
$resModel = $this->addTransaction($bean);
if (!$resModel) throw new ModelException('addTransaction fail');
//变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('dispute add fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
return $resModel->id;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());