新增待办信息
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package com.jeesite.modules.app.Job;
|
||||
|
||||
|
||||
import com.jeesite.modules.app.dao.TableTree;
|
||||
import com.jeesite.modules.app.utils.LoggerUtils;
|
||||
import com.jeesite.modules.app.utils.MyFileUtils;
|
||||
import com.jeesite.modules.app.utils.MysqlUtils;
|
||||
import com.jeesite.modules.app.utils.vDate;
|
||||
import com.jeesite.modules.biz.entity.BizDbConfig;
|
||||
import com.jeesite.modules.biz.entity.BizExecScript;
|
||||
import com.jeesite.modules.biz.entity.BizTableField;
|
||||
import com.jeesite.modules.biz.entity.BizTableInfo;
|
||||
import com.jeesite.modules.biz.service.BizDbConfigService;
|
||||
import com.jeesite.modules.biz.service.BizExecScriptService;
|
||||
import com.jeesite.modules.biz.service.BizTableFieldService;
|
||||
import com.jeesite.modules.biz.service.BizTableInfoService;
|
||||
import jakarta.annotation.Resource;
|
||||
@@ -14,8 +19,10 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -30,6 +37,9 @@ public class dataJob {
|
||||
@Resource
|
||||
private BizTableFieldService tableFieldService;
|
||||
|
||||
@Resource
|
||||
private BizExecScriptService bizExecScriptService;
|
||||
|
||||
|
||||
@Resource(name = "hostMonitorExecutor")
|
||||
private ThreadPoolTaskExecutor hostMonitorExecutor;
|
||||
@@ -71,4 +81,33 @@ public class dataJob {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Scheduled(cron = "0 30 0 * * ?")
|
||||
public void getExecScript() {
|
||||
BizExecScript execScript = new BizExecScript();
|
||||
execScript.setDataStatus("1");
|
||||
List<BizExecScript> bizExecScriptList = bizExecScriptService.findList(execScript);
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>(bizExecScriptList.size());
|
||||
for (BizExecScript script : bizExecScriptList) {
|
||||
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
BizDbConfig dbConfig = dbConfigService.get(script.getDbId());
|
||||
String result = MysqlUtils.getExecScript(dbConfig, script.getScriptName());
|
||||
script.setExecResult(result);
|
||||
script.setUpdateTime(vDate.getNow());
|
||||
bizExecScriptService.save(script);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}, hostMonitorExecutor); // 指定使用配置的线程池
|
||||
futures.add(future);
|
||||
}
|
||||
try {
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,14 @@ public class ExecResult implements Serializable {
|
||||
|
||||
private String fileName;
|
||||
|
||||
private String error;
|
||||
|
||||
public ExecResult() {
|
||||
}
|
||||
|
||||
public ExecResult(String code, String fileName) {
|
||||
public ExecResult(String code, String fileName, String error) {
|
||||
this.code = code;
|
||||
this.fileName = fileName;
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package com.jeesite.modules.app.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MyFileUtils {
|
||||
@@ -93,6 +98,65 @@ public class MyFileUtils {
|
||||
}
|
||||
|
||||
|
||||
public static List<File> getFilesDir(String dirPath) {
|
||||
if (dirPath == null || dirPath.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("目录路径不能为空!");
|
||||
}
|
||||
File directory = new File(dirPath);
|
||||
|
||||
if (!directory.exists()) {
|
||||
throw new IllegalArgumentException("指定路径不存在:" + dirPath);
|
||||
}
|
||||
if (!directory.isDirectory()) {
|
||||
throw new IllegalArgumentException("指定路径不是目录:" + dirPath);
|
||||
}
|
||||
File[] allFiles = directory.listFiles();
|
||||
List<File> fileList = new ArrayList<>();
|
||||
if (allFiles != null) {
|
||||
for (File file : allFiles) {
|
||||
if (file.isFile()) {
|
||||
fileList.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return fileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件写入
|
||||
*/
|
||||
public static void writeFile(String fileName, String content, boolean is_append) {
|
||||
try {
|
||||
writeToFile(fileName, content, StandardCharsets.UTF_8, is_append);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeToFile(String fileName, String content, java.nio.charset.Charset charset, boolean append) throws Exception {
|
||||
if (fileName == null || fileName.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("文件名不能为空");
|
||||
}
|
||||
if (content == null) {
|
||||
content = "";
|
||||
}
|
||||
File file = new File(fileName);
|
||||
File parentDir = file.getParentFile();
|
||||
if (parentDir != null && !parentDir.exists()) {
|
||||
boolean dirCreated = parentDir.mkdirs();
|
||||
if (!dirCreated) {
|
||||
throw new Exception("创建父目录失败: " + parentDir.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, append), charset)) {
|
||||
writer.write(content + "\n");
|
||||
writer.flush();
|
||||
} catch (Exception e) {
|
||||
throw new Exception("写入文件失败: " + file.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String getIcon(String ext) {
|
||||
switch (ext) {
|
||||
case "wps":
|
||||
|
||||
@@ -10,6 +10,9 @@ import com.jeesite.modules.biz.entity.BizDbConfig;
|
||||
import com.jeesite.modules.biz.entity.BizTableField;
|
||||
import com.jeesite.modules.biz.entity.BizTableInfo;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.*;
|
||||
@@ -24,8 +27,10 @@ public class MysqlUtils {
|
||||
|
||||
private static final String filePath = "/ogsapp/resultList/";
|
||||
|
||||
private static final String EXEC_RUN_PATH = "/ogsapp/script/sql/";
|
||||
private static final String EXEC_LOG_PATH = "/ogsapp/script/log/";
|
||||
|
||||
private static String EXEC_CODE = "0";
|
||||
private static String EXEC_FILE = "";
|
||||
|
||||
// 需要排除的系统数据库
|
||||
private static final List<String> SYSTEM_DATABASES = Arrays.asList(
|
||||
@@ -214,8 +219,9 @@ public class MysqlUtils {
|
||||
|
||||
|
||||
public static ExecResult getExecResult(BizDbConfig dbConfig, String sql) {
|
||||
String EXEC_RESULT = "执行成功";
|
||||
String EXEC_FILE = null;
|
||||
try {
|
||||
EXEC_FILE = null;
|
||||
Connection conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(), dbConfig.getDbUsername(), dbConfig.getDbPassword());
|
||||
Statement statement = conn.createStatement();
|
||||
boolean isQuery = sql.trim().toUpperCase().startsWith("SELECT");
|
||||
@@ -247,8 +253,102 @@ public class MysqlUtils {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
EXEC_CODE = "0";
|
||||
EXEC_RESULT = e.getMessage();
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
return new ExecResult(EXEC_CODE, EXEC_FILE);
|
||||
return new ExecResult(EXEC_CODE, EXEC_FILE, EXEC_RESULT);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 脚本执行
|
||||
*
|
||||
* @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);
|
||||
Connection conn = null;
|
||||
try {
|
||||
File file = new File(EXEC_RUN_PATH + scriptName);
|
||||
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();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
line = line.trim();
|
||||
if (line.isEmpty() || line.startsWith("--")) {
|
||||
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());
|
||||
}
|
||||
String[] sqlStatements = replacedSqlContent.split(";");
|
||||
int totalAffectedRows = 0;
|
||||
for (String sql : sqlStatements) {
|
||||
sql = sql.trim();
|
||||
if (sql.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
MyFileUtils.writeFile(LOG_NAME, sql, true);
|
||||
boolean hasResultSet = statement.execute(sql);
|
||||
if (hasResultSet) {
|
||||
try (ResultSet rs = statement.getResultSet()) {
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
StringBuilder header = new StringBuilder();
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
header.append(metaData.getColumnName(i)).append("\t");
|
||||
}
|
||||
MyFileUtils.writeFile(LOG_NAME, header.toString().trim(), true);
|
||||
int rowNum = 0;
|
||||
while (rs.next()) {
|
||||
rowNum++;
|
||||
StringBuilder rowData = new StringBuilder();
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
Object value = rs.getObject(i);
|
||||
rowData.append(value == null ? "NULL" : value.toString()).append("\t");
|
||||
}
|
||||
MyFileUtils.writeFile(LOG_NAME, rowData.toString().trim(), true);
|
||||
}
|
||||
logger.info(sql, "执行成功,总行数:", rowNum);
|
||||
}
|
||||
} else {
|
||||
int affectedRows = statement.getUpdateCount();
|
||||
totalAffectedRows += affectedRows;
|
||||
logger.info(sql, "执行成功, 影响行数:", affectedRows);
|
||||
}
|
||||
}
|
||||
conn.commit();
|
||||
MyFileUtils.writeFile(LOG_NAME, "-- 执行脚本:" + scriptName + ",结束时间:" + vDate.getNow(), true);
|
||||
logger.info(file.getName(), "脚本影响行数:", totalAffectedRows, " 执行结果:", EXEC_RESULT);
|
||||
} catch (Exception e) {
|
||||
conn.rollback();
|
||||
EXEC_RESULT = e.getMessage();
|
||||
logger.error(file.getName(), "执行脚本文件失败:", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
EXEC_RESULT = e.getMessage();
|
||||
logger.error("执行脚本入口异常", e);
|
||||
} finally {
|
||||
// 最终关闭连接
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
logger.error("关闭数据库连接失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return EXEC_RESULT;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user