新增待办信息
This commit is contained in:
@@ -3,6 +3,7 @@ package com.jeesite.modules.app.utils;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
@@ -133,12 +134,13 @@ public class MyFileUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeToFile(String fileName, String content, java.nio.charset.Charset charset, boolean append) throws Exception {
|
public static void writeToFile(String fileName, String content, Charset charset, boolean append) throws Exception {
|
||||||
if (fileName == null || fileName.trim().isEmpty()) {
|
if (fileName == null || fileName.trim().isEmpty()) {
|
||||||
throw new IllegalArgumentException("文件名不能为空");
|
throw new IllegalArgumentException("文件名不能为空");
|
||||||
}
|
}
|
||||||
if (content == null) {
|
String writeContent = content == null ? "" : content;
|
||||||
content = "";
|
if (append && !writeContent.isEmpty()) {
|
||||||
|
writeContent = System.lineSeparator() + writeContent;
|
||||||
}
|
}
|
||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
File parentDir = file.getParentFile();
|
File parentDir = file.getParentFile();
|
||||||
@@ -149,7 +151,7 @@ public class MyFileUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, append), charset)) {
|
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, append), charset)) {
|
||||||
writer.write(content + "\n");
|
writer.write(writeContent);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new Exception("写入文件失败: " + file.getAbsolutePath(), e);
|
throw new Exception("写入文件失败: " + file.getAbsolutePath(), e);
|
||||||
|
|||||||
@@ -266,32 +266,30 @@ public class MysqlUtils {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getExecScript(BizDbConfig dbConfig, String scriptName) {
|
public static String getExecScript(BizDbConfig dbConfig, String scriptName) {
|
||||||
String EXEC_RESULT = "执行成功";
|
String execResult = "执行成功";
|
||||||
String LOG_NAME = EXEC_LOG_PATH + vId.getCid() + "_log_" + scriptName;
|
String logName = EXEC_LOG_PATH + vId.getCid() + "_log_" + scriptName;
|
||||||
MyFileUtils.writeFile(LOG_NAME, "-- 执行脚本:" + scriptName + ",开始时间:" + vDate.getNow(), true);
|
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
|
MyFileUtils.writeFile(logName, "-- 执行脚本:" + scriptName + ",开始时间:" + vDate.getNow(), true);
|
||||||
try {
|
try {
|
||||||
File file = new File(EXEC_RUN_PATH + scriptName);
|
File file = new File(EXEC_RUN_PATH + scriptName);
|
||||||
|
// 1. 建立数据库连接并关闭自动提交
|
||||||
conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(),
|
conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(),
|
||||||
dbConfig.getDbUsername(), dbConfig.getDbPassword());
|
dbConfig.getDbUsername(), dbConfig.getDbPassword());
|
||||||
conn.setAutoCommit(false);
|
conn.setAutoCommit(false);
|
||||||
try (Statement statement = conn.createStatement();
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
||||||
StringBuilder sqlContent = new StringBuilder();
|
StringBuilder sqlContent = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
line = line.trim();
|
String trimmedLine = line.trim();
|
||||||
if (line.isEmpty() || line.startsWith("--")) {
|
if (trimmedLine.isEmpty()) {
|
||||||
continue;
|
continue; // 过滤空行,避免无效拆分
|
||||||
|
}
|
||||||
|
sqlContent.append(line).append("\n"); // 保留原始行内容(含注释)
|
||||||
}
|
}
|
||||||
sqlContent.append(line);
|
|
||||||
}
|
}
|
||||||
Map<String, String> varMap = new HashMap<>();
|
|
||||||
varMap.put("\\$\\{[bB][iI][zZ]_[dD][aA][tT][eE]\\}", vDate.dsValueDaysAgo(1));
|
|
||||||
String replacedSqlContent = sqlContent.toString();
|
String replacedSqlContent = sqlContent.toString();
|
||||||
for (Map.Entry<String, String> entry : varMap.entrySet()) {
|
replacedSqlContent = replacedSqlContent.replaceAll("\\$\\{[bB][iI][zZ]_[dD][aA][tT][eE]\\}", vDate.dsValueDaysAgo(1));
|
||||||
replacedSqlContent = replacedSqlContent.replaceAll(entry.getKey(), entry.getValue());
|
try (Statement statement = conn.createStatement()) {
|
||||||
}
|
|
||||||
String[] sqlStatements = replacedSqlContent.split(";");
|
String[] sqlStatements = replacedSqlContent.split(";");
|
||||||
int totalAffectedRows = 0;
|
int totalAffectedRows = 0;
|
||||||
for (String sql : sqlStatements) {
|
for (String sql : sqlStatements) {
|
||||||
@@ -299,7 +297,12 @@ public class MysqlUtils {
|
|||||||
if (sql.isEmpty()) {
|
if (sql.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
MyFileUtils.writeFile(LOG_NAME, sql, true);
|
String sqlWithSemicolon = sql + ";";
|
||||||
|
try {
|
||||||
|
MyFileUtils.writeFile(logName, sqlWithSemicolon, true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("写入SQL日志失败", e);
|
||||||
|
}
|
||||||
boolean hasResultSet = statement.execute(sql);
|
boolean hasResultSet = statement.execute(sql);
|
||||||
if (hasResultSet) {
|
if (hasResultSet) {
|
||||||
try (ResultSet rs = statement.getResultSet()) {
|
try (ResultSet rs = statement.getResultSet()) {
|
||||||
@@ -309,7 +312,7 @@ public class MysqlUtils {
|
|||||||
for (int i = 1; i <= columnCount; i++) {
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
header.append(metaData.getColumnName(i)).append("\t");
|
header.append(metaData.getColumnName(i)).append("\t");
|
||||||
}
|
}
|
||||||
MyFileUtils.writeFile(LOG_NAME, header.toString().trim(), true);
|
MyFileUtils.writeFile(logName, header.toString().trim(), true);
|
||||||
int rowNum = 0;
|
int rowNum = 0;
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
rowNum++;
|
rowNum++;
|
||||||
@@ -318,7 +321,7 @@ public class MysqlUtils {
|
|||||||
Object value = rs.getObject(i);
|
Object value = rs.getObject(i);
|
||||||
rowData.append(value == null ? "NULL" : value.toString()).append("\t");
|
rowData.append(value == null ? "NULL" : value.toString()).append("\t");
|
||||||
}
|
}
|
||||||
MyFileUtils.writeFile(LOG_NAME, rowData.toString().trim(), true);
|
MyFileUtils.writeFile(logName, rowData.toString().trim(), true);
|
||||||
}
|
}
|
||||||
logger.info(sql, "执行成功,总行数:", rowNum);
|
logger.info(sql, "执行成功,总行数:", rowNum);
|
||||||
}
|
}
|
||||||
@@ -328,27 +331,43 @@ public class MysqlUtils {
|
|||||||
logger.info(sql, "执行成功, 影响行数:", affectedRows);
|
logger.info(sql, "执行成功, 影响行数:", affectedRows);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 提交事务
|
||||||
conn.commit();
|
conn.commit();
|
||||||
MyFileUtils.writeFile(LOG_NAME, "-- 执行脚本:" + scriptName + ",结束时间:" + vDate.getNow(), true);
|
logger.info(file.getName(), "脚本影响行数:", totalAffectedRows, " 执行结果:", execResult);
|
||||||
logger.info(file.getName(), "脚本影响行数:", totalAffectedRows, " 执行结果:", EXEC_RESULT);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// 回滚事务
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
conn.rollback();
|
conn.rollback();
|
||||||
EXEC_RESULT = e.getMessage();
|
} catch (SQLException rollbackEx) {
|
||||||
|
logger.error("事务回滚失败", rollbackEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
execResult = e.getMessage();
|
||||||
logger.error(file.getName(), "执行脚本文件失败:", e);
|
logger.error(file.getName(), "执行脚本文件失败:", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
EXEC_RESULT = e.getMessage();
|
execResult = e.getMessage();
|
||||||
logger.error("执行脚本入口异常", e);
|
logger.error("执行脚本入口异常", e);
|
||||||
} finally {
|
} finally {
|
||||||
// 最终关闭连接
|
// 关闭数据库连接
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
try {
|
try {
|
||||||
conn.close();
|
conn.close();
|
||||||
|
} catch (SQLException closeEx) {
|
||||||
|
logger.error("关闭数据库连接失败", closeEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 写入脚本结束日志
|
||||||
|
try {
|
||||||
|
MyFileUtils.writeFile(logName, "-- 执行脚本:" + scriptName + ",结束时间:" + vDate.getNow(), true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("关闭数据库连接失败", e);
|
logger.error("写入执行结束日志失败", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return EXEC_RESULT;
|
return execResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user