Files
backend/app/Console/Commands/DailyMovingMachineCommand.php

94 lines
2.7 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\ActivityMachineType;
use App\Models\Mission;
use App\Models\DailyMoveMachine;
use App\Services\CMMS\DailyMovingMachinesService;
use Exception;
use Illuminate\Console\Command;
class DailyMovingMachineCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daily_moving_machine';
/**
* The console command description.
*
* @var string
*/
protected $description = 'receive cmms cars information and compare my information';
/**
* Execute the console command.
* @throws Exception
*/
public function handle(DailyMovingMachinesService $dailyMovingMachines): void
{
$response = $dailyMovingMachines->run();
$date = now()->toDateString();
$cmms = $response
->pluck('machineCode');
$mission = Mission::query()
->whereDate('start_time',today())
->pluck('machine_code');
$cmmsOnly = $cmms->diff($mission);
$missionOnly = $mission->diff($cmms);
$matching = $cmms->intersect($mission);
foreach ($matching as $code) {
$cmm = (object) $response->firstWhere('machineCode', $code);
$missionRecord = Mission::where('machine_code', $code)
->whereDate('start_time', today())
->first();
DailyMoveMachine::query()->create([
'machine_code' => $code,
'mission_id' => $missionRecord->id ,
'type' => ActivityMachineType::MOTABEGAT->label(),
'request_date' => $date,
'mileage' => $cmm->mileage,
'exit_time' => $cmm->exit_time,
'enter_time' => $cmm->enter_time,
]);
}
foreach ($cmmsOnly as $code) {
$cmm = (object) $response->firstWhere('machineCode', $code);
DailyMoveMachine::query()->create([
'machine_code' => $code,
'type' => ActivityMachineType::TEDADE_MACHINES_BISHTAR_AS_MISSION->label(),
'request_date' => $date,
'mileage' => $cmm->mileage,
'exit_time' => $cmm->exit_time,
'enter_time' => $cmm->enter_time,
]);
}
foreach ($missionOnly as $code) {
DailyMoveMachine::create([
'machine_code' => $code,
'mission_id' => $mission->id,
'type' => ActivityMachineType::TEDADE_MISSIONS_BISHTAR_AS_MACHINES->label(),
'request_date' => $date,
]);
}
$this->info('Diffs saved.');
}
}