Files
sam-api/app/Services/Equipment/EquipmentRepairService.php
김보곤 069d0206a0 feat: [equipment] 설비관리 API 백엔드 구현 (Phase 1)
- 모델 6개: Equipment, EquipmentInspection, EquipmentInspectionDetail, EquipmentInspectionTemplate, EquipmentRepair, EquipmentProcess
- InspectionCycle Enum: 6주기(일/주/월/격월/분기/반기) 날짜 해석
- 서비스 4개: EquipmentService, EquipmentInspectionService, EquipmentRepairService, EquipmentPhotoService
- 컨트롤러 4개: CRUD + 점검 토글/결과 설정/메모/초기화 + 템플릿 관리 + 수리이력 + 사진
- FormRequest 6개: 설비등록/수정, 수리이력, 점검템플릿, 토글, 메모
- 라우트 26개: equipment prefix 하위 RESTful 엔드포인트
- i18n 메시지: message.equipment.*, error.equipment.*
- 마이그레이션: equipments/equipment_repairs options JSON 컬럼 추가
2026-03-12 10:52:30 +09:00

103 lines
3.0 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\Models\Equipment\EquipmentRepair;
use App\Services\Service;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EquipmentRepairService extends Service
{
public function index(array $filters = []): LengthAwarePaginator
{
$query = EquipmentRepair::query()->with('equipment', 'repairer');
if (! empty($filters['equipment_id'])) {
$query->where('equipment_id', $filters['equipment_id']);
}
if (! empty($filters['repair_type'])) {
$query->where('repair_type', $filters['repair_type']);
}
if (! empty($filters['date_from'])) {
$query->where('repair_date', '>=', $filters['date_from']);
}
if (! empty($filters['date_to'])) {
$query->where('repair_date', '<=', $filters['date_to']);
}
if (! empty($filters['search'])) {
$search = $filters['search'];
$query->where(function ($q) use ($search) {
$q->where('description', 'like', "%{$search}%")
->orWhereHas('equipment', function ($eq) use ($search) {
$eq->where('name', 'like', "%{$search}%")
->orWhere('equipment_code', 'like', "%{$search}%");
});
});
}
return $query->orderBy('repair_date', 'desc')->paginate($filters['per_page'] ?? 20);
}
public function show(int $id): EquipmentRepair
{
$repair = EquipmentRepair::with('equipment', 'repairer')->find($id);
if (! $repair) {
throw new NotFoundHttpException(__('error.equipment.repair_not_found'));
}
return $repair;
}
public function store(array $data): EquipmentRepair
{
return DB::transaction(function () use ($data) {
$data['tenant_id'] = $this->tenantId();
return EquipmentRepair::create($data);
});
}
public function update(int $id, array $data): EquipmentRepair
{
return DB::transaction(function () use ($id, $data) {
$repair = EquipmentRepair::find($id);
if (! $repair) {
throw new NotFoundHttpException(__('error.equipment.repair_not_found'));
}
$repair->update($data);
return $repair->fresh();
});
}
public function destroy(int $id): bool
{
return DB::transaction(function () use ($id) {
$repair = EquipmentRepair::find($id);
if (! $repair) {
throw new NotFoundHttpException(__('error.equipment.repair_not_found'));
}
return $repair->delete();
});
}
public function recentRepairs(int $limit = 5): \Illuminate\Database\Eloquent\Collection
{
return EquipmentRepair::with('equipment')
->orderBy('repair_date', 'desc')
->limit($limit)
->get();
}
}