From 07a44db9ba0e4b28a04b6cb12c53885a26b879f0 Mon Sep 17 00:00:00 2001
From: DESKTOP-71BH0QO\L、ming <172680469@qq.com>
Date: 星期二, 30 三月 2021 10:09:57 +0800
Subject: [PATCH] ADD:实有房屋excel导入

---
 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/api/ComMngVillageApi.java                  |    3 +
 springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/service/community/CommunityService.java                          |    4 +-
 springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/VillageApi.java                    |   21 ++++++----
 springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngVillageServeExcelListen.java                        |   10 ++--
 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngVillageServiceImpl.java |   40 ++++++++++++++------
 springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/vos/community/ComMngVillageVO.java                         |    1 
 6 files changed, 50 insertions(+), 29 deletions(-)

diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngVillageServeExcelListen.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngVillageServeExcelListen.java
index 922104f..eed46f2 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngVillageServeExcelListen.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngVillageServeExcelListen.java
@@ -36,7 +36,7 @@
 
     @Override
     public void invoke(ComMngVillageServeExcelVO comMngVillageServeExcelVO, AnalysisContext analysisContext) {
-        /*list.add(comMngVillageServeExcelVO);
+        list.add(comMngVillageServeExcelVO);
         // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
         if (list.size() >= BATCH_COUNT) {
             log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
@@ -46,16 +46,16 @@
             }
             //清空list
             list.clear();
-        }*/
+        }
     }
 
     @Override
-    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
-       /* log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
+    public void doAfterAllAnalysed(AnalysisContext analysisContext) { log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
         //确保最后遗留的数据保存在数据库中
         R r = this.communityService.listSaveVillageServeExcelVO(list, this.communityId);
         if (!R.isOk(r)) {
             throw new ServiceException(r.getMsg());
-        }*/
+        }
+
     }
 }
diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/vos/community/ComMngVillageVO.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/vos/community/ComMngVillageVO.java
index 40dbc1b..2e5293c 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/vos/community/ComMngVillageVO.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/vos/community/ComMngVillageVO.java
@@ -31,5 +31,6 @@
     private Long communityId;
 
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    @ApiModelProperty("创建时间")
     private Date createAt;
 }
diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/service/community/CommunityService.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/service/community/CommunityService.java
index 7bbb3da..9f63334 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/service/community/CommunityService.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/service/community/CommunityService.java
@@ -1558,6 +1558,6 @@
      * @param communityId
      * @return
      */
-    /*@PostMapping("/village/import")
-    R listSaveVillageServeExcelVO(@RequestBody List<ComMngVillageServeExcelVO> list, @RequestParam Long communityId);*/
+    @PostMapping("/village/import")
+    R listSaveVillageServeExcelVO(@RequestBody List<ComMngVillageServeExcelVO> list, @RequestParam(value = "communityId") Long communityId);
 }
diff --git a/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/VillageApi.java b/springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/VillageApi.java
similarity index 77%
rename from springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/VillageApi.java
rename to springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/VillageApi.java
index 3cbbd2a..380af22 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/VillageApi.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/VillageApi.java
@@ -1,4 +1,4 @@
-package com.panzhihua.applets_backstage.api;
+package com.panzhihua.community_backstage.api;
 
 import com.alibaba.excel.EasyExcel;
 import com.jcraft.jsch.SftpException;
@@ -7,6 +7,7 @@
 import com.panzhihua.common.model.dtos.community.PageComMngVillageDTO;
 import com.panzhihua.common.model.vos.LoginUserInfoVO;
 import com.panzhihua.common.model.vos.R;
+import com.panzhihua.common.model.vos.community.ComMngVillageServeExcelVO;
 import com.panzhihua.common.model.vos.community.ComMngVillageVO;
 import com.panzhihua.common.service.community.CommunityService;
 import io.swagger.annotations.Api;
