package com.hdkj.lt.utils.security;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: wenhao
 * @Create: 2024/05/23 17:06
 * @Version: 1.0
 */
public class StrConvertUtils {

    private final static Pattern SQL_XSS_PATTERN = Pattern.compile("[_%/\\\\]", Pattern.CASE_INSENSITIVE);

    private final static Pattern SQL_XSS_PATTERN_ALL = Pattern.compile(".*[_%/\\\\].*", Pattern.CASE_INSENSITIVE);

    public static String xxsEscape(String s) {
        if (StringUtils.isNotBlank(s) && s.matches(SQL_XSS_PATTERN_ALL.pattern())) {
            s = SQL_XSS_PATTERN.matcher(s).replaceAll("\\\\$0");
        }
        return s;
    }

    /**
     * 转换成中文字符串
     * @param value
     * @return
     */
    public static String toChineseStr(Long value) {
        return toChineseStr(String.valueOf(value));
    }

    /**
     * 转换成中文字符串
     * @param value
     * @return
     */
    public static String toChineseStr(Integer value) {
        return toChineseStr(String.valueOf(value));
    }

    /**
     * 转换成中文字符串
     * @param value
     * @return
     */
    public static String toChineseStr(String value) {
        //数字数组
        String[] strArray1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        //位数数组
        String[] strArray2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        StringBuilder result = new StringBuilder();
        int n = value.length();
        for (int i = 0; i < n; i++) {
            int num = value.charAt(i) - '0';
            if (num != 0) {
                int index = n - 2 - i;
                result.append(strArray1[num]).append(index < 0 ? "" : strArray2[index]);
            } else {
                // 处理连续的零
                if (i < n - 1 && value.charAt(i + 1) != '0') {
                    result.append(strArray1[num]);
                }
            }
            // 处理`一十`开头的特殊情况
            if (result.toString().startsWith("一十")) {
                result.delete(0, 1);
            }
        }
        return result.toString();
    }

}
