/** * 业务维度批量统计 */ public function actionEmailBatchBusi() { try { $busiStat = new EmailStatBusiness(); $serviceBinding = new ServiceBinding(); // 所有应用系统app_id $appIdAll = $serviceBinding->getAllAppId(self::SERVICEID); if (empty($appIdAll)) { $this->stdout("不存在通过审核的第三方应用系统!!!\r\n", Console::FG_RED); $this->_fileLogger->writeOne("不存在通过审核的第三方应用系统!!!", Logger::LEVEL_INFO, 'console\\controllers\\EmailController::email-batch-busi'); exit(6); } // 邮件业务信息 $business = EmailBusinessCategory::find()->where(['status' => 1])->orderBy('app_id')->asArray()->all(); if (arr_null($business)) { $this->stdout("所有系统均没有绑定邮件业务ID!!!\r\n", Console::FG_RED); $this->_fileLogger->writeOne("所有系统均没有绑定邮件业务ID!!!", Logger::LEVEL_INFO, 'console\\controllers\\EmailController::email-batch-busi'); exit(6); } // key为app_id value为邮件业务ID的数组 $appBusi = []; foreach ($business as $busi) { $appBusi[$busi['app_id']][] = $busi['id']; } foreach ($appIdAll as $key => $appId) { if (!isset($appBusi[$appId])) { unset($appIdAll[$key]); } } if (empty($appIdAll)) { $this->stdout("所有系统均没有绑定邮件业务ID!!!\r\n", Console::FG_RED); $this->_fileLogger->writeOne("所有系统均没有绑定邮件业务ID!!!", Logger::LEVEL_INFO, 'console\\controllers\\EmailController::email-batch-busi'); exit(6); } $time = time(); $date = date('Ymd', time() - 24 * 60 * 60); $type = self::BUSI_CACHE_PREFIX; $keys = []; $i = 0; foreach ($appIdAll as $appIda) { foreach ($appBusi[$appIda] as $busiId) { $keys[$i]['key'] = $type . $appIda . ':' . $busiId . ':' . $date; $keys[$i]['id'] = $appIda . '' . $busiId . '' . $date; $keys[$i]['app_id'] = $appIda; $keys[$i]['busiId'] = $busiId; $i++; } } $redis = Yii::$app->redisCache->redis; $rows = []; $appIds = []; foreach ($keys as $key) { $total = 0; $appIds[] = $key['app_id']; if ($redis->exists($key['key'])) { $busis = $redis->hgetall($key['key']); if (!empty($busis)) { $total = count_sms_sum($busis); } } $rows[] = [$key['id'], $key['app_id'], $key['busiId'], $date, $total, $time, $time]; } $appIds = implode(',', array_unique($appIds)); $result = $busiStat->batchInsertStat($rows); if (!$result) { $this->stdout("应用ID为{$appIds}的应用系统业务维度统计失败!!!\r\n", Console::FG_RED); $this->_fileLogger->writeOne("应用ID为{$appIds}的应用系统业务维度统计失败!!!", Logger::LEVEL_INFO, 'console\\controllers\\EmailController::email-batch-busi'); exit(3); } $this->stdout("应用ID为{$appIds}的应用系统业务维度统计成功!!!\r\n", Console::FG_GREEN); $this->_fileLogger->writeOne("应用ID为{$appIds}的应用系统业务维度统计成功!!!", Logger::LEVEL_INFO, 'console\\controllers\\EmailController::email-batch-busi'); // 清除缓存 foreach ($keys as $key) { $redis->del($key['key']); } exit; } catch (\Exception $ex) { $code = $ex->getCode(); if ($code == 23000) { $this->stdout("应用中存在已经被备份(主键重复)~~\r\n", Console::FG_RED); } else { $this->stdout("系统出错,请稍后重试~~\r\n", Console::FG_RED); } $this->_fileLogger->writeOne($ex->getMessage(), Logger::LEVEL_ERROR, 'console\\controllers\\EmailController::email-batch-daily'); // 1表示异常退出 exit(1); } }
/** * 服务“自定义”检测处理 * * @param int $appID 应用ID * @param array $data 检测项数据 * * @return array */ protected function customCheck($appID, $data, $isISystem) { $reEmail = '/^[a-z0-9]([a-z0-9]*[-_\\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\\.][a-z]{2,3}([\\.][a-z]{2})?$/i'; $data['to'] = explode(',', $data['to']); foreach ($data['to'] as $key => $email) { if (!preg_match($reEmail, $email)) { unset($data['to'][$key]); } } if (!$data['to']) { return [1601, 'error_email_format']; } $mEmailSetting = new EmailSettingInfo(); $setting = $mEmailSetting->findByAppid($appID); if (!$setting) { return [2501, 'error_unsetting']; } $serviceConfig = json_decode($setting['service_config'], true); if (!$serviceConfig) { return [2502, 'error_unsetting']; } if (($warning = $this->_checkServiceConfig($serviceConfig)) !== true) { return $warning; } $mSmsBC = new EmailBusinessCategory(); $category = $mSmsBC->getByAidAndCid($appID, $data['business']); if (!$category) { return [2301, 'error_business_category_invalid']; } if (!isset($data['content']) && isset($data['tplno'])) { $mEmailTpl = new EmailTplInfo(); $emailTpl = $mEmailTpl->pk($data['tplno']); if (!$emailTpl) { return [2101, 'error_tpl_noexists']; } else { if ($emailTpl['status'] === EmailTplInfo::STATUS_DISABLED) { return [2102, 'error_tpl_disabled']; } else { try { $content = $this->tplParse($emailTpl['content'], $data); if (!$content) { return [2103, 'error_tpl_missing_var']; } } catch (\Exception $e) { return [$e->getCode(), $e->getMessage]; } } } } $content = json_decode($content, true); if ($content) { $data = array_merge($data, $content); } // 使用明文APPID $data['appID'] = $appID; // 获取请求客户端IP $data['source'] = Yii::$app->util->getClientIP(); // 设置业务ID $data['busiID'] = $category['id']; $data['serviceConfig'] = $serviceConfig; return $this->verifySetting($appID, $data, $isISystem); }