package cn.superdesk.uniorder.common.excel.converters.artificial;

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;
import com.alibaba.excel.util.NumberUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.ParseException;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/4/19 下午2:49
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/4/19 下午2:49
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class CustomStringBigDecimalConverter implements Converter<BigDecimal> {

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

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

	@Override
	public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
	                                    GlobalConfiguration globalConfiguration) throws ParseException {

//		log.info("\n{}\n{}\n{}\n", cellData.getStringValue(), JsonUtils.toJson(contentProperty), JsonUtils.toJson(globalConfiguration));


		String processTmp = cellData.getStringValue()
//				.replace("￥", "") // "\u00A4"
				.replace("元", "");

		BigDecimal processBigDecimal = BigDecimal.ZERO;
		try {
			processBigDecimal = NumberUtils.parseBigDecimal(processTmp, contentProperty);
		} catch (ParseException pe) {
//			log.error(ExceptionUtils.getMessage(pe));
			processBigDecimal = new BigDecimal(processTmp);
		}

//		log.info("最终返回BigDecimal {}", processBigDecimal);

		return processBigDecimal;
	}

	@Override
	public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
	                                   GlobalConfiguration globalConfiguration) {
		return NumberUtils.formatToCellData(value, contentProperty);
	}
}