package cn.pluss.platform.device.impl;

import cn.pluss.platform.device.MercOrderDetailService;
import cn.pluss.platform.device.MercOrderExpressService;
import cn.pluss.platform.entity.MercOrderDetail;
import cn.pluss.platform.entity.MercOrderExpress;
import cn.pluss.platform.enums.LogisticProvider;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MercOrderExpressMapper;
import cn.pluss.platform.util.*;
import cn.pluss.platform.vo.MercOrderExpressVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MercOrderExpressServiceImpl extends ServiceImpl<MercOrderExpressMapper, MercOrderExpress> implements MercOrderExpressService {
    public static final String CHECK_Express = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
    @Resource
    private MercOrderDetailService mercOrderDetailService;
    /**
     * 根据上送物流单号查询商户单号
     * @param logistNo
     * @return
     */
    @Override
    public MercOrderExpress getByLogistNo(String logistNo) {
        return baseMapper.getByLogistNo(logistNo);
    }

    @Override
    public MercOrderExpress getByExpressNo(String expressNo) {
        return baseMapper.getByExpressNo(expressNo);
    }

    @Override
    public JSONObject getExpressInfo(String orderNo) throws Exception {
        List<MercOrderDetail> detailByOrderNo = mercOrderDetailService.getDetailByOrderNo(orderNo, "01");

        if (detailByOrderNo == null || detailByOrderNo.get(0).getExpressNo() == null){
            throw new MsgException("暂无相应的快递单号");
        }

        JSONObject reqData = new JSONObject();
        String subThree = detailByOrderNo.get(0).getExpressNo();
        if ("268".equals(subThree) || "368".equals(subThree) || "468".equals(subThree)|| "334".equals(subThree)
           || "888".equals(subThree) || "77".equals(detailByOrderNo.get(0).getExpressNo().substring(0,2))){
            reqData.put("ShipperCode", LogisticProvider.STO.getCode());
        } else if ("YT".equals(detailByOrderNo.get(0).getExpressNo().substring(0,2))){
            reqData.put("ShipperCode", LogisticProvider.YTO.getCode());
        } else if ("55".equals(detailByOrderNo.get(0).getExpressNo().substring(0,2))){
            reqData.put("ShipperCode", LogisticProvider.HTKY.getCode());
        }else {
            return new JSONObject();
        }

        reqData.put("LogisticCode", detailByOrderNo.get(0).getExpressNo());

        JSONObject jsonObject = reqJsonData(reqData);
        if (!(Boolean) jsonObject.get("Success")){
            log.info("快递查询错误===========>",jsonObject.get("Reason"));
            throw new MsgException("快递查询失败");
        }
            return jsonObject;

    }
    public JSONObject reqJsonData(JSONObject requestData)throws Exception{

        Map<String, Object> hashMap = new HashMap<>(16);
        hashMap.put("RequestData", requestData);
        hashMap.put("EBusinessID", ParametersUtil.USERID);
        hashMap.put("RequestType", "1002");
        hashMap.put("DataSign", dataSign(requestData));
        hashMap.put("DataType", "2");

        HttpResult httpResult = HttpUtil.doPost(CHECK_Express, hashMap);
        JSONObject jsonObject = JSON.parseObject(httpResult.getBody());
        return jsonObject;
    }
    private  String dataSign(JSONObject requestData)throws Exception{
        String sign = requestData.toString()+ParametersUtil.APIKEY;
        //首先再进行MD5
        String lowerMD5 = MD5Util.md5(sign).toLowerCase();
        //然后再进行Base64
        final byte[] lowerMD5Bytes = lowerMD5.getBytes(StandardCharsets.UTF_8);
        String base64= Base64.getEncoder().encodeToString(lowerMD5Bytes);
        //再进行base64URL编码
        return URLEncoder.encode(base64, "UTF-8");
    }


}
