- 설비 유형별(equipment_type) 현황 집계 추가 - 이번달 점검 대상/완료/이슈 건수 통계 추가 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
185 lines
5.7 KiB
PHP
185 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Equipment;
|
|
|
|
use App\Models\Equipment\Equipment;
|
|
use App\Models\Equipment\EquipmentInspection;
|
|
use App\Services\Service;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class EquipmentService extends Service
|
|
{
|
|
public function index(array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = Equipment::query()->with(['manager', 'subManager']);
|
|
|
|
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->byStatus($filters['status']);
|
|
}
|
|
|
|
if (! empty($filters['production_line'])) {
|
|
$query->byLine($filters['production_line']);
|
|
}
|
|
|
|
if (! empty($filters['equipment_type'])) {
|
|
$query->byType($filters['equipment_type']);
|
|
}
|
|
|
|
$sortBy = $filters['sort_by'] ?? 'sort_order';
|
|
$sortDir = $filters['sort_direction'] ?? 'asc';
|
|
$query->orderBy($sortBy, $sortDir);
|
|
|
|
return $query->paginate($filters['per_page'] ?? 20);
|
|
}
|
|
|
|
public function show(int $id): Equipment
|
|
{
|
|
$equipment = Equipment::with(['manager', 'subManager', 'inspectionTemplates', 'repairs', 'processes', 'photos'])->find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
return $equipment;
|
|
}
|
|
|
|
public function store(array $data): Equipment
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
$data['tenant_id'] = $this->tenantId();
|
|
|
|
return Equipment::create($data);
|
|
});
|
|
}
|
|
|
|
public function update(int $id, array $data): Equipment
|
|
{
|
|
return DB::transaction(function () use ($id, $data) {
|
|
$equipment = Equipment::find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
$equipment->update($data);
|
|
|
|
return $equipment->fresh();
|
|
});
|
|
}
|
|
|
|
public function destroy(int $id): bool
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$equipment = Equipment::find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
return $equipment->delete();
|
|
});
|
|
}
|
|
|
|
public function restore(int $id): Equipment
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$equipment = Equipment::onlyTrashed()->find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
$equipment->restore();
|
|
|
|
return $equipment->fresh();
|
|
});
|
|
}
|
|
|
|
public function toggleActive(int $id): Equipment
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$equipment = Equipment::find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
$equipment->update(['is_active' => ! $equipment->is_active]);
|
|
|
|
return $equipment->fresh();
|
|
});
|
|
}
|
|
|
|
public function stats(): array
|
|
{
|
|
$total = Equipment::count();
|
|
$active = Equipment::where('status', 'active')->count();
|
|
$idle = Equipment::where('status', 'idle')->count();
|
|
$disposed = Equipment::where('status', 'disposed')->count();
|
|
|
|
// 설비 유형별 현황
|
|
$typeDistribution = Equipment::where('status', '!=', 'disposed')
|
|
->whereNotNull('equipment_type')
|
|
->selectRaw('equipment_type, count(*) as count')
|
|
->groupBy('equipment_type')
|
|
->orderByDesc('count')
|
|
->get()
|
|
->toArray();
|
|
|
|
// 이번달 점검 현황
|
|
$yearMonth = now()->format('Y-m');
|
|
$targetCount = Equipment::where('status', 'active')->count();
|
|
$completedCount = EquipmentInspection::where('year_month', $yearMonth)->distinct('equipment_id')->count('equipment_id');
|
|
$issueCount = EquipmentInspection::where('year_month', $yearMonth)
|
|
->where(function ($q) {
|
|
$q->whereNotNull('issue_note')->where('issue_note', '!=', '');
|
|
})
|
|
->count();
|
|
|
|
return [
|
|
'total' => $total,
|
|
'active' => $active,
|
|
'idle' => $idle,
|
|
'disposed' => $disposed,
|
|
'type_distribution' => $typeDistribution,
|
|
'inspection_stats' => [
|
|
'target_count' => $targetCount,
|
|
'completed_count' => $completedCount,
|
|
'issue_count' => $issueCount,
|
|
],
|
|
];
|
|
}
|
|
|
|
public function options(): array
|
|
{
|
|
return [
|
|
'equipment_types' => Equipment::getEquipmentTypes(),
|
|
'production_lines' => Equipment::getProductionLines(),
|
|
'statuses' => Equipment::getStatuses(),
|
|
'equipment_list' => Equipment::active()
|
|
->orderBy('sort_order')
|
|
->orderBy('name')
|
|
->get(['id', 'equipment_code', 'name', 'equipment_type', 'production_line']),
|
|
];
|
|
}
|
|
|
|
public function typeStats(): array
|
|
{
|
|
return Equipment::where('status', '!=', 'disposed')
|
|
->selectRaw('equipment_type, count(*) as count')
|
|
->groupBy('equipment_type')
|
|
->pluck('count', 'equipment_type')
|
|
->toArray();
|
|
}
|
|
}
|