feat(production): 删除动作记录、释放料位、删除工件及订单等

This commit is contained in:
lidg
2026-11-05 15:33:51 +08:00
parent 6da5622181
commit 753f9c3ed2
7 changed files with 239 additions and 22 deletions
+29 -11
View File
@@ -4,6 +4,7 @@
#include <QSqlError>
#include <QSqlQuery>
#include <QStringList>
#include <QVariant>
namespace {
@@ -156,15 +157,17 @@ bool OrderRepository::insertOrder(Order *order, QString *error)
return false;
}
QSqlQuery query(DatabaseManager::instance().database());
const QSqlDatabase database = DatabaseManager::instance().database();
const bool sqlite = DatabaseManager::instance().config().driverName == "QSQLITE";
QSqlQuery query(database);
query.prepare("insert into orders "
"(no, production_no, drawing_no, batch_no, plan_num, qualified, "
"unqualified, status, inspection_mode, start_tray_no, start_slot_no, "
"begin_time, end_time, remark) "
"values (:no, :production_no, :drawing_no, :batch_no, :plan_num, "
":qualified, :unqualified, :status, :inspection_mode, "
":start_tray_no, :start_slot_no, :begin_time, :end_time, :remark) "
"returning id");
":start_tray_no, :start_slot_no, :begin_time, :end_time, :remark)"
+ (sqlite ? QString() : QString(" returning id")));
query.bindValue(":no", order->no);
query.bindValue(":production_no", order->productionNo);
query.bindValue(":drawing_no", order->drawingNo);
@@ -180,11 +183,19 @@ bool OrderRepository::insertOrder(Order *order, QString *error)
query.bindValue(":end_time", order->endTime.isValid() ? QVariant(order->endTime) : QVariant(QVariant::DateTime));
query.bindValue(":remark", order->remark);
if (!query.exec() || !query.next()) {
if (!query.exec()) {
setError(query, error);
return false;
}
order->id = query.value(0).toInt();
if (sqlite) {
order->id = query.lastInsertId().toInt();
} else {
if (!query.next()) {
setError(query, error);
return false;
}
order->id = query.value(0).toInt();
}
return true;
}
@@ -257,12 +268,19 @@ bool OrderRepository::removeOrder(int id, QString *error)
return false;
}
QSqlQuery query(DatabaseManager::instance().database());
query.prepare("delete from orders where id = :id");
query.bindValue(":id", id);
if (!query.exec()) {
setError(query, error);
return false;
const QStringList statements = QStringList()
<< "delete from production_state_history where order_id = :id"
<< "delete from production_runs where order_id = :id"
<< "delete from alarm_records where order_id = :id"
<< "delete from orders where id = :id";
for (const QString &statement : statements) {
QSqlQuery query(DatabaseManager::instance().database());
query.prepare(statement);
query.bindValue(":id", id);
if (!query.exec()) {
setError(query, error);
return false;
}
}
return true;
}
+98
View File
@@ -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;
}
+4
View File
@@ -17,6 +17,10 @@ public:
QString *error = nullptr);
bool updateState(int id, const QString &state, const QString &processStep,
QString *error = nullptr);
bool orderHasInspectionRecords(int orderId, bool *hasRecords,
QString *error = nullptr);
bool removeUninspected(int orderId, QList<int> *affectedTrayIds,
int *removedCount, QString *error = nullptr);
};
#endif // WORKPIECEREPOSITORY_H