package com.ruoyi.common.utils;

public class ChineseNumeralUtils {
    private static final String[] CHINESE_NUM = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CHINESE_UNIT = {"", "拾", "佰", "仟"};
    private static final String[] CHINESE_GROUP = {"", "万", "亿", "兆"};
    private static final String[] DECIMAL_UNIT = {"角", "分"};

    public static String convertToChineseMoney(double amount) {
        if (amount == 0) {
            return "零元整";
        }

        StringBuilder result = new StringBuilder();

        // 处理负数
        if (amount < 0) {
            result.append("负");
            amount = -amount;
        }

        // 拆分整数和小数部分
        long integerPart = (long) amount;
        int decimalPart = (int) Math.round((amount - integerPart) * 100);

        // 处理整数部分
        if (integerPart > 0) {
            result.append(convertIntegerPart(integerPart));
        }

        // 处理小数部分
        if (decimalPart > 0) {
            result.append(convertDecimalPart(decimalPart));
        } else if (integerPart > 0) {
            result.append("整"); // 没有角分，补 "整"
        }

        return result.toString();
    }

    private static String convertIntegerPart(long num) {
        String numStr = String.valueOf(num);
        int len = numStr.length();
        StringBuilder result = new StringBuilder();
        boolean needZero = false;

        int groupIndex = 0;
        while (len > 0) {
            int end = len;
            int start = Math.max(0, len - 4);
            String part = numStr.substring(start, end);
            String partChinese = convertFourDigit(part, needZero);
            if (!partChinese.isEmpty()) {
                result.insert(0, partChinese + CHINESE_GROUP[groupIndex]);
            }

            needZero = part.length() < 4 && !part.equals("0000");
            len -= 4;
            groupIndex++;
        }

        result.append("元");
        return result.toString();
    }

    private static String convertFourDigit(String num, boolean needZero) {
        StringBuilder partResult = new StringBuilder();
        int len = num.length();
        boolean leadingZero = true;

        for (int i = 0; i < len; i++) {
            int digit = num.charAt(i) - '0';
            if (digit == 0) {
                leadingZero = true;
            } else {
                if (leadingZero && (i != 0 || needZero)) {
                    partResult.append("零");
                }
                partResult.append(CHINESE_NUM[digit]).append(CHINESE_UNIT[len - 1 - i]);
                leadingZero = false;
            }
        }
        return partResult.toString();
    }

    private static String convertDecimalPart(int num) {
        StringBuilder result = new StringBuilder();
        int jiao = num / 10;
        int fen = num % 10;

        if (jiao > 0) {
            result.append(CHINESE_NUM[jiao]).append(DECIMAL_UNIT[0]);
        }
        if (fen > 0) {
            result.append(CHINESE_NUM[fen]).append(DECIMAL_UNIT[1]);
        }
        return result.toString();
    }

    public static void main(String[] args) {
        double[] testCases = {
                123456789.56,  // 壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元伍角陆分
                100200300.10,  // 壹亿零贰拾万零叁佰元壹角
                100000000.00,  // 壹亿元整
                0.05,          // 伍分
                100000000.01,  // 壹亿元零壹分
                -123456.78,    // 负壹拾贰万叁仟肆佰伍拾陆元柒角捌分
                -0.30,         // 负叁角
                -1000.00,      // 负壹仟元整
                1314.00
        };

        for (double amount : testCases) {
            System.out.println(convertToChineseMoney(amount));
        }
    }
}
