订单、纠纷

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,7 +1,14 @@
<?php
namespace App\Models\Api\Order;
use App\Bean\Model\Api\Order\CustomerUserPostOrderDisputeBean;
use App\Exceptions\ModelException;
use App\Models\Api\Base\ApiBaseModel;
use App\Models\Api\Wallet\CustomerWalletBalanceTransactionModel;
use App\Tools\Logs;
use App\Tools\Times;
use Illuminate\Support\Facades\DB;
use Spatie\FlareClient\Api;
class CustomerUserPostOrderDisputeModel extends ApiBaseModel
@ -16,9 +23,14 @@ class CustomerUserPostOrderDisputeModel extends ApiBaseModel
'seller_submit_datetime',
'buyer_submit_datetime',
'result_status',
'buyer_uid',
'buyer_pay_dispute_status',
'buyer_pay_amount',
'buyer_pay_transaction_id',
'buyer_dispute_fee_pay_status',
'buyer_dispute_fee_amount',
'buyer_dispute_fee_pay_transaction_id',
'seller_uid',
'seller_pay_dispute_status',
'seller_pay_amount',
'seller_pay_transaction_id',
@ -29,4 +41,240 @@ class CustomerUserPostOrderDisputeModel extends ApiBaseModel
'created_at',
'updated_at',
];
const STATUS_NO_DISPUTE = 1;
const STATUS_WAITING = 2;
const STATUS_PROCESSING = 3;
const STATUS_FINISH = 4;
const STATUS_CANCEL = 5;
const STATUS = [
self::STATUS_NO_DISPUTE => '无纠纷',
self::STATUS_WAITING => '待处理',
self::STATUS_PROCESSING => '处理中',
self::STATUS_FINISH => '处理完成',
self::STATUS_CANCEL => '取消',
];
const RESULT_STATUS_SELLER_RESPLITY = 1;
const RESULT_STATUS_BUYER_RESPLITY = 2;
const RESULT_STATUS_EACH_RESPLITY = 3;
const RESULT_STATUS_OVERRULE = 4;
const RESULT_STATUS = [
self::RESULT_STATUS_SELLER_RESPLITY => '卖家责任',
self::RESULT_STATUS_BUYER_RESPLITY => '买家责任',
self::RESULT_STATUS_EACH_RESPLITY => '双方责任',
self::RESULT_STATUS_OVERRULE => '驳回',
];
const PAY_DISPUTE_STATUS_WAITING = 1;
const PAY_DISPUTE_STATUS_SUCCESS = 2;
const PAY_DISPUTE_STATUS_FAIL = 3;
const PAY_DISPUTE_STATUS = [
self::PAY_DISPUTE_STATUS_WAITING => '待处理',
self::PAY_DISPUTE_STATUS_SUCCESS => '成功',
self::PAY_DISPUTE_STATUS_FAIL => '失败',
];
const BUYER_DISPUTE_FEE_PAY_STATUS_NO = 1;
const BUYER_DISPUTE_FEE_PAY_STATUS_YES = 2;
const BUYER_DISPUTE_FEE_PAY_STATUS = [
self::BUYER_DISPUTE_FEE_PAY_STATUS_NO => '未支付',
self::BUYER_DISPUTE_FEE_PAY_STATUS_YES => '已支付',
];
//纠纷管理员接单
public function disputeAdminAccept($dispute_id, $admin_uid, $is_change_admin = false): bool|int
{
try {
$datetimeNow = Times::getNowDateTime();
DB::beginTransaction();
$resModel = $this->findItem($dispute_id);
if (!$resModel) throw new ModelException('dispute not found');
if ($resModel->status != self::STATUS_WAITING) throw new ModelException('dispute status error');
if (!empty($resModel->admin_uid) && !$is_change_admin) throw new ModelException('already have admin');
$updateItem = [
'id' => $resModel->id,
'status' => self::STATUS_PROCESSING,
'admin_uid' => $admin_uid,
'updated_at' => $datetimeNow,
];
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('update dispute status 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;
}
}
//创建纠纷
public function createDispute($post_order_id)
{
try {
$datetimeNow = Times::getNowDateTime();
DB::beginTransaction();
$oCustomerUserPostOrderModel = new CustomerUserPostOrderModel();
$resCustomerUserPostOrderModel = $oCustomerUserPostOrderModel->findItem($post_order_id);
if (!$resCustomerUserPostOrderModel) throw new \Exception('order not found');
if ($resCustomerUserPostOrderModel->pay_status != CustomerUserPostOrderModel::PAY_STATUS_PAID) throw new ModelException('order already in dispute');
if ($resCustomerUserPostOrderModel->dispute_status != CustomerUserPostOrderModel::DISPUTE_STATUS_UN_DISPUTE) throw new ModelException('order already in dispute');
//更新订单纠纷状态
$updateItem = [
'id' => $resCustomerUserPostOrderModel->id,
'dispute_status' => CustomerUserPostOrderModel::DISPUTE_STATUS_WAITING,
'updated_at' => $datetimeNow,
];
$res = $oCustomerUserPostOrderModel->updateItem($updateItem);
if (!$res) throw new ModelException('update order dispute status fail');
//创建纠纷
$bean = new CustomerUserPostOrderDisputeBean();
$bean->setBuyerUid($resCustomerUserPostOrderModel->buyer_uid);
$bean->setSellerUid($resCustomerUserPostOrderModel->seller_uid);
$bean->setPostOrderId($post_order_id);
$bean->setStatus(self::STATUS_WAITING);
$bean->setBuyerDisputeFeePayStatus(self::BUYER_DISPUTE_FEE_PAY_STATUS_NO);
//@@计算纠纷申诉费用
$bean->setBuyerDisputeFeeAmount(5); //默认5u
$bean->setCreatedAt($datetimeNow);
$bean->setUpdatedAt($datetimeNow);
$resModel = $this->addItem($bean->toArrayNotNull());
if (!$resModel) throw new ModelException('create dispute fail');
//创建聊天群组
$chat_id = $this->createChatGroupForDispute($resModel->id);
if (!$chat_id) throw new ModelException('create chat group fail');
$updateItem = [
'id' => $resModel->id,
'chat_group_id' => $chat_id,
];
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('update chat group id fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return $resModel->id;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
return false;
}
}
//@@创建聊天群组用于纠纷
function createChatGroupForDispute($dispute_id)
{
return 1;
}
//纠纷判罚
public function disputePenalty($dispute_id, $result_status, $buyer_amount, $seller_amount, $admin_uid, $admin_remark): bool|int
{
try {
if (
!in_array(
$result_status,
[self::RESULT_STATUS_SELLER_RESPLITY, self::RESULT_STATUS_BUYER_RESPLITY, self::RESULT_STATUS_EACH_RESPLITY, self::RESULT_STATUS_OVERRULE]
)
) throw new ModelException('result_status error');
$datetimeNow = Times::getNowDateTime();
DB::beginTransaction();
$resModel = $this->findItem($dispute_id);
if (!$resModel) throw new ModelException('dispute not found');
if ($resModel->status != self::STATUS_PROCESSING) throw new ModelException('dispute status error');
if ($resModel->admin_uid != $admin_uid) throw new ModelException('admin_uid error');
$oCustomerUserPostOrderModel = new CustomerUserPostOrderModel();
$resCustomerUserPostOrderModel = $oCustomerUserPostOrderModel->findItem($resModel->post_order_id);
if (!$resCustomerUserPostOrderModel) throw new ModelException('order not found');
if ($resCustomerUserPostOrderModel->dispute_status != CustomerUserPostOrderModel::DISPUTE_STATUS_PROCESSING) throw new ModelException('order dispute status error');
//更新订单状态
$updateItem = [
'id' => $resCustomerUserPostOrderModel->id,
'dispute_status' => CustomerUserPostOrderModel::DISPUTE_STATUS_FINISH,
'dispute_result_status' => $result_status,
'updated_at' => $datetimeNow,
];
$res = $oCustomerUserPostOrderModel->updateItem($updateItem);
if (!$res) throw new ModelException('update order dispute status fail');
//执行金额处理
$oCustomerWalletBalanceTransactionModel = new CustomerWalletBalanceTransactionModel();
$buyer_pay_status = self::PAY_DISPUTE_STATUS_SUCCESS;
$seller_pay_status = self::PAY_DISPUTE_STATUS_SUCCESS;
//执行买家
$resBuyerTrsId = $this->changeUserAmountDispute(
$resCustomerUserPostOrderModel->buyer_uid,
$resCustomerUserPostOrderModel->buyer_currency_code,
$buyer_amount,
$resModel->id,
);
if (!$resBuyerTrsId) {
$buyer_pay_status = self::PAY_DISPUTE_STATUS_FAIL;
$this->changeUserAmountNotEnough($resCustomerUserPostOrderModel->buyer_uid, $resCustomerUserPostOrderModel->buyer_currency_code, $buyer_amount);
}
//执行卖家
$resSellerTrsId = $this->changeUserAmountDispute(
$resCustomerUserPostOrderModel->seller_uid,
$resCustomerUserPostOrderModel->seller_currency_code,
$seller_amount,
$resModel->id,
);
if (!$resSellerTrsId) {
$seller_pay_status = self::PAY_DISPUTE_STATUS_FAIL;
$this->changeUserAmountNotEnough($resCustomerUserPostOrderModel->seller_uid, $resCustomerUserPostOrderModel->seller_currency_code, $seller_amount);
}
//更新纠纷
$updateItem = [
'status' => self::STATUS_FINISH,
'result_status' => $result_status,
'admin_remark' => $admin_remark,
'updated_at' => $datetimeNow,
'buyer_pay_amount' => $buyer_amount,
'buyer_pay_dispute_status' => $buyer_pay_status,
'seller_pay_amount' => $seller_amount,
'seller_pay_dispute_status' => $seller_pay_status,
];
if ($buyer_pay_status == self::PAY_DISPUTE_STATUS_SUCCESS) $updateItem['buyer_pay_transaction_id'] = $resBuyerTrsId;
if ($seller_pay_status == self::PAY_DISPUTE_STATUS_SUCCESS) $updateItem['seller_pay_transaction_id'] = $resSellerTrsId;
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('update dispute status 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 changeUserAmountNotEnough($uid, $currency_code, $amount, $remark = '')
{
//@@冻结用户提款功能
//@@提交到定期扣款任务
}
//纠纷金额处理
function changeUserAmountDispute($uid, $currency_code, $amount, $dispute_id, $remark = ''): bool|int
{
if ($amount == 0) return true;
$oCustomerWalletBalanceTransactionModel = new CustomerWalletBalanceTransactionModel();
if ($amount > 0) {
$res = $oCustomerWalletBalanceTransactionModel->typeDisputeAdd($uid, $currency_code, $amount, $dispute_id, $remark);
} else {
$res = $oCustomerWalletBalanceTransactionModel->typeDisputeSub($uid, $currency_code, $amount, $dispute_id, $remark);
}
return $res;
}
}

View File

@ -1,8 +1,15 @@
<?php
namespace App\Models\Api\Order;
use App\Bean\Model\Api\Order\CustomerUserPostOrderBean;
use App\Const\Prefix;
use App\Exceptions\ModelException;
use App\Models\Api\Base\ApiBaseModel;
use Spatie\FlareClient\Api;
use App\Models\Api\Post\PostHistoryModel;
use App\Models\Api\Wallet\CustomerWalletBalanceTransactionModel;
use App\Tools\Logs;
use App\Tools\Times;
use App\Tools\Tools;
class CustomerUserPostOrderModel extends ApiBaseModel
{
@ -19,7 +26,6 @@ class CustomerUserPostOrderModel extends ApiBaseModel
'amount',
'buyer_wallet_transaction_id',
'seller_wallet_transaction_id',
'wallet_checkbook_id',
'dispute_status',
'dispute_result_status',
'created_at',
@ -28,7 +34,14 @@ class CustomerUserPostOrderModel extends ApiBaseModel
const PAY_STATUS_UNPAID = 1;
const PAY_STATUS_PAID = 2;
const PAY_STATUS_CANCEL = 3;
const PAY_STATUS_PAID_FAIL = 3;
const PAY_STATUS_CANCEL = 4;
const PAY_STATUS = [
self::PAY_STATUS_UNPAID => '未支付',
self::PAY_STATUS_PAID => '已支付',
self::PAY_STATUS_PAID_FAIL => '支付失败',
self::PAY_STATUS_CANCEL => '取消',
];
const DISPUTE_STATUS_UN_DISPUTE = 1;
const DISPUTE_STATUS_WAITING = 2;
@ -42,12 +55,48 @@ class CustomerUserPostOrderModel extends ApiBaseModel
self::DISPUTE_STATUS_FINISH => '申诉完成',
];
const DISPUTE_RESULT_STATUS_NO_MISTAKE = 1;
const DISPUTE_RESULT_STATUS_HAVE_MISTAKE = 2;
const DISPUTE_RESULT_STATUS = [
self::DISPUTE_RESULT_STATUS_NO_MISTAKE => '无过错',
self::DISPUTE_RESULT_STATUS_HAVE_MISTAKE => '有过错',
];
//订单购买
public function buy($buyer_uid, $seller_id,$currency_code, $amount, $post_id): bool|int
{
try {
$sn = Tools::genSnowflakeId(Prefix::ORDER_SN_PREFIX);
$datetimeNow = Times::getNowDateTime();
$bean = new CustomerUserPostOrderBean();
$bean->setAmount(abs($amount));
$bean->setSn($sn);
$bean->setPayStatus(self::PAY_STATUS_UNPAID);
$bean->setBuyerUid($buyer_uid); //买家
$bean->setSellerUid($seller_id); //卖家
$bean->setPid($post_id);
$bean->setBuyerCurrencyCode($currency_code); //买家
$bean->setSellerCurrencyCode($currency_code); //卖家
$bean->setDisputeStatus(self::DISPUTE_STATUS_UN_DISPUTE); //纠纷状态
$bean->setCreatedAt($datetimeNow);
$bean->setUpdatedAt($datetimeNow);
//查找post_history_id做快照
$oPostHistoryModel = new PostHistoryModel();
$resPostHistoryModel = $oPostHistoryModel->findLastPostId($post_id,['id']);
if(!$resPostHistoryModel) throw new ModelException('post_history_id not found');
$bean->setPostHistoryId($resPostHistoryModel->id);
$res = $this->addItem($bean->toArray());
if(!$res) throw new ModelException('order add fail');
$bean->setId($res->id);
//进行支付
$oCustomerWalletBalanceTransactionModel = new CustomerWalletBalanceTransactionModel();
$res = $oCustomerWalletBalanceTransactionModel->typeOrderPay($bean); //订单支付内部会进行order状态的更新
if(!$res) throw new ModelException('order pay fail');
Logs::SuccLog(__FUNCTION__, func_get_args());
return $bean->getId();
}catch (\Exception $e){
Logs::ErrLog(__FUNCTION__ . ' ' . 'catchErr', $e, func_get_args());
// throw new ModelException($e->getMessage());
return false;
}
}
}