From 6a70f1943ff2c283c5d23c7cc39f514b9e9af998 Mon Sep 17 00:00:00 2001 From: lidg Date: Mon, 9 Nov 2026 09:22:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(production):=20=E4=BF=AE=E6=94=B9=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E8=AE=A2=E5=8D=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/dashboardpage.cpp | 2 +- pages/orderpage.cpp | 3 +- repositories/orderrepository.cpp | 1 + services/deviceservice.cpp | 114 +++++++++------------- services/orderservice.cpp | 2 +- services/productionservice.cpp | 158 +++++++++++++++++-------------- ssp.pro.user | 2 +- 7 files changed, 138 insertions(+), 144 deletions(-) diff --git a/pages/dashboardpage.cpp b/pages/dashboardpage.cpp index f877cf6..332a931 100644 --- a/pages/dashboardpage.cpp +++ b/pages/dashboardpage.cpp @@ -475,7 +475,7 @@ void DashboardPage::stopProduction() const int confirmedRunId = active ? productionSnapshot.runId : 0; const int confirmedOrderId = active ? productionSnapshot.orderId : selectedOrderId; if (!stopButton->isEnabled() - || !confirmControlOperation("终止订单", "将当前订单标记为已完成,保留实际加工数量及记录,剩余工件不再执行。此操作不是机器人急停。")) return; + || !confirmControlOperation("终止订单", "终止后剩余工件不再执行。无测量记录时自动删除订单、关联工件及动作记录,并释放料位;已有测量记录时保留订单和历史数据。此操作不是机器人急停。")) return; if (recoveryDialog) recoveryDialog->reject(); QMetaObject::invokeMethod(productionWorker, "requestStopOrder", Qt::QueuedConnection, Q_ARG(int, confirmedRunId), diff --git a/pages/orderpage.cpp b/pages/orderpage.cpp index 7f20273..de2cfa7 100644 --- a/pages/orderpage.cpp +++ b/pages/orderpage.cpp @@ -292,7 +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" "删除会清除其未测量工件、动作记录并释放料位。\n\n" "确认删除订单:" + orderNo + "?") != QMessageBox::Yes) { return; diff --git a/repositories/orderrepository.cpp b/repositories/orderrepository.cpp index 404767e..74dc863 100644 --- a/repositories/orderrepository.cpp +++ b/repositories/orderrepository.cpp @@ -269,6 +269,7 @@ bool OrderRepository::removeOrder(int id, QString *error) } 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_runs where order_id = :id" << "delete from alarm_records where order_id = :id" diff --git a/services/deviceservice.cpp b/services/deviceservice.cpp index 7333ee4..b283320 100644 --- a/services/deviceservice.cpp +++ b/services/deviceservice.cpp @@ -582,91 +582,69 @@ void DeviceService::beginProductionAction( } m_productionActionBusy = true; - inspectProductionAction( - action, - [this, action, trayNo, slotNo, addresses, callback, beforeSend]( - bool success, const ProductionActionStatus &status, - const QString &error) { - if (!success) { - m_productionActionBusy = false; - if (callback) { - 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 = + [this, callback](VisionWriteOutcome outcome, const QString &error) { + // An unacknowledged command may already be running. Do not + // permit another action until it has been reconciled. + m_productionActionActive = outcome != VisionWriteOutcome::NotSent; + m_productionActionBusy = false; + if (callback) { + callback(outcome, error); } + }; - auto sendCommand = - [this, callback](VisionWriteOutcome outcome, const QString &error) { - // An unacknowledged command may already be running. Do not - // permit another action until it has been reconciled. - m_productionActionActive = outcome != VisionWriteOutcome::NotSent; - m_productionActionBusy = false; - if (callback) { - callback(outcome, error); - } - }; + auto writeCommand = [this, sendCommand, beforeSend](bool success, + const QString &error) { + if (!success) { + sendCommand(VisionWriteOutcome::NotSent, error); + 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); + }; - auto writeCommand = [this, sendCommand, beforeSend](bool success, - const QString &error) { - if (!success) { - sendCommand(VisionWriteOutcome::NotSent, error); + if (action == PhotoMoveAction || action == RackPick + || action == RackPlace) { + m_modbus->writeHoldingRegister( + addresses.trayNumber, + static_cast(trayNo), + [this, addresses, slotNo, writeCommand]( + bool traySuccess, const QString &trayError) { + if (!traySuccess) { + writeCommand(false, trayError); 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( - addresses.trayNumber, - static_cast(trayNo), + addresses.slotNumber, + static_cast(slotNo), [this, addresses, slotNo, writeCommand]( - bool traySuccess, const QString &trayError) { - if (!traySuccess) { - writeCommand(false, trayError); + bool slotSuccess, const QString &slotError) { + if (!slotSuccess) { + writeCommand(false, slotError); return; } m_modbus->writeHoldingRegister( - addresses.slotNumber, - static_cast(slotNo), + addresses.slotRowNumber, + static_cast((slotNo - 1) / 7 + 1), [this, addresses, slotNo, writeCommand]( - bool slotSuccess, const QString &slotError) { - if (!slotSuccess) { - writeCommand(false, slotError); + bool rowSuccess, const QString &rowError) { + if (!rowSuccess) { + writeCommand(false, rowError); return; } m_modbus->writeHoldingRegister( - addresses.slotRowNumber, - static_cast((slotNo - 1) / 7 + 1), - [this, addresses, slotNo, writeCommand]( - bool rowSuccess, const QString &rowError) { - if (!rowSuccess) { - writeCommand(false, rowError); - return; - } - m_modbus->writeHoldingRegister( - addresses.slotColumNumber, - static_cast((slotNo - 1) % 7 + 1), - writeCommand); - }); + addresses.slotColumNumber, + static_cast((slotNo - 1) % 7 + 1), + writeCommand); }); }); - return; - } + }); + return; + } - writeCommand(true, QString()); - }); + writeCommand(true, QString()); } void DeviceService::inspectProductionAction( diff --git a/services/orderservice.cpp b/services/orderservice.cpp index d650c72..90d1ec2 100644 --- a/services/orderservice.cpp +++ b/services/orderservice.cpp @@ -515,7 +515,7 @@ bool OrderService::deleteOrder(int id, QString *error) if (hasActiveRun) { database.rollback(); if (error) { - *error = "订单存在活动生产任务,请先在控制面板终止订单,终止完成后再删除"; + *error = "订单存在活动生产任务,请先在控制面板终止订单;终止完成后,无测量记录的订单会自动删除"; } return false; } diff --git a/services/productionservice.cpp b/services/productionservice.cpp index 34445d8..2f1e5c1 100644 --- a/services/productionservice.cpp +++ b/services/productionservice.cpp @@ -3,6 +3,8 @@ #include "../config/configmanager.h" #include "../repositories/productionrepository.h" #include "../repositories/orderrepository.h" +#include "../repositories/workpiecerepository.h" +#include "../repositories/trayrepository.h" #include "../repositories/logrepository.h" #include "../db/databasemanager.h" #include "orderservice.h" @@ -855,7 +857,14 @@ bool ProductionService::stopOrder(int orderId, QString *error) if (error) *error = "订单存在待恢复的生产记录或状态已改变,请先核对再终止"; 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(); return true; } @@ -1275,6 +1284,16 @@ bool ProductionService::recover(QString *error) emit recoveryBusyChanged(true); publishVisionControl(); const quint64 generation = ++m_recoveryGeneration; + if (m_recoveryIdleUnlock) { + // 解除视觉遗留锁:跳过 PLC 状态读取与安全校验,直接进入人工确认 + QString snapshotError; + if (!loadRecoverySnapshot(&snapshotError)) { + finishRecoveryCheck(snapshotError); + return true; + } + checkVisionRecovery(); + return true; + } m_deviceService->requestStatus( [this, generation](bool success, const DeviceStatus &, const QString &statusError) { if (generation != m_recoveryGeneration || stopRequested()) return; @@ -1427,6 +1446,14 @@ bool ProductionService::submitRecoveryDecision(const QJsonObject &decision, QStr m_manualContext = QJsonObject(); const quint64 generation = m_recoveryGeneration; 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( [this, generation, expected, decision, choice](bool success, const DeviceStatus &, const QString &statusError) { if (generation != m_recoveryGeneration || stopRequested()) return; @@ -1496,6 +1523,29 @@ void ProductionService::checkVisionRecovery(const QJsonObject &decision) if (m_deviceService->productionActionBusy() || m_visionIoPending) { 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, [this, generation, decision](bool photoRead, const ProductionActionStatus &photo, const QString &photoError) { 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") .arg(config.photoMoveCoil).arg(int(photo.command)) .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; } 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; const ModbusAddressMap addresses = ConfigManager::instance().modbusConfig().addressMap; 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_checkpoint.type == "RACK_PICK" && m_checkpoint.phase == "NOT_SENT") || (m_checkpoint.type == "VISION" && m_checkpoint.phase == "STEP_COMMITTED"))); - if (!m_recoveryIdleUnlock && !pickNotStarted && !historicalVision) { + if (!pickNotStarted && !historicalVision) { requestManualDecision(VisionProcessing, status, "无法证明RackPick尚未开始;请先核对机器人动作,不能直接覆盖R60/M852", {}); return; } if (decision.isEmpty()) { requestManualDecision(VisionProcessing, status, - m_recoveryIdleUnlock ? photoSignals + ";" + pickSignals - + (photo.feedback ? "。确认机器人停止、旧动作已结束后,清除到位反馈并读回确认,再解除遗留锁;不恢复订单" - : "。仅解除已终止任务的视觉遗留锁,不恢复订单") - : historicalVision - ? "历史记录没有视觉检查点,必须现场确认机器人仍在拍照位且RackPick尚未执行;确认后才建立新检查点" - : "现场确认机器人仍在拍照位且RackPick尚未执行,再选择处理方式", - m_recoveryIdleUnlock ? QStringList{"UNLOCK_VISION"} - : QStringList{"RETRY_VISION", "NORMAL_PICK"}); + historicalVision + ? "历史记录没有视觉检查点,必须现场确认机器人仍在拍照位且RackPick尚未执行;确认后才建立新检查点" + : "现场确认机器人仍在拍照位且RackPick尚未执行,再选择处理方式", + QStringList{"RETRY_VISION", "NORMAL_PICK"}); return; } QString error; @@ -1552,66 +1598,6 @@ void ProductionService::checkVisionRecovery(const QJsonObject &decision) ++m_visionGeneration; m_deviceService->cancelVisionRequests("人工恢复作废旧视觉写入"); 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; } m_visionNeedsReconcile = false; m_visionSuspended = false; @@ -2644,12 +2630,22 @@ bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *e 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"; LogRecord log; log.type = "操作日志"; log.module = "订单管理"; log.action = completed ? "完成订单" : (finishOrder ? "终止订单(剩余工件不再执行)" : "取消订单"); log.content = log.action + ":" + order.no; + if (removeTerminatedOrder) + log.content += ";无测量记录,自动删除订单及关联数据并释放料位"; log.status = "成功"; if (!orders.updateOrderLifecycle(m_orderId, targetStatus, order.beginTime, QDateTime::currentDateTime(), error) @@ -2664,6 +2660,24 @@ bool ProductionService::finalizeRun(bool completed, bool finishOrder, QString *e database.rollback(); return false; } + if (removeTerminatedOrder) { + QList 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)) { database.rollback(); m_persistenceUncertain = true; diff --git a/ssp.pro.user b/ssp.pro.user index d7919ce..9a81674 100644 --- a/ssp.pro.user +++ b/ssp.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId