so-manager-dev.com/app/Console/Commands/ShjElevenCommand.php
Your Name 10a917b556
All checks were successful
Deploy so-manager (auto) / deploy (push) Successful in 24s
【更新】SHJ関連の修正
2025-10-10 19:55:46 +09:00

165 lines
6.5 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\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Services\ShjElevenService;
/**
* SHJ-11 現在契約台数集計コマンド
*
* 集計単位每个の契約台数を算出し、ゾーンマスタとの管理処理を実行する
* バックグラウンドで実行される定期バッチ処理
*/
class ShjElevenCommand extends Command
{
/**
* コンソールコマンドの名前とシグネチャ
*
* 引数: なし
* オプション: なし
*
* @var string
*/
protected $signature = 'shj:11';
/**
* コンソールコマンドの説明
*
* @var string
*/
protected $description = 'SHJ-11 現在契約台数集計 - 集計単位每个契約台数を算出しゾーンマスタ管理を実行';
/**
* SHJ-11サービスクラス
*
* @var ShjElevenService
*/
protected $shjElevenService;
/**
* コンストラクタ
*
* @param ShjElevenService $shjElevenService
*/
public function __construct(ShjElevenService $shjElevenService)
{
parent::__construct();
$this->shjElevenService = $shjElevenService;
}
/**
* コンソールコマンドを実行
*
* 処理フロー(仕様書準拠):
* 【処理1】集計単位每个の契約台数を算出する
* 【判断1】取得件数判定
* - 取得件数 = 0 → 【処理4】バッチログ作成「全駐輪場契約なし」→ 終了
* - 取得件数 ≥ 1 → 【処理2】ゾーンマスタ処理循環→ 終了
*
* ※【処理4】は各レコードの処理ごとにService層で呼び出される
*
* @return int
*/
public function handle()
{
try {
// 開始ログ出力
$startTime = now();
$this->info('SHJ-11 現在契約台数集計を開始します。');
Log::info('SHJ-11 現在契約台数集計開始', [
'start_time' => $startTime
]);
// 【処理1】集計単位每个の契約台数を算出する
$this->info('【処理1】集計単位每个の契約台数を算出しています...');
$contractCounts = $this->shjElevenService->calculateContractCounts();
// 【判断1】取得件数判定
$countResults = count($contractCounts);
$this->info("取得件数: {$countResults}");
if ($countResults === 0) {
// 【判断1】取得件数 = 0 の場合
$this->info('対象なしのため処理を終了します。');
// 【処理4】bat_job_logに直接書き込み取得件数=0時
$batchLogResult = $this->shjElevenService->writeBatJobLogForNoContracts();
// bat_job_log書き込み結果をチェック
if (!$batchLogResult['success']) {
$this->warn('bat_job_log書き込みで異常が発生しました');
if (isset($batchLogResult['error_message'])) {
$this->warn('error_message: ' . $batchLogResult['error_message']);
}
Log::warning('bat_job_log書き込み失敗対象なし', [
'error_message' => $batchLogResult['error_message'] ?? null
]);
}
Log::info('SHJ-11 現在契約台数集計完了(対象なし)', [
'end_time' => now(),
'duration_seconds' => $startTime->diffInSeconds(now()),
'bat_job_log_success' => $batchLogResult['success']
]);
return self::SUCCESS;
}
// 【判断1】取得件数 ≥ 1 の場合
// 【処理2・3・4】ゾーンマスタ処理各レコードごとに処理4を実行
$this->info('【処理2】ゾーンマスタ処理を実行しています...');
$this->info('※各レコードごとに【処理4】バッチログを作成します');
$processResult = $this->shjElevenService->processZoneManagement($contractCounts);
// 処理結果確認
if ($processResult['success']) {
$endTime = now();
$this->info('SHJ-11 現在契約台数集計が正常に完了しました。');
$this->info("処理時間: {$startTime->diffInSeconds($endTime)}");
$this->info("処理対象件数: {$countResults}");
$this->info("ゾーン新規作成件数: {$processResult['created_zones']}");
$this->info("ゾーン更新件数: {$processResult['updated_zones']}");
$this->info("限界台数超過件数: {$processResult['over_capacity_count']}");
if (!empty($processResult['batch_log_errors'])) {
$this->warn("バッチログエラー件数: " . count($processResult['batch_log_errors']) . "");
}
Log::info('SHJ-11 現在契約台数集計完了', [
'end_time' => $endTime,
'duration_seconds' => $startTime->diffInSeconds($endTime),
'processed_count' => $countResults,
'created_zones' => $processResult['created_zones'],
'updated_zones' => $processResult['updated_zones'],
'over_capacity_count' => $processResult['over_capacity_count'],
'batch_log_errors' => count($processResult['batch_log_errors'])
]);
return self::SUCCESS;
} else {
$this->error('SHJ-11 現在契約台数集計でエラーが発生しました: ' . $processResult['message']);
Log::error('SHJ-11 現在契約台数集計エラー', [
'error' => $processResult['message'],
'details' => $processResult['details'] ?? null
]);
return self::FAILURE;
}
} catch (\Exception $e) {
$this->error('SHJ-11 現在契約台数集計で予期しないエラーが発生しました: ' . $e->getMessage());
Log::error('SHJ-11 現在契約台数集計例外エラー', [
'exception' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return self::FAILURE;
}
}
}