package com.hfkj.common.pay.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * 签名工具类
 */
public class SignatureUtil {

	/**
	 * 参数签名
	 * @param param 参数
	 * @param key   秘钥
	 * @return
	 * @throws Exception
	 */
	public static String createSign(Object param, String key) {
		Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class);
		try {
			return md5Encode(paramSort(map, key).getBytes());
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 验证签名
	 * @param checkSign  需验证的签名字符串
	 * @param param      参数
	 * @param key        秘钥
	 * @return
	 * @throws Exception
	 */
	public static Boolean checkSign(String checkSign,Object param, String key) {
		Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class);
		// 去除签名
		map.remove("sign");
		if (checkSign.equals(createSign(map, key))) {
			return true;
		}
		return false;
	}

	/**
	 * 参数排序
	 * @param param
	 * @return
	 */
	public static String paramSort(final Map<String, Object> param, String key) {
		Set<String> keySet = param.keySet();
		String[] keyArray = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(keyArray);
		StringBuilder sb = new StringBuilder();
		for (String k : keyArray) {
			if (StringUtils.isBlank(sb.toString())) {
				sb.append(k).append("=").append(param.get(k));
			} else {
				sb.append("&").append(k).append("=").append(param.get(k));
			}
		}
		sb.append("&key=").append(key);
		return sb.toString();
	}

	/**
	 * MD5加密
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String md5Encode(byte[] data) throws Exception {
		 // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 执行摘要信息
        byte[] digest = md.digest(data);
        // 将摘要信息转换为32位的十六进制字符串
        return new String(new HexBinaryAdapter().marshal(digest));
	}

	public static void main(String[] args) throws Exception {
		JSONObject obj = new JSONObject();
	/*	{
			"merchantNo": "2025051417255211903",
				"billDateS": 1763395200000,
				"billDateE": 1763481599000,
				"sign": "8499CEFD077902EAC29DDD81F8A79E40"
		}*/
		//obj.put("merchantNo", "2025051417255211903");
		//obj.put("billDateS", "1763395200000");
		//obj.put("billDateE", "1763481599000");
		//obj.put("appId", "hfe9f054570db612");
		//obj.put("pageNum", "1");
		//obj.put("pageSize", "100");

		// obj.put("appId", "hfe9f054570db612");
		// obj.put("orderNo", "Y202512172014431");

		// obj.put("appId","hffeab7fdb44d4f1");
		// obj.put("gasNo","10021");
		// obj.put("oilNo","92");
		// obj.put("gunNo","3");
		// obj.put("orderNo",System.currentTimeMillis()+"");
		// obj.put("refuelingAmount","100");
		// obj.put("userPhone","17726395120");

		obj.put("appId","hffeab7fdb44d4f1");
		obj.put("orderNo","1766422398251");
		obj.put("refundReason","加错油站");

		String sign = createSign(JSONObject.parseObject(obj.toJSONString()), "3a9b2772b9a2d319a9b18e55fa30246d");
		obj.put("sign", sign);
		System.out.println(JSONObject.toJSONString(obj));
	}

}
