<?php


namespace App\Http\Controllers\api\thirdPart\hengli;

use App\Http\Controllers\api\thirdPart\weight\WeightBaseController;
use Illuminate\Http\Request;

class PackageController extends WeightBaseController
{
    // 参数
    protected $weight = 'weight';     // 重量
    protected $length = 'length';     // 长
    protected $width = 'width';      // 宽
    protected $height = 'height';     // 高
    protected $code = 'code';       // 快递单号
    protected $weight_at = 'weight_at';  // 称重时间
    protected $hid = 'hid';        // 称重设备id
    protected $name = 'HengLi';       // 名称

    public function new_(Request $request)
    {
        app('LogService')->log(__METHOD__, $this->name, "记录上传日志：" . json_encode($request->all()) , null);
        return $this->new($request);
    }

    public function conversionRequest($request)
    {
        $params = [];
        $arr = $request->all();
        $data = array_key_first($arr);
        $data = explode(',', $data);
        foreach ($data as $key => $value) {
            switch ($key) {
                case '0':
                    $params['hid'] = $value;
                    break;
                case '1':
                    $params['code'] = $this->processCodeStr($value);
                    break;
                case '2':
                    $params['weight'] = $value;
                    break;
                case '3':
                    $params['weight_at'] = $value;
                    break;
                case '4':
                    $params['length'] = $value;
                    break;
                case '5':
                    $params['width'] = $value;
                    break;
                case '6':
                    $params['height'] = $value;
                    break;
                default:
                    break;
            }
        }
        return $params;
    }

    public function getWeightValue($params)
    {
        $value = $this->getValue($this->weight, $params);
        return str_replace('_', '.', $value);
    }

    // 信息返回

    // 返回称重成功信息
    public function getSuccessMessage($params, $orderPackage): array
    {
        return ['success' => true, 'message' => '称重成功'];
    }

    // 返回包裹未找到异常
    public function getNotFindOrderPackageMessage($params, $orderPackage): string
    {
        return json_encode(['success' => false, 'message' => '未找打包裹信息', JSON_UNESCAPED_UNICODE]);
    }

    // 返回富勒信息未找到异常
    public function getNotFindOrderHeaderMessage($params, $orderPackage): string
    {
        return json_encode(['success' => false, 'message' => '富勒信息未找到'], JSON_UNESCAPED_UNICODE);
    }

    // 返回称重下发错误
    public function getWeightMessage($orderPackage, $e)
    {
        return json_encode(['success' => false, 'message' => $e->getMessage], JSON_UNESCAPED_UNICODE);
    }

    public function validator(Request $request): array
    {
        $params = $this->conversionRequest($request);
        $errors = [];
        $weight = $this->getWeightValue($params);
        if (empty($weight) || $weight == '') {
            $errors['weight'] = '称重重量为空';
        }
        $code = $this->getCodeValue($params);
        $code = trim($code, "'");
        if (empty($code) || $code == '') {
            $errors['code'] = '快递单号为空';
        }
        $hid = $this->getValue($this->hid, $params);
        if (empty($hid) || $hid == '') {
            $errors['hid'] = '称重设备号不能为空';
        }
        return $errors;
    }

}
