|
|
|
@ -8,9 +8,14 @@ import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
import javafx.application.Platform;
|
|
|
|
|
import javafx.scene.control.Alert;
|
|
|
|
|
|
|
|
|
|
public class DataManager {
|
|
|
|
|
private static DataManager instance;
|
|
|
|
|
private static final String DB_URL = "jdbc:sqlite:db/dbuml.db3";
|
|
|
|
|
private static Connection connection;
|
|
|
|
|
|
|
|
|
|
private Map<String, Goods> goodsMap;
|
|
|
|
|
private List<String> suppliers;
|
|
|
|
|
private Map<String, Integer> inventory;
|
|
|
|
@ -161,58 +166,79 @@ public class DataManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addOperationLog(String operationType, String operationTarget, String operator, String details) {
|
|
|
|
|
String id = String.format("NO%014d", System.currentTimeMillis());
|
|
|
|
|
OperationLog log = new OperationLog(id, operationType, operationTarget,
|
|
|
|
|
operator, LocalDateTime.now(), details);
|
|
|
|
|
operationLogs.add(log);
|
|
|
|
|
System.out.println("开始添加操作日志...");
|
|
|
|
|
System.out.println("类型: " + operationType);
|
|
|
|
|
System.out.println("目标: " + operationTarget);
|
|
|
|
|
System.out.println("详情: " + details);
|
|
|
|
|
|
|
|
|
|
String id = String.format("LOG%014d", System.currentTimeMillis());
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
|
|
|
|
|
// 添加数据库操作
|
|
|
|
|
String url = "jdbc:sqlite:db/dbuml.db3";
|
|
|
|
|
String sql = "INSERT INTO operation_logs (id, operation_type, operation_target, operator, operation_time, details) " +
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?)";
|
|
|
|
|
|
|
|
|
|
try (Connection conn = DriverManager.getConnection(url);
|
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?)";
|
|
|
|
|
|
|
|
|
|
synchronized (DataManager.class) {
|
|
|
|
|
try (PreparedStatement pstmt = getConnection().prepareStatement(sql)) {
|
|
|
|
|
pstmt.setString(1, id);
|
|
|
|
|
pstmt.setString(2, operationType);
|
|
|
|
|
pstmt.setString(3, operationTarget);
|
|
|
|
|
pstmt.setString(4, operator);
|
|
|
|
|
pstmt.setString(5, now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
|
|
|
|
pstmt.setString(6, details);
|
|
|
|
|
|
|
|
|
|
pstmt.setString(1, log.getId());
|
|
|
|
|
pstmt.setString(2, log.getOperationType());
|
|
|
|
|
pstmt.setString(3, log.getOperationTarget());
|
|
|
|
|
pstmt.setString(4, log.getOperator());
|
|
|
|
|
pstmt.setString(5, log.getOperationTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
|
|
|
|
pstmt.setString(6, log.getDetails());
|
|
|
|
|
|
|
|
|
|
pstmt.executeUpdate();
|
|
|
|
|
int result = pstmt.executeUpdate();
|
|
|
|
|
if (result > 0) {
|
|
|
|
|
OperationLog log = new OperationLog(id, operationType, operationTarget,
|
|
|
|
|
operator, now, details);
|
|
|
|
|
operationLogs.add(log);
|
|
|
|
|
System.out.println("操作日志已添加: " + details);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
showAlert("错误", "<22><><EFBFBD>存操作日志失败:" + e.getMessage());
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
System.err.println("添加操作日志失败: " + e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OperationLog> getOperationLogs() {
|
|
|
|
|
List<OperationLog> logs = new ArrayList<>();
|
|
|
|
|
String url = "jdbc:sqlite:db/dbuml.db3";
|
|
|
|
|
String sql = "SELECT * FROM operation_logs ORDER BY operation_time DESC";
|
|
|
|
|
|
|
|
|
|
try (Connection conn = DriverManager.getConnection(url);
|
|
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
|
ResultSet rs = stmt.executeQuery(sql)) {
|
|
|
|
|
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
OperationLog log = new OperationLog(
|
|
|
|
|
rs.getString("id"),
|
|
|
|
|
rs.getString("operation_type"),
|
|
|
|
|
rs.getString("operation_target"),
|
|
|
|
|
rs.getString("operator"),
|
|
|
|
|
LocalDateTime.parse(rs.getString("operation_time")),
|
|
|
|
|
rs.getString("details")
|
|
|
|
|
);
|
|
|
|
|
logs.add(log);
|
|
|
|
|
synchronized (DataManager.class) {
|
|
|
|
|
try (Statement stmt = getConnection().createStatement();
|
|
|
|
|
ResultSet rs = stmt.executeQuery(sql)) {
|
|
|
|
|
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
try {
|
|
|
|
|
String id = rs.getString("id");
|
|
|
|
|
String operationType = rs.getString("operation_type");
|
|
|
|
|
String operationTarget = rs.getString("operation_target");
|
|
|
|
|
String operator = rs.getString("operator");
|
|
|
|
|
String timeStr = rs.getString("operation_time");
|
|
|
|
|
String details = rs.getString("details");
|
|
|
|
|
|
|
|
|
|
LocalDateTime operationTime = LocalDateTime.parse(timeStr);
|
|
|
|
|
|
|
|
|
|
OperationLog log = new OperationLog(
|
|
|
|
|
id, operationType, operationTarget,
|
|
|
|
|
operator, operationTime, details
|
|
|
|
|
);
|
|
|
|
|
logs.add(log);
|
|
|
|
|
|
|
|
|
|
System.out.println("读取到日志: " + operationType + " - " + details);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("解析日志记录失败: " + e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("总共读取到 " + logs.size() + " 条日志记录");
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
System.err.println("查询操作日志失败: " + e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
showAlert("错误", "读取操作日志失败:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return logs;
|
|
|
|
@ -297,6 +323,7 @@ public class DataManager {
|
|
|
|
|
addGoods(goods);
|
|
|
|
|
|
|
|
|
|
// 将操作日志类型从"新增"改为"修改"
|
|
|
|
|
System.out.println("正在添加操作日志: " + "修改" + " - " + "修改货物: " + goods.getName());
|
|
|
|
|
addOperationLog("修改", goods.getId(), "当前用户", "修改货物: " + goods.getName());
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
@ -319,6 +346,7 @@ public class DataManager {
|
|
|
|
|
pstmt.setString(6, goods.getId());
|
|
|
|
|
|
|
|
|
|
pstmt.executeUpdate();
|
|
|
|
|
System.out.println("正在添加操作日志: " + "更新" + " - " + "更新货物: " + goods.getName());
|
|
|
|
|
addOperationLog("更新", goods.getId(), "当前用户", "更新货物: " + goods.getName());
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
@ -327,7 +355,14 @@ public class DataManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static void showAlert(String 错误, String s) {
|
|
|
|
|
protected static void showAlert(String title, String content) {
|
|
|
|
|
Platform.runLater(() -> {
|
|
|
|
|
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
|
|
|
|
alert.setTitle(title);
|
|
|
|
|
alert.setHeaderText(null);
|
|
|
|
|
alert.setContentText(content);
|
|
|
|
|
alert.showAndWait();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> getAllCategories() {
|
|
|
|
@ -336,4 +371,25 @@ public class DataManager {
|
|
|
|
|
.distinct()
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized Connection getConnection() throws SQLException {
|
|
|
|
|
if (connection == null || connection.isClosed()) {
|
|
|
|
|
connection = DriverManager.getConnection(DB_URL);
|
|
|
|
|
// 启用WAL模式,提高并发性能
|
|
|
|
|
try (Statement stmt = connection.createStatement()) {
|
|
|
|
|
stmt.execute("PRAGMA journal_mode=WAL");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void closeConnection() {
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
connection.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|