feat(production): 修改订单流程卡死流程问题

This commit is contained in:
lidg
2026-11-06 15:49:33 +08:00
parent 753f9c3ed2
commit 81313e61b0
24 changed files with 949 additions and 261 deletions
+55 -7
View File
@@ -41,9 +41,9 @@ int ProductionRepository::createRun(int orderId, const QString &productionState,
QSqlQuery query(DatabaseManager::instance().database());
query.prepare("insert into production_runs "
"(order_id, production_state, process_step, inspection_mode, "
"(order_id, run_state, production_state, process_step, inspection_mode, "
"start_tray_no, start_slot_no, plan_num) "
"values (:order_id, :production_state, :process_step, "
"values (:order_id, 'RUNNING', :production_state, :process_step, "
":inspection_mode, :start_tray_no, :start_slot_no, :plan_num) "
"returning id");
query.bindValue(":order_id", orderId);
@@ -96,6 +96,10 @@ bool ProductionRepository::updateRun(int runId, const QString &runState,
setError(query, error);
return false;
}
if (query.numRowsAffected() != 1) {
if (error) *error = "生产运行记录不存在或已改变,请重新核对";
return false;
}
return true;
}
@@ -118,6 +122,10 @@ bool ProductionRepository::finishRun(int runId, const QString &runState,
setError(query, error);
return false;
}
if (query.numRowsAffected() != 1) {
if (error) *error = "生产运行记录不存在或已改变,请重新核对";
return false;
}
return true;
}
@@ -131,7 +139,7 @@ bool ProductionRepository::heartbeatRun(int runId, QString *error)
query.prepare("update production_runs set "
"last_heartbeat_at = now(), updated_at = now() "
"where id = :id "
"and run_state in ('RUNNING', 'PAUSED', 'RECOVERY')");
"and run_state in ('RUNNING', 'PAUSED', 'RECOVERY', 'STOPPING')");
query.bindValue(":id", runId);
if (!query.exec()) {
setError(query, error);
@@ -148,8 +156,8 @@ bool ProductionRepository::findActiveRun(ProductionRunSnapshot *run, QString *er
QSqlQuery query(DatabaseManager::instance().database());
query.prepare("select * from production_runs "
"where run_state in ('RUNNING', 'PAUSED', 'RECOVERY') "
"order by id desc limit 1");
"where run_state in ('RUNNING', 'PAUSED', 'RECOVERY', 'STOPPING') "
"order by id desc limit 2");
if (!query.exec()) {
setError(query, error);
return false;
@@ -183,6 +191,10 @@ bool ProductionRepository::findActiveRun(ProductionRunSnapshot *run, QString *er
run->action.manualDecision = query.value("manual_decision").toString();
run->action.manualOperator = query.value("manual_operator").toString();
run->action.manualDecidedAt = query.value("manual_decided_at").toDateTime();
if (query.next()) {
if (error) *error = "发现多个活动生产任务,请先核对运行记录,不能自动选择订单";
return false;
}
return true;
}
@@ -221,6 +233,40 @@ bool ProductionRepository::findRun(int runId, ProductionRunSnapshot *run, QStrin
return true;
}
bool ProductionRepository::lockOrderRuns(int orderId, QString *latestRunState,
bool *hasActiveRun, QString *error)
{
if (!latestRunState || !hasActiveRun || !ensureOpen(error)) {
return false;
}
latestRunState->clear();
*hasActiveRun = false;
QSqlQuery query(DatabaseManager::instance().database());
query.prepare("select run_state from production_runs "
"where order_id = :order_id order by id desc for update");
query.bindValue(":order_id", orderId);
if (!query.exec()) {
setError(query, error);
return false;
}
bool first = true;
while (query.next()) {
const QString runState = query.value(0).toString();
if (first) {
*latestRunState = runState;
first = false;
}
if (runState == "RUNNING" || runState == "PAUSED"
|| runState == "RECOVERY" || runState == "STOPPING") {
*hasActiveRun = true;
}
}
return true;
}
bool ProductionRepository::checkActionSchema(QString *error)
{
if (!ensureOpen(error)) return false;
@@ -271,7 +317,7 @@ bool ProductionRepository::checkActionSchema(QString *error)
return false;
}
const QMap<QString, QString> expectedChecks = {
{"ck_production_runs_action_type", "CHECK (action_type IS NULL OR action_type = ANY ARRAY['PHOTO_MOVE','VISION','RACK_PICK','FRONT_LOAD','INSPECT','FRONT_UNLOAD','FLIP','BACK_LOAD','BACK_UNLOAD','RACK_PLACE'])"},
{"ck_production_runs_action_type", "CHECK (action_type IS NULL OR action_type = ANY ARRAY['PHOTO_MOVE','VISION','RACK_PICK','FRONT_LOAD','INSPECT','FRONT_UNLOAD','FLIP','BACK_LOAD','BACK_UNLOAD','REVERSE_FLIP','RACK_PLACE'])"},
{"ck_production_runs_action_phase", "CHECK (action_phase IS NULL OR action_phase = ANY ARRAY['NOT_SENT','SENDING','COMMAND_CONFIRMED','FEEDBACK_RECEIVED','SIGNALS_CLEARED','STEP_COMMITTED','RESULT_WRITING','RESULT_CONFIRMED','MODE_WRITING','MODE_CONFIRMED'])"},
{"ck_production_runs_manual_decision", "CHECK (manual_decision IS NULL OR manual_decision = ANY ARRAY['ACTION_COMPLETED','ACTION_NOT_EXECUTED','RETRY_VISION','NORMAL_PICK'])"},
{"ck_production_runs_action_attempt", "CHECK (action_attempt >= 0)"},
@@ -290,7 +336,9 @@ bool ProductionRepository::checkActionSchema(QString *error)
const QString name = query.value(0).toString();
if (!expectedChecks.contains(name)) continue;
if (normalizedCheck(query.value(1).toString()) != normalizedCheck(expectedChecks.value(name))) {
if (error) *error = "动作恢复约束定义不匹配:" + name;
if (error) *error = name == "ck_production_runs_action_type"
? "动作类型约束不匹配,请先执行 db/migrations/2026-11-06-production-plc-flow.sql"
: "动作恢复约束定义不匹配:" + name;
return false;
}
++checked;