so-manager-dev.com/app/Console/Commands/ShjTwelveCommand.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

218 lines
8.6 KiB
PHP
Raw Permalink 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\ShjTwelveService;
/**
* SHJ-12 未払い者通知処理コマンド
*
* 定期契約マスタより未払い者を取得し、通知処理またはオペレーターキュー追加を実行する
* バックグラウンドで実行される定期バッチ処理
*/
class ShjTwelveCommand extends Command
{
/**
* コンソールコマンドの名前とシグネチャ
*
* 引数: なし
* オプション: なし
*
* @var string
*/
protected $signature = 'shj:12';
/**
* コンソールコマンドの説明
*
* @var string
*/
protected $description = 'SHJ-12 未払い者通知処理 - 定期契約マスタより未払い者を取得し通知処理を実行';
/**
* SHJ-12サービスクラス
*
* @var ShjTwelveService
*/
protected $shjTwelveService;
/**
* コンストラクタ
*
* @param ShjTwelveService $shjTwelveService
*/
public function __construct(ShjTwelveService $shjTwelveService)
{
parent::__construct();
$this->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;
}
}
}