From c4d6c6cc7f6a2342cc32e7f67f6f6eeb76a97ac1 Mon Sep 17 00:00:00 2001 From: huanghongfa <huanghongfa123456> Date: 星期二, 30 三月 2021 19:53:29 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationServeExcelListen.java | 94 +++++++++++++++++++++++++++++++++++++---------- 1 files changed, 74 insertions(+), 20 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationServeExcelListen.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationServeExcelListen.java index 0320b0e..ca212cb 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationServeExcelListen.java +++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationServeExcelListen.java @@ -3,59 +3,113 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson.JSONObject; +import com.google.common.collect.Lists; import com.panzhihua.common.exceptions.ServiceException; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.ComMngPopulationServeExcelVO; import com.panzhihua.common.model.vos.community.ComMngVillageServeExcelVO; import com.panzhihua.common.service.community.CommunityService; import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; +import java.util.Map; + +import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER; /** * @description: 实有人口导入监听 * @author: llming */ @Slf4j -public class ComMngPopulationServeExcelListen extends AnalysisEventListener<ComMngPopulationServeExcelVO> { - +public class ComMngPopulationServeExcelListen extends AnalysisEventListener<Map<Integer, String>> { private CommunityService communityService; private Long communityId; + + private static int headSize = 0; + + private Map<Integer, String> headData; + public ComMngPopulationServeExcelListen(CommunityService communityService, Long communityId) { this.communityService = communityService; this.communityId = communityId; } - + /** + * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ private static final int BATCH_COUNT = 100; - private List<ComMngPopulationServeExcelVO> list = new ArrayList<>(); - + List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>(); @Override - public void invoke(ComMngPopulationServeExcelVO comMngPopulationServeExcelVO, AnalysisContext analysisContext) { - list.add(comMngPopulationServeExcelVO); - // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM + public void invoke(Map<Integer, String> data, AnalysisContext context) { + list.add(data); if (list.size() >= BATCH_COUNT) { - log.info("excel导入数据【{}】", JSONObject.toJSONString(list)); - R r = communityService.listSavePopulationServeExcelVO(list, this.communityId); - if (!R.isOk(r)) { - throw new ServiceException(r.getMsg()); - } - //清空list + saveData(); list.clear(); } } + /** + * 这里会一行行的返回头 + * + * @param headMap + * @param context + */ @Override - public void doAfterAllAnalysed(AnalysisContext analysisContext) { log.info("excel导入数据【{}】", JSONObject.toJSONString(list)); - //确保最后遗留的数据保存在数据库中 - R r = this.communityService.listSavePopulationServeExcelVO(list, this.communityId); - if (!R.isOk(r)) { - throw new ServiceException(r.getMsg()); - } + public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { + headSize = headMap.size(); + headData = headMap; + } + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + saveData(); + LOGGER.info("所有数据解析完成!"); + } + + /** + * 不是固定的列只能手动处理 + */ + private void saveData() { + int index = 2; + try { + ArrayList<ComMngPopulationServeExcelVO> voList = Lists.newArrayList(); + for (Map<Integer, String> oneData : list) { + ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO(); + vo.setName(oneData.get(0)); + vo.setSex(Integer.valueOf(oneData.get(1))); + vo.setAge(Integer.valueOf(oneData.get(2))); + vo.setIsRent(Integer.valueOf(oneData.get(3))); + vo.setRoad(oneData.get(4)); + vo.setDoorNo(Integer.valueOf(oneData.get(5))); + vo.setFloor(Integer.valueOf(oneData.get(6))); + vo.setUnitNo(Integer.valueOf(oneData.get(7))); + vo.setHouseNo(Integer.valueOf(oneData.get(8))); + vo.setNation(oneData.get(9)); + vo.setPoliticalOutlook(oneData.get(10)); + vo.setCardNo(oneData.get(11)); + vo.setPhone(oneData.get(12)); + vo.setNativePlace(oneData.get(13)); + vo.setWorkCompany(oneData.get(14)); + for (int i = 15; i < headSize; i++) { + if (oneData.get(i).equals("是")) { + vo.getUserTagStr().add(headData.get(i)); + } + } + voList.add(vo); + index++; + } + R r = communityService.listSavePopulationServeExcelVO(voList, communityId); + if (!R.isOk(r)) { + throw new ServiceException(r.getMsg()); + } + } catch (NumberFormatException e) { + throw new ServiceException("500", "填写数据类型错误:第" + index + "行" + e.getMessage()); + } } } -- Gitblit v1.7.1