package com.chushang.common.excel.convert;

import cn.hutool.core.util.StrUtil;
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;

public class StringConvert implements Converter<String> {

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

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

    /**
     * <h2>转换为java对象</h2>
     *
     * @param cellData            Excel 单元格数据
     * @param contentProperty     内容属性
     * @param globalConfiguration 全局配置
     * @return java对象
     * @author 单傲
     * @date 2023/4/19 18:09
     */
    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (cellData.getStringValue() == null) {
            return null;
        }
        return  cellData.getStringValue();
    }


    /**
     * <h2>转换单元格数据</h2>
     *
     * @param value               Java 数据
     * @param contentProperty     内容属性
     * @param globalConfiguration 全局配置
     * @return excel中的数据
     * @author 单傲
     * @date 2023/4/19 18:11
     */
    @Override
    public WriteCellData<String> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (StrUtil.isEmpty(value)) {
            return new WriteCellData<>();
        }
        return new WriteCellData<>(value);
    }
}
