package com.ruoyi.common.utils.kd100;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.BOrderOfficial;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.BOrderReq;
import com.kuaidi100.sdk.request.PrintReq;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 功能：
 *
 * @author：LCL
 * @create：2022/04/25 15:50
 * @version：2020 Version：1.0
 */
@Component
@Getter
public class Kd100Util {

	@Value("${kd100.key}")
	private String key;
	@Value("${kd100.customer}")
	private String customer;
	@Value("${kd100.secret}")
	private String secret;
	@Value("${kd100.userid}")
	private String userid;
	@Value("${kd100.check}")
	private String check;

	/**
	 * 实时快递查询接口
	 * @param com 快递公司
	 * @param num 运单号
	 * @return 结果
	 */
	public JSONObject getExpressJsonData(String com, String num, String phone, String shippingPollToken) throws Exception {
		QueryTrackReq queryTrackReq = new QueryTrackReq();
		QueryTrackParam queryTrackParam = new QueryTrackParam();
		queryTrackParam.setCom(com);
		queryTrackParam.setNum(num);
		queryTrackParam.setPhone(phone);
		queryTrackParam.setResultv2("4");
		String param = new Gson().toJson(queryTrackParam);

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

		IBaseClient baseClient = new QueryTrack();
		HttpResult execute = baseClient.execute(queryTrackReq);
		String body = execute.getBody();
		return JSONObject.parseObject(body);
	}

	/**
	 * 商家寄件
	 * @param kuaidicom 快递公司的编码，一律用小写字母，见《快递公司编码》
	 * @param sendManName 寄件人姓名
	 * @param sendManMobile 寄件人的手机号
	 * @param sendManPrintAddr 寄件人所在的完整地址，如广东深圳市深圳市南山区科技南十二路2号金蝶软件园B10
	 * @param recManName 收件人姓名
	 * @param recManMobile 收件人的手机号
	 * @param recManPrintAddr 收件人所在完整地址，如广东深圳市深圳市南山区科技南十二路2号金蝶软件园
	 * @throws Exception 异常
	 */
	public JSONObject testBorderOfficial(String kuaidicom, String sendManName, String sendManMobile, String sendManPrintAddr, String recManName, String recManMobile, String recManPrintAddr) throws Exception {
		PrintReq printReq = new PrintReq();
		BOrderReq bOrderReq = new BOrderReq();
		bOrderReq.setKuaidicom(kuaidicom);
		// 寄件人信息
		bOrderReq.setSendManName(sendManName);
		bOrderReq.setSendManMobile(sendManMobile);
		bOrderReq.setSendManPrintAddr(sendManPrintAddr);
		// 收件人信息
		bOrderReq.setRecManName(recManName);
		bOrderReq.setRecManMobile(recManMobile);
		bOrderReq.setRecManPrintAddr(recManPrintAddr);

		bOrderReq.setCallBackUrl("https://server.chsie.com.cn/api/weiXin/kuaiDiCallBack");

		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();
		HttpResult execute = bOrder.execute(printReq);
		String body = execute.getBody();
		return JSONObject.parseObject(body);
	}

}
