first commit
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
#include "trayservice.h"
|
||||
|
||||
#include "../db/databasemanager.h"
|
||||
#include "../repositories/logrepository.h"
|
||||
#include "../repositories/trayrepository.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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TrayService &TrayService::instance()
|
||||
{
|
||||
static TrayService service;
|
||||
return service;
|
||||
}
|
||||
|
||||
QList<Tray> TrayService::listTrays(QString *error)
|
||||
{
|
||||
return TrayRepository().listTrays(error);
|
||||
}
|
||||
|
||||
QList<TraySlot> TrayService::listSlots(int trayId, QString *error)
|
||||
{
|
||||
return TrayRepository().listSlots(trayId, error);
|
||||
}
|
||||
|
||||
bool TrayService::initializeTrayGrid(int trayId, QString *error)
|
||||
{
|
||||
DatabaseManager &database = DatabaseManager::instance();
|
||||
QString transactionError;
|
||||
if (!database.transaction(&transactionError)) {
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TrayRepository repository;
|
||||
if (!repository.initializeSlots(trayId, 7, 7, &transactionError)
|
||||
|| !recordLog("初始化托盘格位",
|
||||
QString("初始化托盘 %1 的 7x7 格位").arg(trayId),
|
||||
&transactionError)
|
||||
|| !database.commit(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TrayService::initializeAllTrayGrids(QString *error)
|
||||
{
|
||||
DatabaseManager &database = DatabaseManager::instance();
|
||||
QString transactionError;
|
||||
if (!database.transaction(&transactionError)) {
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TrayRepository repository;
|
||||
const QList<Tray> trays = repository.listTrays(&transactionError);
|
||||
if (!transactionError.isEmpty()) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
for (const Tray &tray : trays) {
|
||||
if (!repository.initializeSlots(tray.id, 7, 7, &transactionError)
|
||||
|| !repository.refreshStats(tray.id, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!recordLog("初始化托盘格位",
|
||||
QString("初始化 %1 个托盘的 7x7 格位").arg(trays.size()),
|
||||
&transactionError)
|
||||
|| !database.commit(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TrayService::ensureAllTrayGrids(QString *error)
|
||||
{
|
||||
DatabaseManager &database = DatabaseManager::instance();
|
||||
QString transactionError;
|
||||
if (!database.transaction(&transactionError)) {
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TrayRepository repository;
|
||||
const QList<Tray> trays = repository.listTrays(&transactionError);
|
||||
if (!transactionError.isEmpty()) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const Tray &tray : trays) {
|
||||
if (!repository.initializeSlots(tray.id, 7, 7, &transactionError)
|
||||
|| !repository.refreshStats(tray.id, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!database.commit(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TrayService::findFirstEmptySlot(int trayId, TraySlot *slot, QString *error)
|
||||
{
|
||||
if (!slot) {
|
||||
if (error) {
|
||||
*error = "Tray slot pointer is null";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const QList<TraySlot> slots = listSlots(trayId, error);
|
||||
if (error && !error->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (const TraySlot &candidate : slots) {
|
||||
if (candidate.workpieceId <= 0 && candidate.state == "EMPTY") {
|
||||
*slot = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
*error = "No empty slot found";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TrayService::refreshTrayStats(int trayId, QString *error)
|
||||
{
|
||||
return TrayRepository().refreshStats(trayId, error);
|
||||
}
|
||||
Reference in New Issue
Block a user