mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-24 15:04:32 +00:00
82 lines
2.6 KiB
PHP
82 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\V2;
|
|
|
|
|
|
use App\Classes\Jobs\Commands\V2\YD\FetchTrakingInfoYDByPackingListV2CommandJob;
|
|
use App\Classes\Jobs\Commands\V2\YD\FetchTrakingInfoYDByTrackingNoV2CommandJob;
|
|
use App\Classes\ValueObjects\Constants\PackingListType;
|
|
use App\Models\PackingList;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ProcessYDByTrakingNoDataV2Command extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'process-yd-by-traking-no-data-command {trackingNo?}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Process data from YD Portal by traking no';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$trackingNo = $this->argument('trackingNo');
|
|
|
|
if ($trackingNo) {
|
|
$lockKey = "process_yd_tracking_no:{$trackingNo}";
|
|
$lock = Cache::lock($lockKey, 300);
|
|
|
|
if (!$lock->get()) {
|
|
Log::info("Tracking No {$trackingNo} is already being processed. Try again later.");
|
|
return;
|
|
}
|
|
|
|
dispatch(new FetchTrakingInfoYDByTrackingNoV2CommandJob($trackingNo));
|
|
Log::info("Job dispatched for tracking number: {$trackingNo}");
|
|
}
|
|
else {
|
|
$jobs = $this->fetchByTrakingNoYdPortalV2CommandJob();
|
|
|
|
foreach ($jobs as $index => $job) {
|
|
$delayInSeconds = intdiv($index, 2);
|
|
dispatch($job)->delay(now()->addSeconds($delayInSeconds));
|
|
}
|
|
}
|
|
}
|
|
|
|
private function fetchByTrakingNoYdPortalV2CommandJob(){
|
|
$jobs = [];
|
|
|
|
$maxFetchTime = Carbon::now()->subMonths(4);
|
|
$packingLists = PackingList::where('type', PackingListType::SHIPPING_PACKING_LIST)->whereDate('created_at', '>=', $maxFetchTime)->get();
|
|
$count = 0;
|
|
foreach ($packingLists as $packingList) {
|
|
$jobs[] = new FetchTrakingInfoYDByPackingListV2CommandJob($packingList);
|
|
Log::info('Dispatched FetchByTrakingNoYdPortalV2CommandJob for packingList with id ' . $packingList->id . ' reference ' . $packingList->reference . ' created_at ' . $packingList->created_at);
|
|
$count++;
|
|
// Log::info('Dispatched count '. $count);
|
|
// if ($count === 50) {
|
|
// break;
|
|
// }
|
|
}
|
|
Log::info('Total FetchByTrakingNoYdPortalV2CommandJob dispatched: ' . $count);
|
|
|
|
return $jobs;
|
|
}
|
|
}
|