feat(시공사): 2.1 현장관리 - Backend API 확장
- 마이그레이션: site_code, client_id, status 컬럼 추가 - Site 모델: 상태 상수, Client 관계 추가 - SiteService: stats(), bulkDestroy(), 필터 확장 - SiteController: stats, bulkDestroy 엔드포인트 추가 - 라우트: /stats, /bulk 추가 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ public function index(array $params): LengthAwarePaginator
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$query = Site::query()
|
||||
->with('client:id,name')
|
||||
->where('tenant_id', $tenantId);
|
||||
|
||||
// 검색 필터
|
||||
@@ -23,6 +24,7 @@ public function index(array $params): LengthAwarePaginator
|
||||
$search = $params['search'];
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('site_code', 'like', "%{$search}%")
|
||||
->orWhere('address', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
@@ -32,6 +34,24 @@ public function index(array $params): LengthAwarePaginator
|
||||
$query->where('is_active', $params['is_active']);
|
||||
}
|
||||
|
||||
// 상태 필터
|
||||
if (! empty($params['status'])) {
|
||||
$query->where('status', $params['status']);
|
||||
}
|
||||
|
||||
// 거래처 필터
|
||||
if (! empty($params['client_id'])) {
|
||||
$query->where('client_id', $params['client_id']);
|
||||
}
|
||||
|
||||
// 날짜 필터
|
||||
if (! empty($params['start_date'])) {
|
||||
$query->whereDate('created_at', '>=', $params['start_date']);
|
||||
}
|
||||
if (! empty($params['end_date'])) {
|
||||
$query->whereDate('created_at', '<=', $params['end_date']);
|
||||
}
|
||||
|
||||
// 정렬
|
||||
$sortBy = $params['sort_by'] ?? 'created_at';
|
||||
$sortDir = $params['sort_dir'] ?? 'desc';
|
||||
@@ -43,6 +63,30 @@ public function index(array $params): LengthAwarePaginator
|
||||
return $query->paginate($perPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 현장 통계 조회
|
||||
*/
|
||||
public function stats(): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$total = Site::where('tenant_id', $tenantId)->count();
|
||||
|
||||
$statusCounts = Site::where('tenant_id', $tenantId)
|
||||
->selectRaw('status, COUNT(*) as count')
|
||||
->groupBy('status')
|
||||
->pluck('count', 'status')
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
'total' => $total,
|
||||
'construction' => $statusCounts[Site::STATUS_ACTIVE] ?? 0,
|
||||
'unregistered' => $statusCounts[Site::STATUS_UNREGISTERED] ?? 0,
|
||||
'suspended' => $statusCounts[Site::STATUS_SUSPENDED] ?? 0,
|
||||
'pending' => $statusCounts[Site::STATUS_PENDING] ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 현장 상세 조회
|
||||
*/
|
||||
@@ -51,6 +95,7 @@ public function show(int $id): Site
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
return Site::query()
|
||||
->with('client:id,name')
|
||||
->where('tenant_id', $tenantId)
|
||||
->findOrFail($id);
|
||||
}
|
||||
@@ -66,16 +111,19 @@ public function store(array $data): Site
|
||||
return DB::transaction(function () use ($data, $tenantId, $userId) {
|
||||
$site = Site::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'site_code' => $data['site_code'] ?? null,
|
||||
'client_id' => $data['client_id'] ?? null,
|
||||
'name' => $data['name'],
|
||||
'address' => $data['address'] ?? null,
|
||||
'latitude' => $data['latitude'] ?? null,
|
||||
'longitude' => $data['longitude'] ?? null,
|
||||
'is_active' => $data['is_active'] ?? true,
|
||||
'status' => $data['status'] ?? Site::STATUS_UNREGISTERED,
|
||||
'created_by' => $userId,
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
|
||||
return $site;
|
||||
return $site->load('client:id,name');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -93,17 +141,20 @@ public function update(int $id, array $data): Site
|
||||
->findOrFail($id);
|
||||
|
||||
$site->fill([
|
||||
'site_code' => $data['site_code'] ?? $site->site_code,
|
||||
'client_id' => array_key_exists('client_id', $data) ? $data['client_id'] : $site->client_id,
|
||||
'name' => $data['name'] ?? $site->name,
|
||||
'address' => $data['address'] ?? $site->address,
|
||||
'latitude' => $data['latitude'] ?? $site->latitude,
|
||||
'longitude' => $data['longitude'] ?? $site->longitude,
|
||||
'is_active' => $data['is_active'] ?? $site->is_active,
|
||||
'status' => $data['status'] ?? $site->status,
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
|
||||
$site->save();
|
||||
|
||||
return $site->fresh();
|
||||
return $site->fresh()->load('client:id,name');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -128,6 +179,32 @@ public function destroy(int $id): bool
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 현장 일괄 삭제
|
||||
*/
|
||||
public function bulkDestroy(array $ids): int
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
return DB::transaction(function () use ($ids, $tenantId, $userId) {
|
||||
$sites = Site::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereIn('id', $ids)
|
||||
->get();
|
||||
|
||||
$deletedCount = 0;
|
||||
foreach ($sites as $site) {
|
||||
$site->deleted_by = $userId;
|
||||
$site->save();
|
||||
$site->delete();
|
||||
$deletedCount++;
|
||||
}
|
||||
|
||||
return $deletedCount;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성화된 현장 목록 조회 (셀렉트박스용)
|
||||
*/
|
||||
@@ -139,7 +216,7 @@ public function getActiveSites(): array
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('is_active', true)
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'address'])
|
||||
->get(['id', 'site_code', 'name', 'address'])
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user