Files
backend/app/Console/Commands/DailyMovingMachineCommand.php
2025-12-01 14:17:12 +03:30

79 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\ActivityMachineType;
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): void
{
$vehiclesInfo = $dailyMovingMachines->run();
$response = collect($vehiclesInfo['vehiclesInfo']);
$date = Carbon::yesterday()->toDateString();
$cmms = $response->pluck('machineCode');
$mission = Mission::query()
->whereDate('start_time',$date)
->pluck('machine_code');
$cmmsOnly = $cmms->diff($mission);
$missionOnly = $mission->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) {
$missionId = Mission::query()->where('machine_code', $code)
->whereDate('start_time', $date)
->value('id');
MissionViolation::query()->create([
'machine_code' => $code,
'mission_id' => $missionId,
'type' => ActivityMachineType::ADAM_TAHAROK->value,
'request_date' => $date,
]);
}
$this->info("ماشین‌های متحرک بدون مأموریت: " . $cmmsOnly->count());
$this->info("مأموریت‌های بدون حرکت در CMMS: " . $missionOnly->count());
}
}