| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace App\Services;
- use App\OrderPackage;
- use Carbon\Carbon;
- use Exception;
- use Illuminate\Database\Eloquent\Collection;
- class OrderPackageReceivedSyncService
- {
- protected $logisticSFService;
- protected $logisticZopService;
- /**
- * 同步快递信息
- * 1 如果当前时间大于初始化时间 每日执行一次,更新order_packages中创建时间大于初始化时间,没有异常,用户未收货的全部订单的快递路由状态
- * 2 如果当前时间小于等于初始化时间,执行初始化脚本,将数据库中全部小于等于初始化时间的数据更新
- * @throws Exception
- */
- public function syncLogisticRoute()
- {
- $this->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;
- }
- }
|