feat(production): 删除动作记录、释放料位、删除工件及订单等
This commit is contained in:
+3
-3
@@ -8,7 +8,7 @@ password=hardman
|
||||
connection=ssp_postgres
|
||||
|
||||
[modbus]
|
||||
host=127.0.0.1
|
||||
host=192.168.60.10
|
||||
port=502
|
||||
serverAddress=1
|
||||
timeoutMs=1000
|
||||
@@ -45,8 +45,8 @@ inspectionCommand=302
|
||||
inspectionFeedback=312
|
||||
frontUnloadCommand=802
|
||||
frontUnloadFeedback=809
|
||||
flipCommand=804
|
||||
flipFeedback=811
|
||||
flipCommand=840
|
||||
flipFeedback=842
|
||||
backLoadCommand=806
|
||||
backLoadFeedback=813
|
||||
backUnloadCommand=805
|
||||
|
||||
@@ -292,6 +292,8 @@ void OrderPage::deleteSelectedOrder()
|
||||
const QString orderNo = ui->orderTable->item(row, 0)
|
||||
? ui->orderTable->item(row, 0)->text() : QString::number(orderIds.at(row));
|
||||
if (QMessageBox::question(this, "删除订单",
|
||||
"仅未开始或已取消且无测量记录的订单可以删除。\n"
|
||||
"删除会清除其未测量工件、动作记录并释放料位。\n\n"
|
||||
"确认删除订单:" + orderNo + "?") != QMessageBox::Yes) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QStringList>
|
||||
#include <QVariant>
|
||||
|
||||
namespace {
|
||||
@@ -156,15 +157,17 @@ bool OrderRepository::insertOrder(Order *order, QString *error)
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
const QSqlDatabase database = DatabaseManager::instance().database();
|
||||
const bool sqlite = DatabaseManager::instance().config().driverName == "QSQLITE";
|
||||
QSqlQuery query(database);
|
||||
query.prepare("insert into orders "
|
||||
"(no, production_no, drawing_no, batch_no, plan_num, qualified, "
|
||||
"unqualified, status, inspection_mode, start_tray_no, start_slot_no, "
|
||||
"begin_time, end_time, remark) "
|
||||
"values (:no, :production_no, :drawing_no, :batch_no, :plan_num, "
|
||||
":qualified, :unqualified, :status, :inspection_mode, "
|
||||
":start_tray_no, :start_slot_no, :begin_time, :end_time, :remark) "
|
||||
"returning id");
|
||||
":start_tray_no, :start_slot_no, :begin_time, :end_time, :remark)"
|
||||
+ (sqlite ? QString() : QString(" returning id")));
|
||||
query.bindValue(":no", order->no);
|
||||
query.bindValue(":production_no", order->productionNo);
|
||||
query.bindValue(":drawing_no", order->drawingNo);
|
||||
@@ -180,11 +183,19 @@ bool OrderRepository::insertOrder(Order *order, QString *error)
|
||||
query.bindValue(":end_time", order->endTime.isValid() ? QVariant(order->endTime) : QVariant(QVariant::DateTime));
|
||||
query.bindValue(":remark", order->remark);
|
||||
|
||||
if (!query.exec() || !query.next()) {
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
}
|
||||
order->id = query.value(0).toInt();
|
||||
if (sqlite) {
|
||||
order->id = query.lastInsertId().toInt();
|
||||
} else {
|
||||
if (!query.next()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
}
|
||||
order->id = query.value(0).toInt();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -257,12 +268,19 @@ bool OrderRepository::removeOrder(int id, QString *error)
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("delete from orders where id = :id");
|
||||
query.bindValue(":id", id);
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
const QStringList statements = QStringList()
|
||||
<< "delete from production_state_history where order_id = :id"
|
||||
<< "delete from production_runs where order_id = :id"
|
||||
<< "delete from alarm_records where order_id = :id"
|
||||
<< "delete from orders where id = :id";
|
||||
for (const QString &statement : statements) {
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare(statement);
|
||||
query.bindValue(":id", id);
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QStringList>
|
||||
#include <QVariant>
|
||||
|
||||
namespace {
|
||||
@@ -202,3 +203,100 @@ bool WorkpieceRepository::updateState(int id, const QString &state, const QStrin
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WorkpieceRepository::orderHasInspectionRecords(int orderId, bool *hasRecords,
|
||||
QString *error)
|
||||
{
|
||||
if (orderId <= 0 || !hasRecords || !ensureOpen(error)) {
|
||||
if (error && orderId <= 0) {
|
||||
*error = "Order id must be greater than 0";
|
||||
} else if (error && !hasRecords) {
|
||||
*error = "Inspection record result pointer is null";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("select exists (select 1 from inspection_records i "
|
||||
"left join workpieces w on w.id = i.workpiece_id "
|
||||
"where i.order_id = :order_id or w.order_id = :order_id)");
|
||||
query.bindValue(":order_id", orderId);
|
||||
if (!query.exec() || !query.next()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
}
|
||||
*hasRecords = query.value(0).toBool();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WorkpieceRepository::removeUninspected(int orderId,
|
||||
QList<int> *affectedTrayIds,
|
||||
int *removedCount,
|
||||
QString *error)
|
||||
{
|
||||
if (!affectedTrayIds || !removedCount || !ensureOpen(error)) {
|
||||
if (error && !affectedTrayIds) {
|
||||
*error = "Affected tray result pointer is null";
|
||||
} else if (error && !removedCount) {
|
||||
*error = "Removed count result pointer is null";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
affectedTrayIds->clear();
|
||||
*removedCount = 0;
|
||||
const QString scope = orderId > 0 ? " and w.order_id = :order_id" : QString();
|
||||
const QString deleteScope = orderId > 0
|
||||
? " and workpieces.order_id = :order_id" : QString();
|
||||
const QString candidates =
|
||||
"select w.id from workpieces w "
|
||||
"where not exists (select 1 from inspection_records i "
|
||||
"where i.workpiece_id = w.id)" + scope;
|
||||
|
||||
QSqlQuery trays(DatabaseManager::instance().database());
|
||||
trays.prepare("select distinct w.tray_id from workpieces w "
|
||||
"where w.tray_id is not null and not exists "
|
||||
"(select 1 from inspection_records i where i.workpiece_id = w.id)"
|
||||
+ scope);
|
||||
if (orderId > 0) trays.bindValue(":order_id", orderId);
|
||||
if (!trays.exec()) {
|
||||
setError(trays, error);
|
||||
return false;
|
||||
}
|
||||
while (trays.next()) {
|
||||
affectedTrayIds->append(trays.value(0).toInt());
|
||||
}
|
||||
|
||||
const QStringList statements = QStringList()
|
||||
<< "update production_runs set current_workpiece_id = null "
|
||||
"where current_workpiece_id in (" + candidates + ")"
|
||||
<< "update alarm_records set workpiece_id = null "
|
||||
"where workpiece_id in (" + candidates + ")"
|
||||
<< "delete from production_state_history where workpiece_id in ("
|
||||
+ candidates + ")"
|
||||
<< "delete from workpiece_process_records where workpiece_id in ("
|
||||
+ candidates + ")"
|
||||
<< "update tray_slots set workpiece_id = null, state = 'EMPTY' "
|
||||
"where workpiece_id in (" + candidates + ")";
|
||||
for (const QString &statement : statements) {
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare(statement);
|
||||
if (orderId > 0) query.bindValue(":order_id", orderId);
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QSqlQuery remove(DatabaseManager::instance().database());
|
||||
remove.prepare("delete from workpieces where not exists "
|
||||
"(select 1 from inspection_records i "
|
||||
"where i.workpiece_id = workpieces.id)" + deleteScope);
|
||||
if (orderId > 0) remove.bindValue(":order_id", orderId);
|
||||
if (!remove.exec()) {
|
||||
setError(remove, error);
|
||||
return false;
|
||||
}
|
||||
*removedCount = remove.numRowsAffected();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ public:
|
||||
QString *error = nullptr);
|
||||
bool updateState(int id, const QString &state, const QString &processStep,
|
||||
QString *error = nullptr);
|
||||
bool orderHasInspectionRecords(int orderId, bool *hasRecords,
|
||||
QString *error = nullptr);
|
||||
bool removeUninspected(int orderId, QList<int> *affectedTrayIds,
|
||||
int *removedCount, QString *error = nullptr);
|
||||
};
|
||||
|
||||
#endif // WORKPIECEREPOSITORY_H
|
||||
|
||||
+102
-7
@@ -124,12 +124,58 @@ bool OrderService::createOrder(Order *order, QString *error)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!repository.insertOrder(order, &transactionError)
|
||||
|| !recordLog("新建订单", "创建订单:" + order->no, "成功", &transactionError)
|
||||
|| !database.commit(&transactionError)) {
|
||||
WorkpieceRepository workpieceRepository;
|
||||
TrayRepository trayRepository;
|
||||
QList<int> affectedTrayIds;
|
||||
int removedCount = 0;
|
||||
if (!workpieceRepository.removeUninspected(
|
||||
0, &affectedTrayIds, &removedCount, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
*error = "新建订单前清理未测量工件失败:" + transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
for (const int trayId : affectedTrayIds) {
|
||||
if (!trayRepository.refreshStats(trayId, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "重新统计料盘占用失败:" + transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!repository.insertOrder(order, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "写入新订单失败:" + transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (removedCount > 0
|
||||
&& !recordLog("清理未测量工件",
|
||||
QString("新建订单前清理 %1 个未测量工件")
|
||||
.arg(removedCount),
|
||||
"成功", &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "记录清理日志失败:" + transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!recordLog("新建订单", "创建订单:" + order->no,
|
||||
"成功", &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "记录新建订单日志失败:" + transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!database.commit(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "提交新建订单事务失败:" + transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -454,16 +500,65 @@ bool OrderService::deleteOrder(int id, QString *error)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (order.status == "RUNNING" || order.status == "PAUSED") {
|
||||
if (order.status == "FINISHED" || order.status == "COMPLETED") {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "Running or paused orders cannot be deleted";
|
||||
*error = "已完成订单不能删除";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (order.status != "NOT_START" && order.status != "CANCELED") {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "当前订单不能直接删除,请先在控制面板安全终止订单";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
WorkpieceRepository workpieceRepository;
|
||||
bool hasInspectionRecords = false;
|
||||
if (!workpieceRepository.orderHasInspectionRecords(
|
||||
id, &hasInspectionRecords, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (hasInspectionRecords) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "订单已有测量记录,必须保留检测追溯,不能删除";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<int> affectedTrayIds;
|
||||
int removedCount = 0;
|
||||
if (!workpieceRepository.removeUninspected(
|
||||
id, &affectedTrayIds, &removedCount, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
TrayRepository trayRepository;
|
||||
for (const int trayId : affectedTrayIds) {
|
||||
if (!trayRepository.refreshStats(trayId, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!repository.removeOrder(id, &transactionError)
|
||||
|| !recordLog("删除订单", "删除订单:" + order.no, "成功", &transactionError)
|
||||
|| !recordLog("删除订单",
|
||||
QString("删除订单:%1,释放 %2 个未测量工件")
|
||||
.arg(order.no).arg(removedCount),
|
||||
"成功", &transactionError)
|
||||
|| !database.commit(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 4.11.0, 2026-10-30T17:56:26. -->
|
||||
<!-- Written by QtCreator 4.11.0, 2026-11-05T15:19:02. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
||||
Reference in New Issue
Block a user