package com.bonus.core.common.utils;

import cn.hutool.core.util.NumberUtil;
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.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.math.BigDecimal;
import java.util.Objects;

public class CustomNumberConverter implements Converter<Number> {
   public Class supportJavaTypeKey() {
      return Number.class;
   }

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

   public Number convertToJavaData(ReadCellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
      return NumberUtil.parseNumber(cellData.getNumberValue().toString());
   }

   public WriteCellData convertToExcelData(Number s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
      if (Objects.nonNull(s)) {
         double yuanValue = s.doubleValue() / 100.0;
         BigDecimal bd = BigDecimal.valueOf(yuanValue);
         return new WriteCellData(bd);
      } else {
         return new WriteCellData(BigDecimal.ZERO);
      }
   }
}
