package ${domain}.frame.excel.converter;

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 BooleanConverter implements Converter<Boolean> {

    public Class supportJavaTypeKey() {
        return Boolean.class;
    }

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

    public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String lowerCase = cellData.getStringValue().toLowerCase();
        if (lowerCase.matches("y|n")) {
            return "y".equals(lowerCase);
        } else if (lowerCase.matches("yes|no")) {
            return "yes".equals(lowerCase);
        } else if (lowerCase.matches("true|false")) {
            return "true".equals(lowerCase);
        } else if (lowerCase.matches("是|否")) {
            return "是".equals(lowerCase);
        } else if (lowerCase.matches("有|无")) {
            return "有".equals(lowerCase);
        } else if (lowerCase.matches("是|不是")) {
            return "是".equals(lowerCase);
        } else {
            throw new RuntimeException("[" + cellData.getStringValue() + "] 值有错误!");
        }
    }

    public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (value == null) {
            return new CellData("");
        }
        return new CellData(value ? "是" : "否");
    }
}
