first commit
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
#include "workpiecerepository.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();
|
||||
}
|
||||
}
|
||||
|
||||
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 id from workpieces "
|
||||
"where order_id = :order_id and state = 'PENDING' "
|
||||
"order by tray_id nulls last, tray_pos, id "
|
||||
"for update 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()) {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user