From 9a4ba243d70c49e805b96a11ebe2dd801d175350 Mon Sep 17 00:00:00 2001 From: xiaohuo <14141624+dsgfhrh@user.noreply.gitee.com> Date: Mon, 10 Nov 2025 15:30:29 +0800 Subject: [PATCH] =?UTF-8?q?11.10=E9=87=87=E9=9B=86=E8=A7=84=E5=88=99?= =?UTF-8?q?=E6=94=B9=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alops/system/job/GatherJob.java | 2 +- .../impl/ScheduleRulesServiceImpl.java | 87 +++++++++++++------ 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/job/GatherJob.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/job/GatherJob.java index 977ad4aa..74522106 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/job/GatherJob.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/job/GatherJob.java @@ -124,7 +124,7 @@ public class GatherJob implements Job { rule.setStatus(3); scheduleRulesMapper.updateScheduleRules(rule); try { - String Content=String.format("采集任务整体执行失败,规则ID=%s,规则名称=%s,原因:%s", + String Content=String.format("采集任务整体执行失败,规则ID=%s,规则名称=%s,原因:%s,已取消任务", rule.getId(), rule.getRuleName(), e.getMessage()); iAlertSenderService.sendInternalMessage("采集任务整体执行失败" ,Content,"2" ); diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleRulesServiceImpl.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleRulesServiceImpl.java index 8bc738dd..28fc24d4 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleRulesServiceImpl.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/ScheduleRulesServiceImpl.java @@ -205,7 +205,7 @@ public class ScheduleRulesServiceImpl implements IScheduleRulesService { oidMap.put(dev.getEquManuId(), equOidMapper.findByManuIdAndStatus(dev.getEquManuId())); } // 并发采集 - ExecutorService executor = Executors.newFixedThreadPool(10); + ExecutorService executor = Executors.newFixedThreadPool(12); List> futures = new ArrayList<>(); for (EquBase device : devices) { futures.add(executor.submit(() -> { @@ -271,12 +271,12 @@ public class ScheduleRulesServiceImpl implements IScheduleRulesService { gatherValidator.validateBeforeDbOperation(successList, failed, oidMap); - List failedEquIds = validationResult.getFailed().stream() - .map(f -> f.get("equId")) - .collect(Collectors.toList()); +// List failedEquIds = validationResult.getFailed().stream() +// .map(f -> f.get("equId")) +// .collect(Collectors.toList()); // 事务内:删除成功设备依附表 + 插入成功设备数据 + 执行预警 - deleteAllEquipmentData(failedEquIds); // 删除依附表,不含 equ_gather + deleteAllEquipmentData(validationResult.getSuccessList()); // 删除依附表,不含 equ_gather // 插入成功设备数据(内部失败会抛异常,触发事务回滚) persistAndUpdate(validationResult.getSuccessList()); @@ -287,33 +287,64 @@ public class ScheduleRulesServiceImpl implements IScheduleRulesService { return Code.SUCCESS; } - @Transactional(rollbackFor = Exception.class) - public void deleteAllEquipmentData(List failedEquIds) { - // 1️⃣ 查询失败设备最新采集ID - List excludeGatherIds = failedEquIds == null || failedEquIds.isEmpty() ? - Collections.emptyList() : equGatherMapper.selectLatestGatherIdsByEquIds(failedEquIds); - - // 2️⃣ 依附表列表 - String[] dependentTables = {"equ_interface", "equ_routing", "equ_veneer", "equ_vlan"}; - - for (String table : dependentTables) { - int deleted; - if (excludeGatherIds.isEmpty()) { - // 没有失败设备,直接清空表 - deleted = jdbcTemplate.update("DELETE FROM " + table); - } else { - // 使用占位符方式安全删除 - String inSql = excludeGatherIds.stream().map(id -> "?").collect(Collectors.joining(",")); - String sql = String.format("DELETE FROM %s WHERE equ_gather_id NOT IN (%s)", table, inSql); - deleted = jdbcTemplate.update(sql, excludeGatherIds.toArray()); - } - log.info("表 {} 删除 {} 条数据,保留失败设备采集ID: {}", table, deleted, excludeGatherIds); - } +// @Transactional(rollbackFor = Exception.class) +// public void deleteAllEquipmentData(List failedEquIds) { +// // 1️⃣ 查询失败设备最新采集ID +// List excludeGatherIds = failedEquIds == null || failedEquIds.isEmpty() ? +// Collections.emptyList() : equGatherMapper.selectLatestGatherIdsByEquIds(failedEquIds); +// +// // 2️⃣ 依附表列表 +// String[] dependentTables = {"equ_interface", "equ_routing", "equ_veneer", "equ_vlan"}; +// +// for (String table : dependentTables) { +// int deleted; +// if (excludeGatherIds.isEmpty()) { +// // 没有失败设备,直接清空表 +// deleted = jdbcTemplate.update("DELETE FROM " + table); +// } else { +// // 使用占位符方式安全删除 +// String inSql = excludeGatherIds.stream().map(id -> "?").collect(Collectors.joining(",")); +// String sql = String.format("DELETE FROM %s WHERE equ_gather_id NOT IN (%s)", table, inSql); +// deleted = jdbcTemplate.update(sql, excludeGatherIds.toArray()); +// } +// log.info("表 {} 删除 {} 条数据,保留失败设备采集ID: {}", table, deleted, excludeGatherIds); +// } +// +// log.info("依附表数据删除完成,共保留 {} 个失败设备最新采集ID", excludeGatherIds.size()); +// } +@Transactional(rollbackFor = Exception.class) +public void deleteAllEquipmentData(List successList) { + + // 1️⃣ 提取成功设备ID列表 + List successEquIds = successList.stream() + .map(g -> g.getEquGather().getEquId()) + .filter(Objects::nonNull) + .distinct() + .collect(Collectors.toList()); + + // 2️⃣ 查询这些设备的最新采集ID(要保留的) + List latestGatherIds = equGatherMapper.selectLatestGatherIdsByEquIds(successEquIds); + if (latestGatherIds == null || latestGatherIds.isEmpty()) { + log.warn("未查询到成功设备的最新采集ID,跳过删除操作"); + return; + } - log.info("依附表数据删除完成,共保留 {} 个失败设备最新采集ID", excludeGatherIds.size()); + // 3️⃣ 依附表列表 + String[] dependentTables = {"equ_interface", "equ_routing", "equ_veneer", "equ_vlan"}; + + // 4️⃣ 拼 SQL: 删除所有 成功设备 + String placeholders = latestGatherIds.stream().map(id -> "?").collect(Collectors.joining(",")); + for (String table : dependentTables) { + String sql = String.format("DELETE FROM %s WHERE equ_gather_id IN (%s)", table, placeholders); + int deleted = jdbcTemplate.update(sql, latestGatherIds.toArray()); + log.info("表 {} 删除旧数据 {} 条)", table, deleted); } + log.info("✅ 成功清理依附表数据,"); +} + + /** * 批量落库、更新采集次数、执行最新预警 * 事务保证,如果任何步骤失败,整个操作回滚