货物信息修改

master
王城 6 months ago
parent 68d54c3f1d
commit a62d0f6a25

Binary file not shown.

@ -11,8 +11,10 @@ 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.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -39,7 +41,7 @@ public class GoodsManagementView extends BorderPane {
tableView = createTableView(); tableView = createTableView();
setCenter(tableView); setCenter(tableView);
// 加载现有货数据 // 加载现有货<EFBFBD><EFBFBD>数据
loadGoodsData(); loadGoodsData();
// 添加双击事件填充文本字段 // 添加双击事件填充文本字段
@ -181,7 +183,12 @@ public class GoodsManagementView extends BorderPane {
String remark = remarkField.getText().trim(); String remark = remarkField.getText().trim();
// 输入验证 // 输入验证
if (id.isEmpty() || name.isEmpty() || quantityText.isEmpty() || supplier.isEmpty()) { if (id.isEmpty()) {
showAlert("错误", "请先选择要修改的货物记录!");
return;
}
if (name.isEmpty() || quantityText.isEmpty() || supplier.isEmpty()) {
showAlert("错误", "请填写完整的货物信息!"); showAlert("错误", "请填写完整的货物信息!");
return; return;
} }
@ -198,36 +205,57 @@ public class GoodsManagementView extends BorderPane {
return; return;
} }
// 查找货物对象 String url = "jdbc:sqlite:db/dbuml.db3";
Goods existingGoods = DataManager.getInstance().getGoods(id); try (Connection conn = DriverManager.getConnection(url)) {
if (existingGoods == null) { // 修改SQL语句使用UPDATE而不是INSERT
// 插入新货物 String sql = "UPDATE warehouse SET " +
Goods newGoods = new Goods(String.format("NO%014d", System.currentTimeMillis()), "goods_id = ?, " +
name, quantity, remark); "quantity = ?, " +
newGoods.setInboundTime(LocalDateTime.now()); "supplier = ?, " +
newGoods.setOperator("当前用户"); "remark = ? " +
newGoods.setSupplier(supplier); "WHERE id = ?";
DataManager.getInstance().addGoodsToDatabase(newGoods);
} else { try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 更新现有货物 pstmt.setString(1, name);
existingGoods.setName(name); pstmt.setInt(2, quantity);
existingGoods.setQuantity(quantity); pstmt.setString(3, supplier);
existingGoods.setSupplier(supplier); pstmt.setString(4, remark);
existingGoods.setRemark(remark); pstmt.setString(5, id);
DataManager.getInstance().updateGoodsInDatabase(existingGoods);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
// 记录操作日志
String logSql = "INSERT INTO operation_logs " +
"(id, operation_type, operation_target, operator, operation_time, details) " +
"VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement logStmt = conn.prepareStatement(logSql)) {
String logId = "LOG" + System.currentTimeMillis();
logStmt.setString(1, logId);
logStmt.setString(2, "修改");
logStmt.setString(3, name);
logStmt.setString(4, "当前用户");
logStmt.setString(5, LocalDateTime.now().toString());
logStmt.setString(6, String.format("修改货物信息:%s数量%d供应商%s",
name, quantity, supplier));
logStmt.executeUpdate();
}
// 刷新表格
loadGoodsData();
handleClear();
showAlert("成功", "货物信息已更新!");
} else {
showAlert("错误", "未找到要修改的货物记录!");
}
}
} catch (SQLException e) {
e.printStackTrace();
showAlert("错误", "更新失败:" + e.getMessage());
} }
// 更新表格
loadGoodsData();
// 清空输入
handleClear();
showAlert("成功", "货物信息已更新!");
} catch (Exception e) { } catch (Exception e) {
showAlert("错误", "保存失败:" + e.getMessage());
e.printStackTrace(); e.printStackTrace();
showAlert("错误", "保存失败:" + e.getMessage());
} }
} }

Loading…
Cancel
Save