<?php
/**
 * 配送订单管理
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/11/12
 * Time: 3:56 PM
 */
namespace Util\DaDa;

use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;

class Order
{
    private $objCommon;

    // 基础域名
    private $baseUrl = 'http://newopen.qa.imdada.cn';

    //公共header头
    private  $commonHeader = [
        CURLOPT_HTTPHEADER => [
            'Content-Type: application/json'
        ],
    ];

    public function __construct($appKey='', $appSecret = '', $sourceId)
    {
        $this->objCommon = new Common($appKey, $appSecret, $sourceId);
    }

    /**
     * 官网文档地址: http://newopen.imdada.cn/#/development/file/add?_k=yytpqx
     * 新增配送单接口
     */
    public function addOrder($params)
    {
        $postData = $this->objCommon->CommonRequestParams();
        // 业务请求参数
        $postData['body'] = json_encode([
            'shop_no'    => $params['shop_no'],
            'origin_id'  => $params['origin_id'],
            'city_code'  => $params['city_code'],
            'cargo_price' => $params['cargo_price'],
            'cargo_weight' => $params['cargo_weight'],
            'is_prepay'   => $params['is_prepay'],
            'receiver_name' => $params['receiver_name'],
            'receiver_address' => $params['receiver_address'],
            'receiver_lat' => $params['receiver_lat'],
            'receiver_lng' => $params['receiver_lng'],
            'callback'     => $params['callback'],
        ]);
        $postData['signature'] = $this->objCommon->getSignature($postData);

        $url = $this->baseUrl . '/api/order/addOrder';
        $response = request($url, json_encode($postData, JSON_UNESCAPED_UNICODE), 5, true, $this->commonHeader);
        $result = $this->objCommon->commonResponse($response);
        if (!$result->isSuccess()) {
            return ResultWrapper::fail($result->getData(), $result->getErrorCode());
        }

        return ResultWrapper::success($result->getData());
    }
}