From aa76c2b0bb73e9fe463387fb5656ea80094eceec Mon Sep 17 00:00:00 2001
From: tangxiaobao <303826152@qq.com>
Date: 星期三, 04 八月 2021 15:15:59 +0800
Subject: [PATCH] 值班表导入bug修改

---
 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngPopulationServiceImpl.java |  359 ++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 217 insertions(+), 142 deletions(-)

diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngPopulationServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngPopulationServiceImpl.java
index a2ea07c..14fd587 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngPopulationServiceImpl.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngPopulationServiceImpl.java
@@ -51,10 +51,7 @@
 import com.panzhihua.common.utlis.StringUtils;
 import com.panzhihua.service_community.dao.*;
 import com.panzhihua.service_community.model.dos.*;
-import com.panzhihua.service_community.service.ComMngPopulationHouseUserService;
-import com.panzhihua.service_community.service.ComMngPopulationService;
-import com.panzhihua.service_community.service.ComMngVillageService;
-import com.panzhihua.service_community.service.EventResourceService;
+import com.panzhihua.service_community.service.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Value;
@@ -93,6 +90,8 @@
     private ComMngPopulationDAO comMngPopulationDAO;
     @Resource
     private ComMngPopulationHouseUserService comMngPopulationHouseUserService;
+    @Resource
+    private ComMngPopulationHouseService comMngPopulationHouseService;
     @Resource
     private EventResourceService eventResourceService;
     @Resource
