|
@@ -0,0 +1,121 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Jobs;
|
|
|
|
|
+
|
|
|
|
|
+use App\library\zop\ZopClient;
|
|
|
|
|
+use App\library\zop\ZopProperties;
|
|
|
|
|
+use App\library\zop\ZopRequest;
|
|
|
|
|
+use App\Services\OrderPackageReceivedSyncService;
|
|
|
|
|
+use Illuminate\Bus\Queueable;
|
|
|
|
|
+use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
+use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
+use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
+use Illuminate\Queue\SerializesModels;
|
|
|
|
|
+use Illuminate\Support\Carbon;
|
|
|
|
|
+
|
|
|
|
|
+class LogisticZopSync implements ShouldQueue
|
|
|
|
|
+{
|
|
|
|
|
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
+
|
|
|
|
|
+ protected $logistic_number;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create a new job instance.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param $logistic_number
|
|
|
|
|
+ */
|
|
|
|
|
+ public function __construct($logistic_number)
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->logistic_number = $logistic_number;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Execute the job.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ public function handle()
|
|
|
|
|
+ {
|
|
|
|
|
+ //
|
|
|
|
|
+ ini_set('max_execution_time', 10);
|
|
|
|
|
+ sleep(12);
|
|
|
|
|
+ $zopResult = [];
|
|
|
|
|
+ $url = config('api_logistic.ZTO.url');
|
|
|
|
|
+ $xAppKey = config('api_logistic.ZTO.x-appKey');
|
|
|
|
|
+ $appSecret = config('api_logistic.ZTO.appSecret');
|
|
|
|
|
+ $properties = new ZopProperties($xAppKey, $appSecret);
|
|
|
|
|
+ $client = new ZopClient($properties);
|
|
|
|
|
+ $request = new ZopRequest();
|
|
|
|
|
+
|
|
|
|
|
+ $request->setUrl($url);
|
|
|
|
|
+ $request->setBody(json_encode([
|
|
|
|
|
+ 'billCode' => $this->logistic_number,
|
|
|
|
|
+ ]));
|
|
|
|
|
+ $response = json_decode($client->execute($request));
|
|
|
|
|
+ if ($response->status) {
|
|
|
|
|
+ $zopResult[] = [
|
|
|
|
|
+ 'routes' => $response->result,
|
|
|
|
|
+ 'logisticNum' => $this->logistic_number,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ $result = $this->transformRoutes($zopResult);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
|
|
|
|
|
+ */
|
|
|
|
|
+ $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
|
|
|
|
|
+ $orderPackageReceivedSyncService->update($result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 转换快递路由信息
|
|
|
|
|
+ * @param array $routs 快递路由
|
|
|
|
|
+ * @return array
|
|
|
|
|
+ */
|
|
|
|
|
+ public function transformRoutes(array $routs): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $result = [];
|
|
|
|
|
+ foreach ($routs as $route) {
|
|
|
|
|
+ $resultItem = [];
|
|
|
|
|
+ $resultItem['logistic_number'] = $route['logisticNum'];
|
|
|
|
|
+ $itemRoutes = $route['routes'];
|
|
|
|
|
+ if (empty($itemRoutes)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $lastRoute = $itemRoutes[count($itemRoutes) - 1];
|
|
|
|
|
+ switch ($lastRoute->scanType) {
|
|
|
|
|
+ case '收件':
|
|
|
|
|
+ $resultItem['status'] = '已揽收';
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '到件':
|
|
|
|
|
+ case '发件':
|
|
|
|
|
+ $resultItem['status'] = '在途';
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'ARRIVAL':
|
|
|
|
|
+ case '派件':
|
|
|
|
|
+ $resultItem['status'] = '派送中';
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'SIGNED':
|
|
|
|
|
+ case '签收':
|
|
|
|
|
+ $resultItem['status'] = '已收件';
|
|
|
|
|
+ $resultItem['received_at'] = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ $transfer_status = [];
|
|
|
|
|
+ foreach ($itemRoutes as $item) {
|
|
|
|
|
+ $data = [];
|
|
|
|
|
+ $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->toDateTimeString();
|
|
|
|
|
+ $scanSite = $item->scanSite;
|
|
|
|
|
+ $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
|
|
|
|
|
+ $data['remark'] = $item->scanType;
|
|
|
|
|
+
|
|
|
|
|
+ $transfer_status[] = $data;
|
|
|
|
|
+ }
|
|
|
|
|
+ $resultItem['transfer_status'] = $transfer_status;
|
|
|
|
|
+ $result[] = $resultItem;
|
|
|
|
|
+ }
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|