80 lines
3.5 KiB
PHP
80 lines
3.5 KiB
PHP
<?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'] == 200) {
|
||
$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));
|
||
}
|
||
} |