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

81 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\ActivityMachineType;
use App\Models\CMMSMachine;
use App\Models\Mission;
use App\Models\MissionViolation;
use App\Services\CMMS\DailyMovingMachinesService;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
class DailyMovingMachineCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'webservice: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,CMMSMachine $machine): void
{
$vehiclesInfo = $dailyMovingMachines->run();
$response = collect($vehiclesInfo['vehiclesInfo']);
$date = Carbon::yesterday()->toDateString();
$cmms = $response->pluck('machineCode');
$missions = Mission::query()
->whereDate('start_time',$date)
->with('machine')
->where('machine_id', '=',$machine->id )
->get();
$missionCodes = $missions->pluck('machine.machine_code')->filter();
$cmmsOnly = $cmms->diff($missionCodes);
$missionOnly = $missionCodes->diff($cmms);
foreach ($cmmsOnly as $code) {
$cmm = $response->firstWhere('machineCode', '=', $code);
MissionViolation::query()->create([
'machine_code' => $code,
'type' => ActivityMachineType::KHOROJ_BEDONE_MOGAVEZ->value,
'request_date' => $date,
'mileage' => $cmm['mileageKM'],
// 'exit_time' => $cmm['exit_time'],
// 'enter_time' => $cmm['enter_time'],
]);
}
foreach ($missionOnly as $code) {
MissionViolation::query()->create([
'machine_code' => $code,
'mission_id' => $missions->id,
'type' => ActivityMachineType::ADAM_TAHAROK->value,
'request_date' => $date,
]);
}
$this->info("ماشین‌های متحرک بدون مأموریت: " . $cmmsOnly->count());
$this->info("مأموریت‌های بدون حرکت در CMMS: " . $missionOnly->count());
}
}