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('未払い者対象なしのため処理を終了します。'); // バッチ処理ログを作成 $this->shjTwelveService->createBatchLog( 'success', [], '未払い者対象なし', 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['notification_count']}件"); $this->info("オペレーターキュー追加件数: {$processResult['queue_count']}件"); // 【処理3】バッチ処理ログを作成する $this->shjTwelveService->createBatchLog( 'success', $processResult['parameters'], '未払い者通知処理完了', $unpaidCount, $processResult['notification_count'] + $processResult['queue_count'], 0 ); Log::info('SHJ-12 未払い者通知処理完了', [ 'end_time' => $endTime, 'duration_seconds' => $startTime->diffInSeconds($endTime), 'processed_count' => $unpaidCount, 'notification_count' => $processResult['notification_count'], 'queue_count' => $processResult['queue_count'] ]); return self::SUCCESS; } else { $this->error('SHJ-12 未払い者通知処理でエラーが発生しました: ' . $processResult['message']); // エラー時のバッチログ作成 $this->shjTwelveService->createBatchLog( 'error', $processResult['parameters'] ?? [], $processResult['message'], $unpaidCount, $processResult['notification_count'] ?? 0, 1 ); Log::error('SHJ-12 未払い者通知処理エラー', [ 'error' => $processResult['message'], 'details' => $processResult['details'] ?? null ]); return self::FAILURE; } } catch (\Exception $e) { $this->error('SHJ-12 未払い者通知処理で予期しないエラーが発生しました: ' . $e->getMessage()); // 例外時のバッチログ作成 $this->shjTwelveService->createBatchLog( 'error', [], 'システムエラー: ' . $e->getMessage(), 0, 0, 1 ); Log::error('SHJ-12 未払い者通知処理例外エラー', [ 'exception' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return self::FAILURE; } } }