EasyPoi Excel 注解版 导入导出 枚举或者字典值 replace 动态处理

示例代码,需要将 replace 的枚举值动态处理

/**
     * 订单类型
     */
    @ApiModelProperty(value = "订单类型")
    @Dict(dictCode = "ORDER_TYPE")
    @Excel(name = "订单类型" ,width = 15,replace = {"_null","线上直销_1","代理商散客_2","代理商团队_3","大客户_4","代客下单_5","线下_6"})
    private String orderType;

这里我们可以在service层用一个切面处理

/**
 * @author 作者:xujuncheng
 * @date 时间:2021/9/16 - 11:00
 * @email 邮箱:18475346620@163.com
 * @desc 描述:Excel数据字典切面
 */
@Aspect
@Component
@Slf4j
public class ExcelAspect {

    @Autowired
    private RedisTemplate redisTemplate;

    
    private static final String DATA_SYS="Dict-data";


    /**
     * 切点,切入 controller 包下面的所有方法
     */
    @Pointcut("execution(public * com.text.finance.service..*.*ServiceImpl.export*(..))")
    public void dict() {

    }

    @Around("dict()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long time1 = System.currentTimeMillis();
        Object result = pjp.proceed();
        long time2 = System.currentTimeMillis();
        log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");
        long start = System.currentTimeMillis();
        this.parseExcelText(result);
        long end = System.currentTimeMillis();
        log.debug("解析注入JSON数据  耗时" + (end - start) + "ms");
        return result;
    }
    private void parseExcelText(Object result) {
        Object o = redisTemplate.opsForValue().get(DATA_SYS).toString();
        List<SysDataDictionaryRedisDTO> object = JSON.parseArray(o.toString(),SysDataDictionaryRedisDTO.class);
        List<?> list = (List<?>) result;
        if (list != null && list.size() > 0){
            for (Object record : list) {
                for (Field allField : ObjConvertUtils.getAllFields(record)) {
                    Excel annotation = allField.getAnnotation(Excel.class);
                    Dict dict = allField.getAnnotation(Dict.class);
                    if (annotation != null  && dict != null) {
                        //获取字段code
                        try {
                            InvocationHandler dictInvocationHandler = Proxy.getInvocationHandler(dict);
                            Field dictMemberValues = dictInvocationHandler.getClass().getDeclaredField("memberValues");
                            dictMemberValues.setAccessible(true);
                            Map dictMap = (Map) dictMemberValues.get(dictInvocationHandler);
                            String dictCode = (String) dictMap.get("dictCode");
                            List<SysDataDictionaryRedisDTO> collect = object.stream().filter(item -> item.getDictCode().equals(dictCode)).collect(Collectors.toList());
                            if(CollectionUtil.isNotEmpty(collect)){
                                List<String> strings = new ArrayList<>();
                                strings.add("_null");
                                collect.forEach(item -> {
                                    String dictName = item.getDictName();
                                    String dictVal = item.getDictVal();
                                    strings.add(dictName+"_"+dictVal);
                                });
                                String[] strings1 = strings.toArray(new String[0]);
                                InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
                                Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
                                memberValues.setAccessible(true);
                                Map map = (Map) memberValues.get(invocationHandler);
                                map.put("replace", strings1);
                            }
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
上一篇:Linux系统的启动过程


下一篇:EasyPoi教程V1.0