diff --git a/db/dbuml.db3 b/db/dbuml.db3 index 6cb7524..385a51e 100644 Binary files a/db/dbuml.db3 and b/db/dbuml.db3 differ diff --git a/src/model/DataManager.java b/src/model/DataManager.java index 09d1dc3..f072755 100644 --- a/src/model/DataManager.java +++ b/src/model/DataManager.java @@ -169,25 +169,106 @@ public class DataManager { OperationLog log = new OperationLog(id, operationType, operationTarget, operator, LocalDateTime.now(), details); operationLogs.add(log); + + // 添加数据库操作 + 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)) { + + 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(); + + } catch (SQLException e) { + e.printStackTrace(); + showAlert("错误", "保存操作日志失败:" + e.getMessage()); + } } public List getOperationLogs() { - return new ArrayList<>(operationLogs); + List 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); + } + + } catch (SQLException e) { + e.printStackTrace(); + showAlert("错误", "读取操作日志失败:" + e.getMessage()); + } + + return logs; } public List getFilteredOperationLogs(LocalDateTime startDate, LocalDateTime endDate, String operationType) { - return operationLogs.stream() - .filter(log -> { - boolean timeMatch = true; - if (startDate != null && endDate != null) { - timeMatch = !log.getOperationTime().isBefore(startDate) && - !log.getOperationTime().isAfter(endDate); - } - boolean typeMatch = "全部".equals(operationType) || - operationType.equals(log.getOperationType()); - return timeMatch && typeMatch; - }) - .collect(Collectors.toList()); + List logs = new ArrayList<>(); + String url = "jdbc:sqlite:db/dbuml.db3"; + + StringBuilder sql = new StringBuilder("SELECT * FROM operation_logs WHERE 1=1"); + List params = new ArrayList<>(); + + if (startDate != null && endDate != null) { + sql.append(" AND operation_time BETWEEN ? AND ?"); + params.add(startDate.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + params.add(endDate.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + } + + if (!"全部".equals(operationType)) { + sql.append(" AND operation_type = ?"); + params.add(operationType); + } + + sql.append(" ORDER BY operation_time DESC"); + + try (Connection conn = DriverManager.getConnection(url); + PreparedStatement pstmt = conn.prepareStatement(sql.toString())) { + + // 设置参数 + for (int i = 0; i < params.size(); i++) { + pstmt.setObject(i + 1, params.get(i)); + } + + ResultSet rs = pstmt.executeQuery(); + 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); + } + + } catch (SQLException e) { + e.printStackTrace(); + showAlert("错误", "读取操作日志失败:" + e.getMessage()); + } + + return logs; } @@ -219,8 +300,8 @@ public class DataManager { // Synchronize memory data addGoods(goods); - // Add operation log - addOperationLog("新增", goods.getId(), "当前用户", "新增货物: " + goods.getName()); + // 将操作日志类型从"新增"改为"修改" + addOperationLog("修改", goods.getId(), "当前用户", "修改货物: " + goods.getName()); } catch (SQLException e) { e.printStackTrace(); showAlert("错误", "添加货物失败:" + e.getMessage());