cxfoot/commands/Script2minController.php
2023-10-27 14:25:12 +08:00

185 lines
7.6 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\commands;
use app\components\sms\AliSmsMsgSender;
use app\models\BallCart;
use app\models\BallMark;
use app\models\Option;
use app\models\sms\SmsSetting;
use app\models\sms\SmsTpl;
use app\models\StoreUser;
use app\models\User;
use yii\console\Controller;
use yii\console\ExitCode;
use app\models\Store;
/**
* This command echoes the first argument that you have entered.
*
* This command is provided as an example for you to learn how to create console commands.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Script2minController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $message the message to be echoed.
* @return int Exit code
*/
public function actionIndex()
{
$redis_name = "cxaibc:console:script2min_lock";
try {
$get = \Yii::$app->redis->setnx($redis_name,1);
if(empty($get)){
$ttl = \Yii::$app->redis->ttl($redis_name);
if($ttl === -1){
\Yii::$app->redis->expire($redis_name,60);
}
var_dump("2分钟脚本正在执行中");
exit();
}
\Yii::$app->redis->expire($redis_name,60*10);
// 执行体
$this->ScriptBallTimeError();
// 结束执行
\Yii::$app->redis->del($redis_name);
}catch (\Exception $e){
\Yii::$app->redis->del($redis_name);
var_dump($e->getMessage());
}
echo "2分钟脚本";
exit();
}
/**
* 球车超出时长
*/
private function ScriptBallTimeError(){
try{
$qc_wgh_value = "qc_wgh"; // 球车超出时长报警
$find_option = Option::findOne([
'key' => $qc_wgh_value,
]);
if(empty($find_option) || empty($find_option->value)){
var_dump("0000---");
return false;
}
//判断是否在发送短信时间段
$redis_name = "api:cxaibc:send:send_time";
$send_time = \Yii::$app->redis->get($redis_name);
if(empty($send_time)){
var_dump("成功发送---");
return ['code' => 0,'msg' =>"成功发送"];
}
$send_time = explode(' - ',$send_time);
$send_time_arr0 = explode(':',$send_time[0]);
$send_time_arr1 = explode(':',$send_time[1]);
$begin=mktime($send_time_arr0[0],$send_time_arr0[1],$send_time_arr0[2],date('m'),date('d'),date('Y'));
$end = mktime($send_time_arr1[0],$send_time_arr1[1],$send_time_arr1[2],date('m'),date('d'),date('Y'));
if(time() < $begin || time() > $end){
var_dump("不在发送短信区间");
return ['code' => 0,'msg' =>"不在发送短信区间"];
}
$select = BallCart::find()->andWhere([
'status' => 1,
'is_delete' => 0,
])->andWhere([
'<=','updated_at',time()-(60*intval($find_option->value)),
])->asArray()->all();
if(empty($select)){
return false;
}
$redis_name = "api:cxaibc:bj_time_sms:send_sms_pl";
$expire_ttl = \Yii::$app->redis->get($redis_name); // 过期时间
$sms_sender = SmsSetting::findOne(['cx_mch_id' => 0]);
foreach ($select as $key=>$val){
// 发送短信
if($sms_sender->is_prod == '1' && $sms_sender->type == 0 && !empty($sms_sender->access_key_id) && !empty($sms_sender->access_secret) && !empty($sms_sender->sign_name)){
$store_user_ids = StoreUser::find()
->alias('su')
->join('inner join',['u'=>User::tableName()],'u.id = su.user_id')->where([
'su.is_delete' => 0,
'su.status' => 0,
'su.store_id' => $val['store_id'],
'su.user_type' => [1,2]
])->select('u.*')->asArray()->all();
if(empty($store_user_ids)){
var_dump("没有用户");
continue;
}
$tpl_type_key = "ball_time_error";
$sms = SmsTpl::findOne(['key' => $tpl_type_key,'is_delete' => 0,'type' => 0]);
if(empty($sms)){
var_dump("跳过短信发送");
continue;
}
$tpl_type = $sms->tpl_code;
$send_param = [];
$mobile_phones = [];
foreach ($store_user_ids as $index => &$item){
$redis_name = "api:cxaibc:gh:send_sms:{$item['mobile_phone']}";
$get = \Yii::$app->redis->get($redis_name);
if($get == 'nosend'){
// 跳过短信发送
var_dump("跳过短信发送");
continue;
}
$redis_send_name = "api:cxaibc:send_sms:lock:".md5("超时未归还频率".$item['mobile_phone']);
$setnx = \Yii::$app->redis->setnx($redis_send_name,1);
if(empty($setnx)){
$ttl = \Yii::$app->redis->ttl($redis_send_name);
if($ttl === -1){
\Yii::$app->redis->expire($redis_send_name,60*10);
}
// 未过时长
var_dump("跳过短信发送,未过时长");
continue;
}
\Yii::$app->redis->expire($redis_send_name,60*intval($expire_ttl));
$send_param[] = [
'name' => "管理员",
'numbers' => $val['ball_number'],
'error' => "车辆已超出使用时长{$find_option->value}分钟未归还",
];
$mobile_phones[] = $item['mobile_phone'];
var_dump($mobile_phones);
}
if(empty($mobile_phones)){
continue;
}
$sign_names = array_pad([],count($mobile_phones),$sms_sender->sign_name);
$sms_senders = new AliSmsMsgSender();
$sms_senders->access_key_id = $sms_sender->access_key_id;
$sms_senders->access_secret = $sms_sender->access_secret;
$sms_senders->tpl_code = $tpl_type;
$sms_senders->signname = $sign_names;
$res = $sms_senders->batchSender($mobile_phones, $sign_names,$send_param, 86);
if($res['code'] != 0){
var_dump($res);
\Yii::info(json_encode($mobile_phones,JSON_UNESCAPED_UNICODE).'__'.json_encode($sign_names,JSON_UNESCAPED_UNICODE).'__'.json_encode($item,JSON_UNESCAPED_UNICODE),'ball_sms');
}
}
}
}catch (\Exception $e){
var_dump("球车超出时长error",$e->getMessage());
}
}
}