Jelajahi Sumber

新需求:工程划分>>增加批量删除功能

lvy 3 bulan lalu
induk
melakukan
72b6f94cb4

+ 196 - 91
blade-service/blade-business/src/main/java/org/springblade/business/controller/InformationWriteQueryController.java

@@ -147,6 +147,8 @@ public class InformationWriteQueryController extends BladeController {
 
     private final ExcelTabClient excelTabClient;
 
+    private final IRecycleBinService recycleBinService;
+
     @Autowired
     StringRedisTemplate RedisTemplate;
 
@@ -1454,7 +1456,7 @@ public R<Object> batchTask(@RequestBody StartTaskVO startTaskVO) {
                             jdbcTemplate.execute(sql);
                         }
                         var = true;
-                        getPdfs(queryMap.get(id));
+//                        getPdfs(queryMap.get(id));
                     } else {
                         return R.fail("创建主流程失败");
                     }
@@ -3087,8 +3089,8 @@ private void currentNodeAllParent(StringBuilder nodeName, WbsTreeContract curren
 private void currentNodeAllChild(List<WbsTreeContract> resultChildNodes, List<WbsTreeContract> childNodes, String contractId) {
     if (childNodes.size() > 0) {
         //如果存在子级,那么递归获取子级的子级
-        List<Long> ids = childNodes.stream().map(WbsTreeContract::getId).collect(Collectors.toList());
-        List<WbsTreeContract> childNodeList = jdbcTemplate.query("select p_key_id,id,parent_id,type from m_wbs_tree_contract where is_deleted = 0 and contract_id = " + contractId + " and parent_id in(" + StringUtils.join(ids, ",") + ")", new BeanPropertyRowMapper<>(WbsTreeContract.class));
+        List<Long> ids = childNodes.stream().map(WbsTreeContract::getId).distinct().collect(Collectors.toList());
+        List<WbsTreeContract> childNodeList = jdbcTemplate.query("select p_key_id,id,parent_id,type,contract_id,project_id,full_name,node_name from m_wbs_tree_contract where is_deleted = 0 and contract_id = " + contractId + " and parent_id in(" + StringUtils.join(ids, ",") + ")", new BeanPropertyRowMapper<>(WbsTreeContract.class));
         if (childNodeList.size() > 0) {
             resultChildNodes.addAll(childNodeList);
             this.currentNodeAllChild(resultChildNodes, childNodeList, contractId);
@@ -3123,105 +3125,208 @@ public R removeContractTreeNodeJudge(@RequestParam String ids) {
     return R.data(true);
 }
 
-/**
- * 删除节点
- *
- * @param ids 节点的primaryKeyId(只有一个值)
- * @return 删除结果
- */
-@PostMapping("/removeContractTreeNode")
-@ApiOperationSupport(order = 11)
-@ApiOperation(value = "删除节点")
-@ApiImplicitParam(name = "ids", value = "节点的primaryKeyId")
-public R<Boolean> removeContractTreeNode(@RequestParam String ids) {
-    //根据传入的节点,将其所有子节点删除
-    WbsTreeContract removeNode = this.wbsTreeContractClient.getContractWbsTreeByPrimaryKeyId(Long.parseLong(ids));
+    /**
+     * 删除节点
+     * @param ids 节点的primaryKeyId
+     * @return 删除结果
+     */
+    @PostMapping("/removeContractTreeNode")
+    @ApiOperationSupport(order = 11)
+    @ApiOperation(value = "批量删除节点")
+    @ApiImplicitParam(name = "ids", value = "节点的primaryKeyId, 逗号拼接")
+    public R<Boolean> removeContractTreeNode(@RequestParam String ids) {
+        if (!ids.isEmpty()) {
+            List<WbsTreeContract> removeWbsTreeContracts = this.jdbcTemplate.query("select p_key_id,id,parent_id,type,contract_id,project_id,full_name,node_name from m_wbs_tree_contract where is_deleted = 0 and p_key_id in (" + ids + ")", new BeanPropertyRowMapper<>(WbsTreeContract.class));
+            if (removeWbsTreeContracts.isEmpty()) {
+                return R.fail("删除失败:节点不存在");
+            }
 
-        /*//判断是否存在原始节点
-        if (ObjectUtil.isEmpty(removeNode.getOldId())) {
-            return R.data(300, false, "节点【" + removeNode.getFullName() + "】为原始节点,不允许删除");
-        }*/
+            String contractId = removeWbsTreeContracts.get(0).getContractId();
+            String projectId = removeWbsTreeContracts.get(0).getProjectId();
+            removeWbsTreeContracts.forEach(node -> {
+                if (contractId == null || !contractId.equals(node.getContractId())) {
+                    throw new ServiceException("删除失败:节点不属于同一合同段");
+                }
+            });
 
-    //查询当前操作的节点的父级关系
-    StringBuilder parentNodeName = new StringBuilder();
-    this.currentNodeAllParent(parentNodeName, removeNode);
+            List<WbsTreeContract> removeNodeList = new ArrayList<>();
+            Map<Long , WbsTreeContract> unremoveNodeMap = new HashMap<>();
+            // 查询所有子节点
+            this.currentNodeAllChild(removeNodeList, removeWbsTreeContracts, contractId);
+            removeNodeList.addAll(removeWbsTreeContracts);
+            // 根据pKeyId去重
+            removeNodeList = distinct(removeNodeList);
+            //获取pKeyId
+            List<Long> removeList = removeNodeList.stream().map(WbsTreeContract::getPKeyId).distinct().collect(Collectors.toList());
+            if (!removeList.isEmpty()) {
+                String wbsIds = removeList.stream().map(Object::toString).collect(Collectors.joining(","));
+                /** 判断是否子节点有上报或审批过的资料,因为父id和祖级节点错误,直接使用上面的值去查询上报情况*/
+                List<String> unRemoveIds = jdbcTemplate.queryForList("SELECT wbs_id from u_information_query WHERE wbs_id in (" + wbsIds + ") GROUP BY wbs_id HAVING count(1) > 0", String.class);
+                if (!unRemoveIds.isEmpty()) {
+                    // 剔除此节点
+                    Map<String, String> map = unRemoveIds.stream().collect(Collectors.toMap(id -> id, Function.identity()));
+                    removeNodeList.removeIf(node -> {
+                        //删除掉表格 TODO(不清楚为什么要剔除表格,按理说删除节点后,节点下的表也应该一起删除的,猜测或许是为了方便恢复节点的时候表数据还存在)2023年9月19日
+                        boolean removeIf = node.getType() != null && new Integer("2").equals(node.getType());
+                        boolean equals = map.containsKey(node.getPKeyId().toString());
+                        if (equals) {
+                            unremoveNodeMap.put(node.getPKeyId(), node);
+                        }
+                        return removeIf || equals;
+                    });
+                    Map<Long, WbsTreeContract> contractMap = removeNodeList.stream().collect(Collectors.toMap(WbsTreeContract::getId, Function.identity()));
+                    Map<Long, WbsTreeContract> tempMap = new HashMap<>();
+                    unremoveNodeMap.forEach((key, value) -> collectNodeAndAncestors(value, contractMap, tempMap));
+                    unremoveNodeMap.putAll(tempMap);
+                }
+            }
 
-    //判断是否子节点有上报或审批过的资料
-//        List<InformationQuery> list = informationQueryService.selectChildrenNodeInfo(removeNode);
-//        if (list != null && list.size() >= 1) {
-//            return R.data(300, false, "存在已经上报或审批的节点,不允许删除");
-//        }
+            StringBuilder position = new StringBuilder();
+            List<String> idArray = removeNodeList.stream().filter(wbsTreeContract -> !unremoveNodeMap.containsKey(wbsTreeContract.getPKeyId())).map(wbsTreeContract -> {
+                position.append(",").append(StringUtils.isNotEmpty(wbsTreeContract.getFullName()) ? wbsTreeContract.getFullName() : wbsTreeContract.getNodeName());
+                return wbsTreeContract.getPKeyId() + "";
+            }).distinct().collect(Collectors.toList());
 
-    //查询所有子节点 TODO(不能用ancestors字段获取,ancestors字段有问题,目前用递归获取)2023年9月19日
-    //List<WbsTreeContract> removeNodeList = jdbcTemplate.query("select p_key_id,type from m_wbs_tree_contract where is_deleted = 0 and contract_id = " + removeNode.getContractId() + " and ancestors like '%" + removeNode.getId() + "%'", new BeanPropertyRowMapper<>(WbsTreeContract.class));
-    List<WbsTreeContract> removeNodeList = new ArrayList<>();
-    this.currentNodeAllChild(removeNodeList, Collections.singletonList(removeNode), removeNode.getContractId());
+            JSONObject json = new JSONObject();
+            json.put("operationObjIds", idArray);
+            String positionStr = position.substring(1);
+            json.put("operationObjName", positionStr);
+            json.put("projectId", projectId);
+            json.put("contractId", contractId);
+            this.operationLogClient.saveUserOperationLog(4, "资料管理", "工序资料", json);
+
+            //保存进回收站
+            this.recycleBinService.save(new RecycleBin(String.join(",", idArray), "工程划分批量删除", 2, positionStr, projectId, contractId));
+            //改为物理删除 (8.28改为逻辑删除,方便恢复)
+            this.wbsTreeContractClient.removeContractTreeNode(idArray);
+            //更新redis
+            this.informationQueryService.delAsyncWbsTree(removeWbsTreeContracts.get(0).getContractId());
+        }
+        return R.data(true);
+    }
 
-    //获取pKeyId
-    List<Long> removeList = removeNodeList.stream().map(WbsTreeContract::getPKeyId).distinct().collect(Collectors.toList());
-    if (removeList.size() > 0) {
-        /** 判断是否子节点有上报或审批过的资料,因为父id和祖级节点错误,直接使用上面的值去查询上报情况*/
-        List<InformationQuery> list = informationQueryService.selectChildrenNodeInfo(removeList);
-        if (list != null && list.size() >= 1) {
-            return R.data(300, false, "存在已经上报或审批的节点,不允许删除");
+    // 递归收集节点及其所有父节点
+    private  void collectNodeAndAncestors(WbsTreeContract node, Map<Long, WbsTreeContract> contractMap, Map<Long , WbsTreeContract> unremoveNodeMap) {
+        if (node == null || node.getParentId() == null || contractMap == null || contractMap.isEmpty()) return;
+        WbsTreeContract item = contractMap.get(node.getParentId());
+        if (item != null && item.getId() != null && item.getId().equals(node.getParentId())) {
+            unremoveNodeMap.put(item.getPKeyId(), item);
+            collectNodeAndAncestors(item, contractMap, unremoveNodeMap);
         }
-        //拼接
-        ids = ids + "," + String.join(",", JSONArray.parseArray(JSONObject.toJSONString(removeList), String.class));
-        //删除掉表格 TODO(不清楚为什么要剔除表格,按理说删除节点后,节点下的表也应该一起删除的,猜测或许是为了方便恢复节点的时候表数据还存在)2023年9月19日
-        removeNodeList.removeIf(tree -> tree.getType() != null && new Integer("2").equals(tree.getType()));
     }
-    //获取被删除节点名称
-    //String nodeName = StringUtils.isNotEmpty(removeNode.getFullName()) ? removeNode.getFullName() : removeNode.getNodeName() + "," + removeNodeList.stream().map(wbs -> StringUtils.isNotEmpty(wbs.getFullName()) ? wbs.getFullName() : wbs.getNodeName()).collect(Collectors.joining());
-
-    //获取当前节点下所有填报节点
-        /*List<QueryProcessDataVO> queryProcess = new ArrayList<>();
-        if (!Arrays.asList("1,2,3,4".split(",")).contains(removeNode.getMajorDataType().toString())) {
-            queryProcess = this.informationQueryService.queryProcessDataByParentIdAndContractId2(removeNode.getId().toString(), 1, removeNode.getContractId());
-            if (queryProcess == null || queryProcess.size() == 0) {
-                //填报节点
-                queryProcess = this.informationQueryService.queryProcessDataByPrimaryKeyIdAndClassify(removeNode.getPKeyId().toString(), 1);
-            }
+    private List<WbsTreeContract> distinct(List<WbsTreeContract> list) {
+        if (list == null || list.isEmpty()) {
+            return list;
         }
-
-        if (queryProcess != null && queryProcess.size() > 0) {
-            //检查这些填报节点是否存在已经审批或已经上报的节点,如果存在则不允许删除
-            List<QueryProcessDataVO> approvalList = queryProcess.stream().filter(vo -> new Integer("2").equals(vo.getStatus()) && vo.getInformationQueryId() != null).collect(Collectors.toList());
-            List<QueryProcessDataVO> runTaskList = queryProcess.stream().filter(vo -> new Integer("1").equals(vo.getStatus()) && vo.getInformationQueryId() != null).collect(Collectors.toList());
-            if (approvalList.size() > 0 || runTaskList.size() > 0) {
-                //说明存在已经审批或已经上报的节点,不允许删除
-                return R.data(300, false, "存在已经上报或审批的节点,不允许删除");
+        Map<Long, WbsTreeContract> tempMap = new HashMap<>();
+        return list.stream().filter(node -> {
+            if (tempMap.containsKey(node.getPKeyId())) {
+                return false;
             }
-        }*/
-
-    //保存操作记录
-    List<String> idArray = JSONArray.parseArray(JSONObject.toJSONString(ids.split(",")), String.class);
-    //获取当前节点的所有父节点
-    List<WbsTreeContract> result = new ArrayList<>();
-    result.add(removeNode);
-    this.queryParentNode(removeNode, result);
-    StringBuilder pathName = new StringBuilder();
-    for (int i = 1, l = result.size(); i <= l; i++) {
-        WbsTreeContract node = result.get(result.size() - i);
-        pathName.append("-").append(StringUtils.isNotEmpty(node.getFullName()) ? node.getFullName() : node.getNodeName());
+            tempMap.put(node.getPKeyId(), node);
+            return true;
+        }).collect(Collectors.toList());
     }
-    JSONObject json = new JSONObject();
-    json.put("operationObjIds", idArray);
-    json.put("operationObjName", pathName.substring(1));
-    json.put("projectId", removeNode.getProjectId());
-    json.put("contractId", removeNode.getContractId());
-    this.operationLogClient.saveUserOperationLog(4, "资料管理", "工序资料", json);
 
-    //保存进回收站
-    this.recycleBinClient.saveDelBusinessData(idArray, StringUtils.isNotEmpty(removeNode.getFullName()) ? removeNode.getFullName() : removeNode.getNodeName(), 2, parentNodeName.toString(), removeNode.getProjectId(), removeNode.getContractId());
-
-    //改为物理删除 (8.28改为逻辑删除,方便恢复)
-    this.wbsTreeContractClient.removeContractTreeNode(idArray);
-
-    //更新redis
-    this.informationQueryService.delAsyncWbsTree(removeNode.getContractId());
-
-    return R.data(true);
-}
+///**
+// * 删除节点
+// *
+// * @param ids 节点的primaryKeyId(只有一个值)
+// * @return 删除结果
+// */
+//@PostMapping("/removeContractTreeNode")
+//@ApiOperationSupport(order = 11)
+//@ApiOperation(value = "删除节点")
+//@ApiImplicitParam(name = "ids", value = "节点的primaryKeyId")
+//public R<Boolean> removeContractTreeNode(@RequestParam String ids) {
+//    //根据传入的节点,将其所有子节点删除
+//    WbsTreeContract removeNode = this.wbsTreeContractClient.getContractWbsTreeByPrimaryKeyId(Long.parseLong(ids));
+//
+//        /*//判断是否存在原始节点
+//        if (ObjectUtil.isEmpty(removeNode.getOldId())) {
+//            return R.data(300, false, "节点【" + removeNode.getFullName() + "】为原始节点,不允许删除");
+//        }*/
+//
+//    //查询当前操作的节点的父级关系
+//    StringBuilder parentNodeName = new StringBuilder();
+//    this.currentNodeAllParent(parentNodeName, removeNode);
+//
+//    //判断是否子节点有上报或审批过的资料
+////        List<InformationQuery> list = informationQueryService.selectChildrenNodeInfo(removeNode);
+////        if (list != null && list.size() >= 1) {
+////            return R.data(300, false, "存在已经上报或审批的节点,不允许删除");
+////        }
+//
+//    //查询所有子节点 TODO(不能用ancestors字段获取,ancestors字段有问题,目前用递归获取)2023年9月19日
+//    //List<WbsTreeContract> removeNodeList = jdbcTemplate.query("select p_key_id,type from m_wbs_tree_contract where is_deleted = 0 and contract_id = " + removeNode.getContractId() + " and ancestors like '%" + removeNode.getId() + "%'", new BeanPropertyRowMapper<>(WbsTreeContract.class));
+//    List<WbsTreeContract> removeNodeList = new ArrayList<>();
+//    this.currentNodeAllChild(removeNodeList, Collections.singletonList(removeNode), removeNode.getContractId());
+//
+//    //获取pKeyId
+//    List<Long> removeList = removeNodeList.stream().map(WbsTreeContract::getPKeyId).distinct().collect(Collectors.toList());
+//    if (removeList.size() > 0) {
+//        /** 判断是否子节点有上报或审批过的资料,因为父id和祖级节点错误,直接使用上面的值去查询上报情况*/
+//        List<InformationQuery> list = informationQueryService.selectChildrenNodeInfo(removeList);
+//        if (list != null && list.size() >= 1) {
+//            return R.data(300, false, "存在已经上报或审批的节点,不允许删除");
+//        }
+//        //拼接
+//        ids = ids + "," + String.join(",", JSONArray.parseArray(JSONObject.toJSONString(removeList), String.class));
+//        //删除掉表格 TODO(不清楚为什么要剔除表格,按理说删除节点后,节点下的表也应该一起删除的,猜测或许是为了方便恢复节点的时候表数据还存在)2023年9月19日
+//        removeNodeList.removeIf(tree -> tree.getType() != null && new Integer("2").equals(tree.getType()));
+//    }
+//    //获取被删除节点名称
+//    //String nodeName = StringUtils.isNotEmpty(removeNode.getFullName()) ? removeNode.getFullName() : removeNode.getNodeName() + "," + removeNodeList.stream().map(wbs -> StringUtils.isNotEmpty(wbs.getFullName()) ? wbs.getFullName() : wbs.getNodeName()).collect(Collectors.joining());
+//
+//    //获取当前节点下所有填报节点
+//        /*List<QueryProcessDataVO> queryProcess = new ArrayList<>();
+//        if (!Arrays.asList("1,2,3,4".split(",")).contains(removeNode.getMajorDataType().toString())) {
+//            queryProcess = this.informationQueryService.queryProcessDataByParentIdAndContractId2(removeNode.getId().toString(), 1, removeNode.getContractId());
+//            if (queryProcess == null || queryProcess.size() == 0) {
+//                //填报节点
+//                queryProcess = this.informationQueryService.queryProcessDataByPrimaryKeyIdAndClassify(removeNode.getPKeyId().toString(), 1);
+//            }
+//        }
+//
+//        if (queryProcess != null && queryProcess.size() > 0) {
+//            //检查这些填报节点是否存在已经审批或已经上报的节点,如果存在则不允许删除
+//            List<QueryProcessDataVO> approvalList = queryProcess.stream().filter(vo -> new Integer("2").equals(vo.getStatus()) && vo.getInformationQueryId() != null).collect(Collectors.toList());
+//            List<QueryProcessDataVO> runTaskList = queryProcess.stream().filter(vo -> new Integer("1").equals(vo.getStatus()) && vo.getInformationQueryId() != null).collect(Collectors.toList());
+//            if (approvalList.size() > 0 || runTaskList.size() > 0) {
+//                //说明存在已经审批或已经上报的节点,不允许删除
+//                return R.data(300, false, "存在已经上报或审批的节点,不允许删除");
+//            }
+//        }*/
+//
+//    //保存操作记录
+//    List<String> idArray = JSONArray.parseArray(JSONObject.toJSONString(ids.split(",")), String.class);
+//    //获取当前节点的所有父节点
+//    List<WbsTreeContract> result = new ArrayList<>();
+//    result.add(removeNode);
+//    this.queryParentNode(removeNode, result);
+//    StringBuilder pathName = new StringBuilder();
+//    for (int i = 1, l = result.size(); i <= l; i++) {
+//        WbsTreeContract node = result.get(result.size() - i);
+//        pathName.append("-").append(StringUtils.isNotEmpty(node.getFullName()) ? node.getFullName() : node.getNodeName());
+//    }
+//    JSONObject json = new JSONObject();
+//    json.put("operationObjIds", idArray);
+//    json.put("operationObjName", pathName.substring(1));
+//    json.put("projectId", removeNode.getProjectId());
+//    json.put("contractId", removeNode.getContractId());
+//    this.operationLogClient.saveUserOperationLog(4, "资料管理", "工序资料", json);
+//
+//    //保存进回收站
+//    this.recycleBinClient.saveDelBusinessData(idArray, StringUtils.isNotEmpty(removeNode.getFullName()) ? removeNode.getFullName() : removeNode.getNodeName(), 2, parentNodeName.toString(), removeNode.getProjectId(), removeNode.getContractId());
+//
+//    //改为物理删除 (8.28改为逻辑删除,方便恢复)
+//    this.wbsTreeContractClient.removeContractTreeNode(idArray);
+//
+//    //更新redis
+//    this.informationQueryService.delAsyncWbsTree(removeNode.getContractId());
+//
+//    return R.data(true);
+//}
 
 /**
  * 新增子节点