springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngPopulationConfirmServeExcelListen.java
New file @@ -0,0 +1,166 @@ package com.panzhihua.common.listen; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.google.common.collect.Lists; import com.panzhihua.common.enums.PopulIsOkEnum; import com.panzhihua.common.enums.PopulPoliticalOutlookEnum; 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.ComMngPopulationServeExcelVO; import com.panzhihua.common.service.community.CommunityService; import com.panzhihua.common.utlis.AgeUtils; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * @description: 实有人口确认导入监听 * @author: llming */ @Slf4j public class ComMngPopulationConfirmServeExcelListen extends AnalysisEventListener<Map<Integer, String>> { private CommunityService communityService; private Long communityId; private static int headSize = 0; private Map<Integer, String> headData; public ComMngPopulationConfirmServeExcelListen(CommunityService communityService, Long communityId) { this.communityService = communityService; this.communityId = communityId; } /** * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 100; List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>(); @Override public void invoke(Map<Integer, String> data, AnalysisContext context) { list.add(data); if (list.size() >= BATCH_COUNT) { saveData(); list.clear(); } } /** * 这里会一行行的返回头 * * @param headMap * @param context */ @Override public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { headSize = headMap.size(); headData = headMap; } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); log.info("所有数据解析完成!"); } /** * 不是固定的列只能手动处理 */ private void saveData() { int index = 2; try { ArrayList<ComMngPopulationServeExcelVO> voList = Lists.newArrayList(); for (Map<Integer, String> oneData : list) { ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO(); if(oneData.get(0) == null){ throw new ServiceException("500", "名字不可为空:第" + index + "行,第1列"); } vo.setName(oneData.get(0)); if(oneData.get(1) == null){ throw new ServiceException("500", "身份证号不可为空:第" + index + "行,第2列"); } vo.setCardNo(oneData.get(1)); //根据身份证号码解析年龄以及性别 //获取用户生日 String birthday = vo.getCardNo().substring(6, 14); //设置用户年龄 vo.setAge(AgeUtils.getAgeFromBirthTime(birthday)); //获取用户性别 int sex = Integer.parseInt(vo.getCardNo().substring(16, 17)); if(sex%2 == 1){ vo.setSex(PopulSexEnum.nan.getCode()); }else{ vo.setSex(PopulSexEnum.nv.getCode()); } if(oneData.get(2) != null){ vo.setNation(oneData.get(2)); } if(oneData.get(3) != null){ vo.setPoliticalOutlook(PopulPoliticalOutlookEnum.getCodeByName(oneData.get(3))); } if(oneData.get(4) != null){ vo.setIsRent(PopulIsOkEnum.getCodeByName(oneData.get(4))); } if(oneData.get(5) != null){ vo.setRelation(oneData.get(5)); } if(oneData.get(6) != null){ vo.setRoad(oneData.get(6)); } if(oneData.get(7) != null){ vo.setDoorNo(Integer.valueOf(oneData.get(7))); } if(oneData.get(8) != null){ vo.setFloor(oneData.get(8)); } if(oneData.get(9) != null){ vo.setUnitNo(Integer.valueOf(oneData.get(9))); } if(oneData.get(10) != null){ vo.setHouseNo(Integer.valueOf(oneData.get(10))); } if(oneData.get(11) != null){ vo.setPhone(oneData.get(11)); } if(oneData.get(12) != null){ vo.setNativePlace(oneData.get(12)); } if(oneData.get(13) != null){ vo.setCultureLevel(oneData.get(13)); } if(oneData.get(14) != null){ vo.setMarriage(oneData.get(14)); } if(oneData.get(15) != null){ vo.setHealthy(oneData.get(15)); } if(oneData.get(16) != null){ vo.setWorkCompany(oneData.get(16)); } if(oneData.get(17) != null){ vo.setRemark(oneData.get(17)); } for (int i = 18; i < headSize; i++) { if (oneData.get(i) != null && oneData.get(i).equals("是")) { vo.getUserTagStr().add(headData.get(i)); } } voList.add(vo); index++; } R r = communityService.listSavePopulationConfirm(voList, communityId); if (!R.isOk(r)) { throw new ServiceException(r.getMsg()); } } catch (NumberFormatException e) { throw new ServiceException("500", "填写数据格式错误:第" + index + "行" + e.getMessage()); } } } springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/service/community/CommunityService.java
@@ -1612,6 +1612,15 @@ R listSavePopulationServeExcelVO(@RequestBody List<ComMngPopulationServeExcelVO> list, @RequestParam(value = "communityId") Long communityId); /** * 确认导入实有人口(有则更新,无则新建) * @param list 用户信息 * @param communityId 社区id * @return 导入结果 */ @PostMapping("/common/data/population/import/confirm") R listSavePopulationConfirm(@RequestBody List<ComMngPopulationServeExcelVO> list, @RequestParam(value = "communityId") Long communityId); /** * 分页查询实有人口 * * @param comMngPopulationVO 查询参数 springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/CommonDataApi.java
@@ -712,6 +712,25 @@ return R.ok(); } @ApiOperation(value = "excel确认导入实有人口") @PostMapping(value = "/serve/population/import/confirm", consumes = "multipart/*", headers = "content-type=multipart/form-date") public R listSavePopulationConfirm(@RequestParam MultipartFile file, HttpServletRequest request) { //获取文件名 String fileName = file.getOriginalFilename(); log.info("传入文件名字【{}】", fileName); InputStream inputStream = null; try { inputStream = file.getInputStream(); LoginUserInfoVO loginUserInfo = this.getLoginUserInfo(); ComMngPopulationConfirmServeExcelListen comMngPopulationServeExcelListen = new ComMngPopulationConfirmServeExcelListen(communityService, loginUserInfo.getCommunityId()); EasyExcel.read(inputStream, null, comMngPopulationServeExcelListen).sheet().doRead(); } catch (IOException e) { log.error("导入模板失败【{}】", e.getMessage()); e.printStackTrace(); } return R.ok(); } @ApiOperation(value = "实有人口详情", response = ComMngPopulationVO.class) @PostMapping("/population/detail") public R detailPopulation(@RequestParam(value = "populationId") Long populationId) { springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/api/CommonDataApi.java
@@ -277,6 +277,18 @@ } /** * 确认导入实有人口(有则更新,无则新建) * @param list 用户信息 * @param communityId 社区id * @return 导入结果 */ @PostMapping("/population/import/confirm") @Transactional(rollbackFor = Exception.class) public R listSavePopulationConfirm(@RequestBody List<ComMngPopulationServeExcelVO> list, @RequestParam(value = "communityId") Long communityId){ return comMngPopulationService.listSavePopulationConfirm(list,communityId); } /** * 根据实有人口id查询详情 * @param populationId 实有人口id * @return 实有人口详情查询结果 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/ComMngPopulationService.java
@@ -63,6 +63,14 @@ * @return */ R listSavePopulation(List<ComMngPopulationServeExcelVO> list, Long communityId); /** * 确认导入实有人口(有则更新,无则新建) * @param list 用户信息 * @param communityId 社区id * @return 导入结果 */ R listSavePopulationConfirm(List<ComMngPopulationServeExcelVO> list, Long communityId); /** * 根据实有人口id修改用户标签 * @param populationTagDTO 请求参数 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngPopulationServiceImpl.java
@@ -11,6 +11,7 @@ import com.panzhihua.common.model.dtos.community.ComMngPopulationDTO; import com.panzhihua.common.model.dtos.community.ComMngPopulationTagDTO; import com.panzhihua.common.model.dtos.community.PageComActDTO; import com.panzhihua.common.model.helper.AESUtil; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.*; import com.panzhihua.common.model.vos.user.ComHouseMemberVo; @@ -25,6 +26,7 @@ import com.panzhihua.service_community.model.dos.ComMngVillageDO; import com.panzhihua.service_community.service.ComMngPopulationService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -46,6 +48,8 @@ private ComActDAO comActDAO; @Resource private ComActVillageDAO comActVillageDAO; @Value("${domain.aesKey:}") private String aesKey; /** @@ -223,6 +227,64 @@ return R.ok("共计导入实有人口数量:" + populationDOList.size()); } /** * 确认导入实有人口(有则更新,无则新建) * @param list 用户信息 * @param communityId 社区id * @return 导入结果 */ @Override public R listSavePopulationConfirm(List<ComMngPopulationServeExcelVO> list, Long communityId) { if (list.size() == 0) { return R.fail("数据为空!"); } List<ComMngPopulationDO> comMngPopulationDOS = populationDAO.selectList(new QueryWrapper<ComMngPopulationDO>().lambda().eq(ComMngPopulationDO::getActId, communityId)); //查询该社区所有(实有房屋)小区 // List<ComMngVillageDO> villageDOList = comActVillageDAO.selectList(new QueryWrapper<ComMngVillageDO>().lambda().eq(ComMngVillageDO::getCommunityId, communityId)); ComActDO comActDO = comActDAO.selectById(communityId); ArrayList<ComMngPopulationDO> populationDOList = Lists.newArrayList(); list.forEach(vo -> { ComMngPopulationDO comMngPopulationDO = new ComMngPopulationDO(); //查询街路巷是否存在 ComMngVillageDO comMngVillageDO = comActVillageDAO.selectOne(new QueryWrapper<ComMngVillageDO>().eq("alley",vo.getRoad()).eq("house_num",vo.getDoorNo()).eq("community_id",communityId)); // ComMngVillageDO comMngVillageDO = villageDOList.stream().filter(village -> village.getAlley().equals(vo.getRoad()) && village.getHouseNum().equals(Integer.valueOf(vo.getDoorNo()))).findFirst().orElse(null); BeanUtils.copyProperties(vo, comMngPopulationDO); if (comMngVillageDO == null) { throw new ServiceException("街道巷:" + vo.getRoad() + "不存在!"); } comMngPopulationDO.setVillageId(comMngVillageDO.getVillageId()); comMngPopulationDO.setActId(comActDO.getCommunityId()); comMngPopulationDO.setStreetId(comActDO.getStreetId()); comMngPopulationDO.setLabel(Joiner.on(",").join(vo.getUserTagStr())); comMngPopulationDO.setVillageName(comMngVillageDO.getGroupAt()); populationDOList.add(comMngPopulationDO); }); if(!populationDOList.isEmpty()){ //循环遍历待导入人员信息,如果数据库存在则更新,如果不存在则新建 populationDOList.forEach(population -> { try { //加密身份证号码 String cardNo = AESUtil.encrypt128(population.getCardNo(), aesKey); population.setCardNo(cardNo); //查询这个用户是否存在 ComMngPopulationDO comMngPopulationDO = this.populationDAO.selectOne(new QueryWrapper<ComMngPopulationDO>().lambda() .eq(ComMngPopulationDO::getCardNo, population.getCardNo())); if(comMngPopulationDO != null){ population.setId(comMngPopulationDO.getId()); BeanUtils.copyProperties(population,comMngPopulationDO); this.populationDAO.updateById(population); }else{ this.populationDAO.insert(population); } }catch (Exception e){ log.error("导入实有人口失败"); } }); } return R.ok("共计导入实有人口数量:" + populationDOList.size()); } public boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { System.out.println(str.charAt(i));