From e41a3a5f0088c0cbc93fd55191056ba4832c26cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=9F=8E?= <2304508674@qq.com> Date: Wed, 18 Dec 2024 12:21:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BA=E5=BA=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/libraries/junit_4_12.xml | 7 +- db/dbuml.db3 | Bin 36864 -> 36864 bytes src/DML.java | 94 ++++++++++- src/model/OutboundRecord.java | 84 +++++++--- src/view/GoodsManagementView.java | 62 ++++---- src/view/InboundView.java | 12 +- src/view/OutboundView.java | 248 ++++++++++++++++-------------- 7 files changed, 326 insertions(+), 181 deletions(-) diff --git a/.idea/libraries/junit_4_12.xml b/.idea/libraries/junit_4_12.xml index 3e0b5d8..fdd6e34 100644 --- a/.idea/libraries/junit_4_12.xml +++ b/.idea/libraries/junit_4_12.xml @@ -1,9 +1,8 @@ - - + - - + + diff --git a/db/dbuml.db3 b/db/dbuml.db3 index a4b4bc796e9669b6c64f307a90e3040609bd0fe5..c8e1aab96514488de09873414becbeb37227271c 100644 GIT binary patch literal 36864 zcmeI4|8Emz7{Kp(>$ipO6^B{&gW(dhWXsm`zTdSA!Wb2b`{ve(Gnl4q=WN*8(cTy& zira{e8I451p^!*Si1GtQj84P}3F_ZqNc?dvl3-^1&0mb~U3=GFJLb^D40x`4cYVJ+ z&+|T?_uAxr->2`u^Ib_@qhCp9#uGX%5RVZgN$j9$f*^L_yLJK^bgU`MTY2}-Wc68*wxim5<#2EK*QFpQo}5y_%lPYW5qi!bT4wkC7cEuY98IT3vT@6h-TOn6iIkqykJ8beSY&skzvNeTa$;gEsbx&p z+mfjR>B-bcTu+W`bS&B(8Hjas_Z6K_7aL`?@kHjJDbO-A;P#qBkodEei{Cz4~?f>-DuAvd(PlCy;&wy0#Z z;dEvsTam4?{F)i{d!GmfeaGF0liE}=by!R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6H2Be zE7=szDmbaADOU4uKmXCK-5a;}Ur}!FztDN)~qY3;7u)na|&$>~fcqqLuU?cngRp2e9YuQ_CRwXuGGDP6~S7=Nms z~R@*SqbXUbwx#d#Afc z&f-)yZ47us3^dbwGpT9M-PC^bYWth7TJwMM(jPV;00e*l5C8%|00;m9AOHk_01yBI zK;Q#Rz?%OD(ES4X1*(z{*nj{K00KY&2mk>f00e*l5C8%|00`WB1WpFRGZVgvxyhr2 zB2C0nH9eD(75TmW>z(VDqMaMp+HYLuDOh?*(MkSQR!${Jo!)o&ovYVR`rWp$rmLE) zsu^a)-?{O!+m<87H7%u*3@U>e8DvT&sXkWfl;JTIC8HWZ ze-zL^(A(q#HXr~5fB+Bx0zd!=00AHX1b_e#00KbZ{Yv0yV8-WJp$Ir-e5Jwvp}QGwbP?6i3VIklh(3x&i7LQ5@7J)QUqAo|00AHX1b_e# z00KY&2mk>f00anu4+j!H{+oMSK}B>IGMSUOyO4>I#5{#mF=WhJXvum+J%vl&RN=sXG?0jJAhrkr)&Gm71oT6+ ziasv>Mf{%lxG2VdAAdIf$=H8ld$DbD5;hf00iz?0y!~~KoK-I z89%yshU7eRg-V6!sPSy*UO`I5m3B-T<>CWO(Ak*WXM$-=rd#`t%SP(1H0na}_}BmgWSXb;ML`T$I*_U>3SuBS$N-rVssnmJg&o!2?CU}? z7a2f7Ci6#mPz;t-qwI=-P?!%#5;R4PVF~YX z&Xmnk<%5k`ii+4ZKqRf00e*l5C8%|00{iw5U^f^lPS1sk%}e(IE8H4 zDeff9lmEBE7LsD_GP*W2<|(t5duRrLW00-j& 0 THEN '入库' ELSE '出库' END as type " + + "FROM warehouse " + + "ORDER BY inbound_time DESC"; + ResultSet rs = stmt.executeQuery(sql); + + // 打印表头 + System.out.println("===================== 出入库记录 ====================="); + System.out.printf("%-15s %-15s %-8s %-8s %-15s %-25s%n", + "单号", "货物名称", "数量", "类型", "供应商", "时间"); + System.out.println("==================================================="); + + // 打印数据 + while (rs.next()) { + System.out.printf("%-15s %-15s %-8d %-8s %-15s %-25s%n", + rs.getString("id"), + rs.getString("goods_id"), + Math.abs(rs.getInt("quantity")), + rs.getString("type"), + rs.getString("supplier"), + rs.getString("inbound_time") + ); + } + System.out.println("==================================================="); + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test + public void deleteOutboundRecord() { + try (Connection conn = DriverManager.getConnection(URL); + Statement stmt = conn.createStatement()) { + + // 删除出库记录的SQL语句 + String sql = "DELETE FROM warehouse WHERE id = ?"; + + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { + // 这里可以设置要删除的记录ID + pstmt.setString(1, "要删除的记录ID"); + int result = pstmt.executeUpdate(); + + if (result > 0) { + System.out.println("出库记录删除成功"); + } else { + System.out.println("未找到要删除的记录"); + } + } + + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("删除出库记录失败:" + e.getMessage()); + } + } } \ No newline at end of file diff --git a/src/model/OutboundRecord.java b/src/model/OutboundRecord.java index dde4c6e..30bf39d 100644 --- a/src/model/OutboundRecord.java +++ b/src/model/OutboundRecord.java @@ -1,3 +1,4 @@ + package model; import java.time.LocalDateTime; @@ -6,35 +7,76 @@ public class OutboundRecord { private String id; private String goodsId; private int quantity; - private String supplier; - private LocalDateTime inboundTime; + private String customer; + private LocalDateTime outboundTime; private String operator; private String remark; - public OutboundRecord(String id, String goodsId, int quantity, String supplier, - LocalDateTime inboundTime, String operator, String remark) { + public OutboundRecord(String id, String goodsId, int quantity, String customer, + LocalDateTime outboundTime, String operator, String remark) { this.id = id; this.goodsId = goodsId; this.quantity = quantity; - this.supplier = supplier; - this.inboundTime = inboundTime; + this.customer = customer; + this.outboundTime = outboundTime; this.operator = operator; this.remark = remark; } // Getters and Setters - public String getId() { return id; } - public void setId(String id) { this.id = id; } - public String getGoodsId() { return goodsId; } - public void setGoodsId(String goodsId) { this.goodsId = goodsId; } - public int getQuantity() { return quantity; } - public void setQuantity(int quantity) { this.quantity = quantity; } - public String getSupplier() { return supplier; } - public void setSupplier(String supplier) { this.supplier = supplier; } - public LocalDateTime getInboundTime() { return inboundTime; } - public void setInboundTime(LocalDateTime inboundTime) { this.inboundTime = inboundTime; } - public String getOperator() { return operator; } - public void setOperator(String operator) { this.operator = operator; } - public String getRemark() { return remark; } - public void setRemark(String remark) { this.remark = remark; } -} \ No newline at end of file + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getGoodsId() { + return goodsId; + } + + public void setGoodsId(String goodsId) { + this.goodsId = goodsId; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getCustomer() { + return customer; + } + + public void setCustomer(String customer) { + this.customer = customer; + } + + public LocalDateTime getOutboundTime() { + return outboundTime; + } + + public void setOutboundTime(LocalDateTime outboundTime) { + this.outboundTime = outboundTime; + } + + public String getOperator() { + return operator; + } + + public void setOperator(String operator) { + this.operator = operator; + } + + public String getRemark() { + return remark; + } + + public void setRemark(String remark) { + this.remark = remark; + } +} \ No newline at end of file diff --git a/src/view/GoodsManagementView.java b/src/view/GoodsManagementView.java index 63c97c2..0a1a56e 100644 --- a/src/view/GoodsManagementView.java +++ b/src/view/GoodsManagementView.java @@ -11,7 +11,6 @@ import model.Goods; import java.sql.Connection; import java.sql.DriverManager; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.time.LocalDateTime; @@ -146,16 +145,19 @@ public class GoodsManagementView extends BorderPane { String url = "jdbc:sqlite:db/dbuml.db3"; List goodsList = new ArrayList<>(); try (Connection conn = DriverManager.getConnection(url)) { - String sql = "SELECT * FROM warehouse ORDER BY inbound_time DESC"; + String sql = "SELECT id, goods_id as name, quantity, " + + "supplier, inbound_time, operator, remark " + + "FROM warehouse " + + "ORDER BY id"; // 不再使用GROUP BY,直接显示所有记录 try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { Goods goods = new Goods( - rs.getString("id"), - rs.getString("goods_id"), - rs.getInt("quantity"), - rs.getString("remark") + rs.getString("id"), + rs.getString("name"), + rs.getInt("quantity"), + rs.getString("remark") ); goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time"))); goods.setOperator(rs.getString("operator")); @@ -196,41 +198,33 @@ public class GoodsManagementView extends BorderPane { return; } - // 检查是否存在该记录 - String url = "jdbc:sqlite:db/dbuml.db3"; - try (Connection conn = DriverManager.getConnection(url)) { - String checkSql = "SELECT * FROM warehouse WHERE id = ?"; - try (PreparedStatement pstmt = conn.prepareStatement(checkSql)) { - pstmt.setString(1, id); - ResultSet rs = pstmt.executeQuery(); - - if (rs.next()) { - // 记录存在,执行更新操作 - String updateSql = "UPDATE warehouse SET goods_id = ?, quantity = ?, " + - "supplier = ?, remark = ? WHERE id = ?"; - try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) { - updateStmt.setString(1, name); - updateStmt.setInt(2, quantity); - updateStmt.setString(3, supplier); - updateStmt.setString(4, remark); - updateStmt.setString(5, id); - - updateStmt.executeUpdate(); - showAlert("成功", "货物信息已更新!"); - } - } else { - showAlert("错误", "未找到指定ID的货物记录!"); - return; - } - } + // 查找货物对象 + Goods existingGoods = DataManager.getInstance().getGoods(id); + if (existingGoods == null) { + // 插入新货物 + Goods newGoods = new Goods(String.format("NO%014d", System.currentTimeMillis()), + name, quantity, remark); + newGoods.setInboundTime(LocalDateTime.now()); + newGoods.setOperator("当前用户"); + newGoods.setSupplier(supplier); + DataManager.getInstance().addGoodsToDatabase(newGoods); + } else { + // 更新现有货物 + existingGoods.setName(name); + existingGoods.setQuantity(quantity); + existingGoods.setSupplier(supplier); + existingGoods.setRemark(remark); + DataManager.getInstance().updateGoodsInDatabase(existingGoods); } - // 更新表格显示 + // 更新表格 loadGoodsData(); // 清空输入 handleClear(); + showAlert("成功", "货物信息已更新!"); + } catch (Exception e) { showAlert("错误", "保存失败:" + e.getMessage()); e.printStackTrace(); diff --git a/src/view/InboundView.java b/src/view/InboundView.java index 08b6731..e485037 100644 --- a/src/view/InboundView.java +++ b/src/view/InboundView.java @@ -182,14 +182,15 @@ public class InboundView extends BorderPane { } // 创建入库记录 - String id = "IN" + System.currentTimeMillis(); + String id = String.format("NO%014d", System.currentTimeMillis()); LocalDateTime now = LocalDateTime.now(); // 修改数据库连接为相对路径 String url = "jdbc:sqlite:db/dbuml.db3"; try (Connection conn = DriverManager.getConnection(url)) { - String sql = "INSERT INTO warehouse (id, goods_id, quantity, supplier, inbound_time, operator, remark, type) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; + String sql = "INSERT INTO warehouse (id, goods_id, quantity, supplier, inbound_time, operator, remark) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)"; + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setString(1, id); pstmt.setString(2, goodsName); @@ -198,8 +199,7 @@ public class InboundView extends BorderPane { pstmt.setString(5, now.toString()); pstmt.setString(6, "当前用户"); pstmt.setString(7, remark); - pstmt.setString(8, "IN"); // 设置类型为入库 - + pstmt.executeUpdate(); } } @@ -256,7 +256,7 @@ public class InboundView extends BorderPane { private void loadInboundRecords() { String url = "jdbc:sqlite:db/dbuml.db3"; try (Connection conn = DriverManager.getConnection(url)) { - String sql = "SELECT * FROM warehouse WHERE type = 'IN' ORDER BY inbound_time DESC"; + String sql = "SELECT * FROM warehouse ORDER BY inbound_time DESC"; try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { diff --git a/src/view/OutboundView.java b/src/view/OutboundView.java index b734174..3d84a9e 100644 --- a/src/view/OutboundView.java +++ b/src/view/OutboundView.java @@ -11,8 +11,6 @@ import model.OutboundRecord; import model.DataManager; import model.Goods; import javafx.collections.FXCollections; -import model.InboundRecord; - import java.util.List; import java.util.stream.Collectors; import java.sql.*; @@ -20,7 +18,7 @@ import java.sql.*; public class OutboundView extends BorderPane { private TextField goodsField; - private TextField supplierField; + private TextField customerField; private TextField quantityField; private TextField remarkField; private TableView tableView; @@ -49,20 +47,21 @@ public class OutboundView extends BorderPane { VBox topArea = new VBox(10); topArea.setPadding(new Insets(10)); - // 第一行:基本信息 + // 基本信息 GridPane basicInfo = new GridPane(); basicInfo.setHgap(10); basicInfo.setVgap(10); + // 第一行:货物名称和供应商 Label goodsLabel = new Label("货物名称:"); goodsField = new TextField(); goodsField.setPromptText("请输入货物名称"); Label supplierLabel = new Label("供应商:"); - supplierField = new TextField(); - supplierField.setPromptText("请输入供应商"); + customerField = new TextField(); + customerField.setPromptText("请输入供应商"); - basicInfo.addRow(0, goodsLabel, goodsField, supplierLabel, supplierField); + basicInfo.addRow(0, goodsLabel, goodsField, supplierLabel, customerField); // 第二行:数量和备注 Label quantityLabel = new Label("数量:"); @@ -92,7 +91,7 @@ public class OutboundView extends BorderPane { buttonBox.setAlignment(Pos.CENTER_RIGHT); buttonBox.setPadding(new Insets(10, 0, 0, 0)); - Button saveButton = new Button("保存"); + Button saveButton = new Button("出库"); saveButton.setOnAction(e -> handleSave()); Button clearButton = new Button("清空"); @@ -107,32 +106,57 @@ public class OutboundView extends BorderPane { private TableView createTableView() { TableView table = new TableView<>(); - TableColumn idCol = new TableColumn<>("出库单号"); + TableColumn idCol = new TableColumn<>("单号"); idCol.setCellValueFactory(new PropertyValueFactory<>("id")); + idCol.setPrefWidth(150); TableColumn goodsCol = new TableColumn<>("货物名称"); goodsCol.setCellValueFactory(new PropertyValueFactory<>("goodsId")); + goodsCol.setPrefWidth(120); TableColumn quantityCol = new TableColumn<>("数量"); quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity")); + quantityCol.setPrefWidth(80); - TableColumn supplierCol = new TableColumn<>("供应商"); - supplierCol.setCellValueFactory(new PropertyValueFactory<>("supplier")); + TableColumn customerCol = new TableColumn<>("供应商"); + customerCol.setCellValueFactory(new PropertyValueFactory<>("customer")); + customerCol.setPrefWidth(120); TableColumn timeCol = new TableColumn<>("出库时间"); - timeCol.setCellValueFactory(new PropertyValueFactory<>("inboundTime")); // 保持字段名不变,但实际表示出库时间 + timeCol.setCellValueFactory(new PropertyValueFactory<>("outboundTime")); + timeCol.setPrefWidth(180); + timeCol.setCellFactory(column -> new TableCell() { + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + @Override + protected void updateItem(LocalDateTime item, boolean empty) { + super.updateItem(item, empty); + if (empty || item == null) { + setText(null); + } else { + setText(formatter.format(item)); + } + } + }); TableColumn operatorCol = new TableColumn<>("操作员"); operatorCol.setCellValueFactory(new PropertyValueFactory<>("operator")); + operatorCol.setPrefWidth(100); TableColumn remarkCol = new TableColumn<>("备注"); remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark")); + remarkCol.setPrefWidth(150); - table.getColumns().addAll(idCol, goodsCol, quantityCol, supplierCol, + table.getColumns().addAll(idCol, goodsCol, quantityCol, customerCol, timeCol, operatorCol, remarkCol); - table.getItems().addListener((javafx.collections.ListChangeListener.Change c) -> { - updateTotal(); + // 添加双击事件处理 + table.setOnMouseClicked(event -> { + if (event.getClickCount() == 2) { + OutboundRecord selectedRecord = table.getSelectionModel().getSelectedItem(); + if (selectedRecord != null) { + fillFields(selectedRecord); + } + } }); return table; @@ -161,98 +185,89 @@ public class OutboundView extends BorderPane { private void handleSave() { try { String goodsName = goodsField.getText().trim(); - String supplier = supplierField.getText().trim(); + String customer = customerField.getText().trim(); String quantityText = quantityField.getText().trim(); String remark = remarkField.getText().trim(); // 输入验证 - if (goodsName.isEmpty() || supplier.isEmpty() || quantityText.isEmpty()) { + if (goodsName.isEmpty() || customer.isEmpty() || quantityText.isEmpty()) { showAlert("错误", "请填写完整的出库信息!"); return; } - int quantity; - try { - quantity = Integer.parseInt(quantityText); - if (quantity <= 0) { - showAlert("错误", "数量必须大于0!"); - return; - } - } catch (NumberFormatException e) { - showAlert("错误", "请输入有效的数量!"); + int quantity = Integer.parseInt(quantityText); + if (quantity <= 0) { + showAlert("错误", "数量必须大于0!"); return; } - // 检查库存是否足够 - if (!isStockSufficient(goodsName, quantity)) { - showAlert("错误", "库存不足!"); - return; - } - - // 创建出库记录 - String id = "OUT" + System.currentTimeMillis(); - LocalDateTime now = LocalDateTime.now(); - - // 修改数据库连接为相对路径 String url = "jdbc:sqlite:db/dbuml.db3"; try (Connection conn = DriverManager.getConnection(url)) { - String sql = "INSERT INTO warehouse (id, goods_id, quantity, supplier, inbound_time, operator, remark, type) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; - - try (PreparedStatement pstmt = conn.prepareStatement(sql)) { - pstmt.setString(1, id); - pstmt.setString(2, goodsName); - pstmt.setInt(3, quantity); - pstmt.setString(4, supplier); - pstmt.setString(5, now.toString()); - pstmt.setString(6, "当前用户"); - pstmt.setString(7, remark); - pstmt.setString(8, "OUT"); // 设置类型为出库 - - pstmt.executeUpdate(); + // 首先查询当前库存 + String checkSql = "SELECT id, quantity FROM warehouse WHERE goods_id = ?"; + try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) { + checkStmt.setString(1, goodsName); + ResultSet rs = checkStmt.executeQuery(); + + if (rs.next()) { + int currentStock = rs.getInt("quantity"); + String recordId = rs.getString("id"); + + if (currentStock < quantity) { + showAlert("错误", "库存不足!当前库存: " + currentStock); + return; + } + + int remainingStock = currentStock - quantity; + + if (remainingStock == 0) { + // 如果全部出库,删除记录 + String deleteSql = "DELETE FROM warehouse WHERE id = ?"; + try (PreparedStatement deleteStmt = conn.prepareStatement(deleteSql)) { + deleteStmt.setString(1, recordId); + deleteStmt.executeUpdate(); + } + } else { + // 更新剩余库存 + String updateSql = "UPDATE warehouse SET quantity = ? WHERE id = ?"; + try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) { + updateStmt.setInt(1, remainingStock); + updateStmt.setString(2, recordId); + updateStmt.executeUpdate(); + } + } + + // 刷新表格显示 + loadOutboundRecords(); + + // 清空输入 + handleClear(); + + // 添加操作日志 + String details = String.format("出库货物:%s,数量:%d,供应商:%s", + goodsName, quantity, customer); + DataManager.getInstance().addOperationLog("出库", goodsName, "当前用户", details); + + showAlert("成功", "出库记录已保存!"); + } else { + showAlert("错误", "未找到该货物的库存记录!"); + } } } - - // 创建记录对象并添加到表格 - OutboundRecord record = new OutboundRecord( - id, - goodsName, - quantity, - supplier, - now, - "当前用户", - remark - ); - - // 添加到表格 - tableView.getItems().add(record); - - // 更新总计 - updateTotal(); - - // 清空输入 - handleClear(); - - // 添加操作日志 - String details = String.format("出库货物:%s,数量:%d,供应商:%s", - goodsName, quantity, supplier); - DataManager.getInstance().addOperationLog("出库", goodsName, "当前用户", details); - - showAlert("成功", "出库记录已保存!"); - + } catch (NumberFormatException e) { + showAlert("错误", "请输入有效的数量!"); } catch (Exception e) { - showAlert("错误", "保存失败:" + e.getMessage()); e.printStackTrace(); + showAlert("错误", "保存失败:" + e.getMessage()); } } private void handleClear() { goodsField.clear(); - supplierField.clear(); + customerField.clear(); quantityField.clear(); remarkField.clear(); } - private void showAlert(String title, String content) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle(title); @@ -261,26 +276,42 @@ public class OutboundView extends BorderPane { alert.showAndWait(); } - // 添加加载数据的方法 private void loadOutboundRecords() { String url = "jdbc:sqlite:db/dbuml.db3"; try (Connection conn = DriverManager.getConnection(url)) { - String sql = "SELECT * FROM warehouse WHERE type = 'OUT' ORDER BY inbound_time DESC"; + String sql = "SELECT * FROM warehouse ORDER BY inbound_time DESC"; try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { - + + tableView.getItems().clear(); while (rs.next()) { - OutboundRecord record = new OutboundRecord( - rs.getString("id"), - rs.getString("goods_id"), - rs.getInt("quantity"), - rs.getString("supplier"), - LocalDateTime.parse(rs.getString("inbound_time")), // 实际表示出库时间 - rs.getString("operator"), - rs.getString("remark") - ); - tableView.getItems().add(record); + try { + String id = rs.getString("id"); + String goodsId = rs.getString("goods_id"); + int quantity = rs.getInt("quantity"); + String supplier = rs.getString("supplier"); + String timeStr = rs.getString("inbound_time"); + String operator = rs.getString("operator"); + String remark = rs.getString("remark"); + + LocalDateTime dateTime = LocalDateTime.parse(timeStr); + + OutboundRecord record = new OutboundRecord( + id, + goodsId, + quantity, + supplier, + dateTime, + operator, + remark + ); + tableView.getItems().add(record); + + } catch (Exception e) { + e.printStackTrace(); + } } + updateTotal(); } } catch (Exception e) { e.printStackTrace(); @@ -288,24 +319,11 @@ public class OutboundView extends BorderPane { } } - // 查询库存是否足够 - private boolean isStockSufficient(String goodsName, int quantity) { - String url = "jdbc:sqlite:db/dbuml.db3"; - try (Connection conn = DriverManager.getConnection(url)) { - String sql = "SELECT SUM(quantity) AS total_stock FROM warehouse WHERE goods_id = ? AND type = 'IN'"; - try (PreparedStatement pstmt = conn.prepareStatement(sql)) { - pstmt.setString(1, goodsName); - try (ResultSet rs = pstmt.executeQuery()) { - if (rs.next()) { - int totalStock = rs.getInt("total_stock"); - return totalStock >= quantity; - } - } - } - } catch (SQLException e) { - e.printStackTrace(); - showAlert("错误", "查询库存失败:" + e.getMessage()); - } - return false; // 默认返回false,防止未知错误导致库存检查失败 + // ��加填充字段的方法 + private void fillFields(OutboundRecord record) { + goodsField.setText(record.getGoodsId()); + customerField.setText(record.getCustomer()); + quantityField.setText(String.valueOf(record.getQuantity())); + remarkField.setText(record.getRemark()); } -} +} \ No newline at end of file