485 lines
18 KiB
C++
485 lines
18 KiB
C++
#include "productionrepository.h"
|
|
|
|
#include "../db/databasemanager.h"
|
|
|
|
#include <QSqlError>
|
|
#include <QSqlQuery>
|
|
#include <QVariant>
|
|
|
|
namespace {
|
|
|
|
bool ensureOpen(QString *error)
|
|
{
|
|
return DatabaseManager::instance().open(error);
|
|
}
|
|
|
|
void setError(const QSqlQuery &query, QString *error)
|
|
{
|
|
if (error) {
|
|
*error = query.lastError().text();
|
|
}
|
|
}
|
|
|
|
QVariant nullableId(int id)
|
|
{
|
|
return id > 0 ? QVariant(id) : QVariant(QVariant::Int);
|
|
}
|
|
|
|
}
|
|
|
|
int ProductionRepository::createRun(int orderId, const QString &productionState,
|
|
const QString &processStep,
|
|
const QString &inspectionMode,
|
|
int startTrayNo, int startSlotNo, int planNum,
|
|
QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return 0;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into production_runs "
|
|
"(order_id, production_state, process_step, inspection_mode, "
|
|
"start_tray_no, start_slot_no, plan_num) "
|
|
"values (:order_id, :production_state, :process_step, "
|
|
":inspection_mode, :start_tray_no, :start_slot_no, :plan_num) "
|
|
"returning id");
|
|
query.bindValue(":order_id", orderId);
|
|
query.bindValue(":production_state", productionState);
|
|
query.bindValue(":process_step", processStep);
|
|
query.bindValue(":inspection_mode", inspectionMode);
|
|
query.bindValue(":start_tray_no", startTrayNo);
|
|
query.bindValue(":start_slot_no", startSlotNo);
|
|
query.bindValue(":plan_num", planNum);
|
|
if (!query.exec() || !query.next()) {
|
|
setError(query, error);
|
|
return 0;
|
|
}
|
|
return query.value(0).toInt();
|
|
}
|
|
|
|
bool ProductionRepository::updateRun(int runId, const QString &runState,
|
|
const QString &productionState,
|
|
const QString &processStep,
|
|
int currentWorkpieceId,
|
|
const QString &timeoutState,
|
|
int recoveryRetryCount,
|
|
const QString &lastError,
|
|
QString *error)
|
|
{
|
|
if (runId <= 0 || !ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("update production_runs set "
|
|
"run_state = :run_state, "
|
|
"production_state = :production_state, "
|
|
"process_step = :process_step, "
|
|
"current_workpiece_id = :current_workpiece_id, "
|
|
"timeout_state = :timeout_state, "
|
|
"recovery_retry_count = :recovery_retry_count, "
|
|
"last_error = :last_error, "
|
|
"last_heartbeat_at = now(), updated_at = now() "
|
|
"where id = :id");
|
|
query.bindValue(":run_state", runState);
|
|
query.bindValue(":production_state", productionState);
|
|
query.bindValue(":process_step", processStep);
|
|
query.bindValue(":current_workpiece_id", nullableId(currentWorkpieceId));
|
|
query.bindValue(":timeout_state", timeoutState);
|
|
query.bindValue(":recovery_retry_count", recoveryRetryCount);
|
|
query.bindValue(":last_error", lastError);
|
|
query.bindValue(":id", runId);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::finishRun(int runId, const QString &runState,
|
|
const QString &lastError, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("update production_runs set run_state = :run_state, "
|
|
"last_error = :last_error, ended_at = now(), "
|
|
"last_heartbeat_at = now(), updated_at = now() "
|
|
"where id = :id");
|
|
query.bindValue(":run_state", runState);
|
|
query.bindValue(":last_error", lastError);
|
|
query.bindValue(":id", runId);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::heartbeatRun(int runId, QString *error)
|
|
{
|
|
if (runId <= 0 || !ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("update production_runs set "
|
|
"last_heartbeat_at = now(), updated_at = now() "
|
|
"where id = :id "
|
|
"and run_state in ('RUNNING', 'PAUSED', 'RECOVERY')");
|
|
query.bindValue(":id", runId);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return query.numRowsAffected() == 1;
|
|
}
|
|
|
|
bool ProductionRepository::findActiveRun(ProductionRunSnapshot *run, QString *error)
|
|
{
|
|
if (!run || !ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("select id, order_id, current_workpiece_id, plan_num, "
|
|
"start_tray_no, start_slot_no, inspection_mode, run_state, "
|
|
"production_state, process_step, timeout_state, "
|
|
"recovery_retry_count, last_error "
|
|
"from production_runs "
|
|
"where run_state in ('RUNNING', 'PAUSED', 'RECOVERY') "
|
|
"order by id desc limit 1");
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
if (!query.next()) {
|
|
*run = ProductionRunSnapshot();
|
|
return true;
|
|
}
|
|
|
|
run->id = query.value("id").toInt();
|
|
run->orderId = query.value("order_id").toInt();
|
|
run->currentWorkpieceId = query.value("current_workpiece_id").toInt();
|
|
run->planNum = query.value("plan_num").toInt();
|
|
run->startTrayNo = query.value("start_tray_no").toInt();
|
|
run->startSlotNo = query.value("start_slot_no").toInt();
|
|
run->inspectionMode = query.value("inspection_mode").toString();
|
|
run->runState = query.value("run_state").toString();
|
|
run->productionState = query.value("production_state").toString();
|
|
run->processStep = query.value("process_step").toString();
|
|
run->timeoutState = query.value("timeout_state").toString();
|
|
run->recoveryRetryCount = query.value("recovery_retry_count").toInt();
|
|
run->lastError = query.value("last_error").toString();
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::insertStateHistory(
|
|
int runId, int orderId, int workpieceId, const QString &fromState,
|
|
const QString &toState, const QString &processStep, const QString &source,
|
|
const QString &reason, const QString &message, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into production_state_history "
|
|
"(production_run_id, order_id, workpiece_id, from_state, "
|
|
"to_state, process_step, source, reason, message) "
|
|
"values (:run_id, :order_id, :workpiece_id, :from_state, "
|
|
":to_state, :process_step, :source, :reason, :message)");
|
|
query.bindValue(":run_id", nullableId(runId));
|
|
query.bindValue(":order_id", nullableId(orderId));
|
|
query.bindValue(":workpiece_id", nullableId(workpieceId));
|
|
query.bindValue(":from_state", fromState);
|
|
query.bindValue(":to_state", toState);
|
|
query.bindValue(":process_step", processStep);
|
|
query.bindValue(":source", source);
|
|
query.bindValue(":reason", reason);
|
|
query.bindValue(":message", message);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::startWorkpieceProcess(
|
|
int orderId, int workpieceId, const QString &processStep,
|
|
const QString &inspectionMode, int plcStep, const QString &source,
|
|
QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery attemptQuery(DatabaseManager::instance().database());
|
|
attemptQuery.prepare("select coalesce(max(attempt_no), 0) + 1 "
|
|
"from workpiece_process_records "
|
|
"where workpiece_id = :workpiece_id "
|
|
"and process_step = :process_step");
|
|
attemptQuery.bindValue(":workpiece_id", workpieceId);
|
|
attemptQuery.bindValue(":process_step", processStep);
|
|
if (!attemptQuery.exec() || !attemptQuery.next()) {
|
|
setError(attemptQuery, error);
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into workpiece_process_records "
|
|
"(order_id, workpiece_id, process_step, inspection_mode, attempt_no, "
|
|
"process_state, plc_step, source) "
|
|
"values (:order_id, :workpiece_id, :process_step, :inspection_mode, "
|
|
":attempt_no, 'RUNNING', :plc_step, :source)");
|
|
query.bindValue(":order_id", nullableId(orderId));
|
|
query.bindValue(":workpiece_id", workpieceId);
|
|
query.bindValue(":process_step", processStep);
|
|
query.bindValue(":inspection_mode", inspectionMode);
|
|
query.bindValue(":attempt_no", attemptQuery.value(0).toInt());
|
|
query.bindValue(":plc_step", plcStep);
|
|
query.bindValue(":source", source);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::finishWorkpieceProcess(
|
|
int workpieceId, const QString &processStep, const QString &resultState,
|
|
int durationMs, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("update workpiece_process_records set "
|
|
"process_state = 'COMPLETED', result_state = :result_state, "
|
|
"completed_at = now(), duration_ms = :duration_ms "
|
|
"where id = (select id from workpiece_process_records "
|
|
"where workpiece_id = :workpiece_id "
|
|
"and process_step = :process_step "
|
|
"and process_state = 'RUNNING' "
|
|
"order by id desc limit 1)");
|
|
query.bindValue(":result_state", resultState);
|
|
query.bindValue(":duration_ms", durationMs >= 0 ? QVariant(durationMs)
|
|
: QVariant(QVariant::Int));
|
|
query.bindValue(":workpiece_id", workpieceId);
|
|
query.bindValue(":process_step", processStep);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QList<WorkpieceProcessRecord> ProductionRepository::listProcessRecords(
|
|
int workpieceId, QString *error)
|
|
{
|
|
QList<WorkpieceProcessRecord> records;
|
|
if (workpieceId <= 0 || !ensureOpen(error)) {
|
|
return records;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("select process_step, attempt_no, process_state, "
|
|
"result_state, duration_ms, started_at, completed_at "
|
|
"from workpiece_process_records "
|
|
"where workpiece_id = :workpiece_id "
|
|
"order by started_at, id");
|
|
query.bindValue(":workpiece_id", workpieceId);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return records;
|
|
}
|
|
|
|
while (query.next()) {
|
|
WorkpieceProcessRecord record;
|
|
record.processStep = query.value("process_step").toString();
|
|
record.attemptNo = query.value("attempt_no").toInt();
|
|
record.processState = query.value("process_state").toString();
|
|
record.resultState = query.value("result_state").toString();
|
|
record.durationMs = query.value("duration_ms").isNull()
|
|
? -1 : query.value("duration_ms").toInt();
|
|
record.startedAt = query.value("started_at").toDateTime();
|
|
record.completedAt = query.value("completed_at").toDateTime();
|
|
records.append(record);
|
|
}
|
|
return records;
|
|
}
|
|
|
|
bool ProductionRepository::insertInspection(
|
|
int orderId, int workpieceId, const QString &side, int rawResult,
|
|
const QString &resultState, const QString &inspectionMode, int plcStep,
|
|
const QString &source, const QString &message, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery attemptQuery(DatabaseManager::instance().database());
|
|
attemptQuery.prepare("select coalesce(max(attempt_no), 0) + 1 "
|
|
"from inspection_records "
|
|
"where workpiece_id = :workpiece_id "
|
|
"and inspection_side = :side");
|
|
attemptQuery.bindValue(":workpiece_id", workpieceId);
|
|
attemptQuery.bindValue(":side", side);
|
|
if (!attemptQuery.exec() || !attemptQuery.next()) {
|
|
setError(attemptQuery, error);
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into inspection_records "
|
|
"(order_id, workpiece_id, inspection_side, inspection_mode, attempt_no, "
|
|
"raw_result, result_state, inspection_state, source, "
|
|
"plc_step, message, started_at, completed_at) "
|
|
"values (:order_id, :workpiece_id, :side, :inspection_mode, :attempt_no, "
|
|
":raw_result, :result_state, 'COMPLETED', :source, "
|
|
":plc_step, :message, now(), now())");
|
|
query.bindValue(":order_id", nullableId(orderId));
|
|
query.bindValue(":workpiece_id", workpieceId);
|
|
query.bindValue(":side", side);
|
|
query.bindValue(":inspection_mode", inspectionMode);
|
|
query.bindValue(":attempt_no", attemptQuery.value(0).toInt());
|
|
query.bindValue(":raw_result", rawResult);
|
|
query.bindValue(":result_state", resultState);
|
|
query.bindValue(":source", source);
|
|
query.bindValue(":plc_step", plcStep);
|
|
query.bindValue(":message", message);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::insertAlarm(
|
|
const QString &code, const QString &message, int orderId,
|
|
int workpieceId, const QString &fallbackLevel,
|
|
const QString &fallbackSource, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery duplicateQuery(DatabaseManager::instance().database());
|
|
duplicateQuery.prepare("select id from alarm_records "
|
|
"where code = :code "
|
|
"and alarm_state <> 'CLEARED' "
|
|
"and order_id is not distinct from :order_id "
|
|
"and workpiece_id is not distinct from :workpiece_id "
|
|
"limit 1");
|
|
duplicateQuery.bindValue(":code", code);
|
|
duplicateQuery.bindValue(":order_id", nullableId(orderId));
|
|
duplicateQuery.bindValue(":workpiece_id", nullableId(workpieceId));
|
|
if (!duplicateQuery.exec()) {
|
|
setError(duplicateQuery, error);
|
|
return false;
|
|
}
|
|
if (duplicateQuery.next()) {
|
|
return true;
|
|
}
|
|
|
|
QSqlQuery definitionQuery(DatabaseManager::instance().database());
|
|
definitionQuery.prepare("select id, level, source from alarm_definitions "
|
|
"where code = :code");
|
|
definitionQuery.bindValue(":code", code);
|
|
if (!definitionQuery.exec()) {
|
|
setError(definitionQuery, error);
|
|
return false;
|
|
}
|
|
|
|
QVariant definitionId(QVariant::Int);
|
|
QString level = fallbackLevel;
|
|
QString source = fallbackSource;
|
|
if (definitionQuery.next()) {
|
|
definitionId = definitionQuery.value("id");
|
|
level = definitionQuery.value("level").toString();
|
|
source = definitionQuery.value("source").toString();
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into alarm_records "
|
|
"(definition_id, code, level, source, order_id, workpiece_id, message) "
|
|
"values (:definition_id, :code, :level, :source, "
|
|
":order_id, :workpiece_id, :message)");
|
|
query.bindValue(":definition_id", definitionId);
|
|
query.bindValue(":code", code);
|
|
query.bindValue(":level", level);
|
|
query.bindValue(":source", source);
|
|
query.bindValue(":order_id", nullableId(orderId));
|
|
query.bindValue(":workpiece_id", nullableId(workpieceId));
|
|
query.bindValue(":message", message);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::clearActiveAlarms(const QString &code,
|
|
const QString &clearedBy,
|
|
QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare(code.trimmed().isEmpty()
|
|
? "update alarm_records set alarm_state = 'CLEARED', "
|
|
"cleared_at = now(), cleared_by = :cleared_by "
|
|
"where alarm_state <> 'CLEARED'"
|
|
: "update alarm_records set alarm_state = 'CLEARED', "
|
|
"cleared_at = now(), cleared_by = :cleared_by "
|
|
"where alarm_state <> 'CLEARED' and code = :code");
|
|
query.bindValue(":cleared_by", clearedBy);
|
|
if (!code.trimmed().isEmpty()) {
|
|
query.bindValue(":code", code);
|
|
}
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProductionRepository::insertDeviceStatus(
|
|
const QString &deviceCode, bool plcConnected, bool plcReady,
|
|
bool robotReady, bool safetyDoorClosed, bool emergencyStop,
|
|
int currentStep, int checkResult, int errorCode, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into device_status_history "
|
|
"(device_code, plc_connected, plc_ready, robot_ready, "
|
|
"safety_door_closed, emergency_stop, current_step, "
|
|
"check_result, error_code) "
|
|
"values (:device_code, :plc_connected, :plc_ready, "
|
|
":robot_ready, :safety_door_closed, :emergency_stop, "
|
|
":current_step, :check_result, :error_code)");
|
|
query.bindValue(":device_code", deviceCode);
|
|
query.bindValue(":plc_connected", plcConnected);
|
|
query.bindValue(":plc_ready", plcReady);
|
|
query.bindValue(":robot_ready", robotReady);
|
|
query.bindValue(":safety_door_closed", safetyDoorClosed);
|
|
query.bindValue(":emergency_stop", emergencyStop);
|
|
query.bindValue(":current_step", currentStep);
|
|
query.bindValue(":check_result", checkResult);
|
|
query.bindValue(":error_code", errorCode);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|