@@ -34,11 +35,11 @@
 @RequestMapping("/villagemanager/")
 public class VillageApi extends BaseController {
     @Value("${excel.convenientUrl}")
-    private String excelConvenientUrl="http://panzhihua.nhys.cdnhxx.com/web/shiyoufangwudaoru.xlsx";
+    private String excelConvenientUrl = "http://panzhihua.nhys.cdnhxx.com/web/实有房屋导入模板.xlsx";
     @Resource
     private CommunityService communityService;
 
-    @ApiOperation(value = "分页查询实有房屋",response = PageComMngVillageDTO.class )
+    @ApiOperation(value = "分页查询实有房屋", response = ComMngVillageVO.class)
     @PostMapping("pagevillage")
     public R pageVillage(@RequestBody PageComMngVillageDTO pageComMngVillageDTO) {
         LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
@@ -47,7 +48,7 @@
         return communityService.pageVillage(pageComMngVillageDTO);
     }
 
-    @ApiOperation(value = "查询实有房屋",response = ComMngVillageVO.class )
+    @ApiOperation(value = "查询实有房屋", response = ComMngVillageVO.class)
     @PostMapping("listvillage")
     public R listVillage(@RequestBody ComMngVillageVO comMngVillageVO) {
         LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
@@ -56,13 +57,13 @@
         return communityService.listVillage(comMngVillageVO);
     }
 
-    @ApiOperation(value = "新增实有房屋",response = ComMngVillageVO.class )
+    @ApiOperation(value = "新增实有房屋", response = ComMngVillageVO.class)
     @PostMapping("addvillage")
     public R addVillage(@Valid @RequestBody ComMngVillageVO comMngVillageVO) {
         LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
         Long communityId = loginUserInfo.getCommunityId();
         comMngVillageVO.setCommunityId(communityId);
-        return  communityService.addVillage(comMngVillageVO);
+        return communityService.addVillage(comMngVillageVO);
     }
 
     @ApiOperation(value = "删除实有房屋")
@@ -82,14 +83,16 @@
     public R downloadTemplate(@RequestParam MultipartFile file, HttpServletRequest request) {
         //获取文件名
         String fileName = file.getOriginalFilename();
-        log.info("传入文件名字【{}】",fileName);
+        log.info("传入文件名字【{}】", fileName);
         InputStream inputStream = null;
         try {
             inputStream = file.getInputStream();
-            EasyExcel.read(inputStream, ComMngVillageVO.class, new ComMngVillageServeExcelListen(communityService,null)).sheet().doRead();
+            LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
+            ComMngVillageServeExcelListen comMngVillageServeExcelListen = new ComMngVillageServeExcelListen(communityService, loginUserInfo.getCommunityId());
+            EasyExcel.read(inputStream, ComMngVillageServeExcelVO.class, comMngVillageServeExcelListen).sheet().doRead();
         } catch (IOException e) {
-            e.printStackTrace();
             log.error("导入模板失败【{}】", e.getMessage());
+            e.printStackTrace();
         }
         return R.ok();
     }
diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/api/ComMngVillageApi.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/api/ComMngVillageApi.java
index 4c01f93..1b902ea 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/api/ComMngVillageApi.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/api/ComMngVillageApi.java
@@ -78,7 +78,8 @@
      * @return
      */
     @PostMapping("/village/import")
-    public R listSaveMngCarExcelVO(@RequestBody List<ComMngVillageServeExcelVO> list, @RequestParam("communityId") Long communityId){
+    @Transactional(rollbackFor = Exception.class)
+    public R listSaveMngCarExcelVO(@RequestBody List<ComMngVillageServeExcelVO> list, @RequestParam(value = "communityId") Long communityId){
         return comMngVillageService.listSaveVillage(list,communityId);
     }
 
diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngVillageServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngVillageServiceImpl.java
index 6437ed0..227a37a 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngVillageServiceImpl.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComMngVillageServiceImpl.java
@@ -108,20 +108,36 @@
 
     @Override
     public R listSaveVillage(List<ComMngVillageServeExcelVO> list, Long communityId) {
-        //获取社区下所有的实有房屋(小区),打印已经存在的重复数据
+        //思路:获取社区下所有的实有房屋(小区),打印已经存在的重复数据
         List<ComMngVillageDO> comMngVillageDOs = comActVillageDAO.selectList(new QueryWrapper<ComMngVillageDO>().lambda().eq(ComMngVillageDO::getCommunityId, communityId));
-        List<String> alleyList = list.stream().map(vo -> vo.getAlley()).collect(Collectors.toList());
-        List<Integer> houseNumList = list.stream().map(vo -> vo.getHouseNum()).collect(Collectors.toList());
-        List<ComMngVillageDO> resultList = comMngVillageDOs.stream().filter(village -> alleyList.contains(village.getAlley()) && houseNumList.contains(village.getAlley())).collect(Collectors.toList());
-        if (resultList != null && resultList.size() > 0) {
-            List<String> repeatList = resultList.stream().map(ComMngVillageDO::getAlley).collect(Collectors.toList());
-            return R.fail("导入实有房屋,存在数据重复:" + repeatList.toArray());
+        if (list.size() == 0) {
+            return R.fail("数据为空!");
         }
+        int index = 2;
+        for (ComMngVillageServeExcelVO vo : list) {
+            if (vo.getHouseNum() == null) {
+                return R.fail("门牌号第" + index + "行为空!");
+            }
+            if (vo.getAlley() == null) {
+                return R.fail("街路巷第" + index + "行为空!");
+            }
+            //判断DB和exel数据重复判断
+            boolean result = comMngVillageDOs.stream().allMatch(village -> village.getAlley().equals(vo.getAlley()) && village.getHouseNum().equals(vo.getHouseNum()));
+            if (result) {
+                return R.fail("导入街路巷已存在(" + vo.getAlley()+")");
+            }
+            index++;
+        }
+        ComActDO comActDO = comActDAO.selectById(communityId);
         ArrayList<ComMngVillageDO> comMngVillageDOS = Lists.newArrayList();
-        BeanUtils.copyProperties(list, comMngVillageDOS);
-        /*this.saveBatch(comMngCarDOS);
-        return R.ok("共计导入车辆数量:" + comMngCarDOS.size());*/
-        System.out.println(comMngVillageDOS);
-        return null;
+        list.forEach(vo -> {
+            ComMngVillageDO comMngVillageDO = new ComMngVillageDO();
+            BeanUtils.copyProperties(vo, comMngVillageDO);
+            comMngVillageDO.setCommunityId(comActDO.getCommunityId());
+            comMngVillageDO.setStreetId(comActDO.getStreetId());
+            comMngVillageDOS.add(comMngVillageDO);
+        });
+        this.saveBatch(comMngVillageDOS);
+        return R.ok("共计导入实有房屋数量:" + comMngVillageDOS.size());
     }
 }

--
Gitblit v1.7.1