Files
sam-manage/app/Services/ArchivedRecordService.php
hskwon d45ccfb325 feat(mng): 아카이브 레코드 batch 그룹핑 UI 구현
- ArchivedRecordService: batch_id 기준 그룹핑 쿼리 추가
- Controller: batchId 파라미터로 상세 조회 변경
- 목록: 작업 설명, 레코드 타입, 레코드 수 표시
- 상세: batch 내 모든 레코드를 카드 형태로 표시
- 한번의 삭제 작업이 하나의 행으로 표시됨
2025-11-26 23:16:39 +09:00

114 lines
3.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\Archives\ArchivedRecord;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class ArchivedRecordService
{
/**
* 아카이브 레코드 목록 조회 (batch_id로 그룹핑, 페이지네이션)
*/
public function getArchivedRecordsBatched(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
// batch_id별로 그룹핑하여 첫 번째 레코드만 조회
$query = ArchivedRecord::query()
->select([
'batch_id',
'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');
// 레코드 타입 필터
if (! empty($filters['record_type'])) {
$query->having('record_types', 'like', "%{$filters['record_type']}%");
}
// 삭제자 필터
if (! empty($filters['deleted_by'])) {
$query->having('deleted_by', $filters['deleted_by']);
}
// 검색
if (! empty($filters['search'])) {
$search = $filters['search'];
$query->where('batch_description', 'like', "%{$search}%");
}
// 정렬 (기본: 삭제일시 내림차순)
$sortBy = $filters['sort_by'] ?? 'deleted_at';
$sortDirection = $filters['sort_direction'] ?? 'desc';
$query->orderBy($sortBy, $sortDirection);
return $query->paginate($perPage);
}
/**
* 특정 batch의 모든 레코드 조회
*/
public function getRecordsByBatchId(string $batchId): Collection
{
return ArchivedRecord::with(['deletedByUser', 'relations'])
->where('batch_id', $batchId)
->orderBy('id')
->get();
}
/**
* 특정 아카이브 레코드 조회
*/
public function getArchivedRecordById(int $id): ?ArchivedRecord
{
return ArchivedRecord::with(['deletedByUser', 'relations'])
->find($id);
}
/**
* batch_id로 첫 번째 레코드 조회 (상세 페이지 진입용)
*/
public function getFirstRecordByBatchId(string $batchId): ?ArchivedRecord
{
return ArchivedRecord::with(['deletedByUser', 'relations'])
->where('batch_id', $batchId)
->orderBy('id')
->first();
}
/**
* 삭제자 목록 조회 (필터용)
*/
public function getDeletedByUsers(): array
{
return ArchivedRecord::query()
->whereNotNull('deleted_by')
->with('deletedByUser:id,name')
->get()
->pluck('deletedByUser')
->filter()
->unique('id')
->values()
->toArray();
}
/**
* 아카이브 통계
*/
public function getStats(): array
{
return [
'total_batches' => ArchivedRecord::distinct('batch_id')->count('batch_id'),
'total_records' => ArchivedRecord::count(),
'tenants' => ArchivedRecord::tenant()->count(),
'users' => ArchivedRecord::user()->count(),
];
}
}