update($this->getLogisticRoutes($this->getLogisticNumbers())); } /** * 根据传递的承运商与快递单号更新快递信息 * @param array $logisticNumbers 快递单号 * example: ['SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'],'ZT'=>['75424148714142','548464120822', '75424147834290']....] * @throws Exception 快递接口调用或者返回的信息有误,无法更新指定的快递路由信息 */ public function syncLogisticRouteApi(array $logisticNumbers) { $this->update($this->getLogisticRoutes($logisticNumbers)); } /** * 获取快件揽收信息 * @param array $request [ * 'SF' => ['SF1038651915891', 'SF1038651413847', 'SF1038611050071'], * 'ZT'=>['75424148714142','548464120822', '75424147834290'] * ] * @return array * @throws Exception */ public function getLogisticRoutes(array $request): array { $this->logisticSFService = new LogisticSFService(); $this->logisticZopService = new LogisticZopService(); $resultSF = []; $resultZT = []; $resultYD = []; $resultYT = []; $resultOther = []; foreach ($request as $key => $logisticNums) { switch ($key) { case "SF": $resultSF = $this->logisticSFService->get($logisticNums); break; case "ZTO": // $resultZT = $this->logisticZopService->get($logisticNums); $resultZT = []; break; case "YD": $resultYD = []; break; case "YT": $resultYT = []; break; default: $resultOther = []; break; } } return array_merge($resultSF, $resultYD, $resultYT, $resultZT, $resultOther); } public function update(array $orderPackages) { foreach ($orderPackages as $data) { $orderPackage = OrderPackage::query()->where('logistic_number', $data['logistic_number'])->first(); if (isset($data['status'])) $orderPackage->status = $data['status']; if (isset($data['received_at'])) $orderPackage->received_at = $data['received_at']; if (isset($data['exception'])) $orderPackage->exception = $data['exception']; if (isset($data['transfer_status'])) $orderPackage->transfer_status = $data['transfer_status']; $orderPackage->save(); } } /** * 查询当前日期前的快递单号并按照承运商分类 */ public function getLogisticNumbers(): array { //初始化时间 2020-12-31 23:59:59 $initDate = Carbon::parse(config('api_logistic.init_date')); $data = []; $query = OrderPackage::query() ->with(['order' => function ($query) { return $query->with('logistic'); }]); if (Carbon::now()->lte($initDate)) {//当前时间小于等于初始化时间 //初始化查询一个月的数据,exception为否 $query = $query->where('created_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString()) ->where('exception', '否'); } else {//当前时间大于初始化时间,exception为否且未收货 $query = $query->where('created_at', '>=', $initDate->toDateTimeString()) ->where('exception', '否') ->whereNull('received_at'); } return $this->buildData($query->get(), $data); } /** * 将orderPackage集合分类并摘取指定数据 * @param Collection $orderPackages * @param array $data * @return array */ private function buildData(Collection $orderPackages, array $data): array { foreach ($orderPackages as $orderPackage) { try { $logisticCode = $orderPackage->order->logistic->code; } catch (Exception $e) { continue; } $key = config('api_logistic.logistic.' . $logisticCode); if (!isset($data[$key])) { $data[$key] = []; } $data[$key][] = $orderPackage->logistic_number; } return $data; } }