package com.ylcm.sys.excel.converter;

import org.apache.commons.lang.StringUtils;

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

public class AmountConverter implements Converter<Double> {
    @Override
    public Class<Double> supportJavaTypeKey() {
        return Double.class;
    }

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

    @Override
    public Double convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String val = cellData.getStringValue();
        return StringUtils.isBlank(val) ? null:Double.valueOf(val.replace(",",""));
    }

    @Override
    public CellData<String> convertToExcelData(Double d, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(d != null ? d.toString():"");
    }
}
