package com.heyu.api.data.kuaidi100;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.heyu.api.data.dto.vv.KuaiDi100DTO;
import com.heyu.api.data.utils.StringUtils;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.api.Subscribe;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.*;
import com.kuaidi100.sdk.utils.SignUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * https://api.kuaidi100.com/document/5f0ffa8f2977d50a94e1023c
 */
@Slf4j
public class KuaiDi100Utils {


    public static String key = "DnqfHnzY4501";
    public static String customer = "9026FB96E97AF02582856DE4D70B27AF";

    public static KuaiDi100DTO getLogisticsInfo(String trackNumber) {
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        try {


            QueryTrackParam queryTrackParam = new QueryTrackParam();

            //queryTrackParam.setCom(LogisticsMapUtils.getCode("极兔速递"));

            queryTrackParam.setNum(trackNumber);
            // 添加此字段表示开通行政区域解析功能。空：关闭（默认）； 1：开通行政区域解析功能以及物流轨迹增加物流状态名称； 4: 开通行政解析功能以及物流轨迹增加物流高级状态名称、状态值并且返回出发、目的及当前城市信息； 8：在4的基础上额外返回预计到达时间和预计轨迹信息，支持的快递公司见在途时效预估支持的快递公司

            queryTrackParam.setResultv2("2");

            // 返回格式：0：json格式（默认），1：xml，2：html，3：text
            queryTrackParam.setShow("0");

            queryTrackParam.setNeedCourierInfo(true);


//        queryTrackParam.setPhone("17725390266");

            String param = new Gson().toJson(queryTrackParam);

            queryTrackReq.setParam(param);

            queryTrackReq.setCustomer(customer);


            queryTrackReq.setSign(SignUtils.querySign(param, key, customer));

            IBaseClient baseClient = new QueryTrack();
            HttpResult httpResult = baseClient.execute(queryTrackReq);
            String body = httpResult.getBody();

            log.info(" getLogisticsInfo queryTrackParam:{} body:{}", JSON.toJSONString(queryTrackParam), body);


            return JSONObject.parseObject(body, KuaiDi100DTO.class);
        } catch (Exception e) {
            log.error("getLogisticsInfo error:" + JSON.toJSONString(queryTrackReq), e);
        }
        return null;
    }


    /**
     * 订阅接口
     */
    public static String subscribe( String trackNumber, String phone)  {
        try {
            SubscribeParameters subscribeParameters = new SubscribeParameters();
            String companyCode = null;

            if(StringUtils.isBlank(companyCode)){

                KuaiDi100DTO kuaiDi100DTO =  getLogisticsInfo(trackNumber);
                if(kuaiDi100DTO != null){
                    companyCode = kuaiDi100DTO.getCom();
                }

            }
            subscribeParameters.setCallbackurl("https://api.1024api.com/api-interface/app/kuaidi/callback");
            subscribeParameters.setPhone(phone);

            SubscribeParam subscribeParam = new SubscribeParam();
            subscribeParam.setParameters(subscribeParameters);
            subscribeParam.setCompany(companyCode);
            subscribeParam.setNumber(trackNumber);

            subscribeParam.setKey(key);
            SubscribeReq subscribeReq = new SubscribeReq();
            subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
            subscribeReq.setParam(new Gson().toJson(subscribeParam));

            IBaseClient subscribe = new Subscribe();

            HttpResult httpResult = subscribe.execute(subscribeReq);

            String body = httpResult.getBody();
            log.info(" subscribe query result body:{}", body);
            return body;
        } catch (Exception e) {
            log.error(" subscribe error:{}", e);
        }
        return null;
    }


    public static void main(String[] args) {
        subscribe( "773394957915732", "18458195149");
    }


}
