OrderPackageReceivedSyncService.php 5.2 KB

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