Files
ssp/pages/qualitypage.cpp
T

535 lines
20 KiB
C++

#include "qualitypage.h"
#include "uihelpers.h"
#include "../models/qualityrecord.h"
#include "../services/qualityservice.h"
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSplitter>
#include <QTabWidget>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QVBoxLayout>
namespace {
QString formatTime(const QDateTime &dateTime)
{
return dateTime.isValid()
? dateTime.toString("yyyy-MM-dd HH:mm:ss") : "-";
}
QString formatDuration(int durationMs)
{
if (durationMs < 0) {
return "-";
}
if (durationMs >= 1000) {
return QString::number(durationMs / 1000.0, 'f', 2) + " s";
}
return QString::number(durationMs) + " ms";
}
QString modeText(const QString &mode)
{
if (mode == "BEFORE_PLATING") {
return "镀金前";
}
if (mode == "AFTER_PLATING") {
return "镀金后";
}
return mode.isEmpty() ? "-" : mode;
}
QString sideText(const QString &side)
{
if (side == "FRONT") {
return "正面";
}
if (side == "BACK") {
return "反面";
}
if (side == "FINAL") {
return "最终";
}
return side.isEmpty() ? "-" : side;
}
QString resultText(const QString &result)
{
if (result == "OK") {
return "合格";
}
if (result == "NG") {
return "不合格";
}
if (result == "ERROR") {
return "错误";
}
if (result == "UNKNOWN") {
return "未知";
}
return result.isEmpty() ? "-" : result;
}
QString sourceText(const QString &source)
{
if (source == "PLC") {
return "PLC";
}
if (source == "MANUAL") {
return "手动";
}
if (source == "RECOVERY") {
return "恢复";
}
return source.isEmpty() ? "-" : source;
}
QString rawResultText(int rawResult)
{
return rawResult < 0 ? "-" : QString::number(rawResult);
}
QLabel *makeValueLabel()
{
QLabel *label = new QLabel("-");
label->setMinimumWidth(90);
label->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; color: #00695c; }");
return label;
}
void addTableRow(QTableWidget *table, const QStringList &values)
{
const int row = table->rowCount();
table->insertRow(row);
for (int column = 0; column < values.size(); ++column) {
table->setItem(row, column, new QTableWidgetItem(values.at(column)));
}
}
}
QualityPage::QualityPage(QWidget *parent)
: QWidget(parent)
, inspectionOrderEdit(nullptr)
, inspectionBarcodeEdit(nullptr)
, inspectionTrayEdit(nullptr)
, inspectionSlotEdit(nullptr)
, inspectionModeCombo(nullptr)
, inspectionSideCombo(nullptr)
, inspectionResultCombo(nullptr)
, inspectionTable(nullptr)
, inspectionMessageLabel(nullptr)
, statisticsOrderEdit(nullptr)
, planValueLabel(nullptr)
, completedValueLabel(nullptr)
, pendingValueLabel(nullptr)
, okValueLabel(nullptr)
, ngValueLabel(nullptr)
, rateValueLabel(nullptr)
, inspectionCountValueLabel(nullptr)
, statisticsMessageLabel(nullptr)
, sideStatisticsTable(nullptr)
, traceBarcodeEdit(nullptr)
, traceInfoLabel(nullptr)
, traceMessageLabel(nullptr)
, traceInspectionTable(nullptr)
, traceProcessTable(nullptr)
, traceHistoryTable(nullptr)
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(14, 14, 14, 14);
layout->setSpacing(10);
QTabWidget *tabs = new QTabWidget(this);
layout->addWidget(tabs);
setupInspectionTab();
setupStatisticsTab();
setupTraceTab();
QWidget *inspectionTab = new QWidget;
QVBoxLayout *inspectionLayout = new QVBoxLayout(inspectionTab);
inspectionLayout->setContentsMargins(8, 8, 8, 8);
inspectionLayout->setSpacing(8);
QGridLayout *filters = new QGridLayout;
filters->setHorizontalSpacing(8);
filters->setVerticalSpacing(6);
filters->addWidget(new QLabel("订单号"), 0, 0);
filters->addWidget(inspectionOrderEdit, 0, 1);
filters->addWidget(new QLabel("工件条码"), 0, 2);
filters->addWidget(inspectionBarcodeEdit, 0, 3);
filters->addWidget(new QLabel("托盘"), 0, 4);
filters->addWidget(inspectionTrayEdit, 0, 5);
filters->addWidget(new QLabel("位置号"), 0, 6);
filters->addWidget(inspectionSlotEdit, 0, 7);
filters->addWidget(new QLabel("检测模式"), 1, 0);
filters->addWidget(inspectionModeCombo, 1, 1);
filters->addWidget(new QLabel("检测面"), 1, 2);
filters->addWidget(inspectionSideCombo, 1, 3);
filters->addWidget(new QLabel("结果"), 1, 4);
filters->addWidget(inspectionResultCombo, 1, 5);
QPushButton *inspectionQueryButton = new QPushButton("查询");
filters->addWidget(inspectionQueryButton, 1, 7);
inspectionLayout->addLayout(filters);
inspectionLayout->addWidget(inspectionMessageLabel);
inspectionLayout->addWidget(inspectionTable, 1);
tabs->addTab(inspectionTab, "检测记录");
QWidget *statisticsTab = new QWidget;
QVBoxLayout *statisticsLayout = new QVBoxLayout(statisticsTab);
statisticsLayout->setContentsMargins(8, 8, 8, 8);
statisticsLayout->setSpacing(8);
QHBoxLayout *statisticsFilters = new QHBoxLayout;
statisticsFilters->addWidget(new QLabel("订单号"));
statisticsFilters->addWidget(statisticsOrderEdit);
QPushButton *statisticsQueryButton = new QPushButton("查询");
statisticsFilters->addWidget(statisticsQueryButton);
statisticsFilters->addStretch();
statisticsLayout->addLayout(statisticsFilters);
statisticsLayout->addWidget(statisticsMessageLabel);
QGroupBox *summaryBox = new QGroupBox("订单质量汇总");
QGridLayout *summaryLayout = new QGridLayout(summaryBox);
summaryLayout->addWidget(new QLabel("计划数量"), 0, 0);
summaryLayout->addWidget(planValueLabel, 0, 1);
summaryLayout->addWidget(new QLabel("已完成"), 0, 2);
summaryLayout->addWidget(completedValueLabel, 0, 3);
summaryLayout->addWidget(new QLabel("未完成"), 0, 4);
summaryLayout->addWidget(pendingValueLabel, 0, 5);
summaryLayout->addWidget(new QLabel("合格"), 1, 0);
summaryLayout->addWidget(okValueLabel, 1, 1);
summaryLayout->addWidget(new QLabel("不合格"), 1, 2);
summaryLayout->addWidget(ngValueLabel, 1, 3);
summaryLayout->addWidget(new QLabel("合格率"), 1, 4);
summaryLayout->addWidget(rateValueLabel, 1, 5);
summaryLayout->addWidget(new QLabel("检测记录数"), 2, 0);
summaryLayout->addWidget(inspectionCountValueLabel, 2, 1);
statisticsLayout->addWidget(summaryBox);
statisticsLayout->addWidget(sideStatisticsTable, 1);
tabs->addTab(statisticsTab, "合格率统计");
QWidget *traceTab = new QWidget;
QVBoxLayout *traceLayout = new QVBoxLayout(traceTab);
traceLayout->setContentsMargins(8, 8, 8, 8);
traceLayout->setSpacing(8);
QHBoxLayout *traceFilters = new QHBoxLayout;
traceFilters->addWidget(new QLabel("工件条码"));
traceFilters->addWidget(traceBarcodeEdit);
QPushButton *traceQueryButton = new QPushButton("查询");
traceFilters->addWidget(traceQueryButton);
traceFilters->addStretch();
traceLayout->addLayout(traceFilters);
traceLayout->addWidget(traceInfoLabel);
traceLayout->addWidget(traceMessageLabel);
QGroupBox *traceInspectionBox = new QGroupBox("检测记录");
QVBoxLayout *traceInspectionLayout = new QVBoxLayout(traceInspectionBox);
traceInspectionLayout->addWidget(traceInspectionTable);
QGroupBox *traceProcessBox = new QGroupBox("工序记录");
QVBoxLayout *traceProcessLayout = new QVBoxLayout(traceProcessBox);
traceProcessLayout->addWidget(traceProcessTable);
QGroupBox *traceHistoryBox = new QGroupBox("状态历史");
QVBoxLayout *traceHistoryLayout = new QVBoxLayout(traceHistoryBox);
traceHistoryLayout->addWidget(traceHistoryTable);
QSplitter *traceSplitter = new QSplitter(Qt::Vertical);
traceSplitter->addWidget(traceInspectionBox);
traceSplitter->addWidget(traceProcessBox);
traceSplitter->addWidget(traceHistoryBox);
traceLayout->addWidget(traceSplitter, 1);
tabs->addTab(traceTab, "工件追溯");
connect(inspectionQueryButton, &QPushButton::clicked,
this, &QualityPage::loadInspectionRecords);
connect(inspectionSlotEdit, &QLineEdit::returnPressed,
this, &QualityPage::loadInspectionRecords);
connect(statisticsQueryButton, &QPushButton::clicked,
this, &QualityPage::loadStatistics);
connect(statisticsOrderEdit, &QLineEdit::returnPressed,
this, &QualityPage::loadStatistics);
connect(traceQueryButton, &QPushButton::clicked,
this, &QualityPage::loadTrace);
connect(traceBarcodeEdit, &QLineEdit::returnPressed,
this, &QualityPage::loadTrace);
loadInspectionRecords();
loadStatistics();
}
QualityPage::~QualityPage()
{
}
void QualityPage::setupInspectionTab()
{
inspectionOrderEdit = new QLineEdit;
inspectionOrderEdit->setPlaceholderText("支持模糊查询");
inspectionBarcodeEdit = new QLineEdit;
inspectionBarcodeEdit->setPlaceholderText("支持模糊查询");
inspectionTrayEdit = new QLineEdit;
inspectionTrayEdit->setPlaceholderText("如 TRAY-1");
inspectionSlotEdit = new QLineEdit;
inspectionSlotEdit->setPlaceholderText("1-49");
inspectionModeCombo = new QComboBox;
inspectionModeCombo->addItem("全部", "");
inspectionModeCombo->addItem("镀金前", "BEFORE_PLATING");
inspectionModeCombo->addItem("镀金后", "AFTER_PLATING");
inspectionSideCombo = new QComboBox;
inspectionSideCombo->addItem("全部", "");
inspectionSideCombo->addItem("正面", "FRONT");
inspectionSideCombo->addItem("反面", "BACK");
inspectionSideCombo->addItem("最终", "FINAL");
inspectionResultCombo = new QComboBox;
inspectionResultCombo->addItem("全部", "");
inspectionResultCombo->addItem("合格", "OK");
inspectionResultCombo->addItem("不合格", "NG");
inspectionResultCombo->addItem("错误", "ERROR");
inspectionResultCombo->addItem("未知", "UNKNOWN");
inspectionTable = UiHelpers::makeTable(
QStringList() << "时间" << "订单号" << "工件条码" << "托盘" << "位置"
<< "模式" << "检测面" << "结果" << "原始结果"
<< "来源" << "耗时" << "次数");
const int dateTimeWidth = UiHelpers::textColumnWidth(
inspectionTable, "2026-10-30 23:59:59");
UiHelpers::configureTableColumns(
inspectionTable,
QList<int>() << dateTimeWidth << 110 << 150 << 85 << 65 << 90
<< 75 << 75 << 85 << 90 << 90 << 65);
inspectionMessageLabel = new QLabel;
}
void QualityPage::setupStatisticsTab()
{
statisticsOrderEdit = new QLineEdit;
statisticsOrderEdit->setPlaceholderText("留空统计全部订单");
planValueLabel = makeValueLabel();
completedValueLabel = makeValueLabel();
pendingValueLabel = makeValueLabel();
okValueLabel = makeValueLabel();
ngValueLabel = makeValueLabel();
rateValueLabel = makeValueLabel();
inspectionCountValueLabel = makeValueLabel();
statisticsMessageLabel = new QLabel;
sideStatisticsTable = UiHelpers::makeTable(
QStringList() << "检测面" << "合格" << "不合格" << "记录数");
UiHelpers::configureTableColumns(
sideStatisticsTable, QList<int>() << 120 << 100 << 100 << 110);
addTableRow(sideStatisticsTable, QStringList() << "正面" << "-" << "-" << "-");
addTableRow(sideStatisticsTable, QStringList() << "反面" << "-" << "-" << "-");
}
void QualityPage::setupTraceTab()
{
traceBarcodeEdit = new QLineEdit;
traceBarcodeEdit->setPlaceholderText("输入完整工件条码");
traceInfoLabel = new QLabel("请输入工件条码查询生产追溯信息");
traceInfoLabel->setWordWrap(true);
traceMessageLabel = new QLabel;
traceInspectionTable = UiHelpers::makeTable(
QStringList() << "时间" << "模式" << "检测面" << "结果" << "原始结果"
<< "来源" << "耗时" << "次数");
const int dateTimeWidth = UiHelpers::textColumnWidth(
traceInspectionTable, "2026-10-30 23:59:59");
UiHelpers::configureTableColumns(
traceInspectionTable,
QList<int>() << dateTimeWidth << 90 << 75 << 75 << 85 << 90 << 90 << 65);
traceProcessTable = UiHelpers::makeTable(
QStringList() << "开始时间" << "工序" << "状态" << "结果" << "来源"
<< "PLC步骤" << "耗时" << "次数");
UiHelpers::configureTableColumns(
traceProcessTable,
QList<int>() << dateTimeWidth << 130 << 100 << 80 << 90 << 80 << 90 << 65);
traceHistoryTable = UiHelpers::makeTable(
QStringList() << "时间" << "状态变化" << "工序" << "来源"
<< "原因" << "耗时" << "说明");
UiHelpers::configureTableColumns(
traceHistoryTable,
QList<int>() << dateTimeWidth << 150 << 120 << 90 << 160 << 90 << 200);
}
void QualityPage::loadInspectionRecords()
{
bool slotOk = true;
const int trayPos = inspectionSlotEdit->text().trimmed().isEmpty()
? 0 : inspectionSlotEdit->text().trimmed().toInt(&slotOk);
if (!slotOk || trayPos < 1 || trayPos > 49) {
setMessage(inspectionMessageLabel, "位置号必须是 1 到 49", true);
return;
}
QString error;
const QList<InspectionRecord> records = QualityService::instance().listInspectionRecords(
inspectionOrderEdit->text(), inspectionBarcodeEdit->text(),
inspectionTrayEdit->text(), trayPos,
inspectionModeCombo->currentData().toString(),
inspectionSideCombo->currentData().toString(),
inspectionResultCombo->currentData().toString(), &error);
clearTable(inspectionTable);
if (!error.isEmpty()) {
setMessage(inspectionMessageLabel, "查询失败: " + error, true);
return;
}
for (const InspectionRecord &record : records) {
addTableRow(inspectionTable, QStringList()
<< formatTime(record.completedAt.isValid()
? record.completedAt : record.createdAt)
<< record.orderNo
<< record.barcode
<< record.trayCode
<< QString::number(record.trayPos)
<< modeText(record.inspectionMode)
<< sideText(record.inspectionSide)
<< resultText(record.resultState)
<< rawResultText(record.rawResult)
<< sourceText(record.source)
<< formatDuration(record.durationMs)
<< QString::number(record.attemptNo));
}
setMessage(inspectionMessageLabel,
QString("共 %1 条检测记录").arg(records.size()), false);
}
void QualityPage::loadStatistics()
{
QualityStatistics statistics;
QString error;
if (!QualityService::instance().queryStatistics(
statisticsOrderEdit->text(), &statistics, &error)) {
planValueLabel->setText("-");
completedValueLabel->setText("-");
pendingValueLabel->setText("-");
okValueLabel->setText("-");
ngValueLabel->setText("-");
rateValueLabel->setText("-");
inspectionCountValueLabel->setText("-");
setMessage(statisticsMessageLabel, "查询失败: " + error, true);
return;
}
planValueLabel->setText(QString::number(statistics.planNum));
completedValueLabel->setText(QString::number(statistics.completedCount));
pendingValueLabel->setText(QString::number(statistics.pendingCount));
okValueLabel->setText(QString::number(statistics.okCount));
ngValueLabel->setText(QString::number(statistics.ngCount));
rateValueLabel->setText(QString::number(statistics.qualifiedRate, 'f', 2) + "%");
inspectionCountValueLabel->setText(QString::number(statistics.inspectionCount));
sideStatisticsTable->setItem(0, 1,
new QTableWidgetItem(QString::number(statistics.frontOkCount)));
sideStatisticsTable->setItem(0, 2,
new QTableWidgetItem(QString::number(statistics.frontNgCount)));
sideStatisticsTable->setItem(0, 3,
new QTableWidgetItem(QString::number(
statistics.frontOkCount + statistics.frontNgCount)));
sideStatisticsTable->setItem(1, 1,
new QTableWidgetItem(QString::number(statistics.backOkCount)));
sideStatisticsTable->setItem(1, 2,
new QTableWidgetItem(QString::number(statistics.backNgCount)));
sideStatisticsTable->setItem(1, 3,
new QTableWidgetItem(QString::number(
statistics.backOkCount + statistics.backNgCount)));
setMessage(statisticsMessageLabel, "统计完成", false);
}
void QualityPage::loadTrace()
{
const QString barcode = traceBarcodeEdit->text().trimmed();
if (barcode.isEmpty()) {
setMessage(traceMessageLabel, "请输入工件条码", true);
return;
}
Workpiece workpiece;
Order order;
QList<InspectionRecord> inspections;
QList<QualityProcessRecord> processRecords;
QList<QualityStateHistory> stateHistory;
QString error;
if (!QualityService::instance().findWorkpieceTrace(
barcode, &workpiece, &order, &inspections, &processRecords,
&stateHistory, &error)) {
traceInfoLabel->setText("未找到工件追溯信息");
clearTable(traceInspectionTable);
clearTable(traceProcessTable);
clearTable(traceHistoryTable);
setMessage(traceMessageLabel, "查询失败: " + error, true);
return;
}
traceInfoLabel->setText(
QString("工件:%1 订单:%2 托盘:%3 位置:%4 状态:%5 当前工序:%6 检测模式:%7")
.arg(workpiece.barcode)
.arg(order.no)
.arg(workpiece.trayCode.isEmpty()
? QString::number(workpiece.trayId) : workpiece.trayCode)
.arg(workpiece.trayPos)
.arg(resultText(workpiece.state))
.arg(workpiece.processStep.isEmpty() ? "-" : workpiece.processStep)
.arg(modeText(order.inspectionMode)));
clearTable(traceInspectionTable);
for (const InspectionRecord &record : inspections) {
addTableRow(traceInspectionTable, QStringList()
<< formatTime(record.completedAt.isValid()
? record.completedAt : record.createdAt)
<< modeText(record.inspectionMode)
<< sideText(record.inspectionSide)
<< resultText(record.resultState)
<< rawResultText(record.rawResult)
<< sourceText(record.source)
<< formatDuration(record.durationMs)
<< QString::number(record.attemptNo));
}
clearTable(traceProcessTable);
for (const QualityProcessRecord &record : processRecords) {
addTableRow(traceProcessTable, QStringList()
<< formatTime(record.startedAt)
<< record.processStep
<< record.processState
<< resultText(record.resultState)
<< sourceText(record.source)
<< QString::number(record.plcStep)
<< formatDuration(record.durationMs)
<< QString::number(record.attemptNo));
}
clearTable(traceHistoryTable);
for (const QualityStateHistory &history : stateHistory) {
addTableRow(traceHistoryTable, QStringList()
<< formatTime(history.changedAt)
<< (history.fromState.isEmpty()
? history.toState
: history.fromState + " -> " + history.toState)
<< history.processStep
<< sourceText(history.source)
<< history.reason
<< formatDuration(history.elapsedMs)
<< history.message);
}
setMessage(traceMessageLabel,
QString("检测记录 %1 条,工序记录 %2 条,状态历史 %3 条")
.arg(inspections.size()).arg(processRecords.size()).arg(stateHistory.size()),
false);
}
void QualityPage::setMessage(QLabel *label, const QString &message, bool error)
{
label->setText(message);
label->setStyleSheet(error
? "QLabel { color: #c62828; }"
: "QLabel { color: #546e7a; }");
}
void QualityPage::clearTable(QTableWidget *table)
{
table->setRowCount(0);
}