cmbYouku_Api/app/cmd/PayOrderRetry.php

66 lines
2.8 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\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));
}
}