package com.hfkj.service.pay.huift.config;

import com.hfkj.config.CommonSysConst;
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;

/**
 * @className: HuiftConfig
 * @author: HuRui
 * @date: 2024/4/7
 **/
public class HuiftConfig {

    public final static String reqUrl = CommonSysConst.getSysConfig().getHuiftReqUrl();
    public final static String shopcode = CommonSysConst.getSysConfig().getHuiftShopCode();
    public final static String signSecret = CommonSysConst.getSysConfig().getHuiftSignSecret();
    public final static String shopcode98 = "20240653-59c4-4181-9157-17fd4b95c3fc";
    public final static String signSecret98 = "19C17F9E19B8DDDDF2FD2EE4EA9F6C61";
    public final static String shopcode95 = "b4b121b6-ec5c-44e1-a020-d2f21e754b55";
    public final static String signSecret95 = "4AA9E79115848B797B53FBD830B402F4";

    /**
     * 参数排序
     * @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);
        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));
    }

}
