OrderPackageReceivedSyncService.php 3.0 KB

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