库存信息修改功能

master
王城 7 months ago
parent 9f0b592da3
commit f9271f3a74

Binary file not shown.

@ -11,6 +11,7 @@ import model.Goods;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Statement; import java.sql.Statement;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -145,19 +146,16 @@ public class GoodsManagementView extends BorderPane {
String url = "jdbc:sqlite:db/dbuml.db3"; String url = "jdbc:sqlite:db/dbuml.db3";
List<Goods> goodsList = new ArrayList<>(); List<Goods> goodsList = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(url)) { try (Connection conn = DriverManager.getConnection(url)) {
String sql = "SELECT id, goods_id as name, quantity, " + String sql = "SELECT * FROM warehouse ORDER BY inbound_time DESC";
"supplier, inbound_time, operator, remark " +
"FROM warehouse " +
"ORDER BY id"; // 不再使用GROUP BY直接显示所有记录
try (Statement stmt = conn.createStatement(); try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) { ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) { while (rs.next()) {
Goods goods = new Goods( Goods goods = new Goods(
rs.getString("id"), rs.getString("id"),
rs.getString("name"), rs.getString("goods_id"),
rs.getInt("quantity"), rs.getInt("quantity"),
rs.getString("remark") rs.getString("remark")
); );
goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time"))); goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time")));
goods.setOperator(rs.getString("operator")); goods.setOperator(rs.getString("operator"));
@ -198,33 +196,41 @@ public class GoodsManagementView extends BorderPane {
return; return;
} }
// 查找货物对象 // 检查是否存在该记录
Goods existingGoods = DataManager.getInstance().getGoods(id); String url = "jdbc:sqlite:db/dbuml.db3";
if (existingGoods == null) { try (Connection conn = DriverManager.getConnection(url)) {
// 插入新货物 String checkSql = "SELECT * FROM warehouse WHERE id = ?";
Goods newGoods = new Goods(String.format("NO%014d", System.currentTimeMillis()), try (PreparedStatement pstmt = conn.prepareStatement(checkSql)) {
name, quantity, remark); pstmt.setString(1, id);
newGoods.setInboundTime(LocalDateTime.now()); ResultSet rs = pstmt.executeQuery();
newGoods.setOperator("当前用户");
newGoods.setSupplier(supplier); if (rs.next()) {
DataManager.getInstance().addGoodsToDatabase(newGoods); // 记录存在,执行更新操作
} else { String updateSql = "UPDATE warehouse SET goods_id = ?, quantity = ?, " +
// 更新现有货物 "supplier = ?, remark = ? WHERE id = ?";
existingGoods.setName(name); try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) {
existingGoods.setQuantity(quantity); updateStmt.setString(1, name);
existingGoods.setSupplier(supplier); updateStmt.setInt(2, quantity);
existingGoods.setRemark(remark); updateStmt.setString(3, supplier);
DataManager.getInstance().updateGoodsInDatabase(existingGoods); updateStmt.setString(4, remark);
updateStmt.setString(5, id);
updateStmt.executeUpdate();
showAlert("成功", "货物信息已更新!");
}
} else {
showAlert("错误", "未找到指定ID的货物记录");
return;
}
}
} }
// 更新表格 // 更新表格显示
loadGoodsData(); loadGoodsData();
// 清空输入 // 清空输入
handleClear(); handleClear();
showAlert("成功", "货物信息已更新!");
} catch (Exception e) { } catch (Exception e) {
showAlert("错误", "保存失败:" + e.getMessage()); showAlert("错误", "保存失败:" + e.getMessage());
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save