package cn.iocoder.yudao.module.mdm.controller.admin.item.vo;
|
|
import cn.idev.excel.converters.Converter;
|
import cn.idev.excel.enums.CellDataTypeEnum;
|
import cn.idev.excel.metadata.GlobalConfiguration;
|
import cn.idev.excel.metadata.data.ReadCellData;
|
import cn.idev.excel.metadata.data.WriteCellData;
|
import cn.idev.excel.metadata.property.ExcelContentProperty;
|
|
/**
|
* 物料导入“状态”列转换器:
|
* 兼容 数字(0/1)、中文(启用/停用、开启/关闭、是/否)、英文(true/false、enable/disable)。
|
* 无法识别时返回 null(由业务层按默认“启用”处理),避免直接抛出“系统异常”。
|
*/
|
public class StatusTextConverter implements Converter<Integer> {
|
|
@Override
|
public Class<Integer> supportJavaTypeKey() {
|
return Integer.class;
|
}
|
|
@Override
|
public CellDataTypeEnum supportExcelTypeKey() {
|
return CellDataTypeEnum.STRING;
|
}
|
|
@Override
|
public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty,
|
GlobalConfiguration globalConfiguration) {
|
if (value == null) {
|
return new WriteCellData<>("");
|
}
|
return new WriteCellData<>(value == 0 ? "启用" : "停用");
|
}
|
|
@Override
|
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
|
GlobalConfiguration globalConfiguration) {
|
if (cellData == null) {
|
return null;
|
}
|
if (cellData.getType() == CellDataTypeEnum.NUMBER && cellData.getNumberValue() != null) {
|
return cellData.getNumberValue().intValue() >= 1 ? 1 : 0;
|
}
|
String s = cellData.getStringValue();
|
if (s == null) {
|
return null;
|
}
|
s = s.trim();
|
if (s.isEmpty()) {
|
return null;
|
}
|
if ("0".equals(s) || s.contains("启用") || s.contains("开启") || "是".equals(s)
|
|| "true".equalsIgnoreCase(s) || "enable".equalsIgnoreCase(s)) {
|
return 0;
|
}
|
if ("1".equals(s) || s.contains("停用") || s.contains("关闭") || "否".equals(s)
|
|| "false".equalsIgnoreCase(s) || "disable".equalsIgnoreCase(s)) {
|
return 1;
|
}
|
try {
|
return Integer.parseInt(s);
|
} catch (NumberFormatException e) {
|
return null;
|
}
|
}
|
}
|