From bd400c920509640a46728c9f4f4b3cf3245be9c5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 19 Dec 2024 01:20:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=9F=A5=E8=AF=A2=E7=B1=BB?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=80=9A=E8=BF=87=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=91=98=E6=9F=A5=E8=AF=A2=E5=BA=93=E5=AD=98=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=80=9A=E8=BF=87=E4=BE=9B=E5=BA=94?= =?UTF-8?q?=E5=95=86=E6=9F=A5=E8=AF=A2=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/view/InventoryView.java | 66 +++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/src/view/InventoryView.java b/src/view/InventoryView.java index 6b91cf5..a0ab5e4 100644 --- a/src/view/InventoryView.java +++ b/src/view/InventoryView.java @@ -13,6 +13,8 @@ import model.InventoryManager; import java.sql.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; public class InventoryView extends BorderPane { private TextField goodsField; @@ -88,15 +90,33 @@ public class InventoryView extends BorderPane { goodsField = new TextField(); goodsField.setPromptText("请输入货物名称"); - basicInfo.addRow(0, goodsLabel, goodsField); + // 第一行:操作员 + Label operatorLabel = new Label("操作员:"); + TextField operatorField = new TextField(); + operatorField.setPromptText("请输入操作员"); + + // 第一行:供应商 + Label supplierLabel = new Label("供应商:"); + TextField supplierField = new TextField(); + supplierField.setPromptText("请输入供应商"); + + basicInfo.addRow(0, goodsLabel, goodsField, operatorLabel, operatorField, supplierLabel, supplierField); // 设置列宽 ColumnConstraints column1 = new ColumnConstraints(); column1.setMinWidth(80); ColumnConstraints column2 = new ColumnConstraints(); column2.setMinWidth(200); + ColumnConstraints column3 = new ColumnConstraints(); + column3.setMinWidth(80); + ColumnConstraints column4 = new ColumnConstraints(); + column4.setMinWidth(200); + ColumnConstraints column5 = new ColumnConstraints(); + column5.setMinWidth(80); + ColumnConstraints column6 = new ColumnConstraints(); + column6.setMinWidth(200); - basicInfo.getColumnConstraints().addAll(column1, column2); + basicInfo.getColumnConstraints().addAll(column1, column2, column3, column4, column5, column6); // 操作按钮 HBox buttonBox = new HBox(10); @@ -104,7 +124,7 @@ public class InventoryView extends BorderPane { buttonBox.setPadding(new Insets(10, 0, 0, 0)); Button queryButton = new Button("查询库存"); - queryButton.setOnAction(e -> handleQuery()); + queryButton.setOnAction(e -> handleQuery(operatorField.getText(), supplierField.getText())); buttonBox.getChildren().add(queryButton); @@ -112,10 +132,11 @@ public class InventoryView extends BorderPane { return topArea; } - private void handleQuery() { + private void handleQuery(String operator, String supplier) { String goodsName = goodsField.getText().trim(); - if (goodsName.isEmpty()) { - showAlert("错误", "请输入货物名称!"); + + if (goodsName.isEmpty() && operator.isEmpty() && supplier.isEmpty()) { + showAlert("错误", "请输入至少一个查询条件!"); return; } @@ -123,34 +144,51 @@ public class InventoryView extends BorderPane { try { String url = "jdbc:sqlite:db/dbuml.db3"; try (Connection conn = DriverManager.getConnection(url)) { - // 修改SQL查询语句,使用LIKE实现模糊查询 - String sql = "SELECT * FROM warehouse WHERE goods_id LIKE ?"; + StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM warehouse WHERE 1=1"); + List params = new ArrayList<>(); + + if (!goodsName.isEmpty()) { + sqlBuilder.append(" AND goods_id LIKE ?"); + params.add("%" + goodsName + "%"); + } + if (!operator.isEmpty()) { + sqlBuilder.append(" AND operator LIKE ?"); + params.add("%" + operator + "%"); + } + if (!supplier.isEmpty()) { + sqlBuilder.append(" AND supplier LIKE ?"); + params.add("%" + supplier + "%"); + } + + String sql = sqlBuilder.toString(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - // 在搜索词前后添加%实现模糊匹配 - stmt.setString(1, "%" + goodsName + "%"); + for (int i = 0; i < params.size(); i++) { + stmt.setString(i + 1, (String) params.get(i)); + } ResultSet rs = stmt.executeQuery(); // 获取当前表格中的数据 ObservableList existingData = tableView.getItems(); // 清空现有数据 + existingData.clear(); boolean found = false; while (rs.next()) { String id = rs.getString("id"); String goodsId = rs.getString("goods_id"); int quantity = rs.getInt("quantity"); - String supplier = rs.getString("supplier"); - String operator = rs.getString("operator"); + String supplierRs = rs.getString("supplier"); + String operatorRs = rs.getString("operator"); String remark = rs.getString("remark"); InventoryManager record = new InventoryManager( id, goodsId, quantity, - supplier, + supplierRs, null, // 入库时间为 null null, // 出库时间为 null - operator, + operatorRs, remark, null // 客户为 null );