From 7f619c5dd05c76a46c5de33e3ad24e4f2f3e20b7 Mon Sep 17 00:00:00 2001 From: zch Date: Sun, 8 Jun 2025 15:00:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(os-ems):=20=E9=87=8D=E6=9E=84=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E6=95=B0=E6=8D=AE=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=BA=A7=E8=81=94=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 CommonClearController 中的级联删除逻辑- 新增 ICommonClearService接口的 clearTableDataWithCascade 方法 - 实现 CommonClearServiceImpl 中的级联删除逻辑 - 更新 application.yml 文件路径和 Redis密码配置 --- os-admin/src/main/resources/application.yml | 4 +-- .../controller/CommonClearController.java | 19 +---------- .../ems/info/service/ICommonClearService.java | 9 ++++++ .../service/impl/CommonClearServiceImpl.java | 32 +++++++++++++++++++ 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/os-admin/src/main/resources/application.yml b/os-admin/src/main/resources/application.yml index 2bb1075..018290c 100644 --- a/os-admin/src/main/resources/application.yml +++ b/os-admin/src/main/resources/application.yml @@ -9,7 +9,7 @@ ruoyi: # 实例演示开关 demoEnabled: true # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath) - profile: D:/ruoyi/uploadPath + profile: /media/tao_iot/ruoyi/uploadPath # 获取ip地址开关 addressEnabled: false # 验证码类型 math 数字计算 char 字符验证 @@ -86,7 +86,7 @@ spring: # 数据库索引 database: 2 # 密码 - password: 123456 + password: haiwei@123 # 连接超时时间 timeout: 30s lettuce: diff --git a/os-ems/src/main/java/com/os/ems/info/controller/CommonClearController.java b/os-ems/src/main/java/com/os/ems/info/controller/CommonClearController.java index dcf6f33..6c415fb 100644 --- a/os-ems/src/main/java/com/os/ems/info/controller/CommonClearController.java +++ b/os-ems/src/main/java/com/os/ems/info/controller/CommonClearController.java @@ -58,9 +58,6 @@ public class CommonClearController extends BaseController // 监督检查清单 put("supervisionChecklist", new TableInfo("power_energy_supervision_checklist", "监督检查清单", "ems/info:supervisionChecklist:remove")); - - // 备件库记录(特殊处理:需要级联删除明细表) - put("sparePartsInventory", new TableInfo("spare_parts_inventory", "备件库记录", "ems/info:sparePartsInventory:remove", true)); }}; /** @@ -105,14 +102,7 @@ public class CommonClearController extends BaseController } // 5. 执行清空操作 - int result; - if (tableInfo.isCascadeDelete()) { - // 对于需要级联删除的表(如备件库记录),使用特殊处理 - result = commonClearService.clearTableDataWithCascade(tableName, moduleKey); - } else { - // 普通表直接清空 - result = commonClearService.clearTableData(tableName); - } + int result = commonClearService.clearTableData(tableName); // 6. 记录操作日志 logger.info("用户 {} 成功清空了 {} 表,影响行数: {}", getUsername(), tableInfo.getDisplayName(), result); @@ -178,23 +168,16 @@ public class CommonClearController extends BaseController private final String tableName; // 数据库表名 private final String displayName; // 显示名称 private final String permission; // 所需权限 - private final boolean cascadeDelete; // 是否需要级联删除 public TableInfo(String tableName, String displayName, String permission) { - this(tableName, displayName, permission, false); - } - - public TableInfo(String tableName, String displayName, String permission, boolean cascadeDelete) { this.tableName = tableName; this.displayName = displayName; this.permission = permission; - this.cascadeDelete = cascadeDelete; } public String getTableName() { return tableName; } public String getDisplayName() { return displayName; } public String getPermission() { return permission; } - public boolean isCascadeDelete() { return cascadeDelete; } } } \ No newline at end of file diff --git a/os-ems/src/main/java/com/os/ems/info/service/ICommonClearService.java b/os-ems/src/main/java/com/os/ems/info/service/ICommonClearService.java index e96c31d..ea90507 100644 --- a/os-ems/src/main/java/com/os/ems/info/service/ICommonClearService.java +++ b/os-ems/src/main/java/com/os/ems/info/service/ICommonClearService.java @@ -15,4 +15,13 @@ public interface ICommonClearService * @return 删除的记录数 */ public int clearTableData(String tableName); + + /** + * 清空指定表的所有数据(支持级联删除) + * + * @param tableName 主表名 + * @param moduleKey 业务模块标识 + * @return 删除的记录数 + */ + public int clearTableDataWithCascade(String tableName, String moduleKey); } \ No newline at end of file diff --git a/os-ems/src/main/java/com/os/ems/info/service/impl/CommonClearServiceImpl.java b/os-ems/src/main/java/com/os/ems/info/service/impl/CommonClearServiceImpl.java index 928fcf9..4ab012b 100644 --- a/os-ems/src/main/java/com/os/ems/info/service/impl/CommonClearServiceImpl.java +++ b/os-ems/src/main/java/com/os/ems/info/service/impl/CommonClearServiceImpl.java @@ -33,4 +33,36 @@ public class CommonClearServiceImpl implements ICommonClearService // 表名已在Controller层通过白名单验证,这里直接执行 return commonClearMapper.clearTableData(tableName); } + + /** + * 清空指定表的所有数据(支持级联删除) + * 使用事务确保数据一致性 + * + * @param tableName 主表名 + * @param moduleKey 业务模块标识 + * @return 删除的记录数 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public int clearTableDataWithCascade(String tableName, String moduleKey) + { + int totalDeleted = 0; + + // 根据模块标识进行特殊处理 + if ("sparePartsInventory".equals(moduleKey)) { + // 备件库记录:先删除明细表,再删除主表 + // 1. 删除备件盘点记录明细表 + int detailDeleted = commonClearMapper.clearTableData("spare_parts_inventory_check"); + totalDeleted += detailDeleted; + + // 2. 删除备件库记录主表 + int mainDeleted = commonClearMapper.clearTableData("spare_parts_inventory"); + totalDeleted += mainDeleted; + + return totalDeleted; + } else { + // 其他模块使用普通删除 + return commonClearMapper.clearTableData(tableName); + } + } } \ No newline at end of file