tuqin
4 小时以前 83dea93d02925e162ad88954b7d0ed97dd8a442a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
        }
    }
}