新增待办信息

This commit is contained in:
2026-01-03 01:24:45 +08:00
parent 360dde1055
commit 6b8289fdb7
2 changed files with 53 additions and 32 deletions

View File

@@ -3,6 +3,7 @@ package com.jeesite.modules.app.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
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()) {
throw new IllegalArgumentException("文件名不能为空");
}
if (content == null) {
content = "";
String writeContent = content == null ? "" : content;
if (append && !writeContent.isEmpty()) {
writeContent = System.lineSeparator() + writeContent;
}
File file = new File(fileName);
File parentDir = file.getParentFile();
@@ -149,7 +151,7 @@ public class MyFileUtils {
}
}
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, append), charset)) {
writer.write(content + "\n");
writer.write(writeContent);
writer.flush();
} catch (Exception e) {
throw new Exception("写入文件失败: " + file.getAbsolutePath(), e);

View File

@@ -266,32 +266,30 @@ public class MysqlUtils {
* @return
*/
public static String getExecScript(BizDbConfig dbConfig, String scriptName) {
String EXEC_RESULT = "执行成功";
String LOG_NAME = EXEC_LOG_PATH + vId.getCid() + "_log_" + scriptName;
MyFileUtils.writeFile(LOG_NAME, "-- 执行脚本:" + scriptName + ",开始时间:" + vDate.getNow(), true);
String execResult = "执行成功";
String logName = EXEC_LOG_PATH + vId.getCid() + "_log_" + scriptName;
Connection conn = null;
MyFileUtils.writeFile(logName, "-- 执行脚本:" + scriptName + ",开始时间:" + vDate.getNow(), true);
try {
File file = new File(EXEC_RUN_PATH + scriptName);
// 1. 建立数据库连接并关闭自动提交
conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(),
dbConfig.getDbUsername(), dbConfig.getDbPassword());
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;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty() || line.startsWith("--")) {
continue;
String trimmedLine = line.trim();
if (trimmedLine.isEmpty()) {
continue; // 过滤空行,避免无效拆分
}
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();
for (Map.Entry<String, String> entry : varMap.entrySet()) {
replacedSqlContent = replacedSqlContent.replaceAll(entry.getKey(), entry.getValue());
sqlContent.append(line).append("\n"); // 保留原始行内容(含注释)
}
}
String replacedSqlContent = sqlContent.toString();
replacedSqlContent = replacedSqlContent.replaceAll("\\$\\{[bB][iI][zZ]_[dD][aA][tT][eE]\\}", vDate.dsValueDaysAgo(1));
try (Statement statement = conn.createStatement()) {
String[] sqlStatements = replacedSqlContent.split(";");
int totalAffectedRows = 0;
for (String sql : sqlStatements) {
@@ -299,7 +297,12 @@ public class MysqlUtils {
if (sql.isEmpty()) {
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);
if (hasResultSet) {
try (ResultSet rs = statement.getResultSet()) {
@@ -309,7 +312,7 @@ public class MysqlUtils {
for (int i = 1; i <= columnCount; i++) {
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;
while (rs.next()) {
rowNum++;
@@ -318,7 +321,7 @@ public class MysqlUtils {
Object value = rs.getObject(i);
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);
}
@@ -328,27 +331,43 @@ public class MysqlUtils {
logger.info(sql, "执行成功, 影响行数:", affectedRows);
}
}
// 提交事务
conn.commit();
MyFileUtils.writeFile(LOG_NAME, "-- 执行脚本:" + scriptName + ",结束时间:" + vDate.getNow(), true);
logger.info(file.getName(), "脚本影响行数:", totalAffectedRows, " 执行结果:", EXEC_RESULT);
logger.info(file.getName(), "脚本影响行数:", totalAffectedRows, " 执行结果:", execResult);
} catch (Exception e) {
conn.rollback();
EXEC_RESULT = e.getMessage();
// 回滚事务
if (conn != null) {
try {
conn.rollback();
} catch (SQLException rollbackEx) {
logger.error("事务回滚失败", rollbackEx);
}
}
execResult = e.getMessage();
logger.error(file.getName(), "执行脚本文件失败:", e);
}
} catch (Exception e) {
EXEC_RESULT = e.getMessage();
execResult = e.getMessage();
logger.error("执行脚本入口异常", e);
} finally {
// 最终关闭连接
// 关闭数据库连接
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
logger.error("关闭数据库连接失败", e);
} catch (SQLException closeEx) {
logger.error("关闭数据库连接失败", closeEx);
}
}
// 写入脚本结束日志
try {
MyFileUtils.writeFile(logName, "-- 执行脚本:" + scriptName + ",结束时间:" + vDate.getNow(), true);
} catch (Exception e) {
logger.error("写入执行结束日志失败", e);
}
}
return EXEC_RESULT;
return execResult;
}
}