|
|
|
@ -21,8 +21,9 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
|
|
|
|
|
private TextField idField;
|
|
|
|
|
private TextField nameField;
|
|
|
|
|
private TextField priceField;
|
|
|
|
|
private TextField quantityField;
|
|
|
|
|
private TextField supplierField;
|
|
|
|
|
private TextField remarkField;
|
|
|
|
|
private TableView<Goods> tableView;
|
|
|
|
|
|
|
|
|
|
public GoodsManagementView() {
|
|
|
|
@ -41,7 +42,7 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
// 加载现有货物数据
|
|
|
|
|
loadGoodsData();
|
|
|
|
|
|
|
|
|
|
// 添加双击事件以填充文本字段
|
|
|
|
|
// 添加双击事件填充文本字段
|
|
|
|
|
tableView.setOnMouseClicked(event -> {
|
|
|
|
|
if (event.getClickCount() == 2) {
|
|
|
|
|
Goods selectedGoods = tableView.getSelectionModel().getSelectedItem();
|
|
|
|
@ -61,31 +62,29 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
basicInfo.setHgap(10);
|
|
|
|
|
basicInfo.setVgap(10);
|
|
|
|
|
|
|
|
|
|
Label idLabel = new Label("货物ID:");
|
|
|
|
|
Label idLabel = new Label("单号:");
|
|
|
|
|
idField = new TextField();
|
|
|
|
|
idField.setPromptText("请输入货物ID");
|
|
|
|
|
idField.setPromptText("请输入单号");
|
|
|
|
|
|
|
|
|
|
Label nameLabel = new Label("货物名称:");
|
|
|
|
|
nameField = new TextField();
|
|
|
|
|
nameField.setPromptText("请输入货物名称");
|
|
|
|
|
|
|
|
|
|
Label priceLabel = new Label("价格:");
|
|
|
|
|
priceField = new TextField();
|
|
|
|
|
priceField.setPromptText("请输入价格");
|
|
|
|
|
|
|
|
|
|
basicInfo.addRow(0, idLabel, idField, nameLabel, nameField);
|
|
|
|
|
basicInfo.addRow(1, priceLabel, priceField);
|
|
|
|
|
|
|
|
|
|
// 库存信息
|
|
|
|
|
GridPane stockInfo = new GridPane();
|
|
|
|
|
stockInfo.setHgap(10);
|
|
|
|
|
stockInfo.setVgap(10);
|
|
|
|
|
|
|
|
|
|
Label quantityLabel = new Label("数量:");
|
|
|
|
|
quantityField = new TextField();
|
|
|
|
|
quantityField.setPromptText("请输入数量");
|
|
|
|
|
|
|
|
|
|
stockInfo.addRow(0, quantityLabel, quantityField);
|
|
|
|
|
Label supplierLabel = new Label("供应商:");
|
|
|
|
|
supplierField = new TextField();
|
|
|
|
|
supplierField.setPromptText("请输入供应商");
|
|
|
|
|
|
|
|
|
|
Label remarkLabel = new Label("备注:");
|
|
|
|
|
remarkField = new TextField();
|
|
|
|
|
remarkField.setPromptText("请输入备注信息");
|
|
|
|
|
|
|
|
|
|
basicInfo.addRow(0, idLabel, idField, nameLabel, nameField);
|
|
|
|
|
basicInfo.addRow(1, quantityLabel, quantityField, supplierLabel, supplierField);
|
|
|
|
|
basicInfo.addRow(2, remarkLabel, remarkField);
|
|
|
|
|
|
|
|
|
|
// 设置列宽
|
|
|
|
|
ColumnConstraints column1 = new ColumnConstraints();
|
|
|
|
@ -93,8 +92,7 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
ColumnConstraints column2 = new ColumnConstraints();
|
|
|
|
|
column2.setMinWidth(200);
|
|
|
|
|
|
|
|
|
|
basicInfo.getColumnConstraints().addAll(column1, column2);
|
|
|
|
|
stockInfo.getColumnConstraints().addAll(column1, column2);
|
|
|
|
|
basicInfo.getColumnConstraints().addAll(column1, column2, column1, column2);
|
|
|
|
|
|
|
|
|
|
// 操作按钮
|
|
|
|
|
HBox buttonBox = new HBox(10);
|
|
|
|
@ -109,40 +107,36 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
|
|
|
|
|
buttonBox.getChildren().addAll(saveButton, clearButton);
|
|
|
|
|
|
|
|
|
|
topArea.getChildren().addAll(basicInfo, stockInfo, buttonBox);
|
|
|
|
|
topArea.getChildren().addAll(basicInfo, buttonBox);
|
|
|
|
|
return topArea;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TableView<Goods> createTableView() {
|
|
|
|
|
TableView<Goods> table = new TableView<>();
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> idCol = new TableColumn<>("货物ID");
|
|
|
|
|
TableColumn<Goods, String> idCol = new TableColumn<>("单号");
|
|
|
|
|
idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> nameCol = new TableColumn<>("货物名称");
|
|
|
|
|
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, Double> priceCol = new TableColumn<>("价格");
|
|
|
|
|
priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, Integer> quantityCol = new TableColumn<>("数量");
|
|
|
|
|
quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));
|
|
|
|
|
|
|
|
|
|
// 新增的列
|
|
|
|
|
TableColumn<Goods, LocalDateTime> inboundTimeCol = new TableColumn<>("入库时间");
|
|
|
|
|
inboundTimeCol.setCellValueFactory(new PropertyValueFactory<>("inboundTime"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> operatorCol = new TableColumn<>("入库操作员");
|
|
|
|
|
operatorCol.setCellValueFactory(new PropertyValueFactory<>("operator"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> supplierCol = new TableColumn<>("供应商");
|
|
|
|
|
supplierCol.setCellValueFactory(new PropertyValueFactory<>("supplier"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, LocalDateTime> timeCol = new TableColumn<>("入库时间");
|
|
|
|
|
timeCol.setCellValueFactory(new PropertyValueFactory<>("inboundTime"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> operatorCol = new TableColumn<>("操作员");
|
|
|
|
|
operatorCol.setCellValueFactory(new PropertyValueFactory<>("operator"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> remarkCol = new TableColumn<>("备注");
|
|
|
|
|
remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
|
|
|
|
|
|
|
|
|
|
table.getColumns().addAll(idCol, nameCol, priceCol, quantityCol,
|
|
|
|
|
inboundTimeCol, operatorCol, supplierCol, remarkCol);
|
|
|
|
|
table.getColumns().addAll(idCol, nameCol, quantityCol, supplierCol,
|
|
|
|
|
timeCol, operatorCol, remarkCol);
|
|
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
@ -151,15 +145,18 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
String url = "jdbc:sqlite:db/dbuml.db3";
|
|
|
|
|
List<Goods> goodsList = new ArrayList<>();
|
|
|
|
|
try (Connection conn = DriverManager.getConnection(url)) {
|
|
|
|
|
String sql = "SELECT goods_id, SUM(quantity) as total_quantity, supplier, inbound_time, operator, remark FROM warehouse GROUP BY goods_id";
|
|
|
|
|
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("goods_id"),
|
|
|
|
|
rs.getString("goods_id"), // Assuming goods_id is also the name in inbound_records
|
|
|
|
|
rs.getInt("total_quantity"),
|
|
|
|
|
rs.getString("id"),
|
|
|
|
|
rs.getString("name"),
|
|
|
|
|
rs.getInt("quantity"),
|
|
|
|
|
rs.getString("remark")
|
|
|
|
|
);
|
|
|
|
|
goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time")));
|
|
|
|
@ -179,28 +176,17 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
try {
|
|
|
|
|
String id = idField.getText().trim();
|
|
|
|
|
String name = nameField.getText().trim();
|
|
|
|
|
String priceText = priceField.getText().trim();
|
|
|
|
|
String quantityText = quantityField.getText().trim();
|
|
|
|
|
String supplier = supplierField.getText().trim();
|
|
|
|
|
String remark = remarkField.getText().trim();
|
|
|
|
|
|
|
|
|
|
// 输入验证
|
|
|
|
|
if (id.isEmpty() || name.isEmpty() || priceText.isEmpty() || quantityText.isEmpty()) {
|
|
|
|
|
showAlert("错误", "请填写货物ID、名称、价格和数量!");
|
|
|
|
|
if (id.isEmpty() || name.isEmpty() || quantityText.isEmpty() || supplier.isEmpty()) {
|
|
|
|
|
showAlert("错误", "请填写完整的货物信息!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double price;
|
|
|
|
|
int quantity;
|
|
|
|
|
try {
|
|
|
|
|
price = Double.parseDouble(priceText);
|
|
|
|
|
if (price <= 0) {
|
|
|
|
|
showAlert("错误", "价格必须大于0!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
showAlert("错误", "请输入有效的价格!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
quantity = Integer.parseInt(quantityText);
|
|
|
|
|
if (quantity <= 0) {
|
|
|
|
@ -216,18 +202,18 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
Goods existingGoods = DataManager.getInstance().getGoods(id);
|
|
|
|
|
if (existingGoods == null) {
|
|
|
|
|
// 插入新货物
|
|
|
|
|
Goods newGoods = new Goods(id, name, "", "", price, "", quantity, "");
|
|
|
|
|
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.setSpecification("");
|
|
|
|
|
existingGoods.setUnit("");
|
|
|
|
|
existingGoods.setPrice(price);
|
|
|
|
|
existingGoods.setCategory("");
|
|
|
|
|
existingGoods.setQuantity(quantity);
|
|
|
|
|
existingGoods.setSupplier(supplier);
|
|
|
|
|
existingGoods.setRemark(remark);
|
|
|
|
|
DataManager.getInstance().updateGoodsInDatabase(existingGoods);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -241,22 +227,24 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
showAlert("错误", "保存失败:" + e.getMessage());
|
|
|
|
|
e.printStackTrace(); // 添加堆栈跟踪以便调试
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleClear() {
|
|
|
|
|
idField.clear();
|
|
|
|
|
nameField.clear();
|
|
|
|
|
priceField.clear();
|
|
|
|
|
quantityField.clear();
|
|
|
|
|
supplierField.clear();
|
|
|
|
|
remarkField.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fillFields(Goods goods) {
|
|
|
|
|
idField.setText(goods.getId());
|
|
|
|
|
nameField.setText(goods.getName());
|
|
|
|
|
priceField.setText(String.valueOf(goods.getPrice()));
|
|
|
|
|
quantityField.setText(String.valueOf(goods.getQuantity()));
|
|
|
|
|
supplierField.setText(goods.getSupplier());
|
|
|
|
|
remarkField.setText(goods.getRemark());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showAlert(String title, String content) {
|
|
|
|
|