diff --git a/src/model/InventoryManager.java b/src/model/InventoryManager.java index f3fe051..b1f423f 100644 --- a/src/model/InventoryManager.java +++ b/src/model/InventoryManager.java @@ -1,41 +1,62 @@ package model; -import java.util.HashMap; -import java.util.Map; +import java.time.LocalDateTime; public class InventoryManager { - private Map inventory; + private String id; + private String goodsId; + private int quantity; + private String supplier; + private LocalDateTime inboundTime; + private LocalDateTime outboundTime; + private String operator; + private String remark; + private String customer; + + public InventoryManager(String id, String goodsId, int quantity, String supplier, LocalDateTime inboundTime, LocalDateTime outboundTime, String operator, String remark, String customer) { + this.id = id; + this.goodsId = goodsId; + this.quantity = quantity; + this.supplier = supplier; + this.inboundTime = inboundTime; + this.operator = operator; + this.remark = remark; + this.customer = customer; + } + + public String getId() { + return id; + } + + public String getGoodsId() { + return goodsId; + } + + public int getQuantity() { + return quantity; + } - public InventoryManager() { - inventory = new HashMap<>(); + public String getSupplier() { + return supplier; } - public void addOrUpdateInventory(String goodsId, int quantity) { - inventory.put(goodsId, inventory.getOrDefault(goodsId, 0) + quantity); + public LocalDateTime getInboundTime() { + return inboundTime; } - public void removeOrUpdateInventory(String goodsId, int quantity) { - if (inventory.containsKey(goodsId)) { - int currentQuantity = inventory.get(goodsId); - if (currentQuantity >= quantity) { - inventory.put(goodsId, currentQuantity - quantity); - } else { - throw new IllegalArgumentException("库存不足"); - } - } else { - throw new IllegalArgumentException("货物不存在"); - } + public LocalDateTime getOutboundTime() { + return outboundTime; } - public int getInventoryQuantity(String goodsId) { - return inventory.getOrDefault(goodsId, 0); + public String getOperator() { + return operator; } - public void processInboundRecord(InboundRecord record) { - addOrUpdateInventory(record.getGoodsId(), record.getQuantity()); + public String getRemark() { + return remark; } - public void processOutboundRecord(OutboundRecord record) { - removeOrUpdateInventory(record.getGoodsId(), record.getQuantity()); + public String getCustomer() { + return customer; } } diff --git a/src/view/InventoryView.java b/src/view/InventoryView.java index e898f92..646daf1 100644 --- a/src/view/InventoryView.java +++ b/src/view/InventoryView.java @@ -1,24 +1,28 @@ package view; +import javafx.application.Platform; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.*; -import model.Goods; import model.InventoryManager; -public class InventoryView extends BorderPane { +import java.sql.*; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +public class InventoryView extends BorderPane { private TextField goodsField; + private Label resultLabel; + + private TableView tableView; + private TextField customerField; private TextField quantityField; private TextField remarkField; - private TableView tableView; - private Label totalValue; - private final InventoryManager inventoryManager; - public InventoryView() { - inventoryManager = new InventoryManager(); initializeUI(); } @@ -27,165 +31,143 @@ public class InventoryView extends BorderPane { VBox topArea = createTopArea(); setTop(topArea); - // 中间表格区域 + // 中间结果显示区域 tableView = createTableView(); setCenter(tableView); - - // 底部汇总区域 - HBox bottomArea = createBottomArea(); - setBottom(bottomArea); } + private TableView createTableView() { + TableView table = new TableView<>(); + + 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")); + supplierCol.setPrefWidth(120); + + 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, operatorCol, remarkCol); + table.setOnMouseClicked(event -> { + if (event.getClickCount() == 2) { + InventoryManager selectedRecord = table.getSelectionModel().getSelectedItem(); + if (selectedRecord != null) { + fillFields(selectedRecord); + } + } + }); + return table; + } private VBox createTopArea() { 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 quantityLabel = new Label("数量:"); - quantityField = new TextField(); - quantityField.setPromptText("请输入数量"); - - Label remarkLabel = new Label("备注:"); - remarkField = new TextField(); - remarkField.setPromptText("请输入备注信息"); - - basicInfo.addRow(0, goodsLabel, goodsField, quantityLabel, quantityField); - basicInfo.addRow(1, remarkLabel, remarkField); + basicInfo.addRow(0, goodsLabel, goodsField); // 设置列宽 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); - basicInfo.getColumnConstraints().addAll(column1, column2, column3, column4); + basicInfo.getColumnConstraints().addAll(column1, column2); // 操作按钮 HBox buttonBox = new HBox(10); buttonBox.setAlignment(Pos.CENTER_RIGHT); buttonBox.setPadding(new Insets(10, 0, 0, 0)); - Button addButton = new Button("添加库存"); - addButton.setOnAction(e -> handleAdd()); + Button queryButton = new Button("查询库存"); + queryButton.setOnAction(e -> handleQuery()); - Button clearButton = new Button("清空"); - clearButton.setOnAction(e -> handleClear()); - - buttonBox.getChildren().addAll(addButton, clearButton); + buttonBox.getChildren().add(queryButton); topArea.getChildren().addAll(basicInfo, buttonBox); return topArea; } - private TableView createTableView() { - TableView table = new TableView<>(); - - TableColumn idCol = new TableColumn<>("单号"); - idCol.setCellValueFactory(new PropertyValueFactory<>("id")); - - TableColumn nameCol = new TableColumn<>("货物名称"); - nameCol.setCellValueFactory(new PropertyValueFactory<>("name")); - - TableColumn quantityCol = new TableColumn<>("数量"); - quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity")); - - TableColumn remarkCol = new TableColumn<>("备注"); - remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark")); - - table.getColumns().addAll(idCol, nameCol, quantityCol, remarkCol); - - table.getItems().addListener((javafx.collections.ListChangeListener.Change c) -> { - updateTotal(); - }); - - return table; - } - - private HBox createBottomArea() { - HBox bottomArea = new HBox(10); - bottomArea.setPadding(new Insets(10)); - bottomArea.setAlignment(Pos.CENTER_RIGHT); - - Label totalLabel = new Label("库存总数量:"); - totalValue = new Label("0"); - Label unitLabel = new Label("件"); - - bottomArea.getChildren().addAll(totalLabel, totalValue, unitLabel); - return bottomArea; - } - - private void updateTotal() { - int total = tableView.getItems().stream() - .mapToInt(Goods::getQuantity) - .sum(); - totalValue.setText(String.valueOf(total)); - } - - private void handleAdd() { - try { - String goodsName = goodsField.getText().trim(); - String quantityText = quantityField.getText().trim(); - String remark = remarkField.getText().trim(); - - // 输入验证 - if (goodsName.isEmpty() || quantityText.isEmpty()) { - showAlert("错误", "请填写完整的库存信息!"); - return; - } + private void handleQuery() { + String goodsName = goodsField.getText().trim(); + if (goodsName.isEmpty()) { + showAlert("错误", "请输入货物名称!"); + return; + } - int quantity; + Platform.runLater(() -> { try { - quantity = Integer.parseInt(quantityText); - if (quantity <= 0) { - showAlert("错误", "数量必须大于0!"); - return; + String url = "jdbc:sqlite:db/dbuml.db3"; + try (Connection conn = DriverManager.getConnection(url)) { + // 查询当前库存 + String sql = "SELECT * FROM warehouse WHERE goods_id = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, goodsName); + 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 remark = rs.getString("remark"); + + InventoryManager record = new InventoryManager( + id, + goodsId, + quantity, + supplier, + null, // 入库时间为 null + null, // 出库时间为 null + operator, + remark, + null // 客户为 null + ); + // 将新的记录添加到现有数据中 + existingData.add(record); + found = true; + } + if (!found) { + showAlert("错误", "未找到该货物!"); + } + } } - } catch (NumberFormatException e) { - showAlert("错误", "请输入有效的数量!"); - return; + } catch (Exception e) { + e.printStackTrace(); + resultLabel.setText("查询失败:" + e.getMessage()); } - - // 创建货物对象 - String id = String.format("NO%014d", System.currentTimeMillis()); - Goods goods = new Goods(id, goodsName, quantity, remark); - - // 添加到库存管理器 - inventoryManager.addOrUpdateInventory(goods.getId(), quantity); - - // 添加到表格 - tableView.getItems().add(goods); - - // 更新总计 - updateTotal(); - - // 清空输入 - handleClear(); - - showAlert("成功", "库存记录已保存!"); - - } catch (Exception e) { - showAlert("错误", "保存失败:" + e.getMessage()); - } - } - - private void handleClear() { - goodsField.clear(); - quantityField.clear(); - remarkField.clear(); + }); } - private void showAlert(String title, String content) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle(title); @@ -193,4 +175,12 @@ public class InventoryView extends BorderPane { alert.setContentText(content); alert.showAndWait(); } + + // 加填充字段的方法 + private void fillFields(InventoryManager record) { + goodsField.setText(record.getGoodsId()); + customerField.setText(record.getCustomer()); + quantityField.setText(String.valueOf(record.getQuantity())); + remarkField.setText(record.getRemark()); + } }