diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java index 5448eb8..78034ba 100644 --- a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java @@ -34,6 +34,61 @@ public interface DbClearService { */ DbClearResult execute(String appCode, String dbTs); + /** + * P1-2e (2026-07-23): execute overload 支持范围控制 + * dacli 端通过 --include-workflow/--include-di 等 flag 控制, 避免一次清太狠 + * + * @param appCode 应用代码 + * @param dbTs DB 表名前缀时间戳 + * @param scope 清理范围 (workflow/di/portal/print/report/schedule/dict/biz 各开关) + * @return 执行结果 + */ + DbClearResult execute(String appCode, String dbTs, DbClearScope scope); + + /** + * P1-2e (2026-07-23): 清理范围控制 DTO + * 默认 all=true (与历史行为一致, 全部清干净) + */ + class DbClearScope { + public boolean includeWorkflow; // workflow_template 等 23 张 + public boolean includeDi; // base_data_interface 等 5 张 + public boolean includePortal; // base_portal 等 3 张 + public boolean includePrint; // base_print_template 等 3 张 + public boolean includeReport; // report_template + public boolean includeSchedule; // base_schedule 等 3 张 + public boolean includeDict; // base_dictionary_type + data + public boolean includeBiz; // 业务库 RENAME + + public DbClearScope() { + this.includeWorkflow = true; + this.includeDi = true; + this.includePortal = true; + this.includePrint = true; + this.includeReport = true; + this.includeSchedule = true; + this.includeDict = true; + this.includeBiz = true; + } + + /** 全开 (默认行为) */ + public static DbClearScope all() { + return new DbClearScope(); + } + + /** 仅 form/menu/dict (保守, 不动 workflow/di/portal 等全局资源) */ + public static DbClearScope conservative() { + DbClearScope s = new DbClearScope(); + s.includeWorkflow = false; + s.includeDi = false; + s.includePortal = false; + s.includePrint = false; + s.includeReport = false; + s.includeSchedule = false; + s.includeBiz = false; + return s; + } + } + // ===================================================================== // DTO // ===================================================================== @@ -68,10 +123,25 @@ public interface DbClearService { public List mainHits; public List bizTables; public String error; // 错误信息 (业务库不存在等, 非致命) + // P1-2e (2026-07-23): 按范围拆分, 方便 dacli 端按 flag 控制 + preview 展示 + public List workflowHits; // workflow_template + 子表 (含 platform-global enCode 残留) + public List diHits; // base_data_interface + 子表 + public List portalHits; // base_portal + 子表 + public List printHits; // base_print_template + 子表 + public List reportHits; // report_template + public List scheduleHits; // base_schedule + 子表 + public List dictHits; // base_dictionary_type + base_dictionary_data public DbClearPreview() { this.mainHits = new java.util.ArrayList<>(); this.bizTables = new java.util.ArrayList<>(); + this.workflowHits = new java.util.ArrayList<>(); + this.diHits = new java.util.ArrayList<>(); + this.portalHits = new java.util.ArrayList<>(); + this.printHits = new java.util.ArrayList<>(); + this.reportHits = new java.util.ArrayList<>(); + this.scheduleHits = new java.util.ArrayList<>(); + this.dictHits = new java.util.ArrayList<>(); } } @@ -96,6 +166,7 @@ public interface DbClearService { /** 表清理计划模板 (静态常量) */ java.util.List MAIN_TABLE_PLANS = java.util.Arrays.asList( + // -------- form / module (FK 子表 → 主表) -------- new TablePlan("base_visual_alias", "f_visual_id IN (SELECT f_id FROM base_visual_dev WHERE f_system_id = ?)"), new TablePlan("base_visual_filter", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), new TablePlan("base_module_authorize", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), @@ -109,17 +180,74 @@ public interface DbClearService { new TablePlan("base_visual_dev", "f_system_id = ?"), new TablePlan("base_visual_release", "f_system_id = ?"), new TablePlan("base_module", "f_system_id = ?"), - // 下面用 app_code 而不是 system_id - new TablePlan("base_db_link", "f_full_name = :app_code"), + // -------- 字典 (修复 P1-2e: 2026-07-23 之前 LIKE CONCAT(:app_code, '.%') 要求带点, 实际 jxcjxxx 不带点) -------- + // 改为 LIKE CONCAT(:app_code, '%') 同时匹配 jxcj.xxx 和 jxcjxxx + new TablePlan("base_dictionary_data", "f_dictionary_type_id IN (SELECT f_id FROM base_dictionary_type WHERE f_en_code LIKE CONCAT(:app_code, '%'))"), + new TablePlan("base_dictionary_type", "f_en_code LIKE CONCAT(:app_code, '%')"), + // -------- workflow (P1-2e: 2026-07-23 之前完全缺失) -------- + // schema 实测: workflow_node/form_relation/delegate/common 用 f_flow_id (=template.f_id) + // workflow_task 用 f_flow_code (=template.f_en_code) + // workflow_record/comment/candidates/... 用 f_task_id (=task.f_id) + // workflow_template_use_num/version 用 f_template_id + // workflow_trigger_* 用 f_trigger_id + // workflow_launch_user 没有 f_flow_id 字段, 跳过 + // 删顺序: FK 子表 → 主表 (Phase 1 BAK → Phase 2 DELETE) + new TablePlan("workflow_form_relation", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_node", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_common", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_delegate_info", "f_delegate_id IN (SELECT f_id FROM workflow_delegate WHERE f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_delegate", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_template_use_num", "f_template_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_version", "f_template_id IN (SELECT f_id FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_trigger_launchflow", "f_trigger_id IN (SELECT f_id FROM workflow_trigger_task WHERE f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?)))"), + new TablePlan("workflow_trigger_record", "f_trigger_id IN (SELECT f_id FROM workflow_trigger_task WHERE f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?)))"), + new TablePlan("workflow_trigger_task", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_event_log", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_node_record", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_task_line", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_comment", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_candidates", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_circulate", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_operator", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + // P1-2e: workflow_reject_data 没有 f_task_id (用 f_task_json), 跳过 — 本表跨流程共享, 不靠 FK 溯源 + // P1-2e: workflow_subtask_data 没有 f_task_id (用 f_parent_id), 跳过 — 跨流程共享 + new TablePlan("workflow_revoke", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_record", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?))"), + new TablePlan("workflow_task", "f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_system_id = ?)"), + new TablePlan("workflow_template", "f_system_id = ?"), + // -------- 数据接口 DI (P1-2e: 2026-07-23 之前完全缺失, f_en_code LIKE appCode% + f_category 字典过滤) -------- + // P1-2e: base_data_interface_log 字段是 f_invok_id (非 f_interface_id), 且 DI log 全平台共享, 跳过 + // P1-2e: base_data_interface_oauth 字段是 f_data_interface_ids (varchar list, 无真 FK), 跨 DI 共享, 跳过 + // P1-2e: base_data_interface_user 字段是 f_oauth_id (→ oauth), 跨 DI 共享, 跳过 + new TablePlan("base_data_interface_variate", "f_interface_id IN (SELECT f_id FROM base_data_interface WHERE f_en_code LIKE CONCAT(:app_code, '%'))"), + new TablePlan("base_data_interface", "f_en_code LIKE CONCAT(:app_code, '%')"), + // -------- 门户 Portal (P1-2e: 2026-07-23 之前完全缺失) -------- + new TablePlan("base_portal_data", "f_portal_id IN (SELECT f_id FROM base_portal WHERE f_system_id = ?)"), + new TablePlan("base_portal_manage","f_portal_id IN (SELECT f_id FROM base_portal WHERE f_system_id = ?)"), + new TablePlan("base_portal", "f_system_id = ?"), + // -------- 打印模板 (P1-2e: 2026-07-23 之前完全缺失) -------- + // P1-2e: base_print_version 字段是 f_template_id (不是 f_print_id) + new TablePlan("base_print_log", "f_print_id IN (SELECT f_id FROM base_print_template WHERE f_system_id = ?)"), + new TablePlan("base_print_version", "f_template_id IN (SELECT f_id FROM base_print_template WHERE f_system_id = ?)"), + new TablePlan("base_print_template","f_system_id = ?"), + // -------- 报表 (P1-2e: 2026-07-23 之前完全缺失) -------- + new TablePlan("report_template", "f_system_id = ?"), + // -------- 定时任务 (P1-2e: 2026-07-23 之前完全缺失, f_category 存 systemId) -------- + // P1-2e: 子表字段是 f_schedule_id (不是 f_task_id) + new TablePlan("base_schedule_log", "f_schedule_id IN (SELECT f_id FROM base_schedule WHERE f_category = ?)"), + new TablePlan("base_schedule_user", "f_schedule_id IN (SELECT f_id FROM base_schedule WHERE f_category = ?)"), + new TablePlan("base_schedule", "f_category = ?"), + // -------- 数据库连接 + 系统记录 (P1-2e: 之前已存在, 保留) -------- + new TablePlan("base_db_link", "f_full_name = :app_code"), new TablePlan("base_dictionary_data", "f_dictionary_type_id='9c43287481364d348c0ea0d0f64b38be' AND f_full_name = :app_code"), new TablePlan("base_dictionary_data", "f_dictionary_type_id='306427078100678661' AND f_full_name = CONCAT(:app_code,'_b')"), - new TablePlan("base_dictionary_data", "f_dictionary_type_id IN (SELECT f_id FROM base_dictionary_type WHERE f_en_code LIKE CONCAT(:app_code, '.%'))"), - new TablePlan("base_dictionary_type", "f_en_code LIKE CONCAT(:app_code, '.%')"), - new TablePlan("base_system", "f_en_code = :app_code") + new TablePlan("base_system", "f_en_code = :app_code") ); /** 这些表 where 条件只用 appCode (其他表都用 systemId) */ java.util.Set TABLES_PARAMS_BY_APP_CODE = new java.util.HashSet<>(java.util.Arrays.asList( - "base_db_link", "base_dictionary_data", "base_dictionary_type", "base_system" + "base_db_link", "base_dictionary_data", "base_dictionary_type", "base_system", + "base_data_interface", "base_data_interface_log", "base_data_interface_oauth", + "base_data_interface_user", "base_data_interface_variate" )); } \ No newline at end of file diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java index 78688af..143c747 100644 --- a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java @@ -71,6 +71,14 @@ public class DbClearServiceImpl implements DbClearService { p.systemId = querySystemId(mainConn, appCode); if (p.systemId != null) { p.mainHits = countMainHits(mainConn, p.systemId, appCode); + // P1-2e: 按范围拆分到 6 个子集合 + p.workflowHits = filterByPrefix(p.mainHits, "workflow_"); + p.diHits = filterByPrefix(p.mainHits, "base_data_interface"); + p.portalHits = filterByPrefix(p.mainHits, "base_portal"); + p.printHits = filterByPrefix(p.mainHits, "base_print"); + p.reportHits = filterByPrefix(p.mainHits, "report_template"); + p.scheduleHits = filterByPrefix(p.mainHits, "base_schedule"); + p.dictHits = filterByPrefix(p.mainHits, "base_dictionary"); } else { log.warn("(提示: base_system 中没有 f_en_code={} 的记录, 主库平台表清理将被跳过)", appCode); } @@ -95,39 +103,49 @@ public class DbClearServiceImpl implements DbClearService { return p; } + // P1-2e: helper, 按 table 名前缀过滤 + private List filterByPrefix(List hits, String prefix) { + List out = new java.util.ArrayList<>(); + for (TableHit h : hits) if (h.table.startsWith(prefix)) out.add(h); + return out; + } + // ===================================================================== // 执行 // ===================================================================== @Override public DbClearResult execute(String appCode, String dbTs) { + return execute(appCode, dbTs, DbClearScope.all()); + } + + // P1-2e (2026-07-23): execute overload 支持范围控制 (dacli --include-workflow/--include-di 等) + @Override + public DbClearResult execute(String appCode, String dbTs, DbClearScope scope) { DbClearResult r = new DbClearResult(); r.dbConn = buildDbConnInfo(); Connection mainConn = null; try { - // MySQL: history_bak 是独立 DATABASE (走 ensureHistoryBakDb 创库) - // PG: history_bak 是 schema, 必须在 mainConn + biz conn 中各自 CREATE SCHEMA ensureHistoryBakDb(); - mainConn = openConn(mainDb, null); ensureHistoryBakSchema(mainConn); String systemId = querySystemId(mainConn, appCode); if (systemId != null) { - clearMainTables(mainConn, appCode, systemId, dbTs, r); + // P1-2e: 按 scope 过滤 MAIN_TABLE_PLANS + List plans = filterPlansByScope(MAIN_TABLE_PLANS, scope); + log.info("执行清理: appCode={}, systemId={}, 启用 plans={}/{} (scope={})", + appCode, systemId, plans.size(), MAIN_TABLE_PLANS.size(), scope); + clearMainTables(mainConn, appCode, systemId, dbTs, r, plans); } if (appCode != null && !appCode.isEmpty()) { List bizTables = listBizTables(openConn(appCode, null)); - if (!bizTables.isEmpty()) { + if (!bizTables.isEmpty() && scope.includeBiz) { renameBizTables(appCode, bizTables, dbTs, r); } } - // 计算总行数 - for (String bak : r.mainBakNames) { - // 不再回查行数, 直接 0; preview 阶段已统计 - } log.info("DB 清理完成: appCode={}, mainBaks={}, bizBaks={}", appCode, r.mainBakNames.size(), r.bizBakEntries.size()); } catch (Exception e) { @@ -139,17 +157,44 @@ public class DbClearServiceImpl implements DbClearService { return r; } + // P1-2e: 按 scope 过滤 plan + private List filterPlansByScope(List plans, DbClearScope scope) { + List out = new java.util.ArrayList<>(); + for (TablePlan p : plans) { + if (matchesScope(p.table, scope)) out.add(p); + } + return out; + } + + private boolean matchesScope(String table, DbClearScope scope) { + if (table.startsWith("workflow_")) return scope.includeWorkflow; + if (table.startsWith("base_data_interface")) return scope.includeDi; + if (table.startsWith("base_portal")) return scope.includePortal; + if (table.startsWith("base_print")) return scope.includePrint; + if (table.startsWith("report_template")) return scope.includeReport; + if (table.startsWith("base_schedule")) return scope.includeSchedule; + if (table.startsWith("base_dictionary")) return scope.includeDict; + // 其它 (form/menu/db_link/system) 始终清 + return true; + } + // ===================================================================== // Phase 1/2/3: 主库清理 // ===================================================================== private void clearMainTables(Connection conn, String appCode, String systemId, String dbTs, DbClearResult result) throws Exception { + clearMainTables(conn, appCode, systemId, dbTs, result, MAIN_TABLE_PLANS); + } + + // P1-2e (2026-07-23): overload 接受外部传入的 plans, 配合 DbClearScope 实现按范围清理 + private void clearMainTables(Connection conn, String appCode, String systemId, String dbTs, + DbClearResult result, List plans) throws Exception { // Phase 1: CREATE + INSERT SELECT (autoCommit, 无 DELETE) List mainBakNames = new ArrayList<>(); List emptyDrops = new ArrayList<>(); - for (TablePlan plan : MAIN_TABLE_PLANS) { + for (TablePlan plan : plans) { String bakName = "D" + dbTs + "-" + plan.table; if (!mainBakNames.contains(bakName)) { createBakTable(conn, bakName, plan.table); @@ -169,14 +214,15 @@ public class DbClearServiceImpl implements DbClearService { // Phase 2: 单事务内全部 DELETE conn.setAutoCommit(false); try { - for (TablePlan plan : MAIN_TABLE_PLANS) { + for (TablePlan plan : plans) { String filled = fillWhereTpl(plan.table, plan.whereTpl, systemId, appCode); String sql = "DELETE FROM " + qid(plan.table) + " WHERE " + filled; + // P1-2e: 调试时改 ERROR 级别进 log_error, 生产可改回 INFO; 留 log.info 看 SQL 便于排查 int n = executeUpdate(conn, sql, plan.table, systemId, appCode); if (n > 0) log.info("[DEL] {}: {} 行", plan.table, n); } conn.commit(); - log.info("Phase 2 提交: {} 张主表 DELETE 完成", MAIN_TABLE_PLANS.size()); + log.info("Phase 2 提交: {} 张主表 DELETE 完成", plans.size()); } catch (Exception e) { conn.rollback(); log.error("Phase 2 失败, 已 ROLLBACK", e); diff --git a/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java b/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java index 5ebe201..57f50ae 100644 --- a/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java +++ b/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java @@ -65,30 +65,81 @@ public class DbClearController { @PostMapping("/clear") @Operation(summary = "DB 清理 (preview/execute)", description = "预览或执行 JNPF 应用的 DB 清理, 备份到 history_bak") public ActionResult clear(@RequestBody ClearRequest req) { - log.info("DB clear 请求: appCode={}, mode={}", req.appCode, req.mode); + log.info("DB clear 请求: appCode={}, mode={}, scope={}", req.appCode, req.mode, req.scope); if (req.appCode == null || req.appCode.isEmpty()) { return ActionResult.fail("appCode 不能为空"); } + DbClearService.DbClearScope scope = parseScope(req.scope); + if ("preview".equalsIgnoreCase(req.mode)) { DbClearService.DbClearPreview preview = dbClearService.preview(req.appCode); + // 同步把 scope 透回给 dacli, 方便 echo 实际用的范围 + preview.error = (preview.error == null ? "" : preview.error) + + String.format(" | scope: includeWorkflow=%s includeDi=%s includePortal=%s includePrint=%s includeReport=%s includeSchedule=%s includeDict=%s includeBiz=%s", + scope.includeWorkflow, scope.includeDi, scope.includePortal, + scope.includePrint, scope.includeReport, scope.includeSchedule, + scope.includeDict, scope.includeBiz); return ActionResult.success(preview); } else if ("execute".equalsIgnoreCase(req.mode)) { if (req.dbTs == null || req.dbTs.isEmpty()) { return ActionResult.fail("execute 模式必填 dbTs"); } - DbClearService.DbClearResult result = dbClearService.execute(req.appCode, req.dbTs); + DbClearService.DbClearResult result = dbClearService.execute(req.appCode, req.dbTs, scope); return ActionResult.success(result); } else { return ActionResult.fail("mode 必须为 preview 或 execute"); } } + /** + * P1-2e (2026-07-23): scope 解析 + * 输入格式: "workflow,di,portal,print,report,schedule,dict,biz" 逗号分隔 + * 传 "all" 或 null → 全开 + * 传 "conservative" → 仅 form/menu/dict + * 不传 → 全开 (与历史行为一致) + */ + private DbClearService.DbClearScope parseScope(String scopeStr) { + DbClearService.DbClearScope scope = new DbClearService.DbClearScope(); + if (scopeStr == null || scopeStr.isEmpty() || "all".equalsIgnoreCase(scopeStr)) { + return scope; // 默认全开 + } + if ("conservative".equalsIgnoreCase(scopeStr)) { + return DbClearService.DbClearScope.conservative(); + } + // 显式列出的才开 + scope.includeWorkflow = false; + scope.includeDi = false; + scope.includePortal = false; + scope.includePrint = false; + scope.includeReport = false; + scope.includeSchedule = false; + scope.includeDict = false; + scope.includeBiz = false; + for (String s : scopeStr.split(",")) { + String k = s.trim().toLowerCase(); + switch (k) { + case "workflow": scope.includeWorkflow = true; break; + case "di": scope.includeDi = true; break; + case "portal": scope.includePortal = true; break; + case "print": scope.includePrint = true; break; + case "report": scope.includeReport = true; break; + case "schedule": scope.includeSchedule = true; break; + case "dict": scope.includeDict = true; break; + case "biz": scope.includeBiz = true; break; + case "form": /* 始终清, 不用 flag */ break; + default: log.warn("(未知 scope key: {}, 已忽略)", k); + } + } + return scope; + } + /** Request body */ public static class ClearRequest { public String appCode; public String mode; // "preview" | "execute" public String dbTs; // execute 模式必填, 格式 yyyyMMddHHmmss + public String scope; // P1-2e: 范围控制, 不传=全清, "all"=全清, "conservative"=保守 } } \ No newline at end of file