package com.org.hktshop.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.org.hktshop.entity.order.HktOrder;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.properties.KuaidiniaoProperties;
import com.org.hktshop.service.order.HktOrderService;
import com.org.hktshop.service.user.KuaidiniaoService;
import com.org.hktshop.utils.HttpUtil;
import com.org.hktshop.utils.StringUtils;
import com.org.hktshop.utils.coder.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hotking03
 */
@Service
public class KuaidiniaoServiceImpl implements KuaidiniaoService {
    @Autowired
    private KuaidiniaoProperties kuaidiniaoProperties;

    @Autowired
    private HktOrderService hktOrderService;

    @Override
    public JSONObject getOrderTraces(String expNo, HktUser hktUser) throws Exception {
        String expCode = "";
        if(expNo.startsWith("SF")){
            expCode = "SF";
        }else if(expNo.startsWith("SUR")){
            expCode = "SURE";
        }else {
            expCode = "LB";
        }
        String requestData = "{'OrderCode':'','CustomerName':'"+hktUser.getMobile().substring(7)+"','ShipperCode':'" + expCode + "','LogisticCode':'" + expNo + "'}";
        System.out.println(requestData);
        Map<String, String> params = new HashMap<>();
        params.put("RequestData", SecurityUtil.urlEncoder(requestData));
        params.put("EBusinessID", kuaidiniaoProperties.getEBusinessId());
        params.put("RequestType", "8002");
        String dataSign = SecurityUtil.encrypt(requestData, kuaidiniaoProperties.getAppKey());
        params.put("DataSign", SecurityUtil.urlEncoder(dataSign));
        params.put("DataType", "2");
        String s = params.toString();
        String result = HttpUtil.post(kuaidiniaoProperties.getRequestUrl(), params);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }

    @Override
    public Object getLogisticsInfo(HktOrder hktOrder) throws Exception{
        HktOrder orderInfo = hktOrderService.getHktOrderById(hktOrder.getOrderSn());
        if(orderInfo == null){
            return "请输入正确得订单编号";
        }
        String tel = orderInfo.getMobile();
        String expCode = "";
        String expNo = orderInfo.getInvoiceNo();
        if(StringUtils.isNull(expNo)){
            return "订单没有可关联得物流单号";
        }
        if(expNo.startsWith("SF")){
            expCode = "SF";
        }else if(expNo.startsWith("SUR")){
            expCode = "SURE";
        }else {
            expCode = "LB";
        }
        String requestData = "{'OrderCode':'','CustomerName':'"+tel.substring(7)+"','ShipperCode':'" + expCode + "','LogisticCode':'" + expNo + "'}";
        System.out.println(requestData);
        Map<String, String> params = new HashMap<>();
        params.put("RequestData", SecurityUtil.urlEncoder(requestData));
        params.put("EBusinessID", kuaidiniaoProperties.getEBusinessId());
        params.put("RequestType", "8002");
        String dataSign = SecurityUtil.encrypt(requestData, kuaidiniaoProperties.getAppKey());
        params.put("DataSign", SecurityUtil.urlEncoder(dataSign));
        params.put("DataType", "2");
        String result = HttpUtil.post(kuaidiniaoProperties.getRequestUrl(), params);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }
}
