Track machine counters and maintenance with counter and time intervals
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Machines\Domain\Contracts\MaintenanceRepository;
|
||||
use App\Modules\Machines\Domain\Entities\MaintenanceItem;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterReadingModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceItemModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceRecordModel;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentMaintenanceRepository implements MaintenanceRepository
|
||||
{
|
||||
public function findItem(int $id): ?MaintenanceItem
|
||||
{
|
||||
$model = MaintenanceItemModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : MaintenanceItem::reconstitute(
|
||||
$model->id,
|
||||
$model->machine_id,
|
||||
$model->name,
|
||||
$model->machine_counter_id,
|
||||
$model->interval_milli,
|
||||
$model->counter_baseline_milli,
|
||||
$model->period,
|
||||
$model->period_count,
|
||||
new DateTimeImmutable(substr($model->starts_on, 0, 10)),
|
||||
$model->warning_percent,
|
||||
$model->instructions,
|
||||
$model->stock_item_id,
|
||||
$model->stock_quantity_milli,
|
||||
$model->is_active,
|
||||
);
|
||||
}
|
||||
|
||||
public function saveItem(MaintenanceItem $item): MaintenanceItem
|
||||
{
|
||||
$model = $item->id() === null ? new MaintenanceItemModel : MaintenanceItemModel::query()->findOrFail($item->id());
|
||||
|
||||
$model->fill([
|
||||
'machine_id' => $item->machineId(),
|
||||
'name' => $item->name(),
|
||||
'machine_counter_id' => $item->counterId(),
|
||||
'interval_milli' => $item->intervalMilli(),
|
||||
'counter_baseline_milli' => $item->baselineMilli(),
|
||||
'period' => $item->period(),
|
||||
'period_count' => $item->periodCount(),
|
||||
'starts_on' => $item->startsOn()->format('Y-m-d'),
|
||||
'warning_percent' => $item->warningPercent(),
|
||||
'instructions' => $item->instructions(),
|
||||
'stock_item_id' => $item->stockItemId(),
|
||||
'stock_quantity_milli' => $item->stockQuantityMilli(),
|
||||
'is_active' => $item->isActive(),
|
||||
])->save();
|
||||
|
||||
return $this->findItem($model->id) ?? $item;
|
||||
}
|
||||
|
||||
public function machineOfCounter(int $counterId): ?int
|
||||
{
|
||||
$machineId = MachineCounterModel::query()->whereKey($counterId)->value('machine_id');
|
||||
|
||||
return $machineId === null ? null : (int) $machineId;
|
||||
}
|
||||
|
||||
public function latestReading(int $counterId): ?int
|
||||
{
|
||||
$reading = MachineCounterReadingModel::query()
|
||||
->where('machine_counter_id', $counterId)
|
||||
->orderByDesc('read_at')
|
||||
->orderByDesc('id')
|
||||
->value('reading_milli');
|
||||
|
||||
return $reading === null ? null : (int) $reading;
|
||||
}
|
||||
|
||||
public function addReading(int $counterId, int $readingMilli, int $userId, DateTimeImmutable $at): void
|
||||
{
|
||||
MachineCounterReadingModel::query()->create([
|
||||
'machine_counter_id' => $counterId,
|
||||
'reading_milli' => $readingMilli,
|
||||
'user_id' => $userId,
|
||||
'read_at' => $at->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function addRecord(int $itemId, DateTimeImmutable $performedAt, int $userId, ?int $readingMilli, ?string $note): int
|
||||
{
|
||||
return MaintenanceRecordModel::query()->create([
|
||||
'maintenance_item_id' => $itemId,
|
||||
'performed_at' => $performedAt->format('Y-m-d H:i:s'),
|
||||
'user_id' => $userId,
|
||||
'counter_reading_milli' => $readingMilli,
|
||||
'note' => $note,
|
||||
])->id;
|
||||
}
|
||||
|
||||
public function findRecord(int $id): ?array
|
||||
{
|
||||
$model = MaintenanceRecordModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : ['id' => $model->id, 'itemId' => $model->maintenance_item_id, 'cancelled' => $model->cancelled_at !== null];
|
||||
}
|
||||
|
||||
public function cancelRecord(int $id, int $userId, DateTimeImmutable $at): void
|
||||
{
|
||||
MaintenanceRecordModel::query()->whereKey($id)->update(['cancelled_at' => $at->format('Y-m-d H:i:s'), 'cancelled_by' => $userId]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user