diff --git a/app/Services/ArchivedRecordService.php b/app/Services/ArchivedRecordService.php index 79f115a0..6f3cb1a7 100644 --- a/app/Services/ArchivedRecordService.php +++ b/app/Services/ArchivedRecordService.php @@ -11,21 +11,23 @@ class ArchivedRecordService { /** * 아카이브 레코드 목록 조회 (batch_id로 그룹핑, 페이지네이션) + * batch_id가 NULL인 기존 데이터는 각각 개별 batch로 취급 */ public function getArchivedRecordsBatched(array $filters = [], int $perPage = 15): LengthAwarePaginator { // batch_id별로 그룹핑하여 첫 번째 레코드만 조회 + // batch_id가 NULL인 경우 id를 기반으로 가상 batch_id 생성 $query = ArchivedRecord::query() ->select([ - 'batch_id', - 'batch_description', + DB::raw("COALESCE(batch_id, CONCAT('legacy_', id)) as batch_id"), + DB::raw("COALESCE(batch_description, CONCAT(record_type, ' 삭제 (ID: ', original_id, ')')) as batch_description"), DB::raw('MIN(id) as id'), DB::raw('GROUP_CONCAT(DISTINCT record_type) as record_types'), DB::raw('COUNT(*) as record_count'), DB::raw('MIN(deleted_by) as deleted_by'), DB::raw('MIN(deleted_at) as deleted_at'), ]) - ->groupBy('batch_id', 'batch_description'); + ->groupBy(DB::raw("COALESCE(batch_id, CONCAT('legacy_', id))"), DB::raw("COALESCE(batch_description, CONCAT(record_type, ' 삭제 (ID: ', original_id, ')'))")); // 레코드 타입 필터 if (! empty($filters['record_type'])) { @@ -53,9 +55,19 @@ public function getArchivedRecordsBatched(array $filters = [], int $perPage = 15 /** * 특정 batch의 모든 레코드 조회 + * legacy_ 접두사가 붙은 경우 단일 레코드 ID로 조회 */ public function getRecordsByBatchId(string $batchId): Collection { + // legacy_ 접두사가 붙은 경우 (기존 데이터) + if (str_starts_with($batchId, 'legacy_')) { + $id = (int) str_replace('legacy_', '', $batchId); + + return ArchivedRecord::with(['deletedByUser', 'relations']) + ->where('id', $id) + ->get(); + } + return ArchivedRecord::with(['deletedByUser', 'relations']) ->where('batch_id', $batchId) ->orderBy('id')