#include "qualityrepository.h" #include "../db/databasemanager.h" #include #include #include namespace { bool ensureOpen(QString *error) { return DatabaseManager::instance().open(error); } void setError(const QSqlQuery &query, QString *error) { if (error) { *error = query.lastError().text(); } } void readInspectionRecord(QSqlQuery &query, InspectionRecord *record) { record->id = query.value("id").toInt(); record->orderId = query.value("order_id").toInt(); record->workpieceId = query.value("workpiece_id").toInt(); record->orderNo = query.value("order_no").toString(); record->barcode = query.value("barcode").toString(); record->trayId = query.value("tray_id").toInt(); record->trayCode = query.value("tray_code").toString(); record->trayPos = query.value("tray_pos").toInt(); record->inspectionSide = query.value("inspection_side").toString(); record->inspectionMode = query.value("inspection_mode").toString(); record->attemptNo = query.value("attempt_no").toInt(); record->rawResult = query.value("raw_result").isNull() ? -1 : query.value("raw_result").toInt(); record->resultState = query.value("result_state").toString(); record->inspectionState = query.value("inspection_state").toString(); record->source = query.value("source").toString(); record->plcStep = query.value("plc_step").toInt(); record->message = query.value("message").toString(); record->startedAt = query.value("started_at").toDateTime(); record->completedAt = query.value("completed_at").toDateTime(); record->durationMs = query.value("duration_ms").isNull() ? -1 : query.value("duration_ms").toInt(); record->createdAt = query.value("created_at").toDateTime(); } } QList QualityRepository::listInspectionRecords( const QString &orderNo, const QString &barcode, const QString &trayCode, int trayPos, const QString &inspectionMode, const QString &inspectionSide, const QString &resultState, QString *error) { QList records; if (!ensureOpen(error)) { return records; } QString sql = "select ir.id, ir.order_id, ir.workpiece_id, o.no as order_no, " "w.barcode, w.tray_id, t.code as tray_code, w.tray_pos, " "ir.inspection_side, ir.inspection_mode, ir.attempt_no, ir.raw_result, " "ir.result_state, ir.inspection_state, ir.source, ir.plc_step, " "ir.message, ir.started_at, ir.completed_at, ir.duration_ms, ir.created_at " "from inspection_records ir " "join workpieces w on w.id = ir.workpiece_id " "left join orders o on o.id = ir.order_id " "left join trays t on t.id = w.tray_id " "where 1 = 1"; QSqlQuery query(DatabaseManager::instance().database()); if (!orderNo.trimmed().isEmpty()) { sql += " and o.no ilike :order_no"; } if (!barcode.trimmed().isEmpty()) { sql += " and w.barcode ilike :barcode"; } if (!trayCode.trimmed().isEmpty()) { sql += " and t.code ilike :tray_code"; } if (trayPos > 0) { sql += " and w.tray_pos = :tray_pos"; } if (!inspectionMode.isEmpty()) { sql += " and ir.inspection_mode = :inspection_mode"; } if (!inspectionSide.isEmpty()) { sql += " and ir.inspection_side = :inspection_side"; } if (!resultState.isEmpty()) { sql += " and ir.result_state = :result_state"; } sql += " order by ir.created_at desc, ir.id desc"; query.prepare(sql); if (!orderNo.trimmed().isEmpty()) { query.bindValue(":order_no", "%" + orderNo.trimmed() + "%"); } if (!barcode.trimmed().isEmpty()) { query.bindValue(":barcode", "%" + barcode.trimmed() + "%"); } if (!trayCode.trimmed().isEmpty()) { query.bindValue(":tray_code", "%" + trayCode.trimmed() + "%"); } if (trayPos > 0) { query.bindValue(":tray_pos", trayPos); } if (!inspectionMode.isEmpty()) { query.bindValue(":inspection_mode", inspectionMode); } if (!inspectionSide.isEmpty()) { query.bindValue(":inspection_side", inspectionSide); } if (!resultState.isEmpty()) { query.bindValue(":result_state", resultState); } if (!query.exec()) { setError(query, error); return records; } while (query.next()) { InspectionRecord record; readInspectionRecord(query, &record); records.append(record); } return records; } bool QualityRepository::queryStatistics(const QString &orderNo, QualityStatistics *statistics, QString *error) { if (!statistics || !ensureOpen(error)) { return false; } *statistics = QualityStatistics(); int orderId = 0; if (!orderNo.trimmed().isEmpty()) { QSqlQuery orderQuery(DatabaseManager::instance().database()); orderQuery.prepare("select id, plan_num from orders where no = :no"); orderQuery.bindValue(":no", orderNo.trimmed()); if (!orderQuery.exec()) { setError(orderQuery, error); return false; } if (!orderQuery.next()) { if (error) { *error = "订单不存在: " + orderNo.trimmed(); } return false; } orderId = orderQuery.value("id").toInt(); statistics->planNum = orderQuery.value("plan_num").toInt(); } else { QSqlQuery planQuery(DatabaseManager::instance().database()); if (!planQuery.exec("select coalesce(sum(plan_num), 0) from orders")) { setError(planQuery, error); return false; } if (planQuery.next()) { statistics->planNum = planQuery.value(0).toInt(); } } QSqlQuery workpieceQuery(DatabaseManager::instance().database()); const QString workpieceSql = "select count(*) filter (where state in ('OK', 'NG')) as completed_count, " "count(*) filter (where state = 'OK') as ok_count, " "count(*) filter (where state = 'NG') as ng_count " "from workpieces " "where (:order_id = 0 or order_id = :order_id)"; workpieceQuery.prepare(workpieceSql); workpieceQuery.bindValue(":order_id", orderId); if (!workpieceQuery.exec() || !workpieceQuery.next()) { setError(workpieceQuery, error); return false; } statistics->completedCount = workpieceQuery.value("completed_count").toInt(); statistics->okCount = workpieceQuery.value("ok_count").toInt(); statistics->ngCount = workpieceQuery.value("ng_count").toInt(); statistics->pendingCount = qMax(0, statistics->planNum - statistics->completedCount); QSqlQuery inspectionQuery(DatabaseManager::instance().database()); inspectionQuery.prepare( "select count(*) as inspection_count, " "count(*) filter (where inspection_side = 'FRONT' and result_state = 'OK') as front_ok, " "count(*) filter (where inspection_side = 'FRONT' and result_state = 'NG') as front_ng, " "count(*) filter (where inspection_side = 'BACK' and result_state = 'OK') as back_ok, " "count(*) filter (where inspection_side = 'BACK' and result_state = 'NG') as back_ng " "from inspection_records " "where (:order_id = 0 or order_id = :order_id)"); inspectionQuery.bindValue(":order_id", orderId); if (!inspectionQuery.exec() || !inspectionQuery.next()) { setError(inspectionQuery, error); return false; } statistics->inspectionCount = inspectionQuery.value("inspection_count").toInt(); statistics->frontOkCount = inspectionQuery.value("front_ok").toInt(); statistics->frontNgCount = inspectionQuery.value("front_ng").toInt(); statistics->backOkCount = inspectionQuery.value("back_ok").toInt(); statistics->backNgCount = inspectionQuery.value("back_ng").toInt(); statistics->qualifiedRate = statistics->completedCount > 0 ? statistics->okCount * 100.0 / statistics->completedCount : 0.0; return true; } bool QualityRepository::findWorkpieceTrace( const QString &barcode, Workpiece *workpiece, Order *order, QList *inspections, QList *processRecords, QList *stateHistory, QString *error) { if (!workpiece || !order || !inspections || !processRecords || !stateHistory || !ensureOpen(error)) { return false; } *workpiece = Workpiece(); *order = Order(); inspections->clear(); processRecords->clear(); stateHistory->clear(); QSqlQuery workpieceQuery(DatabaseManager::instance().database()); workpieceQuery.prepare( "select w.id, w.barcode, w.order_id, w.tray_id, t.code as tray_code, " "w.tray_pos, w.row_no, " "w.column_no, w.state, w.process_step, w.created_at, w.updated_at, " "o.no as order_no, o.production_no, o.drawing_no, o.batch_no, " "o.plan_num, o.qualified, o.unqualified, o.status as order_status, " "o.inspection_mode, o.start_tray_no, o.start_slot_no, o.begin_time, " "o.end_time, o.remark, o.created_at as order_created_at, " "o.updated_at as order_updated_at " "from workpieces w left join orders o on o.id = w.order_id " "left join trays t on t.id = w.tray_id " "where w.barcode = :barcode"); workpieceQuery.bindValue(":barcode", barcode.trimmed()); if (!workpieceQuery.exec()) { setError(workpieceQuery, error); return false; } if (!workpieceQuery.next()) { if (error) { *error = "工件不存在: " + barcode.trimmed(); } return false; } workpiece->id = workpieceQuery.value("id").toInt(); workpiece->barcode = workpieceQuery.value("barcode").toString(); workpiece->orderId = workpieceQuery.value("order_id").toInt(); workpiece->trayId = workpieceQuery.value("tray_id").toInt(); workpiece->trayCode = workpieceQuery.value("tray_code").toString(); workpiece->trayPos = workpieceQuery.value("tray_pos").toInt(); workpiece->rowNo = workpieceQuery.value("row_no").toInt(); workpiece->columnNo = workpieceQuery.value("column_no").toInt(); workpiece->state = workpieceQuery.value("state").toString(); workpiece->processStep = workpieceQuery.value("process_step").toString(); workpiece->createdAt = workpieceQuery.value("created_at").toDateTime(); workpiece->updatedAt = workpieceQuery.value("updated_at").toDateTime(); order->id = workpieceQuery.value("order_id").toInt(); order->no = workpieceQuery.value("order_no").toString(); order->productionNo = workpieceQuery.value("production_no").toString(); order->drawingNo = workpieceQuery.value("drawing_no").toString(); order->batchNo = workpieceQuery.value("batch_no").toString(); order->planNum = workpieceQuery.value("plan_num").toInt(); order->qualified = workpieceQuery.value("qualified").toInt(); order->unqualified = workpieceQuery.value("unqualified").toInt(); order->status = workpieceQuery.value("order_status").toString(); order->inspectionMode = workpieceQuery.value("inspection_mode").toString(); order->startTrayNo = workpieceQuery.value("start_tray_no").toInt(); order->startSlotNo = workpieceQuery.value("start_slot_no").toInt(); order->beginTime = workpieceQuery.value("begin_time").toDateTime(); order->endTime = workpieceQuery.value("end_time").toDateTime(); order->remark = workpieceQuery.value("remark").toString(); order->createdAt = workpieceQuery.value("order_created_at").toDateTime(); order->updatedAt = workpieceQuery.value("order_updated_at").toDateTime(); QSqlQuery inspectionQuery(DatabaseManager::instance().database()); inspectionQuery.prepare( "select ir.id, ir.order_id, ir.workpiece_id, o.no as order_no, " "w.barcode, w.tray_id, t.code as tray_code, w.tray_pos, " "ir.inspection_side, ir.inspection_mode, ir.attempt_no, ir.raw_result, " "ir.result_state, ir.inspection_state, ir.source, ir.plc_step, " "ir.message, ir.started_at, ir.completed_at, ir.duration_ms, ir.created_at " "from inspection_records ir join workpieces w on w.id = ir.workpiece_id " "left join orders o on o.id = ir.order_id left join trays t on t.id = w.tray_id " "where ir.workpiece_id = :workpiece_id order by ir.created_at, ir.id"); inspectionQuery.bindValue(":workpiece_id", workpiece->id); if (!inspectionQuery.exec()) { setError(inspectionQuery, error); return false; } while (inspectionQuery.next()) { InspectionRecord record; readInspectionRecord(inspectionQuery, &record); inspections->append(record); } QSqlQuery processQuery(DatabaseManager::instance().database()); processQuery.prepare( "select process_step, attempt_no, process_state, result_state, plc_step, " "source, duration_ms, started_at, completed_at " "from workpiece_process_records where workpiece_id = :workpiece_id " "order by started_at, id"); processQuery.bindValue(":workpiece_id", workpiece->id); if (!processQuery.exec()) { setError(processQuery, error); return false; } while (processQuery.next()) { QualityProcessRecord record; record.processStep = processQuery.value("process_step").toString(); record.attemptNo = processQuery.value("attempt_no").toInt(); record.processState = processQuery.value("process_state").toString(); record.resultState = processQuery.value("result_state").toString(); record.plcStep = processQuery.value("plc_step").toInt(); record.source = processQuery.value("source").toString(); record.durationMs = processQuery.value("duration_ms").isNull() ? -1 : processQuery.value("duration_ms").toInt(); record.startedAt = processQuery.value("started_at").toDateTime(); record.completedAt = processQuery.value("completed_at").toDateTime(); processRecords->append(record); } QSqlQuery historyQuery(DatabaseManager::instance().database()); historyQuery.prepare( "select from_state, to_state, process_step, source, reason, message, " "elapsed_ms, changed_at from production_state_history " "where workpiece_id = :workpiece_id order by changed_at, id"); historyQuery.bindValue(":workpiece_id", workpiece->id); if (!historyQuery.exec()) { setError(historyQuery, error); return false; } while (historyQuery.next()) { QualityStateHistory history; history.fromState = historyQuery.value("from_state").toString(); history.toState = historyQuery.value("to_state").toString(); history.processStep = historyQuery.value("process_step").toString(); history.source = historyQuery.value("source").toString(); history.reason = historyQuery.value("reason").toString(); history.message = historyQuery.value("message").toString(); history.elapsedMs = historyQuery.value("elapsed_ms").isNull() ? -1 : historyQuery.value("elapsed_ms").toInt(); history.changedAt = historyQuery.value("changed_at").toDateTime(); stateHistory->append(history); } return true; }