LogisticYTOSync.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. protected $orderPackageReceivedSyncService;
  25. /**
  26. * LogisticYDSync constructor.
  27. * @param $logistic_number
  28. */
  29. public function __construct($logistic_number)
  30. {
  31. $this->logistic_number = $logistic_number;
  32. }
  33. /**
  34. * Execute the job.
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. LogService::log(LogisticYTOSync::class, "JOB-YTO", $this->logistic_number);
  41. //标记上有同步的操作
  42. OrderPackage::query()->where('logistic_number', $this->logistic_number)->update(['sync_routes_flag'=> true]);
  43. $this->logisticYTOService = app('LogisticYTOService');
  44. $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
  45. $nativeResponse = $this->logisticYTOService->query($this->logistic_number);
  46. $formattedData = $this->logisticYTOService->format($nativeResponse,$this->logistic_number);
  47. try {
  48. if ($formattedData && isset($formattedData['logistic_number'])) $this->orderPackageReceivedSyncService->update([$formattedData]);
  49. } catch (\Exception $e) {
  50. LogService::log(LogisticYTOService::class, "YTO快递无快递单号异常", json_encode($formattedData));
  51. }
  52. }
  53. }