shjTwelveService = $shjTwelveService; } /** * コンソールコマンドを実行 * * 処理フロー: * 1. 定期契約マスタより未払い者を取得する * 2. 取得件数判定 * 3. 未払い者への通知、またはオペレーターキュー追加処理 * 4. バッチ処理ログを作成する * * @return int */ public function handle() { try { // 開始ログ出力 $startTime = now(); $this->info('SHJ-12 未払い者通知処理を開始します。'); Log::info('SHJ-12 未払い者通知処理開始', [ 'start_time' => $startTime ]); // 【処理1】定期契約マスタより未払い者を取得する $this->info('【処理1】定期契約マスタより未払い者を取得しています...'); $unpaidUsers = $this->shjTwelveService->getUnpaidUsers(); // 【判断1】取得件数判定 $unpaidCount = count($unpaidUsers); $this->info("取得件数: {$unpaidCount}件"); if ($unpaidCount === 0) { // 未払い者なしの結果を設定する $this->info('未払い者なしのため処理を終了します。'); // バッチ処理ログを作成(SHJ-8仕様に合わせ job_name と status_comment を追加) $this->shjTwelveService->createBatchLog( 'success', [ 'job_name' => 'SHJ-12未払い者アラート', 'status_comment' => '未払い者なし' ], '未払い者なし', 0, 0, 0 ); Log::info('SHJ-12 未払い者通知処理完了(対象なし)', [ 'end_time' => now(), 'duration_seconds' => $startTime->diffInSeconds(now()) ]); return self::SUCCESS; } // 【処理2】未払い者への通知、またはオペレーターキュー追加処理 $this->info('【処理2】未払い者への通知処理を実行しています...'); $processResult = $this->shjTwelveService->processUnpaidUserNotifications($unpaidUsers); // 処理結果確認 if ($processResult['success']) { $endTime = now(); $this->info('SHJ-12 未払い者通知処理が正常に完了しました。'); $this->info("処理時間: {$startTime->diffInSeconds($endTime)}秒"); $this->info("処理対象件数: {$unpaidCount}件"); $this->info("メール送信成功: {$processResult['mail_success_count']}件"); $this->info("メール送信失敗: {$processResult['mail_error_count']}件"); $this->info("キュー登録成功: {$processResult['queue_success_count']}件"); $this->info("キュー登録失敗: {$processResult['queue_error_count']}件"); // 【処理3】バッチ処理ログを作成する $statusComment = $processResult['status_comment']; $batchComments = $processResult['batch_comments']; $totalSuccessCount = $processResult['mail_success_count'] + $processResult['queue_success_count']; $totalErrorCount = $processResult['mail_error_count'] + $processResult['queue_error_count']; // status_commentに詳細エラー情報を付加 $finalMessage = $statusComment; if (!empty($batchComments)) { $finalMessage .= "\n" . $batchComments; } // SHJ-8仕様に合わせ、job_name と status_comment を parameters に追加 $batchLogParameters = array_merge($processResult['parameters'], [ 'job_name' => 'SHJ-12未払い者アラート', 'status_comment' => $statusComment ]); $this->shjTwelveService->createBatchLog( 'success', $batchLogParameters, $finalMessage, $unpaidCount, $totalSuccessCount, $totalErrorCount ); Log::info('SHJ-12 未払い者通知処理完了', [ 'end_time' => $endTime, 'duration_seconds' => $startTime->diffInSeconds($endTime), 'processed_count' => $unpaidCount, 'mail_success_count' => $processResult['mail_success_count'], 'mail_error_count' => $processResult['mail_error_count'], 'queue_success_count' => $processResult['queue_success_count'], 'queue_error_count' => $processResult['queue_error_count'] ]); return self::SUCCESS; } else { $this->error('SHJ-12 未払い者通知処理でエラーが発生しました: ' . $processResult['message']); // エラー時のバッチログ作成 $statusComment = $processResult['status_comment'] ?? 'システムエラー'; $batchComments = $processResult['batch_comments'] ?? ''; $finalMessage = $statusComment; if (!empty($batchComments)) { $finalMessage .= "\n" . $batchComments; } // SHJ-8仕様に合わせ、job_name と status_comment を parameters に追加 $batchLogParameters = array_merge($processResult['parameters'] ?? [], [ 'job_name' => 'SHJ-12未払い者アラート', 'status_comment' => $statusComment ]); $this->shjTwelveService->createBatchLog( 'error', $batchLogParameters, $finalMessage, $unpaidCount, ($processResult['mail_success_count'] ?? 0) + ($processResult['queue_success_count'] ?? 0), ($processResult['mail_error_count'] ?? 0) + ($processResult['queue_error_count'] ?? 0) ); Log::error('SHJ-12 未払い者通知処理エラー', [ 'error' => $processResult['message'], 'details' => $processResult['details'] ?? null ]); return self::FAILURE; } } catch (\Exception $e) { $this->error('SHJ-12 未払い者通知処理で予期しないエラーが発生しました: ' . $e->getMessage()); // 例外時のバッチログ作成(SHJ-8仕様に合わせ job_name と status_comment を追加) $this->shjTwelveService->createBatchLog( 'error', [ 'job_name' => 'SHJ-12未払い者アラート', 'status_comment' => 'システムエラー' ], 'システムエラー: ' . $e->getMessage(), 0, 0, 1 ); Log::error('SHJ-12 未払い者通知処理例外エラー', [ 'exception' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return self::FAILURE; } } }