2024-08-07 07:54:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire\Maintenance;
|
|
|
|
|
|
|
|
use App\Models\MaintenanceHistory;
|
2024-08-09 06:37:00 +00:00
|
|
|
use App\Models\MaintenanceHostHistory;
|
2024-08-07 08:30:51 +00:00
|
|
|
use App\Models\MaintenanceTaskHistory;
|
2024-08-07 07:54:20 +00:00
|
|
|
use Carbon\Carbon;
|
2024-08-07 08:30:51 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-08-07 07:54:20 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class ProgressForm extends Component
|
|
|
|
{
|
|
|
|
public $maintenanceHistory;
|
|
|
|
|
|
|
|
//INPUTS
|
|
|
|
public array $maintenance_task_status;
|
2024-08-07 08:30:51 +00:00
|
|
|
public array $maintenance_task_status_old;
|
2024-08-09 06:37:00 +00:00
|
|
|
public array $maintenance_host_skipped;
|
2024-08-07 07:54:20 +00:00
|
|
|
|
|
|
|
public function mount(int $maintenanceHistory)
|
|
|
|
{
|
2024-08-09 06:37:00 +00:00
|
|
|
$this->maintenanceHistory = self::getMaintenanceHistory($maintenanceHistory, true);
|
|
|
|
if (!empty($this->maintenanceHistory->finished_at)) {
|
2024-08-08 08:33:21 +00:00
|
|
|
abort(404);
|
|
|
|
}
|
2024-08-07 08:30:51 +00:00
|
|
|
|
2024-08-07 07:54:20 +00:00
|
|
|
foreach ($this->maintenanceHistory->historyHosts as $maintenanceHost) {
|
2024-08-09 06:37:00 +00:00
|
|
|
$this->maintenance_host_skipped[$maintenanceHost->id] = (bool)$maintenanceHost->is_skipped;
|
2024-08-07 07:54:20 +00:00
|
|
|
foreach ($maintenanceHost->historyTasks as $maintenanceTask) {
|
|
|
|
$this->maintenance_task_status[$maintenanceHost->id][$maintenanceTask->id] = (!empty($maintenanceTask->finished_at) ? true : false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:37:00 +00:00
|
|
|
public function updatedMaintenanceHostSkipped($value)
|
|
|
|
{
|
|
|
|
$skipped = [];
|
|
|
|
$notSkipped = [];
|
|
|
|
|
|
|
|
foreach ($this->maintenance_host_skipped as $host_history_id => $is_skipped) {
|
|
|
|
if ($is_skipped === true) {
|
|
|
|
$skipped[] = $host_history_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notSkipped[] = $host_history_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($skipped) != 0) {
|
|
|
|
MaintenanceHostHistory::whereIn('id', $skipped)->update([
|
|
|
|
'is_skipped' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($notSkipped) != 0) {
|
|
|
|
MaintenanceHostHistory::whereIn('id', $notSkipped)->update([
|
|
|
|
'is_skipped' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 07:54:20 +00:00
|
|
|
public function updatedMaintenanceTaskStatus($value)
|
|
|
|
{
|
|
|
|
foreach ($this->maintenance_task_status as $host_history_id => $tasks) {
|
2024-08-07 08:30:51 +00:00
|
|
|
|
2024-08-07 07:54:20 +00:00
|
|
|
$done = [];
|
|
|
|
$notDone = [];
|
|
|
|
|
|
|
|
foreach ($tasks as $task_history_id => $state) {
|
2024-08-09 06:37:00 +00:00
|
|
|
if (isset($this->maintenance_task_status_old[$host_history_id][$task_history_id]) && $this->maintenance_task_status_old[$host_history_id][$task_history_id] === $state) {
|
2024-08-07 08:30:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-07 07:54:20 +00:00
|
|
|
if ($state === true) {
|
|
|
|
$done[] = $task_history_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notDone[] = $task_history_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($done) != 0) {
|
2024-08-07 08:30:51 +00:00
|
|
|
MaintenanceTaskHistory::where('maintenance_task_histories.maintenance_host_history_id', $host_history_id)
|
|
|
|
->where('maintenance_task_histories.status', '!=', 2)
|
2024-08-07 07:54:20 +00:00
|
|
|
->whereIn('maintenance_task_histories.id', $done)
|
|
|
|
->update([
|
|
|
|
'maintenance_task_histories.status' => 2,
|
|
|
|
'maintenance_task_histories.finished_at' => Carbon::now(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($notDone) != 0) {
|
2024-08-07 08:30:51 +00:00
|
|
|
MaintenanceTaskHistory::where('maintenance_task_histories.maintenance_host_history_id', $host_history_id)
|
|
|
|
->where('maintenance_task_histories.status', '!=', 1)
|
2024-08-07 07:54:20 +00:00
|
|
|
->whereIn('maintenance_task_histories.id', $notDone)
|
|
|
|
->update([
|
|
|
|
'maintenance_task_histories.status' => 1,
|
|
|
|
'maintenance_task_histories.finished_at' => null,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-08-07 08:30:51 +00:00
|
|
|
|
|
|
|
$this->maintenance_task_status_old = $this->maintenance_task_status;
|
2024-08-07 07:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
2024-08-07 08:30:51 +00:00
|
|
|
$this->maintenanceHistory = self::getMaintenanceHistory($this->maintenanceHistory->id);
|
2024-08-07 07:54:20 +00:00
|
|
|
return view('livewire.maintenance.progress-form');
|
|
|
|
}
|
2024-08-07 08:30:51 +00:00
|
|
|
|
2024-08-09 06:37:00 +00:00
|
|
|
private static function getMaintenanceHistory(int $maintenanceHistoryId, bool $force = false)
|
|
|
|
{
|
|
|
|
if ($force){
|
|
|
|
Cache::forget("maintenance_history_{$maintenanceHistoryId}");
|
|
|
|
}
|
|
|
|
|
2024-08-07 08:30:51 +00:00
|
|
|
return Cache::remember("maintenance_history_{$maintenanceHistoryId}", 60, function () use ($maintenanceHistoryId) {
|
|
|
|
return MaintenanceHistory::with([
|
|
|
|
'maintenance.tasks',
|
2024-08-07 09:50:10 +00:00
|
|
|
'maintenance.hosts',
|
2024-08-07 08:30:51 +00:00
|
|
|
'historyHosts',
|
2024-08-07 09:50:10 +00:00
|
|
|
'historyHosts.host',
|
2024-08-07 08:30:51 +00:00
|
|
|
'historyHosts.historyTasks',
|
|
|
|
'historyHosts.historyTasks.maintenanceTask',
|
|
|
|
'historyHosts.historyTasks.maintenanceTask.task'
|
|
|
|
])->find($maintenanceHistoryId);
|
|
|
|
});
|
|
|
|
}
|
2024-08-07 07:54:20 +00:00
|
|
|
}
|