p_ysk/app/command/QuerySmsStatus.php

173 lines
7.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\command;
use app\model\AlibabaSms;
use app\model\ShopInfo;
use support\Log;
use support\think\Db;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use ZipArchive;
// 查询短信发送状态
class QuerySmsStatus extends Command
{
protected static $defaultName = 'querysmsstatus';
protected static $defaultDescription = 'querysmsstatus';
/**
* @return void
*/
protected function configure()
{
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
}
/**
* 查询短信发送状态
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
// 一条短信的价格
$sms_price = Db::table('sys_params')->where(['param_code' => 'sms_fee'])->column('param_value');
if(!$sms_price) {
Log::info('短信价格未定,停止发送');
return false;
}else {
$sms_price = $sms_price[0];
}
$push_resp = '';
// 生日有礼
$bir_list = Db::table('mk_birthday_gift_record')->where(['push_status' => 'await_push'])->select()->toArray();
if($bir_list) {
Log::info('生日有礼短信发送状态查询开始->' . json_encode($bir_list));
foreach ($bir_list as $k => $value) {
$data = [
'phoneNumber' => $value['phone'],
'bizId' => $value['biz_id'],
'sendDate' => date('Ymd'),
'pageSize' => 10,
'currentPage' => 1,
];
$res = AlibabaSms::QuerySendDetails($data);
try {
Db::startTrans();
if($res['Code'] == 'OK') {
if($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 3) {
$push_status = 'success';
// 扣掉余额
ShopInfo::moeny($sms_price, $value['main_shop_id'], '生日有礼短信发送任务ID:' . $value['id'], 2);
}elseif ($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 2) {
$push_resp = $res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['ErrCode'];
$push_status = 'failed';
}elseif ($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 1) {
$push_status = 'await_push';
}
$update_data = [
'update_time' => date('Y-m-d H:i:s'),
'push_status' => $push_status,
'push_resp' => $push_resp,
];
$update_status = Db::table('mk_birthday_gift_record')->where(['id' => $value['id']])->update($update_data);
Log::info('生日有礼短信查询完毕数据库更新结果---》[ID]' . $value['id'] . '更新结果-->' . $update_status . '-->更新数据 . ' . json_encode($update_data));
}
Db::commit();
}catch (\Exception $e) {
Db::rollback();
Log::info('生日有礼短信查询错误---》[ID]' . $e->getMessage());
}
sleep(1);
}
}else {
Log::info('没有生日有礼短信状态查询的数据');
}
// 营销短信发送结果查询
$bir_list = Db::table('sms_push_event_record')->where(['status' => 1])->select()->toArray();
if($bir_list) {
Log::info('营销短信发送状态查询开始->' . json_encode($bir_list));
foreach ($bir_list as $k => $value) {
$data = [
'bizId' => $value['biz_id'],
'phoneNumber' => $value['user_phone'],
'sendDate' => date('Ymd'),
'pageSize' => 10,
'currentPage' => 1,
];
$res = AlibabaSms::QuerySendDetails($data);
try {
Db::startTrans();
if($res['Code'] == 'OK') {
if($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 3) {
$push_status = 2;
// 扣掉余额
ShopInfo::moeny($sms_price, $value['shop_id'], '营销短信发送任务ID' . $value['event_id'], 2);
}elseif ($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 2) {
$push_resp = $res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['ErrCode'];
$push_status = -1;
}elseif ($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 1) {
$push_status = 1;
}
$update_data = [
'status' => $push_status,
'error' => $push_resp,
];
$update_status = Db::table('sms_push_event_record')->where(['id' => $value['id']])->update($update_data);
Log::info('营销短信查询完毕数据库更新结果---》[ID]' . $value['id'] . '更新结果-->' . $update_status . '-->更新数据 . ' . json_encode($update_data));
}
Db::commit();
}catch (\Exception $e) {
Db::rollback();
Log::info('短信查询错误---》[ID]' . $e->getMessage());
}
sleep(1);
}
}else {
Log::info('没有营销短信状态查询的数据');
}
// 营销短信发送失败成功统计
$sms_push_event_list = Db::name('sms_push_event')->where(['status' => 1])->field('id')->select()->toArray();
if($sms_push_event_list) {
Log::info('营销短信发送失败成功统计开始->' . json_encode($sms_push_event_list));
foreach ($sms_push_event_list as $k => $v) {
$result = Db::table('sms_push_event_record')
->where('event_id', $v['id']) // 统一筛选 event_id=2 的数据
->where('type', 'sms')
->field([
// 1. event_id=2 的实际人数
'COUNT(*) AS total_count',
// 成功人数
'COUNT(CASE WHEN status = 1 THEN 1 END) AS success_count',
// 失败人数
'COUNT(CASE WHEN status = -1 THEN 1 END) AS fail_count'
])
->find(); // 单条结果(返回关联数组)
// 如果成功人数和失败人数等于实际人数,代表发送任务已经完成
if(($result['success_count'] + $result['fail_count']) >= $result['total_count']) {
Log::info('发送任务【' .$v['id']. '】已经完成-> 成功' . $result['success_count'] . ' 失败' . $result['fail_count']);
$update = [
'status' => 2,
'actual_num' => $result['total_count'],
'success_num' => $result['success_count'],
];
Db::name('sms_push_event')->where(['id' => $v['id']])->update($update);
}
}
}else {
Log::info('没有营销短信发送统计数据');
}
return self::SUCCESS;
}
}