OrderPackageReceivedSyncService.php 3.9 KB

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