kongzy
2024-10-30 e7afed8cdb301a311dbad5ed23c7ff9f826b2c48
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
96
97
98
99
100
101
102
103
104
package com.gkhy.assess.system.excel;
 
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gkhy.assess.common.utils.StringUtils;
import com.gkhy.assess.system.domain.SysExpertInfo;
import com.gkhy.assess.system.domain.vo.SysExpertInfoExcelVO;
import com.gkhy.assess.system.service.SysExpertInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Slf4j
 
public class ExpertExcelListener extends AnalysisEventListener<SysExpertInfoExcelVO> {
 
 
    private static final int BATCH_COUNT=100;
 
    private List<SysExpertInfoExcelVO> cachedDateList= ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
 
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        log.error("解析异常:",exception);
        throw exception;
    }
 
    @Override
    public void invoke(SysExpertInfoExcelVO sysExpertInfoExcelVO, AnalysisContext analysisContext) {
        if(!StringUtils.isBlank(sysExpertInfoExcelVO.getName())){
            cachedDateList.add(sysExpertInfoExcelVO);
        }else{
            System.out.println("ffffff");
        }
        if(cachedDateList.size()>=BATCH_COUNT){
            saveData();
            cachedDateList.clear();
        }
    }
 
    private void saveData(){
        for(SysExpertInfoExcelVO sysExpertInfoExcelVO:cachedDateList){
            String direction=sysExpertInfoExcelVO.getSupportDirectionSafety();
            String name=sysExpertInfoExcelVO.getName();
            String idCard = sysExpertInfoExcelVO.getIdCard();
            SysExpertInfo existExpertInfo = SpringUtil.getBean(SysExpertInfoService.class).getOne(Wrappers.<SysExpertInfo>lambdaQuery()
                    .eq(true, SysExpertInfo::getIdCard, idCard)
                    .last(" limit 1"));
            List<String> safetyArray = new ArrayList<>();
            if(!StringUtils.isBlank(direction)) {
                direction = direction.replace("安全生产:", "").trim();
                String[] splits = direction.split("、");
                for (String str : splits) {
                    str = str.trim();
                    String code = "1";
                    if (str.equals("现场检查")) {
                        code = "1";
                    } else if (str.equals("调查评估")) {
                        code = "2";
                    } else if (str.equals("咨询服务")) {
                        code = "3";
                    } else if (str.equals("教育培训")) {
                        code = "4";
                    } else {
                        code = "5";
                    }
                    safetyArray.add(code);
                }
            }else{
                safetyArray.add("5");
            }
            if(existExpertInfo!=null){
                existExpertInfo.setState(2);
                existExpertInfo.setSupportDirectionSafety(String.join( ",",safetyArray));
                existExpertInfo.setDomain(sysExpertInfoExcelVO.getDomain());
                existExpertInfo.setLevel(sysExpertInfoExcelVO.getLevel());
                SpringUtil.getBean(SysExpertInfoService.class).updateById(existExpertInfo);
            }else{
                SysExpertInfo sysExpertInfo=new SysExpertInfo();
                BeanUtils.copyProperties(sysExpertInfoExcelVO,sysExpertInfo);
                sysExpertInfo.setSupportDirectionSafety(String.join( ",",safetyArray));
                sysExpertInfo.setState(2);
                SpringUtil.getBean(SysExpertInfoService.class).save(sysExpertInfo);
            }
        }
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
        log.info("sheet={}解析完成",analysisContext.readSheetHolder().getSheetName());
    }
}