货物信息修改

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.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@ -39,7 +41,7 @@ public class GoodsManagementView extends BorderPane {
tableView = createTableView();
setCenter(tableView);
// 加载现有货数据
// 加载现有货<EFBFBD><EFBFBD>数据
loadGoodsData();
// 添加双击事件填充文本字段
@ -181,7 +183,12 @@ public class GoodsManagementView extends BorderPane {
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("错误", "请填写完整的货物信息!");
return;
}
@ -198,36 +205,57 @@ public class GoodsManagementView extends BorderPane {
return;
}
// 查找货物对象
Goods existingGoods = DataManager.getInstance().getGoods(id);
if (existingGoods == null) {
// 插入新货物
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.setQuantity(quantity);
existingGoods.setSupplier(supplier);
existingGoods.setRemark(remark);
DataManager.getInstance().updateGoodsInDatabase(existingGoods);
String url = "jdbc:sqlite:db/dbuml.db3";
try (Connection conn = DriverManager.getConnection(url)) {
// 修改SQL语句使用UPDATE而不是INSERT
String sql = "UPDATE warehouse SET " +
"goods_id = ?, " +
"quantity = ?, " +
"supplier = ?, " +
"remark = ? " +
"WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setInt(2, quantity);
pstmt.setString(3, supplier);
pstmt.setString(4, remark);
pstmt.setString(5, id);
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());
}
} catch (Exception e) {
showAlert("错误", "保存失败:" + e.getMessage());
e.printStackTrace();
showAlert("错误", "保存失败:" + e.getMessage());
}
}

Loading…
Cancel
Save