feat(production): 删除动作记录、释放料位、删除工件及订单等
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QStringList>
|
||||
#include <QVariant>
|
||||
|
||||
namespace {
|
||||
@@ -202,3 +203,100 @@ bool WorkpieceRepository::updateState(int id, const QString &state, const QStrin
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user