Files
backend/app/Console/Commands/DailyMovingMachineCommand.php
2025-12-29 11:45:42 +03:30

85 lines
2.7 KiB
PHP

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