package org.springblade.openinterface.carbarrier.utils;

import oracle.jdbc.driver.Const;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.openinterface.carbarrier.constant.Constants;
import org.springblade.openinterface.carbarrier.entity.OpenOrCloseGateQuery;
import org.springblade.openinterface.carbarrier.entity.ParkInfoQuery;
import org.springblade.openinterface.carbarrier.entity.RecordQuery;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created By lidexi in 2021/1/4
 * 签名工具类
 **/
public class MD5Util {

	/**
	 * 获取应用关联的停车场信息签名
	 *
	 * @param parkInfoQuery
	 * @return
	 */
	public static Map<String, Object> getRequestMap(ParkInfoQuery parkInfoQuery) {
		Map<String, Object> paramMap = new HashMap<>();
		StringBuffer stringBuffer = new StringBuffer();
		//封装参数，参数按字典序排序
		if (StringUtil.isNoneBlank(parkInfoQuery.getAppid())) {
			paramMap.put("appid", parkInfoQuery.getAppid());
			stringBuffer.append("appid=" + parkInfoQuery.getAppid());
		}

		if (StringUtil.isNoneBlank(parkInfoQuery.getIndexId())) {
			paramMap.put("indexId", parkInfoQuery.getIndexId());
			stringBuffer.append("&indexId=" + parkInfoQuery.getIndexId());
		}

		if (StringUtil.isNotBlank(parkInfoQuery.getParkKey())) {
			paramMap.put("parkKey", parkInfoQuery.getParkKey());
			stringBuffer.append("&parkKey=" + parkInfoQuery.getParkKey());
		}

		parkInfoQuery.setRand(String.valueOf(Math.random()));
		if (StringUtil.isNoneBlank(parkInfoQuery.getRand())) {

			paramMap.put("rand", parkInfoQuery.getRand());
			stringBuffer.append("&rand=" + parkInfoQuery.getRand());
		}

		if (StringUtil.isNoneBlank(parkInfoQuery.getSize())) {
			paramMap.put("size", parkInfoQuery.getSize());
			stringBuffer.append("&size=" + parkInfoQuery.getSize());
		}

		if (StringUtil.isNoneBlank(parkInfoQuery.getVersion())) {
			paramMap.put("version", parkInfoQuery.getVersion());
			stringBuffer.append("&version=" + parkInfoQuery.getVersion());
		}

		stringBuffer.append("&" + Constants.APPSECRET);

		String sign = MD5(stringBuffer.toString());
		paramMap.put("sign", sign);

		return paramMap;
	}

	/**
	 * 获取进出场记录签名
	 *
	 * @param recordQuery
	 * @return
	 */
	public static Map<String, Object> getRequestMap(RecordQuery recordQuery) {
		Map<String, Object> paramMap = new HashMap<>();
		StringBuffer stringBuffer = new StringBuffer();
		//封装参数，参数按字典序排序
		if (StringUtil.isNoneBlank(recordQuery.getAppid())) {
			paramMap.put("appid", recordQuery.getAppid());
			stringBuffer.append("appid=" + recordQuery.getAppid());
		}

		if (StringUtil.isNoneBlank(recordQuery.getCarNo())) {
			paramMap.put("carNo", recordQuery.getCarNo());
			stringBuffer.append("&carNo=" + recordQuery.getCarNo());
		}


		if (StringUtil.isNoneBlank(recordQuery.getEndTime())) {
			paramMap.put("endTime", recordQuery.getEndTime());
			stringBuffer.append("&endTime=" + recordQuery.getEndTime());
		}

		if (StringUtil.isNoneBlank(recordQuery.getIndexId())) {
			paramMap.put("indexId", recordQuery.getIndexId());
			stringBuffer.append("&indexId=" + recordQuery.getIndexId());
		}

		if (StringUtil.isNotBlank(recordQuery.getParkKey())) {
			paramMap.put("parkKey", recordQuery.getParkKey());
			stringBuffer.append("&parkKey=" + recordQuery.getParkKey());
		}

		recordQuery.setRand(String.valueOf(Math.random()));
		if (StringUtil.isNoneBlank(recordQuery.getRand())) {

			paramMap.put("rand", recordQuery.getRand());
			stringBuffer.append("&rand=" + recordQuery.getRand());
		}

		if (StringUtil.isNoneBlank(recordQuery.getSize())) {
			paramMap.put("size", recordQuery.getSize());
			stringBuffer.append("&size=" + recordQuery.getSize());
		}

		if (StringUtil.isNoneBlank(recordQuery.getStartTime())) {
			paramMap.put("startTime", recordQuery.getStartTime());
			stringBuffer.append("&startTime=" + recordQuery.getStartTime());
		}

		if (StringUtil.isNoneBlank(recordQuery.getVersion())) {
			paramMap.put("version", recordQuery.getVersion());
			stringBuffer.append("&version=" + recordQuery.getVersion());
		}

		stringBuffer.append("&" + Constants.APPSECRET);

		String sign = MD5(stringBuffer.toString());
		paramMap.put("sign", sign);

		return paramMap;
	}

	/**
	 * 获取远程开关闸签名
	 *
	 * @param openOrCloseGateQuery
	 * @return
	 */
	public static Map<String, Object> getRequestMap(OpenOrCloseGateQuery openOrCloseGateQuery) {
		Map<String, Object> paramMap = new HashMap<>();
		StringBuffer stringBuffer = new StringBuffer();
		//封装参数，参数按字典序排序

		paramMap.put("appid", Constants.APPID);
		stringBuffer.append("appid=" + Constants.APPID);


		if (StringUtil.isNotBlank(openOrCloseGateQuery.getParkKey())) {
			paramMap.put("parkKey", openOrCloseGateQuery.getParkKey());
			stringBuffer.append("&parkKey=" + openOrCloseGateQuery.getParkKey());
		}

		String rand = String.valueOf(Math.random());
		paramMap.put("rand", rand);
		stringBuffer.append("&rand=" + rand);


		if (StringUtil.isNoneBlank(openOrCloseGateQuery.getVersion())) {
			paramMap.put("version", openOrCloseGateQuery.getVersion());
			stringBuffer.append("&version=" + openOrCloseGateQuery.getVersion());
		}
		if (StringUtil.isNoneBlank(openOrCloseGateQuery.getVlCtrNo())) {
			paramMap.put("vlCtrNo", openOrCloseGateQuery.getVlCtrNo());
			stringBuffer.append("&vlCtrNo=" + openOrCloseGateQuery.getVlCtrNo());
		}
		stringBuffer.append("&" + Constants.APPSECRET);

		String sign = MD5(stringBuffer.toString());
		paramMap.put("sign", sign);

		return paramMap;
	}


	public final static String MD5(String s) {
		char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'A', 'B', 'C', 'D', 'E', 'F'};
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
