package com.gxwebsoft.shop.service.impl;

import com.google.gson.Gson;
import com.gxwebsoft.shop.service.KuaiDi100;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.BaseClient;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.*;
import com.kuaidi100.sdk.utils.SignUtils;
import org.springframework.stereotype.Service;

@Service
public class KuaiDi100Impl implements KuaiDi100 {
    private String key = "bekcrvqy1510";
    private String secret = "8c9dd34b6ff34cad9dd6a239bd0c4d26";
    private String customer = "2E5DE2DB6B39822D793FE45EDDFB00B1";

    @Override
    public HttpResult border(BOrderReq bOrderReq) throws Exception {
        PrintReq printReq = new PrintReq();

        String t = String.valueOf(System.currentTimeMillis());
        String param = new Gson().toJson(bOrderReq);

        printReq.setKey(key);
        printReq.setSign(SignUtils.printSign(param, t, key, secret));
        printReq.setT(t);
        printReq.setParam(param);
        printReq.setMethod(ApiInfoConstant.B_ORDER_OFFICIAL_ORDER_METHOD);
        System.out.println(printReq);
//        IBaseClient bOrder = new BOrderOfficial();
        IBaseClient bOrder = new BaseClient() {
            @Override
            public String getApiUrl(BaseRequest baseRequest) {
                return "https://api.kuaidi100.com/apiMock/border";
            }
        };
        return bOrder.execute(printReq);
    }

    @Override
    public String pollList(String com, String num, String phone) throws Exception {
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(com);
        queryTrackParam.setNum(num);
        queryTrackParam.setPhone(phone);
        String param = new Gson().toJson(queryTrackParam);
        queryTrackReq.setSign(SignUtils.querySign(param, key, customer));
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setParam(param);
        IBaseClient baseClient = new QueryTrack();
        return baseClient.execute(queryTrackReq).getBody();
    }
}
