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); } } }