159 lines
4.4 KiB
PHP
159 lines
4.4 KiB
PHP
<?php
|
||
|
||
namespace App\Models\Batch;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Carbon\Carbon;
|
||
|
||
/**
|
||
* 共通バッチログモデル - batch_logテーブル
|
||
*
|
||
* 全てのSHJ系バッチ処理の実行ログを管理する統一モデル
|
||
*/
|
||
class BatchLog extends Model
|
||
{
|
||
/**
|
||
* テーブル名
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $table = 'batch_log';
|
||
|
||
/**
|
||
* プライマリキー
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $primaryKey = 'id';
|
||
|
||
/**
|
||
* 一括代入可能な属性
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $fillable = [
|
||
'process_name', // プロセス名
|
||
'status', // ステータス
|
||
'start_time', // 開始時刻
|
||
'end_time', // 終了時刻
|
||
'parameters', // パラメータ(JSON形式)
|
||
'message', // メッセージ
|
||
'error_details', // エラー詳細
|
||
'execution_count', // 実行回数
|
||
'success_count', // 成功回数
|
||
'error_count', // エラー回数
|
||
'created_at', // 作成日時
|
||
'updated_at' // 更新日時
|
||
];
|
||
|
||
/**
|
||
* キャストする属性
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $casts = [
|
||
'id' => 'integer',
|
||
'start_time' => 'datetime',
|
||
'end_time' => 'datetime',
|
||
'parameters' => 'array',
|
||
'execution_count' => 'integer',
|
||
'success_count' => 'integer',
|
||
'error_count' => 'integer',
|
||
'created_at' => 'datetime',
|
||
'updated_at' => 'datetime'
|
||
];
|
||
|
||
/**
|
||
* ステータスの定数
|
||
*/
|
||
const STATUS_START = 'start'; // 開始
|
||
const STATUS_RUNNING = 'running'; // 実行中
|
||
const STATUS_SUCCESS = 'success'; // 成功
|
||
const STATUS_ERROR = 'error'; // エラー
|
||
const STATUS_WARNING = 'warning'; // 警告
|
||
const STATUS_CANCELLED = 'cancelled'; // キャンセル
|
||
|
||
|
||
|
||
/**
|
||
* 通用バッチログ作成メソッド
|
||
*
|
||
* 任意のバッチ処理で使用可能な統一ログ記録機能
|
||
* 実際の実行コマンド名をそのまま記録
|
||
*
|
||
* @param string $processName プロセス名(shj1, shj9:daily等)
|
||
* @param string $status ステータス
|
||
* @param array $parameters パラメーター配列
|
||
* @param string $message メッセージ
|
||
* @param array $additionalData 追加データ(device_id等)
|
||
* @return BatchLog 作成されたバッチログ
|
||
*/
|
||
public static function createBatchLog(
|
||
string $processName,
|
||
string $status,
|
||
array $parameters = [],
|
||
string $message = '',
|
||
array $additionalData = []
|
||
): BatchLog {
|
||
// パラメーターに追加データをマージ
|
||
$allParameters = array_merge($parameters, $additionalData, [
|
||
'executed_at' => now()->toISOString()
|
||
]);
|
||
|
||
return self::create([
|
||
'process_name' => $processName,
|
||
'status' => $status,
|
||
'start_time' => now(),
|
||
'end_time' => ($status === self::STATUS_SUCCESS || $status === self::STATUS_ERROR) ? now() : null,
|
||
'parameters' => $allParameters,
|
||
'message' => $message,
|
||
'error_details' => ($status === self::STATUS_ERROR) ? $message : null,
|
||
'execution_count' => 1,
|
||
'success_count' => $status === self::STATUS_SUCCESS ? 1 : 0,
|
||
'error_count' => $status === self::STATUS_ERROR ? 1 : 0
|
||
]);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 成功時のステータスコメント生成
|
||
*
|
||
* @return string ステータスコメント
|
||
*/
|
||
public static function getSuccessComment(): string
|
||
{
|
||
return '処理成功';
|
||
}
|
||
|
||
/**
|
||
* エラー時のステータスコメント生成
|
||
*
|
||
* @param string $errorMessage エラーメッセージ
|
||
* @return string ステータスコメント
|
||
*/
|
||
public static function getErrorComment(string $errorMessage): string
|
||
{
|
||
return 'エラー: ' . $errorMessage;
|
||
}
|
||
|
||
/**
|
||
* バッチログの文字列表現
|
||
*
|
||
* @return string
|
||
*/
|
||
public function __toString(): string
|
||
{
|
||
return sprintf(
|
||
'BatchLog[ID:%d, Process:%s, Status:%s, Time:%s]',
|
||
$this->id,
|
||
$this->process_name,
|
||
$this->status,
|
||
$this->start_time ? $this->start_time->format('Y-m-d H:i:s') : 'N/A'
|
||
);
|
||
}
|
||
} |