package com.hzya.frame.seeyon.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * @ClassName YzfSignUtil
 * @Description
 * @Author llg
 * Date 2025/4/2 2:14 下午
 */
public class YzfSignUtil {
    private static final String CHARSET = "utf-8";

    public static String signRequest(Map<String, Object> parametersMap, String secretkey) {
        ArrayList<String> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : parametersMap.entrySet()) {
            String key = entry.getKey();
            Object val = entry.getValue();
            if (StringUtils.isBlank(key) || val == null) {
                continue;
            }

            if (YzfObjectUtil.isSimpleTypeOrString(val.getClass())) {
                if (null != val && !"".equals(val)) {
                    list.add(key + "=" + val + "&");
                }
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += secretkey;
        result = YzfSHA256Util.getSHA256StrJava(result, CHARSET);
        return result;
    }
}
