- EquipmentPhotoService: GCS 기반 사진 업로드/삭제/조회 (최대 10장) - EquipmentImportService: 엑셀 파싱 → 설비 일괄 등록 (한글 헤더 자동 매핑) - API: 사진 업로드/목록/삭제, Import 미리보기/실행 엔드포인트 - 뷰: create/edit에 드래그앤드롭 사진 업로드, show에 갤러리 표시 - import.blade.php: 3단계 Import UI (파일선택 → 미리보기 → 결과) - phpoffice/phpspreadsheet 패키지 추가
106 lines
3.1 KiB
PHP
106 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Equipment\Equipment;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
|
|
class EquipmentService
|
|
{
|
|
public function getEquipments(array $filters = [], int $perPage = 20): LengthAwarePaginator
|
|
{
|
|
$query = Equipment::query()->with('manager');
|
|
|
|
if (! empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('equipment_code', 'like', "%{$search}%")
|
|
->orWhere('name', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if (! empty($filters['status'])) {
|
|
$query->where('status', $filters['status']);
|
|
}
|
|
|
|
if (! empty($filters['production_line'])) {
|
|
$query->where('production_line', $filters['production_line']);
|
|
}
|
|
|
|
if (! empty($filters['equipment_type'])) {
|
|
$query->where('equipment_type', $filters['equipment_type']);
|
|
}
|
|
|
|
$sortBy = $filters['sort_by'] ?? 'sort_order';
|
|
$sortDir = $filters['sort_direction'] ?? 'asc';
|
|
$query->orderBy($sortBy, $sortDir);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getEquipmentById(int $id): ?Equipment
|
|
{
|
|
return Equipment::with(['manager', 'inspectionTemplates', 'repairs', 'processes', 'photos'])->find($id);
|
|
}
|
|
|
|
public function createEquipment(array $data): Equipment
|
|
{
|
|
$data['tenant_id'] = session('selected_tenant_id', 1);
|
|
$data['created_by'] = auth()->id();
|
|
|
|
return Equipment::create($data);
|
|
}
|
|
|
|
public function updateEquipment(int $id, array $data): Equipment
|
|
{
|
|
$equipment = Equipment::findOrFail($id);
|
|
$data['updated_by'] = auth()->id();
|
|
$equipment->update($data);
|
|
|
|
return $equipment->fresh();
|
|
}
|
|
|
|
public function deleteEquipment(int $id): bool
|
|
{
|
|
$equipment = Equipment::findOrFail($id);
|
|
$equipment->deleted_by = auth()->id();
|
|
$equipment->save();
|
|
|
|
return $equipment->delete();
|
|
}
|
|
|
|
public function restoreEquipment(int $id): bool
|
|
{
|
|
$equipment = Equipment::onlyTrashed()->findOrFail($id);
|
|
|
|
return $equipment->restore();
|
|
}
|
|
|
|
public function getDashboardStats(): array
|
|
{
|
|
$total = Equipment::count();
|
|
$active = Equipment::where('status', 'active')->count();
|
|
$idle = Equipment::where('status', 'idle')->count();
|
|
$disposed = Equipment::where('status', 'disposed')->count();
|
|
|
|
return compact('total', 'active', 'idle', 'disposed');
|
|
}
|
|
|
|
public function getTypeStats(): array
|
|
{
|
|
return Equipment::where('status', '!=', 'disposed')
|
|
->selectRaw('equipment_type, count(*) as count')
|
|
->groupBy('equipment_type')
|
|
->pluck('count', 'equipment_type')
|
|
->toArray();
|
|
}
|
|
|
|
public function getEquipmentList(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Equipment::where('is_active', true)
|
|
->orderBy('sort_order')
|
|
->orderBy('name')
|
|
->get(['id', 'equipment_code', 'name', 'equipment_type', 'production_line']);
|
|
}
|
|
}
|