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 { private static final int BATCH_COUNT=100; private List 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); } 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.lambdaQuery() .eq(true, SysExpertInfo::getIdCard, idCard) .last(" limit 1")); direction=direction.replace("安全生产:","").trim(); String[] splits = direction.split("、"); List safetyArray=new ArrayList<>(); 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); } 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()); } }