320 lines
11 KiB
C++
320 lines
11 KiB
C++
#include "workpiecerepository.h"
|
|
|
|
#include "../db/databasemanager.h"
|
|
|
|
#include <QSqlError>
|
|
#include <QSqlQuery>
|
|
#include <QStringList>
|
|
#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();
|
|
}
|
|
}
|
|
|
|
Workpiece readWorkpiece(QSqlQuery &query)
|
|
{
|
|
Workpiece workpiece;
|
|
workpiece.id = query.value("id").toInt();
|
|
workpiece.barcode = query.value("barcode").toString();
|
|
workpiece.orderId = query.value("order_id").toInt();
|
|
workpiece.trayId = query.value("tray_id").toInt();
|
|
workpiece.trayPos = query.value("tray_pos").toInt();
|
|
workpiece.rowNo = query.value("row_no").toInt();
|
|
workpiece.columnNo = query.value("column_no").toInt();
|
|
workpiece.state = query.value("state").toString();
|
|
workpiece.processStep = query.value("process_step").toString();
|
|
workpiece.createdAt = query.value("created_at").toDateTime();
|
|
workpiece.updatedAt = query.value("updated_at").toDateTime();
|
|
return workpiece;
|
|
}
|
|
|
|
QList<Workpiece> listWithSql(const QString &sql, int id, QString *error)
|
|
{
|
|
QList<Workpiece> workpieces;
|
|
if (!ensureOpen(error)) {
|
|
return workpieces;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare(sql);
|
|
query.bindValue(":id", id);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return workpieces;
|
|
}
|
|
|
|
while (query.next()) {
|
|
workpieces.append(readWorkpiece(query));
|
|
}
|
|
return workpieces;
|
|
}
|
|
|
|
}
|
|
|
|
QList<Workpiece> WorkpieceRepository::listByOrder(int orderId, QString *error)
|
|
{
|
|
return listWithSql("select * from workpieces where order_id = :id order by tray_id, tray_pos", orderId, error);
|
|
}
|
|
|
|
QList<Workpiece> WorkpieceRepository::listByTray(int trayId, QString *error)
|
|
{
|
|
return listWithSql("select * from workpieces where tray_id = :id order by tray_pos", trayId, error);
|
|
}
|
|
|
|
bool WorkpieceRepository::findWorkpiece(int id, Workpiece *workpiece, QString *error)
|
|
{
|
|
if (!workpiece || !ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("select * from workpieces where id = :id");
|
|
query.bindValue(":id", id);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
if (!query.next()) {
|
|
if (error) {
|
|
*error = QString("Workpiece not found: %1").arg(id);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
*workpiece = readWorkpiece(query);
|
|
return true;
|
|
}
|
|
|
|
bool WorkpieceRepository::claimNextPending(int orderId, Workpiece *workpiece,
|
|
QString *error)
|
|
{
|
|
if (!workpiece || !ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
*workpiece = Workpiece();
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("with candidate as ("
|
|
"select w.id from workpieces w "
|
|
"join trays t on t.id = w.tray_id "
|
|
"join tray_slots ts on ts.tray_id = w.tray_id "
|
|
"and ts.row_no = w.row_no and ts.column_no = w.column_no "
|
|
"and ts.workpiece_id = w.id and ts.state = 'PENDING' "
|
|
"where w.order_id = :order_id and w.state = 'PENDING' "
|
|
"order by t.rack_row nulls last, t.rack_column nulls last, "
|
|
"w.tray_pos, w.id "
|
|
"for update of w skip locked limit 1"
|
|
") "
|
|
"update workpieces w set state = 'PROCESSING', "
|
|
"process_step = 'WAITING_DEVICE_READY', updated_at = now() "
|
|
"from candidate where w.id = candidate.id "
|
|
"returning w.*");
|
|
query.bindValue(":order_id", orderId);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
if (!query.next()) {
|
|
QSqlQuery pending(DatabaseManager::instance().database());
|
|
pending.prepare("select exists (select 1 from workpieces "
|
|
"where order_id = :order_id and state = 'PENDING')");
|
|
pending.bindValue(":order_id", orderId);
|
|
if (!pending.exec() || !pending.next()) {
|
|
setError(pending, error);
|
|
return false;
|
|
}
|
|
if (pending.value(0).toBool()) {
|
|
if (error) *error = "待加工工件与当前料位绑定不一致,不能继续生产";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
*workpiece = readWorkpiece(query);
|
|
return true;
|
|
}
|
|
|
|
bool WorkpieceRepository::insertWorkpiece(Workpiece *workpiece, QString *error)
|
|
{
|
|
if (!workpiece || !ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("insert into workpieces "
|
|
"(barcode, order_id, tray_id, tray_pos, row_no, column_no, state, process_step) "
|
|
"values (:barcode, :order_id, :tray_id, :tray_pos, :row_no, :column_no, :state, :process_step) "
|
|
"returning id");
|
|
query.bindValue(":barcode", workpiece->barcode);
|
|
query.bindValue(":order_id", workpiece->orderId > 0 ? QVariant(workpiece->orderId) : QVariant(QVariant::Int));
|
|
query.bindValue(":tray_id", workpiece->trayId > 0 ? QVariant(workpiece->trayId) : QVariant(QVariant::Int));
|
|
query.bindValue(":tray_pos", workpiece->trayPos);
|
|
query.bindValue(":row_no", workpiece->rowNo);
|
|
query.bindValue(":column_no", workpiece->columnNo);
|
|
query.bindValue(":state", workpiece->state.isEmpty() ? "PENDING" : workpiece->state);
|
|
query.bindValue(":process_step", workpiece->processStep);
|
|
|
|
if (!query.exec() || !query.next()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
workpiece->id = query.value(0).toInt();
|
|
return true;
|
|
}
|
|
|
|
bool WorkpieceRepository::assignToSlot(int id, int trayId, int trayPos,
|
|
int rowNo, int columnNo, QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("update workpieces set tray_id = :tray_id, tray_pos = :tray_pos, "
|
|
"row_no = :row_no, column_no = :column_no, updated_at = now() "
|
|
"where id = :id and (tray_id is null or tray_id = 0)");
|
|
query.bindValue(":tray_id", trayId);
|
|
query.bindValue(":tray_pos", trayPos);
|
|
query.bindValue(":row_no", rowNo);
|
|
query.bindValue(":column_no", columnNo);
|
|
query.bindValue(":id", id);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
if (query.numRowsAffected() != 1) {
|
|
if (error) {
|
|
*error = "Workpiece is already assigned to a tray";
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool WorkpieceRepository::updateState(int id, const QString &state, const QString &processStep,
|
|
QString *error)
|
|
{
|
|
if (!ensureOpen(error)) {
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("update workpieces set state = :state, process_step = :process_step, "
|
|
"updated_at = now() where id = :id");
|
|
query.bindValue(":state", state);
|
|
query.bindValue(":process_step", processStep);
|
|
query.bindValue(":id", id);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool WorkpieceRepository::orderHasInspectionRecords(int orderId, bool *hasRecords,
|
|
QString *error)
|
|
{
|
|
if (orderId <= 0 || !hasRecords || !ensureOpen(error)) {
|
|
if (error && orderId <= 0) {
|
|
*error = "Order id must be greater than 0";
|
|
} else if (error && !hasRecords) {
|
|
*error = "Inspection record result pointer is null";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("select exists (select 1 from inspection_records i "
|
|
"left join workpieces w on w.id = i.workpiece_id "
|
|
"where i.order_id = :order_id or w.order_id = :order_id)");
|
|
query.bindValue(":order_id", orderId);
|
|
if (!query.exec() || !query.next()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
*hasRecords = query.value(0).toBool();
|
|
return true;
|
|
}
|
|
|
|
bool WorkpieceRepository::removeUninspected(int orderId,
|
|
QList<int> *affectedTrayIds,
|
|
int *removedCount,
|
|
QString *error)
|
|
{
|
|
if (!affectedTrayIds || !removedCount || !ensureOpen(error)) {
|
|
if (error && !affectedTrayIds) {
|
|
*error = "Affected tray result pointer is null";
|
|
} else if (error && !removedCount) {
|
|
*error = "Removed count result pointer is null";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
affectedTrayIds->clear();
|
|
*removedCount = 0;
|
|
const QString scope = orderId > 0 ? " and w.order_id = :order_id" : QString();
|
|
const QString deleteScope = orderId > 0
|
|
? " and workpieces.order_id = :order_id" : QString();
|
|
const QString candidates =
|
|
"select w.id from workpieces w "
|
|
"where not exists (select 1 from inspection_records i "
|
|
"where i.workpiece_id = w.id)" + scope;
|
|
|
|
QSqlQuery trays(DatabaseManager::instance().database());
|
|
trays.prepare("select distinct w.tray_id from workpieces w "
|
|
"where w.tray_id is not null and not exists "
|
|
"(select 1 from inspection_records i where i.workpiece_id = w.id)"
|
|
+ scope);
|
|
if (orderId > 0) trays.bindValue(":order_id", orderId);
|
|
if (!trays.exec()) {
|
|
setError(trays, error);
|
|
return false;
|
|
}
|
|
while (trays.next()) {
|
|
affectedTrayIds->append(trays.value(0).toInt());
|
|
}
|
|
|
|
const QStringList statements = QStringList()
|
|
<< "update production_runs set current_workpiece_id = null "
|
|
"where current_workpiece_id in (" + candidates + ")"
|
|
<< "update alarm_records set workpiece_id = null "
|
|
"where workpiece_id in (" + candidates + ")"
|
|
<< "delete from production_state_history where workpiece_id in ("
|
|
+ candidates + ")"
|
|
<< "delete from workpiece_process_records where workpiece_id in ("
|
|
+ candidates + ")"
|
|
<< "update tray_slots set workpiece_id = null, state = 'EMPTY' "
|
|
"where workpiece_id in (" + candidates + ")";
|
|
for (const QString &statement : statements) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare(statement);
|
|
if (orderId > 0) query.bindValue(":order_id", orderId);
|
|
if (!query.exec()) {
|
|
setError(query, error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
QSqlQuery remove(DatabaseManager::instance().database());
|
|
remove.prepare("delete from workpieces where not exists "
|
|
"(select 1 from inspection_records i "
|
|
"where i.workpiece_id = workpieces.id)" + deleteScope);
|
|
if (orderId > 0) remove.bindValue(":order_id", orderId);
|
|
if (!remove.exec()) {
|
|
setError(remove, error);
|
|
return false;
|
|
}
|
|
*removedCount = remove.numRowsAffected();
|
|
return true;
|
|
}
|