package com.yunyan.business.document.y23.budget;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;

import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CellStringNumberConverter implements Converter<String> {

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

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {

        // 如果传递了 .class 接收，则 contentProperty 不为空
        // If there are "DateTimeFormat", read as date,
        // 获取 contentProperty  对象上是否有加日期格式注解
        /*if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
            return DateUtils.format(
                    HSSFDateUtil.getJavaDate(cellData.getDoubleValue(),
                            contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
                    contentProperty.getDateTimeFormatProperty().getFormat());
        }*/
        // If there are "NumberFormat", read as number
        // 获取 contentProperty  对象上是否有加数字格式注解
        /*if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
            return NumberUtils.format(cellData.getOriginalNumberValue(), contentProperty);
        }*/
        // Excel defines formatting
        // 如果没有格式注解
        // 注意： 如果 Excel 为Number 类型，则 cellData.getDataFormat() = 0！！！
        /*if (cellData.getDataFormatData() != null) {
            // 判断是否为日期格式
            if (DateUtil.isADateFormat(cellData.getDataFormatData(), cellData.getFormulaData())) {
                return DateUtils.format(HSSFDateUtil.getJavaDate(cellData.getDoubleValue(),
                        globalConfiguration.getUse1904windowing(), null));
            } else {
                // 如果不是日期格式就直接调用这个方法， 该方法会 保留 .0的情况。可以进行拓展
                return NumberUtils.format(cellData.getDoubleValue(), contentProperty);
            }
        }*/

        if (cellData.getDataFormatData() != null && cellData.getType().equals(CellDataTypeEnum.NUMBER)) {
            String result =  NumberUtils.format(cellData.getNumberValue(), contentProperty);
            return result;
        }


        // Default conversion number
        return NumberUtils.format(cellData.getNumberValue(), contentProperty);
    }

    /*@Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
                                       GlobalConfiguration globalConfiguration) {
        return new CellData(Double.valueOf(value));
    }*/
}

