- Hours the counter reading cannot parse (",5") no longer fail after
they are saved; they just get no hour counter suggestion.
- The console's sign-in falls back to a console machine list
(machines::console-picker at /console/machines) instead of /machines,
so it stays in the console's own session.
- Recording counter usage locks the counter first, so usage saved at
the same time from two devices is not lost.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
116 lines
4.3 KiB
PHP
116 lines
4.3 KiB
PHP
<?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 lockCounter(int $counterId): bool
|
|
{
|
|
return MachineCounterModel::query()->whereKey($counterId)->lockForUpdate()->exists();
|
|
}
|
|
|
|
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]);
|
|
}
|
|
}
|