api.so-manager-dev.com/app/Services/UserInformationHistoryService.php
Your Name 0b4acd7475
All checks were successful
Deploy api / deploy (push) Successful in 22s
Batch & API
2026-01-16 19:28:13 +09:00

185 lines
6.0 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\Services;
use App\Models\UserInformationHistory;
use App\Models\User;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
class UserInformationHistoryService
{
/**
* 履歴一覧取得
*
* @param array $params クエリパラメータ
* @return array
* @throws ApiException
*/
public function getList(array $params): array
{
try {
$query = UserInformationHistory::query();
// user_id フィルター
if (!empty($params['user_id'])) {
// user_idの存在チェック
if (!User::where('user_id', $params['user_id'])->exists()) {
throw new ApiException('E03', 'user_id の値が不正です(存在しないユーザー)。', 400);
}
$query->where('user_id', $params['user_id']);
}
// 日付範囲フィルター
if (!empty($params['entry_date_from'])) {
$query->where('entry_date', '>=', $params['entry_date_from']);
}
if (!empty($params['entry_date_to'])) {
$query->where('entry_date', '<=', $params['entry_date_to']);
}
// ソートentry_date降順、ID降順
$query->orderBy('entry_date', 'desc')
->orderBy('user_information_history_id', 'desc');
// ページネーション
$perPage = $params['per_page'] ?? config('api.pagination.default_per_page', 20);
$perPage = min($perPage, config('api.pagination.max_per_page', 100));
$page = $params['page'] ?? 1;
$paginator = $query->paginate($perPage, ['*'], 'page', $page);
return [
'data' => collect($paginator->items())->map(fn($item) => $item->toApiArray())->toArray(),
'pagination' => [
'current_page' => $paginator->currentPage(),
'per_page' => $paginator->perPage(),
'total' => $paginator->total(),
'total_pages' => $paginator->lastPage(),
]
];
} catch (ApiException $e) {
throw $e;
} catch (\Exception $e) {
Log::error('UserInformationHistoryService::getList error', [
'error' => $e->getMessage(),
'params' => $params
]);
throw new ApiException('E99', 'システム内部エラーが発生しました。', 500);
}
}
/**
* 単一履歴取得
*
* @param int $id 履歴ID
* @return array
* @throws ApiException
*/
public function getById(int $id): array
{
try {
$history = UserInformationHistory::find($id);
if (!$history) {
throw new ApiException('E02', '指定された履歴IDが存在しません。', 404);
}
return $history->toApiArray();
} catch (ApiException $e) {
throw $e;
} catch (\Exception $e) {
Log::error('UserInformationHistoryService::getById error', [
'error' => $e->getMessage(),
'id' => $id
]);
throw new ApiException('E99', 'システム内部エラーが発生しました。', 500);
}
}
/**
* 履歴新規作成
*
* @param array $data リクエストデータ
* @return array
* @throws ApiException
*/
public function create(array $data): array
{
try {
DB::beginTransaction();
$history = UserInformationHistory::create([
'user_id' => $data['user_id'],
'entry_date' => $data['entry_date'],
'user_information_history' => $data['user_information_history'],
]);
DB::commit();
Log::info('UserInformationHistory created', [
'user_information_history_id' => $history->user_information_history_id,
'user_id' => $history->user_id
]);
return $history->fresh()->toApiArray();
} catch (\Exception $e) {
DB::rollBack();
Log::error('UserInformationHistoryService::create error', [
'error' => $e->getMessage(),
'data' => $data
]);
throw new ApiException('E99', 'システム内部エラーが発生しました。', 500);
}
}
/**
* 履歴更新
*
* @param int $id 履歴ID
* @param array $data リクエストデータ
* @return array
* @throws ApiException
*/
public function update(int $id, array $data): array
{
try {
DB::beginTransaction();
$history = UserInformationHistory::find($id);
if (!$history) {
throw new ApiException('E02', '指定された履歴IDが存在しません。', 404);
}
// 更新可能フィールドのみ抽出
$updateData = array_filter($data, function ($value, $key) {
return in_array($key, ['user_id', 'entry_date', 'user_information_history']) && $value !== null;
}, ARRAY_FILTER_USE_BOTH);
$history->update($updateData);
DB::commit();
Log::info('UserInformationHistory updated', [
'user_information_history_id' => $id,
'updated_fields' => array_keys($updateData)
]);
return $history->fresh()->toApiArray();
} catch (ApiException $e) {
DB::rollBack();
throw $e;
} catch (\Exception $e) {
DB::rollBack();
Log::error('UserInformationHistoryService::update error', [
'error' => $e->getMessage(),
'id' => $id,
'data' => $data
]);
throw new ApiException('E99', 'システム内部エラーが発生しました。', 500);
}
}
}