|
|
|
@ -8,20 +8,20 @@ import javafx.scene.layout.*;
|
|
|
|
|
import javafx.scene.control.cell.PropertyValueFactory;
|
|
|
|
|
import model.DataManager;
|
|
|
|
|
import model.Goods;
|
|
|
|
|
import model.InventoryManager;
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.Statement;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class GoodsManagementView extends BorderPane {
|
|
|
|
|
|
|
|
|
|
private TextField idField;
|
|
|
|
|
private TextField nameField;
|
|
|
|
|
private TextField specificationField;
|
|
|
|
|
private TextField unitField;
|
|
|
|
|
private TextField priceField;
|
|
|
|
|
private TextField categoryField;
|
|
|
|
|
private TextField quantityField;
|
|
|
|
|
private TableView<Goods> tableView;
|
|
|
|
|
|
|
|
|
@ -64,31 +64,17 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
Label idLabel = new Label("货物ID:");
|
|
|
|
|
idField = new TextField();
|
|
|
|
|
idField.setPromptText("请输入货物ID");
|
|
|
|
|
idField.setEditable(false); // ID 不可编辑
|
|
|
|
|
|
|
|
|
|
Label nameLabel = new Label("货物名称:");
|
|
|
|
|
nameField = new TextField();
|
|
|
|
|
nameField.setPromptText("请输入货物名称");
|
|
|
|
|
|
|
|
|
|
Label specificationLabel = new Label("规格:");
|
|
|
|
|
specificationField = new TextField();
|
|
|
|
|
specificationField.setPromptText("请输入规格");
|
|
|
|
|
|
|
|
|
|
Label unitLabel = new Label("单位:");
|
|
|
|
|
unitField = new TextField();
|
|
|
|
|
unitField.setPromptText("请输入单位");
|
|
|
|
|
|
|
|
|
|
Label priceLabel = new Label("价格:");
|
|
|
|
|
priceField = new TextField();
|
|
|
|
|
priceField.setPromptText("请输入价格");
|
|
|
|
|
|
|
|
|
|
Label categoryLabel = new Label("类别:");
|
|
|
|
|
categoryField = new TextField();
|
|
|
|
|
categoryField.setPromptText("请输入类别");
|
|
|
|
|
|
|
|
|
|
basicInfo.addRow(0, idLabel, idField, nameLabel, nameField);
|
|
|
|
|
basicInfo.addRow(1, specificationLabel, specificationField, unitLabel, unitField);
|
|
|
|
|
basicInfo.addRow(2, priceLabel, priceField, categoryLabel, categoryField);
|
|
|
|
|
basicInfo.addRow(1, priceLabel, priceField);
|
|
|
|
|
|
|
|
|
|
// 库存信息
|
|
|
|
|
GridPane stockInfo = new GridPane();
|
|
|
|
@ -106,12 +92,8 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
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);
|
|
|
|
|
stockInfo.getColumnConstraints().addAll(column1, column2);
|
|
|
|
|
|
|
|
|
|
// 操作按钮
|
|
|
|
@ -140,18 +122,9 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
TableColumn<Goods, String> nameCol = new TableColumn<>("货物名称");
|
|
|
|
|
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> specificationCol = new TableColumn<>("规格");
|
|
|
|
|
specificationCol.setCellValueFactory(new PropertyValueFactory<>("specification"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> unitCol = new TableColumn<>("单位");
|
|
|
|
|
unitCol.setCellValueFactory(new PropertyValueFactory<>("unit"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, Double> priceCol = new TableColumn<>("价格");
|
|
|
|
|
priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, String> categoryCol = new TableColumn<>("类别");
|
|
|
|
|
categoryCol.setCellValueFactory(new PropertyValueFactory<>("category"));
|
|
|
|
|
|
|
|
|
|
TableColumn<Goods, Integer> quantityCol = new TableColumn<>("数量");
|
|
|
|
|
quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));
|
|
|
|
|
|
|
|
|
@ -168,14 +141,37 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
TableColumn<Goods, String> remarkCol = new TableColumn<>("备注");
|
|
|
|
|
remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
|
|
|
|
|
|
|
|
|
|
table.getColumns().addAll(idCol, nameCol, specificationCol, unitCol, priceCol, categoryCol, quantityCol,
|
|
|
|
|
table.getColumns().addAll(idCol, nameCol, priceCol, quantityCol,
|
|
|
|
|
inboundTimeCol, operatorCol, supplierCol, remarkCol);
|
|
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadGoodsData() {
|
|
|
|
|
List<Goods> goodsList = DataManager.getInstance().getAllGoods();
|
|
|
|
|
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";
|
|
|
|
|
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("remark")
|
|
|
|
|
);
|
|
|
|
|
goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time")));
|
|
|
|
|
goods.setOperator(rs.getString("operator"));
|
|
|
|
|
goods.setSupplier(rs.getString("supplier"));
|
|
|
|
|
goodsList.add(goods);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
showAlert("错误", "加载货物信息失败:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
tableView.setItems(FXCollections.observableArrayList(goodsList));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -183,16 +179,12 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
try {
|
|
|
|
|
String id = idField.getText().trim();
|
|
|
|
|
String name = nameField.getText().trim();
|
|
|
|
|
String specification = specificationField.getText().trim();
|
|
|
|
|
String unit = unitField.getText().trim();
|
|
|
|
|
String priceText = priceField.getText().trim();
|
|
|
|
|
String category = categoryField.getText().trim();
|
|
|
|
|
String quantityText = quantityField.getText().trim();
|
|
|
|
|
|
|
|
|
|
// 输入验证
|
|
|
|
|
if (id.isEmpty() || name.isEmpty() || specification.isEmpty() || unit.isEmpty() ||
|
|
|
|
|
priceText.isEmpty() || category.isEmpty() || quantityText.isEmpty()) {
|
|
|
|
|
showAlert("错误", "请填写完整的货物信息!");
|
|
|
|
|
if (id.isEmpty() || name.isEmpty() || priceText.isEmpty() || quantityText.isEmpty()) {
|
|
|
|
|
showAlert("错误", "请填写货物ID、名称、价格和数量!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -223,20 +215,24 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
// 查找货物对象
|
|
|
|
|
Goods existingGoods = DataManager.getInstance().getGoods(id);
|
|
|
|
|
if (existingGoods == null) {
|
|
|
|
|
showAlert("错误", "货物ID不存在!");
|
|
|
|
|
return;
|
|
|
|
|
// 插入新货物
|
|
|
|
|
Goods newGoods = new Goods(id, name, "", "", price, "", quantity, "");
|
|
|
|
|
newGoods.setInboundTime(LocalDateTime.now());
|
|
|
|
|
newGoods.setOperator("当前用户");
|
|
|
|
|
DataManager.getInstance().addGoodsToDatabase(newGoods);
|
|
|
|
|
} else {
|
|
|
|
|
// 更新现有货物
|
|
|
|
|
existingGoods.setName(name);
|
|
|
|
|
existingGoods.setSpecification("");
|
|
|
|
|
existingGoods.setUnit("");
|
|
|
|
|
existingGoods.setPrice(price);
|
|
|
|
|
existingGoods.setCategory("");
|
|
|
|
|
existingGoods.setQuantity(quantity);
|
|
|
|
|
DataManager.getInstance().updateGoodsInDatabase(existingGoods);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新货物对象
|
|
|
|
|
existingGoods.setName(name);
|
|
|
|
|
existingGoods.setSpecification(specification);
|
|
|
|
|
existingGoods.setUnit(unit);
|
|
|
|
|
existingGoods.setPrice(price);
|
|
|
|
|
existingGoods.setCategory(category);
|
|
|
|
|
existingGoods.setQuantity(quantity);
|
|
|
|
|
|
|
|
|
|
// 更新表格
|
|
|
|
|
tableView.refresh();
|
|
|
|
|
loadGoodsData();
|
|
|
|
|
|
|
|
|
|
// 清空输入
|
|
|
|
|
handleClear();
|
|
|
|
@ -245,26 +241,21 @@ public class GoodsManagementView extends BorderPane {
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
showAlert("错误", "保存失败:" + e.getMessage());
|
|
|
|
|
e.printStackTrace(); // 添加堆栈跟踪以便调试
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleClear() {
|
|
|
|
|
idField.clear();
|
|
|
|
|
nameField.clear();
|
|
|
|
|
specificationField.clear();
|
|
|
|
|
unitField.clear();
|
|
|
|
|
priceField.clear();
|
|
|
|
|
categoryField.clear();
|
|
|
|
|
quantityField.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fillFields(Goods goods) {
|
|
|
|
|
idField.setText(goods.getId());
|
|
|
|
|
nameField.setText(goods.getName());
|
|
|
|
|
specificationField.setText(goods.getSpecification());
|
|
|
|
|
unitField.setText(goods.getUnit());
|
|
|
|
|
priceField.setText(String.valueOf(goods.getPrice()));
|
|
|
|
|
categoryField.setText(goods.getCategory());
|
|
|
|
|
quantityField.setText(String.valueOf(goods.getQuantity()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|