- 법인차량 CRUD (CorporateVehicle) - 차량 운행일지 CRUD (VehicleLog) - 차량 정비이력 CRUD (VehicleMaintenance) - 모델, 서비스, 컨트롤러, 라우트 구성 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Vehicle;
|
|
|
|
use App\Models\Tenants\VehicleMaintenance;
|
|
use App\Services\Service;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class VehicleMaintenanceService extends Service
|
|
{
|
|
public function index(array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = VehicleMaintenance::query()->with(['vehicle:id,plate_number,model']);
|
|
|
|
if (! empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('description', 'like', "%{$search}%")
|
|
->orWhere('vendor', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if (! empty($filters['vehicle_id'])) {
|
|
$query->where('vehicle_id', $filters['vehicle_id']);
|
|
}
|
|
|
|
if (! empty($filters['category'])) {
|
|
$query->where('category', $filters['category']);
|
|
}
|
|
|
|
if (! empty($filters['start_date'])) {
|
|
$query->where('date', '>=', $filters['start_date']);
|
|
}
|
|
|
|
if (! empty($filters['end_date'])) {
|
|
$query->where('date', '<=', $filters['end_date']);
|
|
}
|
|
|
|
$query->orderByDesc('date')->orderByDesc('id');
|
|
|
|
return $query->paginate($filters['per_page'] ?? 20);
|
|
}
|
|
|
|
public function show(int $id): VehicleMaintenance
|
|
{
|
|
$item = VehicleMaintenance::with('vehicle:id,plate_number,model')->find($id);
|
|
|
|
if (! $item) {
|
|
throw new NotFoundHttpException('정비이력을 찾을 수 없습니다.');
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
public function store(array $data): VehicleMaintenance
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
$data['tenant_id'] = $this->tenantId();
|
|
|
|
return VehicleMaintenance::create($data);
|
|
});
|
|
}
|
|
|
|
public function update(int $id, array $data): VehicleMaintenance
|
|
{
|
|
return DB::transaction(function () use ($id, $data) {
|
|
$item = VehicleMaintenance::find($id);
|
|
|
|
if (! $item) {
|
|
throw new NotFoundHttpException('정비이력을 찾을 수 없습니다.');
|
|
}
|
|
|
|
$item->update($data);
|
|
|
|
return $item->fresh(['vehicle:id,plate_number,model']);
|
|
});
|
|
}
|
|
|
|
public function destroy(int $id): bool
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$item = VehicleMaintenance::find($id);
|
|
|
|
if (! $item) {
|
|
throw new NotFoundHttpException('정비이력을 찾을 수 없습니다.');
|
|
}
|
|
|
|
return $item->delete();
|
|
});
|
|
}
|
|
}
|