package com.ruoyi.common.utils;

import java.util.HashMap;
import java.util.Map;

public class ConvertUtil {
    private static final Map<Character, Integer> digitMap = new HashMap<>();
    private static final Map<Character, Integer> unitMap = new HashMap<>();

    static {
        // 数字映射
        digitMap.put('零', 0);
        digitMap.put('壹', 1);
        digitMap.put('贰', 2);
        digitMap.put('叁', 3);
        digitMap.put('肆', 4);
        digitMap.put('伍', 5);
        digitMap.put('陆', 6);
        digitMap.put('柒', 7);
        digitMap.put('捌', 8);
        digitMap.put('玖', 9);

        // 单位映射
        unitMap.put('拾', 10);
        unitMap.put('佰', 100);
        unitMap.put('仟', 1000);
        unitMap.put('万', 10000);
    }

    public static int convertChineseNum(String chineseNumber) {
        int result = 0;
        int temp = 0;
        int prevDigit = 0;

        for (int i = 0; i < chineseNumber.length(); i++) {
            char c = chineseNumber.charAt(i);

            if (digitMap.containsKey(c)) {
                prevDigit = digitMap.get(c);
                temp = temp * 10 + prevDigit;
            } else if (unitMap.containsKey(c)) {
                int unit = unitMap.get(c);
                if (unit == 10000) {
                    result = (result + temp) * unit;
                    temp = 0;
                } else {
                    temp += prevDigit * (unit - 1);
                }
                prevDigit = 0;
            }
        }

        return result + temp;
    }
}
