423 lines
14 KiB
C++
423 lines
14 KiB
C++
#include "workpieceservice.h"
|
|||
|
|
|
||
|
|
#include "../db/databasemanager.h"
|
||
|
|
#include "../repositories/logrepository.h"
|
||
|
|
#include "../repositories/orderrepository.h"
|
||
|
|
#include "../repositories/productionrepository.h"
|
||
|
|
#include "../repositories/trayrepository.h"
|
||
|
|
#include "../repositories/workpiecerepository.h"
|
||
|
|
#include "trayservice.h"
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
bool recordLog(const QString &action, const QString &content, QString *error)
|
||
|
|
{
|
||
|
|
LogRecord record;
|
||
|
|
record.type = "生产日志";
|
||
|
|
record.module = "工件管理";
|
||
|
|
record.action = action;
|
||
|
|
record.content = content;
|
||
|
|
record.status = "成功";
|
||
|
|
return LogRepository().insertLog(record, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool validSlotCoordinate(int rowNo, int columnNo)
|
||
|
|
{
|
||
|
|
return rowNo >= 1 && rowNo <= 7 && columnNo >= 1 && columnNo <= 7;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
WorkpieceService &WorkpieceService::instance()
|
||
|
|
{
|
||
|
|
static WorkpieceService service;
|
||
|
|
return service;
|
||
|
|
}
|
||
|
|
|
||
|
|
QList<Workpiece> WorkpieceService::listByOrder(int orderId, QString *error)
|
||
|
|
{
|
||
|
|
return WorkpieceRepository().listByOrder(orderId, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
QList<Workpiece> WorkpieceService::listByTray(int trayId, QString *error)
|
||
|
|
{
|
||
|
|
return WorkpieceRepository().listByTray(trayId, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::findWorkpiece(int id, Workpiece *workpiece, QString *error)
|
||
|
|
{
|
||
|
|
return WorkpieceRepository().findWorkpiece(id, workpiece, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::claimNextPending(int orderId, Workpiece *workpiece,
|
||
|
|
QString *error)
|
||
|
|
{
|
||
|
|
if (orderId <= 0 || !workpiece) {
|
||
|
|
if (error) {
|
||
|
|
*error = orderId <= 0 ? "Order id must be greater than 0"
|
||
|
|
: "Workpiece pointer is null";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
DatabaseManager &database = DatabaseManager::instance();
|
||
|
|
QString transactionError;
|
||
|
|
if (!database.transaction(&transactionError)) {
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
WorkpieceRepository workpieceRepository;
|
||
|
|
if (!workpieceRepository.claimNextPending(orderId, workpiece, &transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (workpiece->id <= 0) {
|
||
|
|
if (!database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (workpiece->trayId > 0
|
||
|
|
&& (!TrayRepository().updateSlotState(workpiece->trayId, workpiece->rowNo,
|
||
|
|
workpiece->columnNo, "PROCESSING",
|
||
|
|
workpiece->id, &transactionError)
|
||
|
|
|| !TrayRepository().refreshStats(workpiece->trayId, &transactionError))) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!OrderRepository().refreshProgress(orderId, &transactionError)
|
||
|
|
|| !recordLog("CLAIM_WORKPIECE",
|
||
|
|
QString("Claimed workpiece %1").arg(workpiece->barcode),
|
||
|
|
&transactionError)
|
||
|
|
|| !database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::createWorkpiece(Workpiece *workpiece, QString *error)
|
||
|
|
{
|
||
|
|
if (!workpiece || workpiece->barcode.trimmed().isEmpty()) {
|
||
|
|
if (error) {
|
||
|
|
*error = workpiece ? "Workpiece barcode cannot be empty"
|
||
|
|
: "Workpiece pointer is null";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
workpiece->barcode = workpiece->barcode.trimmed();
|
||
|
|
workpiece->state = "PENDING";
|
||
|
|
DatabaseManager &database = DatabaseManager::instance();
|
||
|
|
QString transactionError;
|
||
|
|
if (!database.transaction(&transactionError)) {
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!WorkpieceRepository().insertWorkpiece(workpiece, &transactionError)
|
||
|
|
|| !recordLog("创建工件", "创建工件:" + workpiece->barcode,
|
||
|
|
&transactionError)
|
||
|
|
|| !database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::assignToFirstEmptySlot(int workpieceId, int trayId,
|
||
|
|
QString *error)
|
||
|
|
{
|
||
|
|
TraySlot slot;
|
||
|
|
if (!TrayService::instance().findFirstEmptySlot(trayId, &slot, error)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return assignToSlot(workpieceId, trayId, slot.rowNo, slot.columnNo, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::assignToSlot(int workpieceId, int trayId, int rowNo,
|
||
|
|
int columnNo, QString *error)
|
||
|
|
{
|
||
|
|
if (!validSlotCoordinate(rowNo, columnNo)) {
|
||
|
|
if (error) {
|
||
|
|
*error = "Tray slot coordinate must be between 1 and 7";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
DatabaseManager &database = DatabaseManager::instance();
|
||
|
|
QString transactionError;
|
||
|
|
if (!database.transaction(&transactionError)) {
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
WorkpieceRepository workpieceRepository;
|
||
|
|
TrayRepository trayRepository;
|
||
|
|
Workpiece workpiece;
|
||
|
|
if (!workpieceRepository.findWorkpiece(workpieceId, &workpiece, &transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (workpiece.trayId > 0) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = "Workpiece is already assigned to a tray";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const QList<Tray> trays = trayRepository.listTrays(&transactionError);
|
||
|
|
Tray tray;
|
||
|
|
bool trayFound = false;
|
||
|
|
for (const Tray &candidate : trays) {
|
||
|
|
if (candidate.id == trayId) {
|
||
|
|
tray = candidate;
|
||
|
|
trayFound = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!trayFound || !tray.present) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = "Tray does not exist or is not present";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const int trayPos = (rowNo - 1) * 7 + columnNo;
|
||
|
|
if (!workpieceRepository.assignToSlot(workpieceId, trayId, trayPos,
|
||
|
|
rowNo, columnNo, &transactionError)
|
||
|
|
|| !trayRepository.assignSlot(trayId, rowNo, columnNo, workpieceId,
|
||
|
|
"PENDING", &transactionError)
|
||
|
|
|| !trayRepository.refreshStats(trayId, &transactionError)
|
||
|
|
|| (workpiece.orderId > 0
|
||
|
|
&& !OrderRepository().refreshProgress(workpiece.orderId, &transactionError))
|
||
|
|
|| !recordLog("分配工件", QString("工件 %1 分配到托盘 %2 [%3,%4]")
|
||
|
|
.arg(workpiece.barcode).arg(tray.code)
|
||
|
|
.arg(rowNo).arg(columnNo), &transactionError)
|
||
|
|
|| !database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::updateState(int id, const QString &state,
|
||
|
|
const QString &processStep, QString *error)
|
||
|
|
{
|
||
|
|
if (!isValidState(state)) {
|
||
|
|
if (error) {
|
||
|
|
*error = "Invalid workpiece state: " + state;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
DatabaseManager &database = DatabaseManager::instance();
|
||
|
|
QString transactionError;
|
||
|
|
if (!database.transaction(&transactionError)) {
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
WorkpieceRepository workpieceRepository;
|
||
|
|
Workpiece workpiece;
|
||
|
|
if (!workpieceRepository.findWorkpiece(id, &workpiece, &transactionError)
|
||
|
|
|| !workpieceRepository.updateState(id, state, processStep, &transactionError)
|
||
|
|
|| (workpiece.trayId > 0
|
||
|
|
&& (!TrayRepository().updateSlotState(workpiece.trayId, workpiece.rowNo,
|
||
|
|
workpiece.columnNo, state, id,
|
||
|
|
&transactionError)
|
||
|
|
|| !TrayRepository().refreshStats(workpiece.trayId, &transactionError)))
|
||
|
|
|| (workpiece.orderId > 0
|
||
|
|
&& !OrderRepository().refreshProgress(workpiece.orderId, &transactionError))
|
||
|
|
|| !recordLog("更新工件状态",
|
||
|
|
QString("工件 %1 状态更新为 %2").arg(workpiece.barcode, state),
|
||
|
|
&transactionError)
|
||
|
|
|| !database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::updateProcessStepWithRecord(
|
||
|
|
int id, const QString &processStep, int plcStep, const QString &source,
|
||
|
|
int durationMs, QString *error, bool forceRestart)
|
||
|
|
{
|
||
|
|
if (id <= 0 || processStep.trimmed().isEmpty()) {
|
||
|
|
if (error) {
|
||
|
|
*error = id <= 0 ? "Workpiece id must be greater than 0"
|
||
|
|
: "Process step cannot be empty";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
DatabaseManager &database = DatabaseManager::instance();
|
||
|
|
QString transactionError;
|
||
|
|
if (!database.transaction(&transactionError)) {
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
WorkpieceRepository workpieceRepository;
|
||
|
|
Workpiece workpiece;
|
||
|
|
if (!workpieceRepository.findWorkpiece(id, &workpiece, &transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const bool sameProcess = workpiece.processStep == processStep
|
||
|
|
&& !forceRestart;
|
||
|
|
if (!sameProcess
|
||
|
|
&& !workpiece.processStep.trimmed().isEmpty()
|
||
|
|
&& !ProductionRepository().finishWorkpieceProcess(
|
||
|
|
id, workpiece.processStep,
|
||
|
|
forceRestart ? "ERROR" : "UNKNOWN", durationMs,
|
||
|
|
&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!workpieceRepository.updateState(
|
||
|
|
id, "PROCESSING", processStep, &transactionError)
|
||
|
|
|| (workpiece.trayId > 0
|
||
|
|
&& (!TrayRepository().updateSlotState(
|
||
|
|
workpiece.trayId, workpiece.rowNo, workpiece.columnNo,
|
||
|
|
"PROCESSING", id, &transactionError)
|
||
|
|
|| !TrayRepository().refreshStats(
|
||
|
|
workpiece.trayId, &transactionError)))
|
||
|
|
|| (workpiece.orderId > 0
|
||
|
|
&& !OrderRepository().refreshProgress(
|
||
|
|
workpiece.orderId, &transactionError))
|
||
|
|
|| (!sameProcess
|
||
|
|
&& !ProductionRepository().startWorkpieceProcess(
|
||
|
|
workpiece.orderId, id, processStep, plcStep, source,
|
||
|
|
&transactionError))
|
||
|
|
|| !recordLog("更新工件工序",
|
||
|
|
QString("工件 %1 工序更新为 %2")
|
||
|
|
.arg(workpiece.barcode, processStep),
|
||
|
|
&transactionError)
|
||
|
|
|| !database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::completeWithInspection(
|
||
|
|
int id, const QString &state, const QString &processStep,
|
||
|
|
const QString &previousProcessStep, int durationMs, int rawResult,
|
||
|
|
const QString &resultState, int plcStep, const QString &source,
|
||
|
|
QString *error)
|
||
|
|
{
|
||
|
|
if (id <= 0) {
|
||
|
|
if (error) {
|
||
|
|
*error = "Workpiece id must be greater than 0";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (!isValidState(state) || state == "PENDING" || state == "PROCESSING") {
|
||
|
|
if (error) {
|
||
|
|
*error = "Completion state must be OK, NG or ERROR";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
DatabaseManager &database = DatabaseManager::instance();
|
||
|
|
QString transactionError;
|
||
|
|
if (!database.transaction(&transactionError)) {
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
WorkpieceRepository workpieceRepository;
|
||
|
|
Workpiece workpiece;
|
||
|
|
if (!workpieceRepository.findWorkpiece(id, &workpiece, &transactionError)
|
||
|
|
|| !workpieceRepository.updateState(
|
||
|
|
id, state, processStep, &transactionError)
|
||
|
|
|| (workpiece.trayId > 0
|
||
|
|
&& (!TrayRepository().updateSlotState(
|
||
|
|
workpiece.trayId, workpiece.rowNo, workpiece.columnNo,
|
||
|
|
state, id, &transactionError)
|
||
|
|
|| !TrayRepository().refreshStats(
|
||
|
|
workpiece.trayId, &transactionError)))
|
||
|
|
|| (workpiece.orderId > 0
|
||
|
|
&& !OrderRepository().refreshProgress(
|
||
|
|
workpiece.orderId, &transactionError))
|
||
|
|
|| (!previousProcessStep.trimmed().isEmpty()
|
||
|
|
&& !ProductionRepository().finishWorkpieceProcess(
|
||
|
|
id, previousProcessStep, resultState, durationMs,
|
||
|
|
&transactionError))
|
||
|
|
|| !ProductionRepository().insertInspection(
|
||
|
|
workpiece.orderId, id, "FINAL", rawResult, resultState,
|
||
|
|
plcStep, source, "Final production result", &transactionError)
|
||
|
|
|| !recordLog("完成工件检测",
|
||
|
|
QString("工件 %1 检测结果为 %2")
|
||
|
|
.arg(workpiece.barcode, resultState),
|
||
|
|
&transactionError)
|
||
|
|
|| !database.commit(&transactionError)) {
|
||
|
|
database.rollback();
|
||
|
|
if (error) {
|
||
|
|
*error = transactionError;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool WorkpieceService::isValidState(const QString &state) const
|
||
|
|
{
|
||
|
|
return state == "PENDING" || state == "PROCESSING"
|
||
|
|
|| state == "OK" || state == "NG" || state == "ERROR";
|
||
|
|
}
|