<?php


namespace App\Services;


use App\library\zop\ZopClient;
use App\library\zop\ZopProperties;
use App\library\zop\ZopRequest;
use Carbon\Carbon;

class LogisticZopService
{


    /**
     * 获取中通快递路由信息
     * @param array $logisticNums
     * @return array
     */
    public function get(array $logisticNums): array
    {
        $result = [];

        $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);
        foreach ($logisticNums as $logisticNum) {
            $request->setBody(json_encode([
                'billCode' => $logisticNum,
            ]));
            $response = json_decode($client->execute($request));

            if ($response->status) {
                $result[] = [
                    'routes' => $response->result,
                    'logisticNum' => $logisticNum,
                ];
            } else {
//                throw new WarningException("单号没有查询到快递路由信息','LogisticZopService->get->{$logisticNum}");
            }
        }
        return $this->transformRoutes($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;
    }
}
