feat(production): 修改删除订单问题

This commit is contained in:
lidg
2026-11-09 09:22:03 +08:00
parent 81313e61b0
commit 6a70f1943f
7 changed files with 138 additions and 144 deletions
+1 -1
View File
@@ -475,7 +475,7 @@ void DashboardPage::stopProduction()
const int confirmedRunId = active ? productionSnapshot.runId : 0; const int confirmedRunId = active ? productionSnapshot.runId : 0;
const int confirmedOrderId = active ? productionSnapshot.orderId : selectedOrderId; const int confirmedOrderId = active ? productionSnapshot.orderId : selectedOrderId;
if (!stopButton->isEnabled() if (!stopButton->isEnabled()
|| !confirmControlOperation("终止订单", "将当前订单标记为已完成,保留实际加工数量及记录,剩余工件不再执行。此操作不是机器人急停。")) return; || !confirmControlOperation("终止订单", "终止后剩余工件不再执行。无测量记录时自动删除订单、关联工件及动作记录,并释放料位;已有测量记录时保留订单和历史数据。此操作不是机器人急停。")) return;
if (recoveryDialog) recoveryDialog->reject(); if (recoveryDialog) recoveryDialog->reject();
QMetaObject::invokeMethod(productionWorker, "requestStopOrder", QMetaObject::invokeMethod(productionWorker, "requestStopOrder",
Qt::QueuedConnection, Q_ARG(int, confirmedRunId), Qt::QueuedConnection, Q_ARG(int, confirmedRunId),
+2 -1
View File
@@ -292,7 +292,8 @@ void OrderPage::deleteSelectedOrder()
const QString orderNo = ui->orderTable->item(row, 0) const QString orderNo = ui->orderTable->item(row, 0)
? ui->orderTable->item(row, 0)->text() : QString::number(orderIds.at(row)); ? ui->orderTable->item(row, 0)->text() : QString::number(orderIds.at(row));
if (QMessageBox::question(this, "删除订单", if (QMessageBox::question(this, "删除订单",
"仅未开始或已取消且无测量记录的订单可以删除。\n" "仅未开始、已取消或手动终止,且无测量记录的订单可以删除。\n"
"正常完成的订单不能删除。\n"
"删除会清除其未测量工件、动作记录并释放料位。\n\n" "删除会清除其未测量工件、动作记录并释放料位。\n\n"
"确认删除订单:" + orderNo + "?") != QMessageBox::Yes) { "确认删除订单:" + orderNo + "?") != QMessageBox::Yes) {
return; return;
+1
View File
@@ -269,6 +269,7 @@ bool OrderRepository::removeOrder(int id, QString *error)
} }
const QStringList statements = QStringList() const QStringList statements = QStringList()
<< "delete from workpiece_process_records where order_id = :id"
<< "delete from production_state_history where order_id = :id" << "delete from production_state_history where order_id = :id"
<< "delete from production_runs where order_id = :id" << "delete from production_runs where order_id = :id"
<< "delete from alarm_records where order_id = :id" << "delete from alarm_records where order_id = :id"
+46 -68
View File
@@ -582,91 +582,69 @@ void DeviceService::beginProductionAction(
} }
m_productionActionBusy = true; m_productionActionBusy = true;
inspectProductionAction( auto sendCommand =
action, [this, callback](VisionWriteOutcome outcome, const QString &error) {
[this, action, trayNo, slotNo, addresses, callback, beforeSend]( // An unacknowledged command may already be running. Do not
bool success, const ProductionActionStatus &status, // permit another action until it has been reconciled.
const QString &error) { m_productionActionActive = outcome != VisionWriteOutcome::NotSent;
if (!success) { m_productionActionBusy = false;
m_productionActionBusy = false; if (callback) {
if (callback) { callback(outcome, error);
callback(VisionWriteOutcome::NotSent, error);
}
return;
}
if (status.command || status.feedback) {
m_productionActionBusy = false;
if (callback) {
callback(VisionWriteOutcome::NotSent,
"PLC command or feedback is still active before a new action");
}
return;
} }
};
auto sendCommand = auto writeCommand = [this, sendCommand, beforeSend](bool success,
[this, callback](VisionWriteOutcome outcome, const QString &error) { const QString &error) {
// An unacknowledged command may already be running. Do not if (!success) {
// permit another action until it has been reconciled. sendCommand(VisionWriteOutcome::NotSent, error);
m_productionActionActive = outcome != VisionWriteOutcome::NotSent; return;
m_productionActionBusy = false; }
if (callback) { // Action commands must never use the legacy automatic retry
callback(outcome, error); // path, even when no persistence hook is supplied.
} m_modbus->writeVisionCoil(m_productionCommandAddress, true,
}; sendCommand, beforeSend);
};
auto writeCommand = [this, sendCommand, beforeSend](bool success, if (action == PhotoMoveAction || action == RackPick
const QString &error) { || action == RackPlace) {
if (!success) { m_modbus->writeHoldingRegister(
sendCommand(VisionWriteOutcome::NotSent, error); addresses.trayNumber,
static_cast<quint16>(trayNo),
[this, addresses, slotNo, writeCommand](
bool traySuccess, const QString &trayError) {
if (!traySuccess) {
writeCommand(false, trayError);
return; return;
} }
// Action commands must never use the legacy automatic retry
// path, even when no persistence hook is supplied.
m_modbus->writeVisionCoil(m_productionCommandAddress, true,
sendCommand, beforeSend);
};
if (action == PhotoMoveAction || action == RackPick
|| action == RackPlace) {
m_modbus->writeHoldingRegister( m_modbus->writeHoldingRegister(
addresses.trayNumber, addresses.slotNumber,
static_cast<quint16>(trayNo), static_cast<quint16>(slotNo),
[this, addresses, slotNo, writeCommand]( [this, addresses, slotNo, writeCommand](
bool traySuccess, const QString &trayError) { bool slotSuccess, const QString &slotError) {
if (!traySuccess) { if (!slotSuccess) {
writeCommand(false, trayError); writeCommand(false, slotError);
return; return;
} }
m_modbus->writeHoldingRegister( m_modbus->writeHoldingRegister(
addresses.slotNumber, addresses.slotRowNumber,
static_cast<quint16>(slotNo), static_cast<quint16>((slotNo - 1) / 7 + 1),
[this, addresses, slotNo, writeCommand]( [this, addresses, slotNo, writeCommand](
bool slotSuccess, const QString &slotError) { bool rowSuccess, const QString &rowError) {
if (!slotSuccess) { if (!rowSuccess) {
writeCommand(false, slotError); writeCommand(false, rowError);
return; return;
} }
m_modbus->writeHoldingRegister( m_modbus->writeHoldingRegister(
addresses.slotRowNumber, addresses.slotColumNumber,
static_cast<quint16>((slotNo - 1) / 7 + 1), static_cast<quint16>((slotNo - 1) % 7 + 1),
[this, addresses, slotNo, writeCommand]( writeCommand);
bool rowSuccess, const QString &rowError) {
if (!rowSuccess) {
writeCommand(false, rowError);
return;
}
m_modbus->writeHoldingRegister(
addresses.slotColumNumber,
static_cast<quint16>((slotNo - 1) % 7 + 1),
writeCommand);
});
}); });
}); });
return; });
} return;
}
writeCommand(true, QString()); writeCommand(true, QString());
});
} }
void DeviceService::inspectProductionAction( void DeviceService::inspectProductionAction(
+1 -1
View File
@@ -515,7 +515,7 @@ bool OrderService::deleteOrder(int id, QString *error)
if (hasActiveRun) { if (hasActiveRun) {
database.rollback(); database.rollback();
if (error) { if (error) {
*error = "订单存在活动生产任务,请先在控制面板终止订单,终止完成后再删除"; *error = "订单存在活动生产任务,请先在控制面板终止订单;终止完成后,无测量记录的订单会自动删除";
} }
return false; return false;
} }
+86 -72
View File
@@ -3,6 +3,8 @@
#include "../config/configmanager.h" #include "../config/configmanager.h"
#include "../repositories/productionrepository.h" #include "../repositories/productionrepository.h"
#include "../repositories/orderrepository.h" #include "../repositories/orderrepository.h"
#include "../repositories/workpiecerepository.h"
#include "../repositories/trayrepository.h"
#include "../repositories/logrepository.h" #include "../repositories/logrepository.h"
#include "../db/databasemanager.h" #include "../db/databasemanager.h"
#include "orderservice.h" #include "orderservice.h"
@@ -855,7 +857,14 @@ bool ProductionService::stopOrder(int orderId, QString *error)
if (error) *error = "订单存在待恢复的生产记录或状态已改变,请先核对再终止"; if (error) *error = "订单存在待恢复的生产记录或状态已改变,请先核对再终止";
return false; return false;
} }
if (!OrderService::instance().terminateOrder(orderId, error)) return false; bool hasInspectionRecords = false;
if (!WorkpieceRepository().orderHasInspectionRecords(orderId, &hasInspectionRecords, error))
return false;
if (hasInspectionRecords) {
if (!OrderService::instance().terminateOrder(orderId, error)) return false;
} else if (!OrderService::instance().deleteOrder(orderId, error)) {
return false;
}
publishState(); publishState();
return true; return true;
} }
@@ -1275,6 +1284,16 @@ bool ProductionService::recover(QString *error)
emit recoveryBusyChanged(true); emit recoveryBusyChanged(true);
publishVisionControl(); publishVisionControl();
const quint64 generation = ++m_recoveryGeneration; const quint64 generation = ++m_recoveryGeneration;
if (m_recoveryIdleUnlock) {
// 解除视觉遗留锁:跳过 PLC 状态读取与安全校验,直接进入人工确认
QString snapshotError;
if (!loadRecoverySnapshot(&snapshotError)) {
finishRecoveryCheck(snapshotError);
return true;
}
checkVisionRecovery();
return true;
}
m_deviceService->requestStatus( m_deviceService->requestStatus(
[this, generation](bool success, const DeviceStatus &, const QString &statusError) { [this, generation](bool success, const DeviceStatus &, const QString &statusError) {
if (generation != m_recoveryGeneration || stopRequested()) return; if (generation != m_recoveryGeneration || stopRequested()) return;
@@ -1427,6 +1446,14 @@ bool ProductionService::submitRecoveryDecision(const QJsonObject &decision, QStr
m_manualContext = QJsonObject(); m_manualContext = QJsonObject();
const quint64 generation = m_recoveryGeneration; const quint64 generation = m_recoveryGeneration;
const ProductionActionCheckpoint expected = m_checkpoint; const ProductionActionCheckpoint expected = m_checkpoint;
if (m_recoveryIdleUnlock) {
// 解除视觉遗留锁:不读 PLC,直接执行解锁
QString error;
if (!loadRecoverySnapshot(&error)) { finishRecoveryCheck(error); return true; }
if (choice == "UNLOCK_VISION") { checkVisionRecovery(decision); return true; }
finishRecoveryCheck("无效的解锁选择");
return true;
}
m_deviceService->requestStatus( m_deviceService->requestStatus(
[this, generation, expected, decision, choice](bool success, const DeviceStatus &, const QString &statusError) { [this, generation, expected, decision, choice](bool success, const DeviceStatus &, const QString &statusError) {
if (generation != m_recoveryGeneration || stopRequested()) return; if (generation != m_recoveryGeneration || stopRequested()) return;
@@ -1496,6 +1523,29 @@ void ProductionService::checkVisionRecovery(const QJsonObject &decision)
if (m_deviceService->productionActionBusy() || m_visionIoPending) { if (m_deviceService->productionActionBusy() || m_visionIoPending) {
finishRecoveryCheck("仍有 PLC 写入在途,不能重测或覆盖取料模式"); return; finishRecoveryCheck("仍有 PLC 写入在途,不能重测或覆盖取料模式"); return;
} }
if (m_recoveryIdleUnlock) {
if (decision.isEmpty()) {
requestManualDecision(VisionProcessing, ProductionActionStatus(),
"解除视觉遗留锁:跳过 PLC 信号校验,确认后直接清锁,不恢复订单",
QStringList{"UNLOCK_VISION"});
return;
}
QString error;
if (!saveManualDecision(decision, &error)) { finishRecoveryCheck(error); return; }
++m_visionGeneration;
m_deviceService->cancelVisionRequests("人工解除遗留锁,作废旧视觉写入");
m_targetReady = false;
if (!saveVisionRecoveryState(false, &error)) { finishRecoveryCheck(error); return; }
m_visionNeedsReconcile = false;
m_visionSuspended = false;
m_visionStage = VisionStage::Idle;
m_lastError.clear();
reportVision("视觉锁已解除", "旧任务已清理,可启动未开始订单", true);
publishState();
publishVisionControl();
finishRecoveryCheck();
return;
}
m_deviceService->inspectProductionAction(DeviceService::PhotoMoveAction, m_deviceService->inspectProductionAction(DeviceService::PhotoMoveAction,
[this, generation, decision](bool photoRead, const ProductionActionStatus &photo, const QString &photoError) { [this, generation, decision](bool photoRead, const ProductionActionStatus &photo, const QString &photoError) {
if (generation != m_recoveryGeneration || stopRequested()) return; if (generation != m_recoveryGeneration || stopRequested()) return;
@@ -1503,11 +1553,11 @@ void ProductionService::checkVisionRecovery(const QJsonObject &decision)
const QString photoSignals = QString("拍照命令 M%1=%2,到位反馈 M%3=%4") const QString photoSignals = QString("拍照命令 M%1=%2,到位反馈 M%3=%4")
.arg(config.photoMoveCoil).arg(int(photo.command)) .arg(config.photoMoveCoil).arg(int(photo.command))
.arg(config.photoMoveCoilFeedback).arg(int(photo.feedback)); .arg(config.photoMoveCoilFeedback).arg(int(photo.feedback));
if (!photoRead || photo.command || (photo.feedback && !m_recoveryIdleUnlock)) { if (!photoRead || photo.command || photo.feedback) {
finishRecoveryCheck(photoRead ? photoSignals + ";拍照位握手未复位,请先核对动作" : photoError); return; finishRecoveryCheck(photoRead ? photoSignals + ";拍照位握手未复位,请先核对动作" : photoError); return;
} }
m_deviceService->inspectProductionAction(DeviceService::RackPick, m_deviceService->inspectProductionAction(DeviceService::RackPick,
[this, generation, decision, photo, photoSignals](bool read, const ProductionActionStatus &status, const QString &readError) { [this, generation, decision](bool read, const ProductionActionStatus &status, const QString &readError) {
if (generation != m_recoveryGeneration || stopRequested()) return; if (generation != m_recoveryGeneration || stopRequested()) return;
const ModbusAddressMap addresses = ConfigManager::instance().modbusConfig().addressMap; const ModbusAddressMap addresses = ConfigManager::instance().modbusConfig().addressMap;
const QString pickSignals = QString("取料命令 M%1=%2,取料反馈 M%3=%4") const QString pickSignals = QString("取料命令 M%1=%2,取料反馈 M%3=%4")
@@ -1530,21 +1580,17 @@ void ProductionService::checkVisionRecovery(const QJsonObject &decision)
|| (m_currentWorkpiece.processStep == "RACK_PICKING" || (m_currentWorkpiece.processStep == "RACK_PICKING"
&& ((m_checkpoint.type == "RACK_PICK" && m_checkpoint.phase == "NOT_SENT") && ((m_checkpoint.type == "RACK_PICK" && m_checkpoint.phase == "NOT_SENT")
|| (m_checkpoint.type == "VISION" && m_checkpoint.phase == "STEP_COMMITTED"))); || (m_checkpoint.type == "VISION" && m_checkpoint.phase == "STEP_COMMITTED")));
if (!m_recoveryIdleUnlock && !pickNotStarted && !historicalVision) { if (!pickNotStarted && !historicalVision) {
requestManualDecision(VisionProcessing, status, requestManualDecision(VisionProcessing, status,
"无法证明RackPick尚未开始;请先核对机器人动作,不能直接覆盖R60/M852", {}); "无法证明RackPick尚未开始;请先核对机器人动作,不能直接覆盖R60/M852", {});
return; return;
} }
if (decision.isEmpty()) { if (decision.isEmpty()) {
requestManualDecision(VisionProcessing, status, requestManualDecision(VisionProcessing, status,
m_recoveryIdleUnlock ? photoSignals + ";" + pickSignals historicalVision
+ (photo.feedback ? "。确认机器人停止、旧动作已结束后,清除到位反馈并读回确认,再解除遗留锁;不恢复订单" ? "历史记录没有视觉检查点,必须现场确认机器人仍在拍照位且RackPick尚未执行;确认后才建立新检查点"
: "。仅解除已终止任务的视觉遗留锁,不恢复订单") : "现场确认机器人仍在拍照位且RackPick尚未执行,再选择处理方式",
: historicalVision QStringList{"RETRY_VISION", "NORMAL_PICK"});
? "历史记录没有视觉检查点,必须现场确认机器人仍在拍照位且RackPick尚未执行;确认后才建立新检查点"
: "现场确认机器人仍在拍照位且RackPick尚未执行,再选择处理方式",
m_recoveryIdleUnlock ? QStringList{"UNLOCK_VISION"}
: QStringList{"RETRY_VISION", "NORMAL_PICK"});
return; return;
} }
QString error; QString error;
@@ -1552,66 +1598,6 @@ void ProductionService::checkVisionRecovery(const QJsonObject &decision)
++m_visionGeneration; ++m_visionGeneration;
m_deviceService->cancelVisionRequests("人工恢复作废旧视觉写入"); m_deviceService->cancelVisionRequests("人工恢复作废旧视觉写入");
m_targetReady = false; m_targetReady = false;
if (m_recoveryIdleUnlock) {
const auto verifyAndUnlock = [this, generation]() {
m_deviceService->inspectProductionAction(DeviceService::PhotoMoveAction,
[this, generation](bool read, const ProductionActionStatus &photo, const QString &readError) {
if (generation != m_recoveryGeneration || stopRequested()) return;
const VisionConfig config = ConfigManager::instance().visionConfig();
if (!read || photo.command || photo.feedback) {
finishRecoveryCheck(!read ? readError : QString("解锁读回失败:M%1=%2,M%3=%4,保留视觉锁")
.arg(config.photoMoveCoil).arg(int(photo.command))
.arg(config.photoMoveCoilFeedback).arg(int(photo.feedback)));
return;
}
m_deviceService->inspectProductionAction(DeviceService::RackPick,
[this, generation](bool read, const ProductionActionStatus &pick, const QString &readError) {
if (generation != m_recoveryGeneration || stopRequested()) return;
QString error;
if (!read || pick.command || pick.feedback) {
const auto addresses = ConfigManager::instance().modbusConfig().addressMap;
finishRecoveryCheck(!read ? readError : QString("取料状态已改变:M%1=%2,M%3=%4,保留视觉锁")
.arg(addresses.rackPickCommand).arg(int(pick.command))
.arg(addresses.rackPickFeedback).arg(int(pick.feedback)));
return;
}
if (!checkRecoverySafety(&error) || !loadRecoverySnapshot(&error)
|| !saveVisionRecoveryState(false, &error)) {
finishRecoveryCheck(error); return;
}
m_visionNeedsReconcile = false;
m_visionSuspended = false;
m_visionStage = VisionStage::Idle;
m_lastError.clear();
reportVision("视觉锁已解除", "旧任务已清理,可启动未开始订单", true);
publishState();
publishVisionControl();
finishRecoveryCheck();
});
});
};
if (!photo.feedback) { verifyAndUnlock(); return; }
m_visionIoPending = true;
m_deviceService->writeVisionSignal(DeviceService::PhotoMoveFeedback, false,
[this, generation, verifyAndUnlock](VisionWriteOutcome outcome, const QString &writeError) {
if (generation != m_recoveryGeneration || stopRequested()) return;
m_visionIoPending = false;
if (outcome != VisionWriteOutcome::Confirmed) {
finishRecoveryCheck(QString("清除 M%1 到位反馈未确认,保留视觉锁:%2")
.arg(ConfigManager::instance().visionConfig().photoMoveCoilFeedback).arg(writeError));
return;
}
verifyAndUnlock();
}, [this, generation]() {
if (generation != m_recoveryGeneration || stopRequested()) return false;
QString error;
if (checkRecoverySafety(&error) && loadRecoverySnapshot(&error)) return true;
m_visionIoPending = false;
finishRecoveryCheck(error);
return false;
});
return;
}
if (!saveVisionRecoveryState(true, &error)) { finishRecoveryCheck(error); return; } if (!saveVisionRecoveryState(true, &error)) { finishRecoveryCheck(error); return; }
m_visionNeedsReconcile = false; m_visionNeedsReconcile = false;
m_visionSuspended = false; m_visionSuspended = false;
@@ -2644,12 +2630,22 @@ bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *e
return false; return false;
} }
} }
WorkpieceRepository workpieces;
bool hasInspectionRecords = false;
if (!completed && finishOrder
&& !workpieces.orderHasInspectionRecords(m_orderId, &hasInspectionRecords, error)) {
database.rollback();
return false;
}
const bool removeTerminatedOrder = !completed && finishOrder && !hasInspectionRecords;
const QString targetStatus = completed || finishOrder ? "FINISHED" : "CANCELED"; const QString targetStatus = completed || finishOrder ? "FINISHED" : "CANCELED";
LogRecord log; LogRecord log;
log.type = "操作日志"; log.type = "操作日志";
log.module = "订单管理"; log.module = "订单管理";
log.action = completed ? "完成订单" : (finishOrder ? "终止订单(剩余工件不再执行)" : "取消订单"); log.action = completed ? "完成订单" : (finishOrder ? "终止订单(剩余工件不再执行)" : "取消订单");
log.content = log.action + ":" + order.no; log.content = log.action + ":" + order.no;
if (removeTerminatedOrder)
log.content += ";无测量记录,自动删除订单及关联数据并释放料位";
log.status = "成功"; log.status = "成功";
if (!orders.updateOrderLifecycle(m_orderId, targetStatus, order.beginTime, if (!orders.updateOrderLifecycle(m_orderId, targetStatus, order.beginTime,
QDateTime::currentDateTime(), error) QDateTime::currentDateTime(), error)
@@ -2664,6 +2660,24 @@ bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *e
database.rollback(); database.rollback();
return false; return false;
} }
if (removeTerminatedOrder) {
QList<int> affectedTrayIds;
int removedCount = 0;
if (!workpieces.removeUninspected(m_orderId, &affectedTrayIds, &removedCount, error)) {
database.rollback();
return false;
}
for (const int trayId : affectedTrayIds) {
if (!TrayRepository().refreshStats(trayId, error)) {
database.rollback();
return false;
}
}
if (!orders.removeOrder(m_orderId, error)) {
database.rollback();
return false;
}
}
if (!database.commit(error)) { if (!database.commit(error)) {
database.rollback(); database.rollback();
m_persistenceUncertain = true; m_persistenceUncertain = true;
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.0, 2026-11-05T15:19:02. --> <!-- Written by QtCreator 4.11.0, 2026-11-06T17:25:42. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>