#include "trayrepository.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(); } } } QList TrayRepository::listTrays(QString *error) { QList trays; if (!ensureOpen(error)) { return trays; } QSqlQuery query(DatabaseManager::instance().database()); if (!query.exec("select * from trays order by rack_row, rack_column")) { setError(query, error); return trays; } while (query.next()) { Tray tray; tray.id = query.value("id").toInt(); tray.code = query.value("code").toString(); tray.rfid = query.value("rfid").toString(); tray.rackRow = query.value("rack_row").toInt(); tray.rackColumn = query.value("rack_column").toInt(); tray.present = query.value("present").toBool(); tray.loadedCount = query.value("loaded_count").toInt(); tray.qualified = query.value("qualified").toInt(); tray.unqualified = query.value("unqualified").toInt(); tray.status = query.value("status").toString(); tray.createdAt = query.value("created_at").toDateTime(); tray.updatedAt = query.value("updated_at").toDateTime(); trays.append(tray); } return trays; } QList TrayRepository::listSlots(int trayId, QString *error) { QList items; if (!ensureOpen(error)) { return items; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("select * from tray_slots where tray_id = :tray_id order by row_no, column_no"); query.bindValue(":tray_id", trayId); if (!query.exec()) { setError(query, error); return items; } while (query.next()) { TraySlot slot; slot.id = query.value("id").toInt(); slot.trayId = query.value("tray_id").toInt(); slot.rowNo = query.value("row_no").toInt(); slot.columnNo = query.value("column_no").toInt(); slot.workpieceId = query.value("workpiece_id").toInt(); slot.state = query.value("state").toString(); items.append(slot); } return items; } QList TrayRepository::listEmptySlotsForAllocation(QString *error) { QList items; if (!ensureOpen(error)) { return items; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("select ts.id, ts.tray_id, ts.row_no, ts.column_no, " "ts.workpiece_id, ts.state " "from tray_slots ts " "join trays t on t.id = ts.tray_id " "where t.present = true " "and ts.workpiece_id is null " "and ts.state = 'EMPTY' " "order by t.rack_row, t.rack_column, ts.row_no, ts.column_no " "for update of ts skip locked"); if (!query.exec()) { setError(query, error); return items; } while (query.next()) { TraySlot slot; slot.id = query.value("id").toInt(); slot.trayId = query.value("tray_id").toInt(); slot.rowNo = query.value("row_no").toInt(); slot.columnNo = query.value("column_no").toInt(); slot.workpieceId = query.value("workpiece_id").toInt(); slot.state = query.value("state").toString(); items.append(slot); } return items; } bool TrayRepository::initializeSlots(int trayId, int rows, int columns, QString *error) { if (rows <= 0 || columns <= 0 || !ensureOpen(error)) { if (error && rows <= 0) { *error = "Tray row count must be greater than 0"; } else if (error && columns <= 0) { *error = "Tray column count must be greater than 0"; } return false; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("insert into tray_slots (tray_id, row_no, column_no, state) " "values (:tray_id, :row_no, :column_no, 'EMPTY') " "on conflict (tray_id, row_no, column_no) do nothing"); for (int row = 1; row <= rows; ++row) { for (int column = 1; column <= columns; ++column) { query.bindValue(":tray_id", trayId); query.bindValue(":row_no", row); query.bindValue(":column_no", column); if (!query.exec()) { setError(query, error); return false; } } } return true; } bool TrayRepository::assignSlot(int trayId, int rowNo, int columnNo, int workpieceId, const QString &state, QString *error) { if (!ensureOpen(error)) { return false; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("update tray_slots set workpiece_id = :workpiece_id, state = :state " "where tray_id = :tray_id and row_no = :row_no and column_no = :column_no " "and workpiece_id is null and state = 'EMPTY'"); query.bindValue(":workpiece_id", workpieceId); query.bindValue(":state", state); query.bindValue(":tray_id", trayId); query.bindValue(":row_no", rowNo); query.bindValue(":column_no", columnNo); if (!query.exec()) { setError(query, error); return false; } if (query.numRowsAffected() != 1) { if (error) { *error = "Tray slot is not empty or does not exist"; } return false; } return true; } bool TrayRepository::clearAllSlots(int *releasedCount, QString *error) { if (!ensureOpen(error)) { return false; } QSqlQuery query(DatabaseManager::instance().database()); if (!query.exec("update tray_slots set workpiece_id = null, state = 'EMPTY' " "where workpiece_id is not null or state <> 'EMPTY'")) { setError(query, error); return false; } if (releasedCount) { *releasedCount = query.numRowsAffected(); } return true; } bool TrayRepository::refreshStats(int trayId, QString *error) { if (!ensureOpen(error)) { return false; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("update trays set " "loaded_count = (select count(*) from tray_slots " "where tray_id = :tray_id and workpiece_id is not null), " "qualified = (select count(*) from tray_slots " "where tray_id = :tray_id and state = 'OK'), " "unqualified = (select count(*) from tray_slots " "where tray_id = :tray_id and state = 'NG'), " "status = case when exists (select 1 from tray_slots " "where tray_id = :tray_id and workpiece_id is not null) " "then 'PRESENT' when present then 'EMPTY' else 'ABSENT' end, " "updated_at = now() where id = :tray_id"); query.bindValue(":tray_id", trayId); if (!query.exec()) { setError(query, error); return false; } return query.numRowsAffected() == 1; } bool TrayRepository::upsertTray(const Tray &tray, QString *error) { if (!ensureOpen(error)) { return false; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("insert into trays " "(code, rfid, rack_row, rack_column, present, loaded_count, qualified, unqualified, status) " "values (:code, :rfid, :rack_row, :rack_column, :present, :loaded_count, :qualified, :unqualified, :status) " "on conflict (rack_row, rack_column) do update set " "code = excluded.code, rfid = excluded.rfid, present = excluded.present, " "loaded_count = excluded.loaded_count, qualified = excluded.qualified, " "unqualified = excluded.unqualified, status = excluded.status, updated_at = now()"); query.bindValue(":code", tray.code); query.bindValue(":rfid", tray.rfid); query.bindValue(":rack_row", tray.rackRow); query.bindValue(":rack_column", tray.rackColumn); query.bindValue(":present", tray.present); query.bindValue(":loaded_count", tray.loadedCount); query.bindValue(":qualified", tray.qualified); query.bindValue(":unqualified", tray.unqualified); query.bindValue(":status", tray.status); if (!query.exec()) { setError(query, error); return false; } return true; } bool TrayRepository::updateSlotState(int trayId, int rowNo, int columnNo, const QString &state, int workpieceId, QString *error) { if (!ensureOpen(error)) { return false; } QSqlQuery query(DatabaseManager::instance().database()); query.prepare("update tray_slots set state = :state " "where tray_id = :tray_id and row_no = :row_no " "and column_no = :column_no and workpiece_id = :workpiece_id"); query.bindValue(":tray_id", trayId); query.bindValue(":row_no", rowNo); query.bindValue(":column_no", columnNo); query.bindValue(":workpiece_id", workpieceId); query.bindValue(":state", state); if (!query.exec()) { setError(query, error); return false; } if (query.numRowsAffected() != 1) { if (error) *error = "Tray slot is no longer assigned to this workpiece"; return false; } return true; }