package com.storlead.framework.common.util.sso;

import com.storlead.framework.common.util.encryptor.AccessKeyEncryptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: storlead-centre-platform
 * @description:
 * @author: chenkq
 * @create: 2025-11-21 15:01
 */
public class HttpSecurityCheckUtil {

    private static final String SALT_KEY = "G4YfTqX6Kh8cHQy5";
    /**
     * 除数
     */
    public static long divisor = 100861;

    public static String makeSign(Map<String, String> sginArgMap) {
        List<Map.Entry<String, String>> entryList = new ArrayList<>(sginArgMap.entrySet());
        entryList.sort(Map.Entry.comparingByKey());
        /**
         * 构建字符串
         */
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : entryList) {
            if ("sign".equals(entry.getKey())) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(entry.getKey()).append("=").append(entry.getValue());
        }
        String dataToEncrypt = sb.toString();
        String sign = AccessKeyEncryptor.getAccessKeyEncryptor(SALT_KEY).encrypt(dataToEncrypt);
        return  sign;
    }

    public static String buildUrlParams(Map<String, String> paramMap) {
        if (paramMap == null || paramMap.isEmpty()) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            if (entry.getValue() == null) continue; // 忽略 null
            if (sb.length() > 0) sb.append("&");

            sb.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue());
        }

        return sb.toString();
    }
}
