From 6f331a22d50c5158daf8fcb322884e6e46dfeefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=AE=8F?= <389838709@qq.com> Date: Fri, 5 Jul 2024 15:22:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A3=E6=AC=BE=E7=9F=AD=E4=BF=A1=E9=80=9A?= =?UTF-8?q?=E7=9F=A5Job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/cmd/AgreementPay.php | 2 + app/cmd/AgreementPaySendSms.php | 80 +++++++++++++++++++++++++++++++++ app/config/BusinessCacheKey.php | 5 +++ app/front/Order.php | 3 +- app/service/OrderService.php | 8 ++-- app/sms/AliSms.php | 2 + 6 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 app/cmd/AgreementPaySendSms.php diff --git a/app/cmd/AgreementPay.php b/app/cmd/AgreementPay.php index d7d0481..0279624 100644 --- a/app/cmd/AgreementPay.php +++ b/app/cmd/AgreementPay.php @@ -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)); } } \ No newline at end of file diff --git a/app/cmd/AgreementPaySendSms.php b/app/cmd/AgreementPaySendSms.php new file mode 100644 index 0000000..b6e0bfb --- /dev/null +++ b/app/cmd/AgreementPaySendSms.php @@ -0,0 +1,80 @@ +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)); + } +} \ No newline at end of file diff --git a/app/config/BusinessCacheKey.php b/app/config/BusinessCacheKey.php index b64f181..4e70309 100644 --- a/app/config/BusinessCacheKey.php +++ b/app/config/BusinessCacheKey.php @@ -34,4 +34,9 @@ class BusinessCacheKey 'ttl' => self::MINUTE * 5 ]; + const AGREEMENT_PAY_SEND_SMS = [ + 'key' => 'agreement_pay_send_sms', + 'ttl' => self::DAY * 2 + ]; + } \ No newline at end of file diff --git a/app/front/Order.php b/app/front/Order.php index 0f1ee20..f2ca03f 100644 --- a/app/front/Order.php +++ b/app/front/Order.php @@ -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 @@ -36,7 +37,7 @@ class Order extends Base { $params = $request->post(); $res = (new RechargeService())->rechargeNotify($params); - echo $res; + echo $res; exit; } diff --git a/app/service/OrderService.php b/app/service/OrderService.php index 3a5e8c5..4305436 100644 --- a/app/service/OrderService.php +++ b/app/service/OrderService.php @@ -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(); } /** @@ -33,9 +33,9 @@ class OrderService extends BaseService * @return array * @throws \think\db\exception\DbException */ - public function getMonthSale(): array + public function getMonthSale(): array { - $count = $this->model->getCountLastMonth(); - return ['count' => $count]; + $count = $this->model->getCountLastMonth(); + return ['count' => $count]; } } \ No newline at end of file diff --git a/app/sms/AliSms.php b/app/sms/AliSms.php index 7447339..29bde5d 100644 --- a/app/sms/AliSms.php +++ b/app/sms/AliSms.php @@ -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([]);