@@ -362,173 +361,239 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public R listSavePopulation(List<ComMngPopulationServeExcelVO> list, Long communityId) throws Exception {
+    public R listSavePopulation(List<ComMngPopulationServeExcelVO> list, Long communityId){
+        //需要新增的房屋集合
+        List<ComMngPopulationHouseDO> houseList = new ArrayList<>();
+        //需要新增的人口集合
+        List<ComMngPopulationDO> saveList = new ArrayList<>();
+        //需要修改的人口集合
+        List<ComMngPopulationDO> updateList = new ArrayList<>();
+        //需要新增的人口与房屋关系集合
+        List<ComMngPopulationHouseUserDO> houseUserList = new ArrayList<>();
+
+        log.info("开始处理导入数据");
         List<ComMngPopulationMistakeExcelVO> mistakes = new ArrayList<>();
-        List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
-        //查询该社区
-        ComActDO comActDO = comActDAO.selectById(communityId);
-        //查询该社区的省市区地址
-        AreaAddressVO areaAddressVO = populationDAO.getAreaAddress(comActDO.getProvinceCode(), comActDO.getCityCode(), comActDO.getAreaCode());
-        //查询社区上街道信息
-        String streetName = "";
-        if (comActDO.getStreetId() != null) {
-            ComStreetDO streetDO = comStreetDAO.selectById(comActDO.getStreetId());
-            if (streetDO != null) {
-                streetName = streetDO.getName();
+        try {
+            //查询社区信息
+            log.info("开始查询社区信息数据");
+            ComPopulationActVO populationActVO = comActDAO.getPopulationActById(communityId);
+            if(populationActVO == null){
+                log.error("未查询到社区信息");
+                return R.fail("未查询到社区信息");
             }
-        }
-        StringBuilder areaPath = new StringBuilder();
-        areaPath.append(areaAddressVO.getProvince()).append(">").append(areaAddressVO.getCity()).append(">").append(areaAddressVO.getDistrict()).append(">");
-        //处理实有人口信息
-        for (ComMngPopulationServeExcelVO vo : list) {
-            if (vo.getDoorNo().contains("号")) {
-                vo.setDoorNo(vo.getDoorNo().replace("号",""));
-            }
-            if(vo.getFloor().contains("栋")){
-                vo.setFloor(vo.getFloor().replace("栋",""));
-            }
-            if(vo.getUnitNo().contains("单元")){
-                vo.setUnitNo(vo.getUnitNo().replace("单元",""));
-            }
-            if(vo.getHouseNo().contains("号")){
-                vo.setHouseNo(vo.getHouseNo().replace("号",""));
-            }
-            //查询小区街路巷是否存在
-            ComMngVillageDO comMngVillageDO = comActVillageDAO.selectOne(new QueryWrapper<ComMngVillageDO>().eq("alley", vo.getRoad()).eq("house_num", vo.getDoorNo()).eq("community_id", communityId));
-            if (comMngVillageDO == null) {
-                ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
-                importErrorVO.setErrorMsg("街路巷或小区号不存在,请先在“小区管理”中添加该小区");
-                importErrorVO.setErrorPosition("街路巷:" + vo.getRoad() + ",小区号:" + vo.getDoorNo());
-                populationImportErrorVOList.add(importErrorVO);
-//                index++;
-                ComMngPopulationMistakeExcelVO mistake = new ComMngPopulationMistakeExcelVO();
-                BeanUtils.copyProperties(vo,mistake);
-                setMistake(mistake, vo);
-                mistake.setMistake("街路巷或小区号不存在,请先在“小区管理”中添加该小区:街路巷:"+ vo.getRoad() +",小区号:" + vo.getDoorNo());
-                mistakes.add(mistake);
-                continue;
+            log.info("开始查询社区信息数据完成");
+
+            //查询当前社区标签列表
+            List<String> labelList = new ArrayList<>();
+            List<ComMngUserTagDO> comMngUserTagDOS = comMngUserTagDAO.selectList(new QueryWrapper<ComMngUserTagDO>().eq("sys_flag", 1).or().eq("community_id", communityId));
+            if (!ObjectUtils.isEmpty(comMngUserTagDOS)) {
+                labelList = comMngUserTagDOS.stream().map(comMngUserTagDO -> comMngUserTagDO.getTagName()).collect(Collectors.toList());
             }
 
-            StringBuilder address = new StringBuilder();
-            address.append(areaAddressVO.getProvince()).append(areaAddressVO.getCity()).append(areaAddressVO.getDistrict())
-                    .append(streetName).append(comMngVillageDO.getAlley()).append(vo.getDoorNo()).append("号").append(vo.getFloor()).append("栋")
-                    .append(vo.getUnitNo()).append("单元").append(vo.getHouseNo()).append("号");
+            StringBuilder areaPath = new StringBuilder();
+            areaPath.append(populationActVO.getProvinceName()).append(">").append(populationActVO.getCityName()).append(">").append(populationActVO.getDistrictName()).append(">");
+            //处理实有人口信息
+            Integer nub = 1;
 
-            vo.setAddress(address.toString());
+            //查询所有人口数据放入HashMap中
+            List<ComMngPopulationDO> populationList = this.baseMapper.selectList(null);
+            HashMap<String,Object> populationMap = new HashMap<>();
+            populationList.forEach(population -> {
+                String key = population.getCardNo();
+                populationMap.put(key,population);
+            });
+            //查询所有房屋信息放入到HashMap中
+            List<ComMngPopulationHouseDO> houseLists = comMngPopulationHouseDAO.selectList(null);
+            HashMap<String,Object> houseMap = new HashMap<>();
+            houseLists.forEach(house -> {
+                String key = house.getCommunityId() + house.getVillageId() + house.getFloor() + house.getUnitNo() + house.getHouseNo();
+                houseMap.put(key,house);
+            });
 
-            //todo 后期优化改为批量
-            //先判断房屋是否存在
-            ComMngPopulationHouseDO populationHouseDO = comMngPopulationHouseDAO.selectOne(new QueryWrapper<ComMngPopulationHouseDO>().lambda()
-                    .eq(ComMngPopulationHouseDO::getCommunityId, communityId).eq(ComMngPopulationHouseDO::getVillageId, comMngVillageDO.getVillageId())
-                    .eq(ComMngPopulationHouseDO::getFloor, vo.getFloor()).eq(ComMngPopulationHouseDO::getUnitNo, vo.getUnitNo())
-                    .eq(ComMngPopulationHouseDO::getHouseNo, vo.getHouseNo()));
-            if (populationHouseDO == null) {
-                //房屋信息不存在建立房屋信息
-                populationHouseDO = savePopulationHouse(vo, comMngVillageDO, communityId, areaPath, comActDO.getName());
-                vo.setHouseId(populationHouseDO.getId());
-            }
+            List<ComMngVillageDO> villageList = comActVillageDAO.selectList(null);
+            HashMap<String,Object> villageMap = new HashMap<>();
+            villageList.forEach(village -> {
+                String key = village.getCommunityId() + village.getAlley() + village.getHouseNum();
+                villageMap.put(key,village);
+            });
 
-            if (StringUtils.isEmpty(vo.getName()) && StringUtils.isEmpty(vo.getCardNo())) {
-                //空户处理完房屋信息,直接返回
-                continue;
-            }
-            String cardNoAES = AESUtil.encrypt128(vo.getCardNo(), aesKey);
-            //判断实有人口是否已存在
-            ComMngPopulationDO populationDO = this.baseMapper.selectOne(new QueryWrapper<ComMngPopulationDO>().lambda()
-                    .eq(ComMngPopulationDO::getCardNo, cardNoAES));
-            if (populationDO == null) {
-                //不存在实有人口,则新增
-                populationDO = savePopulationDO(vo, comActDO, comMngVillageDO);
-//                ComMngPopulationHouseUserDO comMngPopulationHouseUserDO = savePopulationHouseUser(vo, populationDO, populationHouseDO);
-            } else {
-                //如果存在人口信息,且是自用房,则更新人口默认的房屋信息
-//                if (vo.getIsRent() != null && vo.getIsRent().equals(PopulHouseUseEnum.SELF.getCode())) {
-//                    populationDO = updatePopulationDO(vo, populationDO);
-//                }
-                populationDO = updatePopulationDO(vo, comActDO, populationDO);
-            }
+            List<ComMngPopulationHouseUserDO> houseUserLists = comMngPopulationHouseUserDAO.selectList(null);
+            HashMap<String,Object> houseUserMap = new HashMap<>();
+            houseUserLists.forEach(houseUser -> {
+                String key = houseUser.getPopulId() + houseUser.getHouseId() + "";
+                houseUserMap.put(key,houseUser);
+            });
 
-            //处理实有人口房屋居住信息
-            if (populationDO != null) {
-                ComMngPopulationHouseUserDO populationHouseUserDO = comMngPopulationHouseUserDAO.selectOne(new QueryWrapper<ComMngPopulationHouseUserDO>()
-                        .lambda().eq(ComMngPopulationHouseUserDO::getHouseId, populationHouseDO.getId())
-                        .eq(ComMngPopulationHouseUserDO::getPopulId, populationDO.getId()));
-                if (populationHouseUserDO == null) {
-                    populationHouseUserDO = new ComMngPopulationHouseUserDO();
-                    populationHouseUserDO.setId(Snowflake.getId());
-                    populationHouseUserDO.setHouseId(populationHouseDO.getId());
-                    populationHouseUserDO.setPopulId(populationDO.getId());
-                    populationHouseUserDO.setRelation(vo.getRelation());
-                    populationHouseUserDO.setRelationId(vo.getIsRent());
-                    populationHouseUserDO.setResidence(vo.getResidence());
-                    comMngPopulationHouseUserDAO.insert(populationHouseUserDO);
+
+            for (ComMngPopulationServeExcelVO vo : list) {
+                if (vo.getDoorNo().contains("号")) {
+                    vo.setDoorNo(vo.getDoorNo().replace("号",""));
                 }
-            }
-        }
-        //如果有错误,返回错误
-        //暂时注释,等客户完成任务需要恢复
-        /*if(!populationImportErrorVOList.isEmpty()){
-            return R.fail(JSON.toJSONString(populationImportErrorVOList));
-        }
-        this.saveBatch(populationDOList);*/
+                if(vo.getFloor().contains("栋")){
+                    vo.setFloor(vo.getFloor().replace("栋",""));
+                }
+                if(vo.getUnitNo().contains("单元")){
+                    vo.setUnitNo(vo.getUnitNo().replace("单元",""));
+                }
+                if(vo.getHouseNo().contains("号")){
+                    vo.setHouseNo(vo.getHouseNo().replace("号",""));
+                }
+                log.info("开始查询小区街路巷是否存在");
+                //查询小区街路巷是否存在
+                ComMngVillageDO comMngVillageDO = null;
+                String villageKey = communityId + vo.getRoad() + vo.getDoorNo();
+                if(!isOnly(villageKey,villageMap)){
+                    comMngVillageDO = (ComMngVillageDO)villageMap.get(villageKey);
+                }else{
+                    ComMngPopulationMistakeExcelVO mistake = new ComMngPopulationMistakeExcelVO();
+                    BeanUtils.copyProperties(vo,mistake);
+                    setMistake(mistake, vo);
+                    mistake.setMistake("街路巷或小区号不存在,请先在“小区管理”中添加该小区:街路巷:"+ vo.getRoad() +",小区号:" + vo.getDoorNo());
+                    mistakes.add(mistake);
+                    log.info("未查询到街路巷:"+ vo.getRoad() +",小区号:" + vo.getDoorNo());
+                    continue;
+                }
+                StringBuilder address = new StringBuilder();
+                address.append(populationActVO.getProvinceName()).append(populationActVO.getCityName()).append(populationActVO.getDistrictName())
+                        .append(populationActVO.getStreetName()).append(comMngVillageDO.getAlley()).append(vo.getDoorNo()).append("号").append(vo.getFloor()).append("栋")
+                        .append(vo.getUnitNo()).append("单元").append(vo.getHouseNo()).append("号");
 
-        if (!populationImportErrorVOList.isEmpty()) {
-            return R.fail(mistakes,JSON.toJSONString(populationImportErrorVOList));
+                vo.setAddress(address.toString());
+                log.info("开始查询小区街路巷是否存在完成");
+
+                log.info("开始查询房屋是否存在");
+                //先判断房屋是否存在
+                ComMngPopulationHouseDO populationHouseDO = null;
+                String houseKey = communityId + comMngVillageDO.getVillageId() + vo.getFloor() + vo.getUnitNo() + vo.getHouseNo();
+                if(isOnly(houseKey,houseMap)){
+                    if(!houseList.isEmpty()){
+                        for (ComMngPopulationHouseDO house:houseList) {
+                            if(house.getVillageId().equals(comMngVillageDO.getVillageId()) && house.getCommunityId().equals(communityId)
+                                    && house.getFloor().equals(vo.getFloor()) && house.getUnitNo().equals(vo.getUnitNo())
+                                    && house.getHouseNo().equals(vo.getHouseNo())){
+                                populationHouseDO = house;
+                                break;
+                            }
+                        }
+                    }
+                    if(populationHouseDO == null){
+                        //房屋信息不存在建立房屋信息
+                        populationHouseDO = savePopulationHouse(vo, comMngVillageDO, communityId, areaPath, populationActVO.getName());
+                        houseList.add(populationHouseDO);
+                    }
+                }else{
+                    populationHouseDO = (ComMngPopulationHouseDO)houseMap.get(houseKey);
+                }
+                vo.setHouseId(populationHouseDO.getId());
+                log.info("开始查询房屋是否存在完成");
+
+                if (StringUtils.isEmpty(vo.getName()) && StringUtils.isEmpty(vo.getCardNo())) {
+                    //空户处理完房屋信息,直接返回
+                    continue;
+                }
+                String cardNoAES = AESUtil.encrypt128(vo.getCardNo(), aesKey);
+                //判断实有人口是否已存在
+                log.info("开始查询实有人口是否已存在");
+                ComMngPopulationDO populationDO = null;
+                String populationKey = vo.getCardNo();
+                if(!isOnly(populationKey,populationMap)){
+                    //存在实有人口信息,则更新
+                    populationDO = (ComMngPopulationDO)populationMap.get(populationKey);
+                    ComMngPopulationDO updatePopulationDO = updatePopulationDO(vo,populationDO,labelList);
+                    updateList.add(updatePopulationDO);
+                }else{
+                    //不存在实有人口,则新增
+                    populationDO = savePopulationDO(vo, populationActVO, comMngVillageDO,labelList);
+                    saveList.add(populationDO);
+                }
+                log.info("开始查询实有人口是否已存在完成");
+
+                log.info("开始查询实有人口房屋居住信息");
+                //处理实有人口房屋居住信息
+                if (populationDO != null) {
+                    ComMngPopulationHouseUserDO populationHouseUserDO = null;
+                    String houseUserKey = populationDO.getId() + populationHouseDO.getId() + "";
+                    if(isOnly(houseUserKey,houseUserMap)){
+                        populationHouseUserDO = new ComMngPopulationHouseUserDO();
+                        populationHouseUserDO.setId(Snowflake.getId());
+                        populationHouseUserDO.setHouseId(populationHouseDO.getId());
+                        populationHouseUserDO.setPopulId(populationDO.getId());
+                        populationHouseUserDO.setRelation(vo.getRelation());
+                        populationHouseUserDO.setRelationId(vo.getIsRent());
+                        populationHouseUserDO.setResidence(vo.getResidence());
+                        houseUserList.add(populationHouseUserDO);
+                    }
+                }
+                log.info("开始查询实有人口房屋居住信息完成");
+            }
+        }catch (Exception e){
+            log.info("出现错误,错误原因:" + e.getMessage());
         }
-        return R.ok();
+        log.info("处理完成导入数据");
+
+        log.info("开始执行数据库导入");
+        if(!houseList.isEmpty()){
+            log.info("执行数据库导入房屋");
+            comMngPopulationHouseDAO.insertAll(houseList);
+            log.info("执行数据库导入房屋完成");
+        }
+        if(!saveList.isEmpty()){
+            log.info("执行数据库导入人口");
+            this.baseMapper.insertAll(saveList);
+            log.info("执行数据库导入人口完成");
+        }
+        if(!updateList.isEmpty()){
+            log.info("执行数据库更新人口");
+            this.baseMapper.updateAll(updateList);
+//            this.updateBatchById(updateList);
+            log.info("执行数据库更新人口完成");
+        }
+        if(!houseUserList.isEmpty()){
+            log.info("执行数据库导入人口房屋关系");
+            comMngPopulationHouseUserService.saveBatch(houseUserList);
+            log.info("执行数据库导入人口房屋关系完成");
+        }
+        log.info("执行数据库导入完成");
+
+        if (!mistakes.isEmpty()) {
+            log.info("返回错误数据");
+            return R.fail(mistakes);
+        }
+        return R.ok(mistakes);
     }
 
-    private ComMngPopulationDO updatePopulationDO(ComMngPopulationServeExcelVO vo, ComActDO comActDO, ComMngPopulationDO populationDO) throws Exception {
-        UpdateWrapper<ComMngPopulationDO> updateWrapper = new UpdateWrapper<>();
-        updateWrapper.eq("id",populationDO.getId());
-        ComMngPopulationDO update = new ComMngPopulationDO();
-//        update.setRoad(vo.getRoad());
-//        update.setDoorNo(vo.getDoorNo());
-//        update.setFloor(vo.getFloor());
-//        update.setUnitNo(vo.getUnitNo());
-//        update.setHouseNo(vo.getHouseNo());
-//        update.setHouseId(vo.getHouseId());
-//        if (StringUtils.isNotEmpty(populationDO.getPhone())) {
-//            update.setPhone(populationDO.getPhone());
-//        }
-        String cardNoAES = AESUtil.encrypt128(vo.getCardNo(), aesKey);
-        BeanUtils.copyProperties(vo, update);
+    private ComMngPopulationDO updatePopulationDO(ComMngPopulationServeExcelVO vo, ComMngPopulationDO populationDO,List<String> labelList){
+        BeanUtils.copyProperties(vo, populationDO);
         List<String> userTag = vo.getUserTagStr().stream().map(userTagStr -> userTagStr.split("\\(")[0]).collect(Collectors.toList());
-        //查询当前社区标签列表
-        List<String> list = new ArrayList<>();
-        List<ComMngUserTagDO> comMngUserTagDOS = comMngUserTagDAO.selectList(new QueryWrapper<ComMngUserTagDO>().eq("sys_flag", 1).or().eq("community_id", comActDO.getCommunityId()));
-        if (!ObjectUtils.isEmpty(comMngUserTagDOS)) {
-            list = comMngUserTagDOS.stream().map(comMngUserTagDO -> comMngUserTagDO.getTagName()).collect(Collectors.toList());
-        }
         //如果导入数据标签中有当前社区标签列表中不包含的标签,则删除
         Iterator<String> iterator = userTag.iterator();
         while (iterator.hasNext()) {
             String s = iterator.next();
-            if (!list.contains(s))
+            if (!labelList.contains(s))
                 iterator.remove();
         }
-        update.setLabel(Joiner.on(",").join(userTag));
-        update.setCardNo(cardNoAES);
-        this.baseMapper.update(update,updateWrapper);
+        String cardNoAES = populationDO.getCardNo();
+        try {
+            cardNoAES = AESUtil.encrypt128(vo.getCardNo(), aesKey);
+        }catch (Exception e){
+            log.error("身份证加密失败");
+        }
+        populationDO.setLabel(Joiner.on(",").join(userTag));
+        populationDO.setCardNo(cardNoAES);
         return populationDO;
     }
 
-    private ComMngPopulationDO savePopulationDO(ComMngPopulationServeExcelVO vo, ComActDO comActDO, ComMngVillageDO comMngVillageDO) {
+    private ComMngPopulationDO savePopulationDO(ComMngPopulationServeExcelVO vo, ComPopulationActVO comActDO, ComMngVillageDO comMngVillageDO,List<String> labelList) {
         ComMngPopulationDO populationDO = new ComMngPopulationDO();
         BeanUtils.copyProperties(vo, populationDO);
         populationDO.setId(Snowflake.getId());
         List<String> userTag = vo.getUserTagStr().stream().map(userTagStr -> userTagStr.split("\\(")[0]).collect(Collectors.toList());
-        //查询当前社区标签列表
-        List<String> list = new ArrayList<>();
-        List<ComMngUserTagDO> comMngUserTagDOS = comMngUserTagDAO.selectList(new QueryWrapper<ComMngUserTagDO>().eq("sys_flag", 1).or().eq("community_id", comActDO.getCommunityId()));
-        if (!ObjectUtils.isEmpty(comMngUserTagDOS)) {
-            list = comMngUserTagDOS.stream().map(comMngUserTagDO -> comMngUserTagDO.getTagName()).collect(Collectors.toList());
-        }
+
         //如果导入数据标签中有当前社区标签列表中不包含的标签,则删除
         Iterator<String> iterator = userTag.iterator();
         while (iterator.hasNext()) {
             String s = iterator.next();
-            if (!list.contains(s))
+            if (!labelList.contains(s))
                 iterator.remove();
         }
         populationDO.setVillageId(comMngVillageDO.getVillageId());
@@ -546,7 +611,7 @@
 //            populationDO.setHouseId(null);
 //        }
 
-        this.baseMapper.insert(populationDO);
+//        this.baseMapper.insert(populationDO);
         return populationDO;
     }
 
@@ -587,7 +652,7 @@
         if (vo.getControlStatus() != null) {
             populationHouseDO.setControlStatus(vo.getControlStatus());
         }
-        comMngPopulationHouseDAO.insert(populationHouseDO);
+//        comMngPopulationHouseDAO.insert(populationHouseDO);
         return populationHouseDO;
     }
 
@@ -1929,4 +1994,14 @@
         mvo.setDeath(PopulOutOrLocalEnum.getCnDescByName(vo.getDeath()));
     }
 
+    /**
+     * 判重方法
+     * @param key   主键
+     * @param hashMap   数据集
+     * @return  存在即为false 不存在则为true
+     */
+    private boolean isOnly(String key,HashMap<String,Object> hashMap){
+        return ObjectUtils.isEmpty(hashMap.get(key));
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.7.1