linwe
2024-08-09 8b7258c9427882bb1798f1502eaa35184c6e374e
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.springblade.common.excel;
 
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.github.xiaoymin.knife4j.core.util.StrUtil;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.utils.SpringUtils;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Field;
import java.util.List;
 
/**
 * 字典映射
 *
 * @author zhongrj
 * @date 2023-11-17
 */
@Component
public class ExcelDictConverter implements Converter<String> {
 
    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }
 
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }
 
    /**
     * 导入excel 解析到java 对象
     * @param cellData
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取字典类型
        Field field = excelContentProperty.getField();
        ExcelDictItemLabel excel = field.getAnnotation(ExcelDictItemLabel.class);
        String dictType = excel.type();
        // 为空返回
        String dictLabel = cellData.getStringValue();
        if (StrUtil.isBlank(dictLabel)) {
            return dictLabel;
        }
        // 查询字典
        List<DictBiz> list = DictBizCache.getList(dictType);
        //解析返回
        String key = "";
        for (DictBiz dictBiz : list) {
            if (dictBiz.getDictValue().equals(dictLabel)){
                key = dictBiz.getDictKey();
                break;
            }
        }
        // 返回key
        return key;
    }
 
    /**
     * java 导出到 excel
     * @param dictValue
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public CellData convertToExcelData(String dictValue, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取字典类型
        Field field = excelContentProperty.getField();
        ExcelDictItem excel = field.getAnnotation(ExcelDictItem.class);
        String dictType = excel.type();
        List<DictBiz> list = DictBizCache.getList(dictType);
        String value = "";
        //解析返回
        for (DictBiz dictBiz : list) {
            if (dictBiz.getDictKey().equals(dictValue)){
                value = dictBiz.getDictValue();
                break;
            }
        }
        return new CellData(value);
    }
}