OrderPackageReceivedSyncService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Services;
  3. use App\Jobs\LogisticYDSync;
  4. use App\Jobs\LogisticZopSync;
  5. use App\OrderPackage;
  6. use Carbon\Carbon;
  7. use Exception;
  8. use Illuminate\Database\Eloquent\Collection;
  9. class OrderPackageReceivedSyncService
  10. {
  11. protected $logisticSFService;
  12. protected $logisticZopService;
  13. /**
  14. * 同步快递信息
  15. * 1 如果当前时间大于初始化时间 每日执行一次,更新order_packages中创建时间大于初始化时间,没有异常,用户未收货的全部订单的快递路由状态
  16. * 2 如果当前时间小于等于初始化时间,执行初始化脚本,将数据库中全部小于等于初始化时间的数据更新
  17. * @throws Exception
  18. */
  19. public function syncLogisticRoute()
  20. {
  21. $logisticNumbers = $this->getLogisticNumbers();
  22. $this->update($this->getLogisticRoutes($logisticNumbers));
  23. //更新中通
  24. $ZTOLogisticNumbers = $logisticNumbers['ZTO'];
  25. foreach ($ZTOLogisticNumbers as $logisticNumber) {
  26. LogisticZopSync::dispatch($logisticNumber);
  27. }
  28. //更新韵达
  29. $YDLogisticNumbers = $logisticNumbers['YUNDA'];
  30. foreach ($YDLogisticNumbers as $logistic_number) {
  31. LogisticYDSync::dispatch($logistic_number);
  32. }
  33. }
  34. /**
  35. * 根据传递的承运商与快递单号更新快递信息
  36. * @param array $logisticNumbers 快递单号
  37. * example: ['SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'],'ZT'=>['75424148714142','548464120822', '75424147834290']....]
  38. * @throws Exception 快递接口调用或者返回的信息有误,无法更新指定的快递路由信息
  39. */
  40. public function syncLogisticRouteApi(array $logisticNumbers)
  41. {
  42. $this->update($this->getLogisticRoutes($logisticNumbers));
  43. }
  44. /**
  45. * 获取快件揽收信息
  46. * @param array $request [
  47. * 'SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'],
  48. * 'ZT'=>['75424148714142','548464120822', '75424147834290']
  49. * ]
  50. * @return array
  51. * @throws Exception
  52. */
  53. public function getLogisticRoutes(array $request): array
  54. {
  55. $this->logisticSFService = new LogisticSFService();
  56. $resultSF = [];
  57. $resultYD = [];
  58. $resultYT = [];
  59. $resultOther = [];
  60. foreach ($request as $key => $logisticNums) {
  61. switch ($key) {
  62. case "SF":
  63. $resultSF = $this->logisticSFService->get($logisticNums);
  64. break;
  65. case "YD":
  66. $resultYD = [];
  67. break;
  68. case "YT":
  69. $resultYT = [];
  70. break;
  71. default:
  72. $resultOther = [];
  73. break;
  74. }
  75. }
  76. return array_merge($resultSF, $resultYD, $resultYT, $resultOther);
  77. }
  78. /**
  79. * 根据快递单号更新状态
  80. * @param array $orderPackages
  81. */
  82. public function update(array $orderPackages)
  83. {
  84. foreach ($orderPackages as $data) {
  85. $orderPackage = OrderPackage::query()->where('logistic_number', $data['logistic_number'])->first();
  86. if (isset($data['status'])) $orderPackage->status = $data['status'];
  87. if (isset($data['received_at'])) $orderPackage->received_at = $data['received_at'];
  88. if (isset($data['exception'])) $orderPackage->exception = $data['exception'];
  89. if (isset($data['transfer_status'])) $orderPackage->transfer_status = $data['transfer_status'];
  90. if (isset($data['exception_type'])) $orderPackage->exception_type = $data['exception_type'];
  91. $orderPackage->save();
  92. }
  93. }
  94. /**
  95. * 查询当前日期前的快递单号并按照承运商分类
  96. */
  97. public function getLogisticNumbers(): array
  98. {
  99. //初始化时间 2020-12-31 23:59:59
  100. $initDate = Carbon::parse(config('api_logistic.init_date'));
  101. $query = OrderPackage::query()
  102. ->with(['order' => function ($query) {
  103. return $query->with('logistic');
  104. }]);
  105. if (Carbon::now()->lte($initDate)) {//当前时间小于等于初始化时间
  106. //初始化查询一个月的数据,exception为否
  107. $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
  108. ->whereNull('received_at');
  109. } else {//查询20天以内的数据
  110. $query = $query->where('sent_at', '>=', now()->subDays(20))
  111. ->whereNull('received_at');
  112. }
  113. $result = [];
  114. $query->chunk(200, function ($orderPackages) use (&$result) {
  115. return $result = array_merge($result, $this->buildData($orderPackages));
  116. });
  117. return $result;
  118. }
  119. /**
  120. * 将orderPackage集合分类并摘取指定数据
  121. * @param Collection $orderPackages
  122. * @return array
  123. */
  124. private function buildData(Collection $orderPackages): array
  125. {
  126. $data = [];
  127. foreach ($orderPackages as $orderPackage) {
  128. try {
  129. $logisticCode = $orderPackage->order->logistic->code;
  130. } catch (Exception $e) {
  131. continue;
  132. }
  133. $key = config('api_logistic.logistic.' . $logisticCode);
  134. if (!isset($data[$key])) {
  135. $data[$key] = [];
  136. }
  137. $data[$key][] = $orderPackage->logistic_number;
  138. }
  139. return $data;
  140. }
  141. }