67 lines
2.8 KiB
PHP
67 lines
2.8 KiB
PHP
<?php
|
||
|
||
namespace app\cmd;
|
||
|
||
use app\model\Order;
|
||
use app\model\Sign;
|
||
use app\service\AgreementService;
|
||
use think\Collection;
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\facade\Log;
|
||
|
||
class PayOrderRetry extends Command
|
||
{
|
||
protected int $count = 0;
|
||
protected int $successCount = 0;
|
||
protected int $failedCount = 0;
|
||
|
||
protected function configure()
|
||
{
|
||
$this->setName('PayOrderRetry')->setDescription('重试扣款失败的订单');
|
||
|
||
}
|
||
|
||
/**
|
||
* 所有失败订单每天重试一次,重试到订单创建时间的月底。如果还是失败就进行解约操作
|
||
* 每天11点执行
|
||
* @param Input $input
|
||
* @param Output $output
|
||
* @return void
|
||
*/
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
// 本月内的支付失败订单
|
||
// 获取本月的开始时间
|
||
$startOfMonth = new \DateTime('first day of this month 00:00:00');
|
||
$startOfMonthFormatted = $startOfMonth->format('Y-m-d H:i:s');
|
||
|
||
// 获取本月的结束时间
|
||
$endOfMonth = new \DateTime('last day of this month 23:59:59');
|
||
$endOfMonthFormatted = $endOfMonth->format('Y-m-d H:i:s');
|
||
|
||
Order::where(['pay_status' => Order::PAY_STATUS_FAIL])->whereBetweenTime('create_time', strtotime($startOfMonthFormatted), strtotime($endOfMonthFormatted))
|
||
->where(['is_retry' => Order::RETRY_STATUS_NO])
|
||
->field('id,order_number,agreement_id,user_id')->chunk(100, function (Collection $orderCollection) use (&$output) {
|
||
// 拉取支付失败的订单
|
||
foreach ($orderCollection as $order) {
|
||
// 查询签约状态是否取消
|
||
$signInfo = Sign::getByAgreementId($order->agreement_id);
|
||
if ($signInfo->isEmpty() || $signInfo->sign_status != Order::STATUS_SIGNED) {
|
||
continue;
|
||
}
|
||
$res = AgreementService::RetryPayOrder($order->order_number, $signInfo->agreement_id);
|
||
if ($res['respCode'] == 1000) {
|
||
$this->successCount += 1;
|
||
$output->writeln("受理成功,等待支付通知");
|
||
} else {
|
||
$this->failedCount += 1;
|
||
$output->writeln("用户" . $order->user_id . "扣款失败:" . $res['respMsg']);
|
||
}
|
||
}
|
||
});
|
||
Log::info(sprintf("扣款job执行情况:执行成功,累计 %s, 受理成功 %s, 失败 %s", $this->count, $this->successCount, $this->failedCount));
|
||
$output->writeln(sprintf("重试扣款失败的订单执行成功,累计 %s, 受理成功 %s, 失败 %s", $this->count, $this->successCount, $this->failedCount));
|
||
}
|
||
} |