u
This commit is contained in:
1 parent
aa813c9649
commit
62550b8182
45 files changed
+12535
-566
No files matched your search
+171
-171
@@ -10,203 +10,203 @@ import java.util.*;
|
||||
|
||||
|
||||
public class BatchTableDelete {
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// === 入口 ===
|
||||
public static Result<Map<String, Integer>> executdeletetable(String orgCode, String moduleid, String ID) throws SQLException {
|
||||
boolean candelete = deletebefore(orgCode, moduleid, ID);
|
||||
if (candelete) {
|
||||
List<Map<String, Object>> deletetablelist = getDeletetableList(orgCode, moduleid, ID);
|
||||
return deleteDatabaseOperations(orgCode, deletetablelist);
|
||||
}
|
||||
return Result.fail("存在依赖数据,禁止删除: table=" + moduleid + ", id=" + ID);
|
||||
}
|
||||
|
||||
// === 删除前检查依赖 ===
|
||||
public static boolean deletebefore(String orgCode, String moduleid, String ID) throws SQLException {
|
||||
String sql = "SELECT b_JudgeCondition FROM s_module WHERE b_id = ?";
|
||||
|
||||
try (Connection conn = DruidDataSourceManager.getConnection(orgCode);
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, moduleid);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
String conditionSql = rs.getString("b_JudgeCondition");
|
||||
|
||||
if (conditionSql == null || conditionSql.trim().isEmpty()) {
|
||||
return true; // 没配置依赖条件,默认允许删除
|
||||
}
|
||||
|
||||
// 替换占位符
|
||||
conditionSql = conditionSql.replace(":b_id", "'" + ID + "'");
|
||||
System.out.println("[执行条件SQL] " + conditionSql);
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(conditionSql);
|
||||
ResultSet crs = ps.executeQuery()) {
|
||||
|
||||
if (crs.next()) {
|
||||
System.out.println("存在依赖数据,不允许删除: ID=" + ID);
|
||||
return false;
|
||||
} else {
|
||||
System.out.println("未发现依赖数据,可以删除: ID=" + ID);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// === 入口 ===
|
||||
public static Result<Map<String, Integer>> executdeletetable(String orgCode, String moduleid, String ID) throws SQLException {
|
||||
boolean candelete = deletebefore(orgCode, moduleid, ID);
|
||||
if (candelete) {
|
||||
List<Map<String, Object>> deletetablelist = getDeletetableList(orgCode, moduleid, ID);
|
||||
return deleteDatabaseOperations(orgCode, deletetablelist);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("执行 deletebefore 失败", e);
|
||||
return Result.fail("存在依赖数据,禁止删除: table=" + moduleid + ", id=" + ID);
|
||||
}
|
||||
|
||||
return false; // 默认不允许删除
|
||||
}
|
||||
// === 删除前检查依赖 ===
|
||||
public static boolean deletebefore(String orgCode, String moduleid, String ID) throws SQLException {
|
||||
String sql = "SELECT b_JudgeCondition FROM s_module WHERE b_id = ?";
|
||||
|
||||
// === 获取需要删除的表集合 ===
|
||||
public static List<Map<String, Object>> getDeletetableList(String orgCode, String moduleid, String ID) throws SQLException {
|
||||
String sqlData = "SELECT b_SaveTable FROM s_module WHERE b_id = ?";
|
||||
String sql = "SELECT b_SaveTable FROM s_module WHERE b_LinkTable = ?";
|
||||
|
||||
try (Connection conn = DruidDataSourceManager.getConnection(orgCode)) {
|
||||
String saveTable = "";
|
||||
try (PreparedStatement stmtData = conn.prepareStatement(sqlData)) {
|
||||
stmtData.setString(1, moduleid);
|
||||
try (ResultSet rs = stmtData.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
saveTable = rs.getString("b_SaveTable");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
try (Connection conn = DruidDataSourceManager.getConnection(orgCode);
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, moduleid);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
List<Map<String, Object>> tablelist = getlinktablelist(rs, ID);
|
||||
if (rs.next()) {
|
||||
String conditionSql = rs.getString("b_JudgeCondition");
|
||||
|
||||
// 添加主表
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("tableName", saveTable);
|
||||
map.put("keyField", "b_id");
|
||||
map.put("Fieldvalue", ID);
|
||||
tablelist.add(map);
|
||||
if (conditionSql == null || conditionSql.trim().isEmpty()) {
|
||||
return true; // 没配置依赖条件,默认允许删除
|
||||
}
|
||||
|
||||
return tablelist;
|
||||
// 替换占位符
|
||||
conditionSql = conditionSql.replace(":b_id", "'" + ID + "'");
|
||||
System.out.println("[执行条件SQL] " + conditionSql);
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(conditionSql);
|
||||
ResultSet crs = ps.executeQuery()) {
|
||||
|
||||
if (crs.next()) {
|
||||
System.out.println("存在依赖数据,不允许删除: ID=" + ID);
|
||||
return false;
|
||||
} else {
|
||||
System.out.println("未发现依赖数据,可以删除: ID=" + ID);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("执行 deletebefore 失败", e);
|
||||
}
|
||||
|
||||
return false; // 默认不允许删除
|
||||
}
|
||||
|
||||
// === 获取需要删除的表集合 ===
|
||||
public static List<Map<String, Object>> getDeletetableList(String orgCode, String moduleid, String ID) throws SQLException {
|
||||
String sqlData = "SELECT b_SaveTable FROM s_module WHERE b_id = ?";
|
||||
String sql = "SELECT b_SaveTable FROM s_module WHERE b_LinkTable = ?";
|
||||
|
||||
try (Connection conn = DruidDataSourceManager.getConnection(orgCode)) {
|
||||
String saveTable = "";
|
||||
try (PreparedStatement stmtData = conn.prepareStatement(sqlData)) {
|
||||
stmtData.setString(1, moduleid);
|
||||
try (ResultSet rs = stmtData.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
saveTable = rs.getString("b_SaveTable");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, moduleid);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
List<Map<String, Object>> tablelist = getlinktablelist(rs, ID);
|
||||
|
||||
// 添加主表
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("tableName", saveTable);
|
||||
map.put("keyField", "b_id");
|
||||
map.put("Fieldvalue", ID);
|
||||
tablelist.add(map);
|
||||
|
||||
return tablelist;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("查询 s_module 失败", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("查询 s_module 失败", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("查询 s_module 失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> getlinktablelist(ResultSet rs, String ID) throws SQLException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("tableName", rs.getString("b_SaveTable"));
|
||||
map.put("keyField", "father");
|
||||
map.put("Fieldvalue", ID);
|
||||
list.add(map);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// === 执行批量删除 ===
|
||||
public static Result<Map<String, Integer>> deleteDatabaseOperations(String orgCode, List<Map<String, Object>> tableMaps) {
|
||||
Map<String, Integer> operationCounts = new LinkedHashMap<>();
|
||||
operationCounts.put("total", 0);
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DruidDataSourceManager.getConnection(orgCode);
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
for (Map<String, Object> tablemap : tableMaps) {
|
||||
String tableName = (String) tablemap.get("tableName");
|
||||
String keyField = (String) tablemap.get("keyField");
|
||||
String fieldValue = (String) tablemap.get("Fieldvalue");
|
||||
|
||||
validateIdentifier(tableName);
|
||||
validateIdentifier(keyField);
|
||||
|
||||
int count = processDeleteOperations(conn, tableName, keyField, fieldValue);
|
||||
|
||||
// 每个表单独统计
|
||||
operationCounts.merge(tableName, count, Integer::sum);
|
||||
// 总数统计
|
||||
operationCounts.merge("total", count, Integer::sum);
|
||||
|
||||
System.out.println("表 [" + tableName + "] 删除记录数: " + count);
|
||||
private static List<Map<String, Object>> getlinktablelist(ResultSet rs, String ID) throws SQLException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("tableName", rs.getString("b_SaveTable"));
|
||||
map.put("keyField", "father");
|
||||
map.put("Fieldvalue", ID);
|
||||
list.add(map);
|
||||
}
|
||||
|
||||
conn.commit();
|
||||
System.out.println("批量操作成功!操作统计: " + operationCounts);
|
||||
return Result.ok("批量操作成功", operationCounts);
|
||||
} catch (Exception e) {
|
||||
rollbackTransaction(conn);
|
||||
System.err.println("批量操作失败: " + e.getMessage());
|
||||
return Result.fail("批量操作失败: " + e.getMessage());
|
||||
} finally {
|
||||
closeConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
// === 删除逻辑 ===
|
||||
private static int processDeleteOperations(Connection conn, String tableName, String keyField, String fieldValue) throws SQLException {
|
||||
return deleteRecord(conn, tableName, keyField, fieldValue);
|
||||
}
|
||||
|
||||
private static int deleteRecord(Connection conn, String tableName, String pkField, String pkFieldvalue) throws SQLException {
|
||||
if (pkFieldvalue == null) {
|
||||
throw new IllegalArgumentException("缺少主键字段: " + pkField + " 值为空");
|
||||
return list;
|
||||
}
|
||||
|
||||
String sql = getDeleteSql(tableName, pkField);
|
||||
System.out.println("[SQL-DELETE] " + sql);
|
||||
System.out.println("[PARAMS] " + pkFieldvalue);
|
||||
// === 执行批量删除 ===
|
||||
public static Result<Map<String, Integer>> deleteDatabaseOperations(String orgCode, List<Map<String, Object>> tableMaps) {
|
||||
Map<String, Integer> operationCounts = new LinkedHashMap<>();
|
||||
operationCounts.put("total", 0);
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, pkFieldvalue);
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected == 0) {
|
||||
System.out.println("未找到要删除的记录: " + pkFieldvalue);
|
||||
}
|
||||
return affected;
|
||||
}
|
||||
}
|
||||
|
||||
// === 辅助方法 ===
|
||||
private static void validateIdentifier(String identifier) {
|
||||
if (!identifier.matches("^[a-zA-Z0-9_]+$")) {
|
||||
throw new IllegalArgumentException("非法的表名或字段名: " + identifier);
|
||||
}
|
||||
}
|
||||
|
||||
private static void rollbackTransaction(Connection conn) {
|
||||
if (conn != null) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("回滚事务时出错: " + ex.getMessage());
|
||||
conn = DruidDataSourceManager.getConnection(orgCode);
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
for (Map<String, Object> tablemap : tableMaps) {
|
||||
String tableName = (String) tablemap.get("tableName");
|
||||
String keyField = (String) tablemap.get("keyField");
|
||||
String fieldValue = (String) tablemap.get("Fieldvalue");
|
||||
|
||||
validateIdentifier(tableName);
|
||||
validateIdentifier(keyField);
|
||||
|
||||
int count = processDeleteOperations(conn, tableName, keyField, fieldValue);
|
||||
|
||||
// 每个表单独统计
|
||||
operationCounts.merge(tableName, count, Integer::sum);
|
||||
// 总数统计
|
||||
operationCounts.merge("total", count, Integer::sum);
|
||||
|
||||
System.out.println("表 [" + tableName + "] 删除记录数: " + count);
|
||||
}
|
||||
|
||||
conn.commit();
|
||||
System.out.println("批量操作成功!操作统计: " + operationCounts);
|
||||
return Result.ok("批量操作成功", operationCounts);
|
||||
} catch (Exception e) {
|
||||
rollbackTransaction(conn);
|
||||
System.err.println("批量操作失败: " + e.getMessage());
|
||||
return Result.fail("批量操作失败: " + e.getMessage());
|
||||
} finally {
|
||||
closeConnection(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void closeConnection(Connection conn) {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("关闭数据库连接时出错: " + e.getMessage());
|
||||
// === 删除逻辑 ===
|
||||
private static int processDeleteOperations(Connection conn, String tableName, String keyField, String fieldValue) throws SQLException {
|
||||
return deleteRecord(conn, tableName, keyField, fieldValue);
|
||||
}
|
||||
|
||||
private static int deleteRecord(Connection conn, String tableName, String pkField, String pkFieldvalue) throws SQLException {
|
||||
if (pkFieldvalue == null) {
|
||||
throw new IllegalArgumentException("缺少主键字段: " + pkField + " 值为空");
|
||||
}
|
||||
|
||||
String sql = getDeleteSql(tableName, pkField);
|
||||
System.out.println("[SQL-DELETE] " + sql);
|
||||
System.out.println("[PARAMS] " + pkFieldvalue);
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, pkFieldvalue);
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected == 0) {
|
||||
System.out.println("未找到要删除的记录: " + pkFieldvalue);
|
||||
}
|
||||
return affected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDeleteSql(String tableName, String pkName) {
|
||||
return "DELETE FROM " + tableName + " WHERE " + pkName + " = ?";
|
||||
}
|
||||
// === 辅助方法 ===
|
||||
private static void validateIdentifier(String identifier) {
|
||||
if (!identifier.matches("^[a-zA-Z0-9_]+$")) {
|
||||
throw new IllegalArgumentException("非法的表名或字段名: " + identifier);
|
||||
}
|
||||
}
|
||||
|
||||
private static void rollbackTransaction(Connection conn) {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("回滚事务时出错: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void closeConnection(Connection conn) {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("关闭数据库连接时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDeleteSql(String tableName, String pkName) {
|
||||
return "DELETE FROM " + tableName + " WHERE " + pkName + " = ?";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
url=jdbc:sqlserver://192.168.1.189:1433;databaseName=G3HY2025
|
||||
url=jdbc:sqlserver://118.89.70.199:1433;databaseName=G3BoxOCR
|
||||
username=sa
|
||||
password=g3soft
|
||||
password=zhang520..
|
||||
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
Reference in new issue
Block a user