fix(mng): 아카이브 레코드 batch_id NULL 처리

- 기존 데이터(batch_id NULL)는 legacy_ 접두사로 가상 batch_id 생성
- legacy 데이터 조회 시 id 기반으로 단일 레코드 조회
- batch_description NULL인 경우 레코드 타입과 원본 ID로 자동 생성
This commit is contained in:
2025-11-27 09:09:05 +09:00
parent d45ccfb325
commit 82c072d72e

View File

@@ -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')