Files
cycle_api/app/Models/Api/Order/CustomerUserPostOrderModel.php
2024-03-11 02:21:52 +08:00

103 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 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
{
protected $table = 'customer_user_post_order';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'sn',
'pay_status',
'buyer_uid',
'seller_uid',
'pid',
'post_history_id',
'amount',
'buyer_wallet_transaction_id',
'seller_wallet_transaction_id',
'dispute_status',
'dispute_result_status',
'created_at',
'updated_at',
];
const PAY_STATUS_UNPAID = 1;
const PAY_STATUS_PAID = 2;
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;
const DISPUTE_STATUS_PROCESSING = 3;
const DISPUTE_STATUS_FINISH = 4;
const DISPUTE_STATUS = [
self::DISPUTE_STATUS_UN_DISPUTE => '未申诉',
self::DISPUTE_STATUS_WAITING => '申诉待处理',
self::DISPUTE_STATUS_PROCESSING => '申诉处理中',
self::DISPUTE_STATUS_FINISH => '申诉完成',
];
//订单购买
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;
}
}
}