罗yu 元桥
2021-04-28 b6082fe75b377ac9c177736d98f36ae46e7cdb70
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationServeExcelListen.java
@@ -2,6 +2,7 @@
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.panzhihua.common.enums.PopulIsOkEnum;
@@ -9,11 +10,13 @@
import com.panzhihua.common.enums.PopulSexEnum;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComMngPopulationImportErrorVO;
import com.panzhihua.common.model.vos.community.ComMngPopulationServeExcelVO;
import com.panzhihua.common.model.vos.community.ComMngVillageServeExcelVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.AgeUtils;
import com.panzhihua.common.utlis.IdCardUtil;
import com.panzhihua.common.utlis.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -45,7 +48,7 @@
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private static final int BATCH_COUNT = 5000;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
    @Override
@@ -82,14 +85,37 @@
        int index = 2;
        try {
            ArrayList<ComMngPopulationServeExcelVO> voList = Lists.newArrayList();
            List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
            for (Map<Integer, String> oneData : list) {
                ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO();
                if(oneData.get(0) == null){
                    throw new ServiceException("500", "名字不可为空:第" + index + "行,第1列");
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第1列");
                    importErrorVO.setErrorMsg("名字不可为空,请填写姓名");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setName(oneData.get(0));
                if(oneData.get(1) == null){
                    throw new ServiceException("500", "身份证号不可为空:第" + index + "行,第2列");
                if(StringUtils.isEmpty(oneData.get(1))){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第2列");
                    importErrorVO.setErrorMsg("身份证号不可为空,请填写身份证号");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
//                if(!IdCardUtil.identityValidator(oneData.get(1))){
//                    throw new ServiceException("500", "身份证号格式错误:第" + index + "行,第2列");
//                }
                //判断身份证号码位数
                if(oneData.get(1).length() != 18){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第2列");
                    importErrorVO.setErrorMsg("身份证号位数有误,请检查身份证号码是否正确");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setCardNo(oneData.get(1));
                //根据身份证号码解析年龄以及性别
@@ -154,18 +180,42 @@
                }
                for (int i = 18; i < headSize; i++) {
                    if (oneData.get(i) != null && oneData.get(i).equals("是")) {
                        vo.getUserTagStr().add(headData.get(i));
                        vo.getUserTagStr().add(headData.get(i).substring(0,headData.get(i).indexOf("(")));
                    }
                }
                voList.add(vo);
                index++;
            }
            //客户需要暂时注释,等客户处理完成需要恢复
//            if(populationImportErrorVOList.isEmpty()){
//                R r = communityService.listSavePopulationServeExcelVO(voList, communityId);
//                if (!R.isOk(r)) {
//                    throw new ServiceException(r.getMsg());
//                }
//            }else{
//                throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
//            }
            R r = communityService.listSavePopulationServeExcelVO(voList, communityId);
            if (!R.isOk(r)) {
                throw new ServiceException(r.getMsg());
                String errMsg = r.getMsg();
                List<ComMngPopulationImportErrorVO> errorList = JSON.parseArray(errMsg,ComMngPopulationImportErrorVO.class);
                if(!errorList.isEmpty()){
                    populationImportErrorVOList.addAll(errorList);
                }
                throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
            }else{
                if(!populationImportErrorVOList.isEmpty()){
                    throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
                }
            }
        } catch (NumberFormatException e) {
            throw new ServiceException("500", "填写数据格式错误:第" + index + "行" + e.getMessage());
            List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
            ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
            importErrorVO.setErrorPosition("第" + index + "行");
            importErrorVO.setErrorMsg("数据格式有误,请检查文档内数据");
            populationImportErrorVOList.add(importErrorVO);
            index++;
            throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
        }
    }
}