feat(production): 修改删除订单问题
This commit is contained in:
+46
-68
@@ -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<quint16>(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<quint16>(trayNo),
|
||||
addresses.slotNumber,
|
||||
static_cast<quint16>(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<quint16>(slotNo),
|
||||
addresses.slotRowNumber,
|
||||
static_cast<quint16>((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<quint16>((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<quint16>((slotNo - 1) % 7 + 1),
|
||||
writeCommand);
|
||||
});
|
||||
addresses.slotColumNumber,
|
||||
static_cast<quint16>((slotNo - 1) % 7 + 1),
|
||||
writeCommand);
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
writeCommand(true, QString());
|
||||
});
|
||||
writeCommand(true, QString());
|
||||
}
|
||||
|
||||
void DeviceService::inspectProductionAction(
|
||||
|
||||
@@ -515,7 +515,7 @@ bool OrderService::deleteOrder(int id, QString *error)
|
||||
if (hasActiveRun) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "订单存在活动生产任务,请先在控制面板终止订单,终止完成后再删除";
|
||||
*error = "订单存在活动生产任务,请先在控制面板终止订单;终止完成后,无测量记录的订单会自动删除";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -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<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)) {
|
||||
database.rollback();
|
||||
m_persistenceUncertain = true;
|
||||
|
||||
Reference in New Issue
Block a user