package org.dromara.zhishu.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class PriceDivideBy100Converter implements Converter<String> {

    @Override
    public Class<String> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        // 价格在Excel中是数字类型
        return CellDataTypeEnum.NUMBER;
    }

    /**
     * 读取Excel时：将Excel中的数字（元）转换为String（分）
     * 兼容数值和字符串类型
     */
    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        if (context == null || context.getReadCellData() == null) {
            return "0";
        }
        try {
            ReadCellData<?> cellData = context.getReadCellData();
            BigDecimal priceInYuan = null;

            // 根据单元格类型获取数值
            switch (cellData.getType()) {
                case NUMBER:
                    // 数值类型：直接获取
                    priceInYuan = cellData.getNumberValue();
                    break;

                case STRING:
                    // 字符串类型：转换为BigDecimal
                    String stringValue = cellData.getStringValue();
                    if (stringValue == null || stringValue.trim().isEmpty()) {
                        return "0";
                    }
                    try {
                        // 去除千分位逗号和空格
                        stringValue = stringValue.trim().replaceAll(",", "").replaceAll(" ", "");
                        priceInYuan = new BigDecimal(stringValue);
                    } catch (NumberFormatException e) {
                        return "0";
                    }
                    break;

                default:
                    // 其他类型返回0
                    return "0";
            }

            // 数值转换：元转分
            if (priceInYuan == null) {
                return "0";
            }

            BigDecimal priceInCent = priceInYuan.multiply(BigDecimal.valueOf(100));
            return priceInCent.setScale(0, RoundingMode.HALF_UP).toString();

        } catch (Exception e) {
            return "0";
        }
    }

    /**
     * 写入Excel时：将String（分）转换为Excel中的数字（元）
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
        String value = context.getValue();
        if (value == null || value.isEmpty()) {
            return new WriteCellData<>(BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
        }
        try {
            BigDecimal priceInCent = new BigDecimal(value);
            // 分转元：除以100，保留2位小数
            BigDecimal priceInYuan = priceInCent.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
            return new WriteCellData<>(priceInYuan);
        } catch (NumberFormatException e) {
            return new WriteCellData<>(BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
        }
    }
}