LogisticYTOSync.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Jobs;
  3. use App\OrderPackage;
  4. use App\Services\LogisticYTOService;
  5. use App\Services\LogService;
  6. use App\Services\OrderPackageReceivedSyncService;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. class LogisticYTOSync implements ShouldQueue
  13. {
  14. public $tries = 2;
  15. public $timeout = 10;
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. /**
  18. * @var $logisticYTOService LogisticYTOService
  19. * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
  20. * @var $logistic_number string
  21. */
  22. protected $logistic_number;
  23. protected $logisticYTOService;
  24. /** @var orderPackageReceivedSyncService OrderPackageReceivedSyncService */
  25. protected $orderPackageReceivedSyncService;
  26. /**
  27. * LogisticYDSync constructor.
  28. * @param $logistic_number
  29. */
  30. public function __construct($logistic_number)
  31. {
  32. $this->logistic_number = $logistic_number;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. LogService::log(LogisticYTOSync::class, "JOB-YTO", $this->logistic_number);
  42. //标记上有同步的操作
  43. OrderPackage::query()->where('logistic_number', $this->logistic_number)->update(['sync_routes_flag'=> true]);
  44. $this->logisticYTOService = app('LogisticYTOService');
  45. $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  46. $nativeResponse = $this->logisticYTOService->query($this->logistic_number);
  47. $formattedData = $this->logisticYTOService->format($nativeResponse,$this->logistic_number);
  48. try {
  49. if ($formattedData && isset($formattedData['logistic_number'])) $this->orderPackageReceivedSyncService->update([$formattedData]);
  50. } catch (\Exception $e) {
  51. LogService::log(LogisticYTOService::class, "YTO快递无快递单号异常", json_encode($formattedData));
  52. }
  53. }
  54. }