cmbYouku_Api/app/cmd/AgreementPay.php

64 lines
2.7 KiB
PHP
Raw Permalink 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 app\service\PayService;
use think\Collection;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
class AgreementPay extends Command
{
protected int $count = 0;
protected int $successCount = 0;
protected int $failedCount = 0;
protected function configure()
{
$this->setName('agreementPay')
->setDescription('招商银行协议扣款');
}
protected function execute(Input $input, Output $output)
{
Sign::where(['sign_status' => Sign::SIGN_STATUS_SUCCESS])->field('*')->chunk(50, function (Collection $signCollection) use (&$output) {
foreach ($signCollection as $collection) {
// 查询是否已经生成过订单
$order = Order::getPaidOrderByAgreementId($collection->m_agreement_id);
if ($order->isEmpty() || empty($collection->agree_recharge_time) || $order->order_status != Order::STATUS_RECHARGE_SUCCESS) {
continue;
}
// 该协议号扣款订单次数
$orderCount = Order::getCountByAgreementId($collection->m_agreement_id);
if ($orderCount == 12) { // 1年扣款12次
continue;
}
$oneYearAgo = strtotime("-1 year", time());
if ($oneYearAgo > strtotime($collection->create_time)) { // 签约时间超过一年
continue;
}
$currentTimestamp = strtotime(date('Y-m-d'));
$agreeRechargeTime = $collection->agree_recharge_time;
$day = env('cmb.agree_pay_day');
if ($currentTimestamp < $agreeRechargeTime + $day * 24 * 60 * 60 ) { //未到扣款时间
continue;
}
$this->count += 1;
$res = AgreementService::agreementPay($collection, $order);
if ($res['respCode'] == 1000) {
$this->successCount += 1;
$output->writeln("受理成功,等待支付通知");
} else {
$this->failedCount += 1;
$output->writeln("用户".$collection->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));
}
}