OrderPackageReceivedSyncService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Services;
  3. use App\OrderPackage;
  4. use Carbon\Carbon;
  5. use Exception;
  6. use Illuminate\Database\Eloquent\Collection;
  7. class OrderPackageReceivedSyncService
  8. {
  9. protected $logisticSFService;
  10. protected $logisticZopService;
  11. /**
  12. *同步快递信息
  13. * @throws Exception
  14. */
  15. public function syncLogisticRoute()
  16. {
  17. $this->update($this->getLogisticRoutes($this->getLogisticNumbers()));
  18. }
  19. /**
  20. * 获取快件揽收信息
  21. * @param array $request [
  22. * 'SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'],
  23. * 'ZT'=>['75424148714142','548464120822', '75424147834290']
  24. * ]
  25. * @return array
  26. * @throws Exception
  27. */
  28. public function getLogisticRoutes(array $request): array
  29. {
  30. $this->logisticSFService = new LogisticSFService();
  31. $this->logisticZopService = new LogisticZopService();
  32. $resultSF = [];
  33. $resultZT = [];
  34. $resultYD = [];
  35. $resultYT = [];
  36. $resultOther = [];
  37. foreach ($request as $key => $logisticNums) {
  38. switch ($key) {
  39. case "SF":
  40. $resultSF = $this->logisticSFService->get($logisticNums);
  41. break;
  42. case "ZTO":
  43. // $resultZT = $this->logisticZopService->get($logisticNums);
  44. $resultZT = [];
  45. break;
  46. case "YD":
  47. $resultYD = [];
  48. break;
  49. case "YT":
  50. $resultYT = [];
  51. break;
  52. default:
  53. $resultOther = [];
  54. break;
  55. }
  56. }
  57. return array_merge($resultSF, $resultYD, $resultYT, $resultZT, $resultOther);
  58. }
  59. public function update(array $orderPackages)
  60. {
  61. foreach ($orderPackages as $data) {
  62. $orderPackage = OrderPackage::query()->where('logistic_number',$data['logistic_number'])->first();
  63. if (isset($data['status'])) $orderPackage->status = $data['status'];
  64. if (isset($data['received_at'])) $orderPackage->received_at = $data['received_at'];
  65. if (isset($data['exception'])) $orderPackage->exception = $data['exception'];
  66. if (isset($data['transfer_status'])) $orderPackage->transfer_status = $data['transfer_status'];
  67. $orderPackage->save();
  68. }
  69. }
  70. /**
  71. * 查询当前日期前的快递单号并按照承运商分类
  72. */
  73. public function getLogisticNumbers(): array
  74. {
  75. $initDate = Carbon::parse(config('api_logistic.init_date'));
  76. $data = [];
  77. $query = OrderPackage::query()
  78. ->with(['order' => function ($query) {
  79. return $query->with('logistic');
  80. }]);
  81. if (Carbon::now()->gt($initDate)) {
  82. $query = $query->where('created_at', '>=', $initDate->toDateTimeString()); //大于初始化时间的全部快递单号
  83. } else {
  84. $query = $query->where('created_at', '>=', $initDate->subMonths(1)->toDateTimeString()); //初始化查询一个月的数据
  85. }
  86. $query = $query
  87. ->where('exception', '否')
  88. ->whereNull('received_at');
  89. return $this->buildData($query->get(), $data);
  90. }
  91. /**
  92. * 将orderPackage集合分类并摘取指定数据
  93. * @param Collection $orderPackages
  94. * @param array $data
  95. * @return array
  96. */
  97. private function buildData(Collection $orderPackages, array $data): array
  98. {
  99. foreach ($orderPackages as $orderPackage) {
  100. try {
  101. $logisticCode = $orderPackage->order->logistic->code;
  102. } catch (Exception $e) {
  103. continue;
  104. }
  105. $key = config('api_logistic.logistic.' . $logisticCode);
  106. if (!isset($data[$key])) {
  107. $data[$key] = [];
  108. } else {
  109. $data[$key][] = $orderPackage->logistic_number;
  110. }
  111. }
  112. return $data;
  113. }
  114. }