扣款短信通知Job

This commit is contained in:
陈俊宏 2024-07-05 15:22:57 +08:00
parent da8a512777
commit 6f331a22d5
6 changed files with 95 additions and 5 deletions

View File

@ -10,6 +10,7 @@ use think\Collection;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
class AgreementPay extends Command
{
@ -57,6 +58,7 @@ class AgreementPay extends Command
}
}
});
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));
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace app\cmd;
use app\config\BusinessCacheKey;
use app\model\Order;
use app\model\Sign;
use app\service\PayService;
use app\service\util\RedisService;
use app\sms\AliSms;
use think\Collection;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
class AgreementPaySendSms extends Command
{
protected int $count = 0;
protected int $successCount = 0;
protected int $failedCount = 0;
protected function configure()
{
$this->setName('agreementPaySendSms')
->setDescription('招商银行协议扣款前发送短信');
}
protected function execute(Input $input, Output $output)
{
$redis = RedisService::getRedisInstance();
Sign::where(['sign_status' => Sign::SIGN_STATUS_SUCCESS])->field('*')->chunk(50, function (Collection $signCollection) use (&$output, $redis) {
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;
}
// 使用redis判断消息短时间不会重复发送
$cacheKeys = BusinessCacheKey::AGREEMENT_PAY_SEND_SMS;
$cacheKey = $cacheKeys['key'] . "_" . $collection->mobile;
$key = $redis->get($cacheKey);
if (!empty($key)) {
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 - 1) * 24 * 60 * 60) { //未到发送短信的时间,只发送刚满30天的记录超过30天不再发送
continue;
}
if (empty($collection->mobile)) {
continue;
}
$this->count += 1;
// 开始发送扣款短信
$res = AliSms::sendSms(['phone_numbers' => $collection->mobile], 3);
$redis->set($cacheKey, true, 'ex', $cacheKeys['ttl']);
if ($res['code'] == "OK") {
$this->successCount += 1;
$output->writeln("短信发送受理成功");
} else {
$this->failedCount += 1;
$output->writeln("手机号" . $collection->mobile . "发送短信失败:" . $res['message']);
}
}
});
Log::info(sprintf("扣款前发送短信执行情况:执行成功,累计 %s, 受理成功 %s, 失败 %s", $this->count, $this->successCount, $this->failedCount));
$output->writeln(sprintf("执行成功,累计 %s, 受理成功 %s, 失败 %s", $this->count, $this->successCount, $this->failedCount));
}
}

View File

@ -34,4 +34,9 @@ class BusinessCacheKey
'ttl' => self::MINUTE * 5
];
const AGREEMENT_PAY_SEND_SMS = [
'key' => 'agreement_pay_send_sms',
'ttl' => self::DAY * 2
];
}

View File

@ -15,6 +15,7 @@ class Order extends Base
$params['user_id'] = $request->user_id;
return responseOk(app()->make(OrderService::class)->list($params));
}
/**
* 订单退订
* @param Request $request

View File

@ -25,7 +25,7 @@ class OrderService extends BaseService
public function list($params)
{
return $this->model->searchPages(['order_number', 'type', 'status', 'create_at', 'user_id'], $params)->toArray();
return $this->model->searchPages(['order_number', 'type', 'status', 'pay_status', 'create_at', 'user_id'], $params)->toArray();
}
/**

View File

@ -34,6 +34,8 @@ class AliSms
$template = env('alisms.template_sms_code');
if($type == 2) {
$template = env('alisms.template_issue_code');
} else if ($type == 3) {
$template = env('alisms.template_agreement_pay_code');
}
$client = self::getInstance(env('alisms.app_id'),env('alisms.app_key'));
$sms = new SendSmsRequest([]);