package com.dayou.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2024/3/19
 * created with IntelliJ IDEA.
 */
public class ChineseUtils {
    public static String formatMoney(BigDecimal number) {
        Double money = number.doubleValue();
        if (money == null || money.equals(0d)) {
            return "零元整";
        }
        DecimalFormat format = new DecimalFormat("0.00");
        String[] part = format.format(money).split("\\.");
        // 整数部分
        StringBuffer integer = new StringBuffer();
        if (!part[0].equals("0")) {
            for (int i = 0; i < part[0].length(); i++) {
                char perchar = part[0].charAt(i);
                integer.append(upperNumber(perchar));
                integer.append(upperNumber(part[0].length() - i - 1));
            }
        }
        // 小数部分：只取2位
        StringBuffer decimal = new StringBuffer();
        if (!part[1].equals("00")) {
            for (int i = 0; i < 2; i++) {
                char perchar = part[1].charAt(i);
                decimal.append(upperNumber(perchar));
                if (i == 0) decimal.append('角');
                if (i == 1) decimal.append('分');
            }
        }
        String result = integer.toString() + decimal.toString();
        return dispose(result);
    }

    private static char upperNumber(char number) {
        switch (number) {
            case '0':
                return '零';
            case '1':
                return '壹';
            case '2':
                return '贰';
            case '3':
                return '叁';
            case '4':
                return '肆';
            case '5':
                return '伍';
            case '6':
                return '陆';
            case '7':
                return '柒';
            case '8':
                return '捌';
            case '9':
                return '玖';
        }
        return 0;
    }

    private static char upperNumber(int index) {
        switch (index) {
            case 0:
                return '元';
            case 1:
                return '拾';
            case 2:
                return '佰';
            case 3:
                return '仟';
            case 4:
                return '万';
            case 5:
                return '拾';
            case 6:
                return '佰';
            case 7:
                return '仟';
            case 8:
                return '亿';
            case 9:
                return '拾';
            case 10:
                return '佰';
            case 11:
                return '仟';
        }
        return 0;
    }

    private static String dispose(String result) {
        result = result.replaceAll("零仟零佰零拾|零仟零佰|零佰零拾|零仟|零佰|零拾", "零")
                .replaceAll("零+", "零")
                .replace("零亿", "亿");
        result = result.matches("^.*亿零万[^零]仟.*$") ? result.replace("零万", "零")
                : result.replace("零万", "万");
        result = result.replace("亿万", "亿")
                .replace("零元", "元")
                .replace("零角", "")
                .replace("零分", "")
                .replaceAll("零角零分|零角$|元$", "元整");
        return result;
    }
}
