feat(production): 修改订单加工位置不一致问题
-创建订单时解除所有占用 -修改数据库位置唯一约束 -严格按照订单起始位置分配 -订单启动时重新初始化位置
This commit is contained in:
+181
-85
@@ -9,8 +9,12 @@
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QSet>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QStringList>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace {
|
||||
|
||||
bool recordLog(const QString &action, const QString &content, const QString &status,
|
||||
@@ -35,6 +39,46 @@ QString workpieceBarcode(const QString &orderNo, int sequence)
|
||||
return QString("%1-%2").arg(orderNo).arg(sequence, 6, 10, QChar('0'));
|
||||
}
|
||||
|
||||
bool validateAllocationRange(const Order &order, const QList<Tray> &trays,
|
||||
QString *error)
|
||||
{
|
||||
const int startTrayIndex = order.startTrayNo - 1;
|
||||
const qint64 lastOffset = qint64(order.startSlotNo) - 1 + order.planNum - 1;
|
||||
const qint64 endTrayIndex = startTrayIndex + lastOffset / 49;
|
||||
if (startTrayIndex < 0 || lastOffset < 0 || startTrayIndex >= trays.size()
|
||||
|| endTrayIndex >= trays.size()) {
|
||||
if (error) *error = "订单起始位置后的托盘容量不足";
|
||||
return false;
|
||||
}
|
||||
for (int trayIndex = startTrayIndex; trayIndex <= endTrayIndex; ++trayIndex) {
|
||||
if (!trays.at(trayIndex).present) {
|
||||
if (error) *error = QString("托盘 %1 未在位").arg(trayIndex + 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ensureTrayPositionMigration(QString *error)
|
||||
{
|
||||
const QSqlDatabase database = DatabaseManager::instance().database();
|
||||
if (database.driverName() == "QSQLITE") return true;
|
||||
|
||||
QSqlQuery query(database);
|
||||
if (!query.exec("select to_regclass('public.ux_workpieces_tray_position')")
|
||||
|| !query.next()) {
|
||||
if (error) *error = query.lastError().text();
|
||||
return false;
|
||||
}
|
||||
if (!query.value(0).isNull()) {
|
||||
if (error) {
|
||||
*error = "请先执行 db/migrations/2026-11-09-reuse-tray-positions.sql";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
OrderService &OrderService::instance()
|
||||
@@ -125,20 +169,47 @@ bool OrderService::createOrder(Order *order, QString *error)
|
||||
return false;
|
||||
}
|
||||
|
||||
WorkpieceRepository workpieceRepository;
|
||||
TrayRepository trayRepository;
|
||||
QList<int> affectedTrayIds;
|
||||
int removedCount = 0;
|
||||
if (!workpieceRepository.removeUninspected(
|
||||
0, &affectedTrayIds, &removedCount, &transactionError)) {
|
||||
ProductionRunSnapshot activeRun;
|
||||
if (!ProductionRepository().findActiveRun(&activeRun, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "新建订单前清理未测量工件失败:" + transactionError;
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
for (const int trayId : affectedTrayIds) {
|
||||
if (!trayRepository.refreshStats(trayId, &transactionError)) {
|
||||
if (activeRun.id > 0) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "存在活动生产任务,请先终止或复位后再创建订单";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!ensureTrayPositionMigration(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) *error = transactionError;
|
||||
return false;
|
||||
}
|
||||
|
||||
const QList<Tray> trays = trayRepository.listTrays(&transactionError);
|
||||
if (!transactionError.isEmpty()) {
|
||||
database.rollback();
|
||||
if (error) *error = transactionError;
|
||||
return false;
|
||||
}
|
||||
if (!validateAllocationRange(*order, trays, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) *error = transactionError;
|
||||
return false;
|
||||
}
|
||||
int releasedCount = 0;
|
||||
if (!trayRepository.clearAllSlots(&releasedCount, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) *error = "新建订单前解除料位占用失败:" + transactionError;
|
||||
return false;
|
||||
}
|
||||
for (const Tray &tray : trays) {
|
||||
if (!trayRepository.refreshStats(tray.id, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = "重新统计料盘占用失败:" + transactionError;
|
||||
@@ -154,10 +225,10 @@ bool OrderService::createOrder(Order *order, QString *error)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (removedCount > 0
|
||||
&& !recordLog("清理未测量工件",
|
||||
QString("新建订单前清理 %1 个未测量工件")
|
||||
.arg(removedCount),
|
||||
if (releasedCount > 0
|
||||
&& !recordLog("解除料位占用",
|
||||
QString("新建订单前解除 %1 个料位占用,保留历史工件及检测数据")
|
||||
.arg(releasedCount),
|
||||
"成功", &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
@@ -217,6 +288,11 @@ bool OrderService::generateWorkpiecesAndAssign(int orderId, QString *error)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!ensureTrayPositionMigration(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) *error = transactionError;
|
||||
return false;
|
||||
}
|
||||
|
||||
WorkpieceRepository workpieceRepository;
|
||||
TrayRepository trayRepository;
|
||||
@@ -245,6 +321,11 @@ bool OrderService::generateWorkpiecesAndAssign(int orderId, QString *error)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!validateAllocationRange(order, trays, &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) *error = transactionError;
|
||||
return false;
|
||||
}
|
||||
for (const Tray &tray : trays) {
|
||||
if (!trayRepository.initializeSlots(tray.id, 7, 7, &transactionError)) {
|
||||
database.rollback();
|
||||
@@ -256,12 +337,8 @@ bool OrderService::generateWorkpiecesAndAssign(int orderId, QString *error)
|
||||
}
|
||||
|
||||
QSet<QString> existingBarcodes;
|
||||
int assignedCount = 0;
|
||||
for (const Workpiece &workpiece : workpieces) {
|
||||
existingBarcodes.insert(workpiece.barcode);
|
||||
if (workpiece.trayId > 0) {
|
||||
++assignedCount;
|
||||
}
|
||||
}
|
||||
|
||||
const int missingCount = order.planNum - workpieces.size();
|
||||
@@ -287,91 +364,66 @@ bool OrderService::generateWorkpiecesAndAssign(int orderId, QString *error)
|
||||
++nextSequence;
|
||||
}
|
||||
|
||||
const int requiredSlotCount = order.planNum - assignedCount;
|
||||
QList<TraySlot> emptySlots;
|
||||
if (requiredSlotCount > 0) {
|
||||
const int startTrayIndex = order.startTrayNo - 1;
|
||||
if (startTrayIndex < 0 || startTrayIndex >= trays.size()) {
|
||||
std::sort(workpieces.begin(), workpieces.end(),
|
||||
[](const Workpiece &left, const Workpiece &right) {
|
||||
return left.id < right.id;
|
||||
});
|
||||
const int startTrayIndex = order.startTrayNo - 1;
|
||||
|
||||
QList<int> touchedTrayIds;
|
||||
bool allocationChanged = missingCount > 0;
|
||||
for (int index = 0; index < workpieces.size(); ++index) {
|
||||
const int offset = order.startSlotNo - 1 + index;
|
||||
const int trayIndex = startTrayIndex + offset / 49;
|
||||
const int position = offset % 49 + 1;
|
||||
const Tray &tray = trays.at(trayIndex);
|
||||
const int rowNo = (position - 1) / 7 + 1;
|
||||
const int columnNo = (position - 1) % 7 + 1;
|
||||
const QList<TraySlot> traySlots = trayRepository.listSlots(tray.id, &transactionError);
|
||||
if (!transactionError.isEmpty() || traySlots.size() != 49) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = QString("Start tray %1 is outside the configured tray range")
|
||||
.arg(order.startTrayNo);
|
||||
}
|
||||
if (error) *error = transactionError.isEmpty() ? "托盘格位数据不完整" : transactionError;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int trayIndex = startTrayIndex;
|
||||
trayIndex < trays.size() && emptySlots.size() < requiredSlotCount;
|
||||
++trayIndex) {
|
||||
const Tray &tray = trays.at(trayIndex);
|
||||
if (!tray.present) {
|
||||
continue;
|
||||
const TraySlot &slot = traySlots.at(position - 1);
|
||||
const Workpiece &workpiece = workpieces.at(index);
|
||||
if (workpiece.trayId > 0) {
|
||||
if (workpiece.trayId != tray.id || workpiece.trayPos != position
|
||||
|| workpiece.rowNo != rowNo || workpiece.columnNo != columnNo
|
||||
|| workpiece.state != "PENDING"
|
||||
|| slot.workpieceId != workpiece.id || slot.state != "PENDING") {
|
||||
database.rollback();
|
||||
if (error) *error = "已有工件分配与订单起始位置不一致";
|
||||
return false;
|
||||
}
|
||||
|
||||
const QList<TraySlot> traySlots =
|
||||
trayRepository.listSlots(tray.id, &transactionError);
|
||||
if (!transactionError.isEmpty()) {
|
||||
} else {
|
||||
if (slot.workpieceId != 0 || slot.state != "EMPTY"
|
||||
|| !workpieceRepository.assignToSlot(workpiece.id, tray.id, position,
|
||||
rowNo, columnNo, &transactionError)
|
||||
|| !trayRepository.assignSlot(tray.id, rowNo, columnNo,
|
||||
workpiece.id, "PENDING", &transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
*error = transactionError.isEmpty()
|
||||
? QString("托盘 %1 位置 %2 已被占用").arg(trayIndex + 1).arg(position)
|
||||
: transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const TraySlot &slot : traySlots) {
|
||||
const int position = (slot.rowNo - 1) * 7 + slot.columnNo;
|
||||
if (trayIndex == startTrayIndex && position < order.startSlotNo) {
|
||||
continue;
|
||||
}
|
||||
if (slot.workpieceId == 0 && slot.state == "EMPTY") {
|
||||
emptySlots.append(slot);
|
||||
if (emptySlots.size() >= requiredSlotCount) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
allocationChanged = true;
|
||||
if (!touchedTrayIds.contains(tray.id)) {
|
||||
touchedTrayIds.append(tray.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!transactionError.isEmpty()) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (emptySlots.size() < requiredSlotCount) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = QString("Not enough empty slots: required %1, available %2")
|
||||
.arg(requiredSlotCount).arg(emptySlots.size());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<int> touchedTrayIds;
|
||||
int slotIndex = 0;
|
||||
for (const Workpiece &workpiece : workpieces) {
|
||||
if (workpiece.trayId > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const TraySlot &slot = emptySlots.at(slotIndex++);
|
||||
if (!workpieceRepository.assignToSlot(workpiece.id, slot.trayId,
|
||||
(slot.rowNo - 1) * 7 + slot.columnNo,
|
||||
slot.rowNo, slot.columnNo,
|
||||
&transactionError)
|
||||
|| !trayRepository.assignSlot(slot.trayId, slot.rowNo, slot.columnNo,
|
||||
workpiece.id, "PENDING",
|
||||
&transactionError)) {
|
||||
if (!allocationChanged) {
|
||||
if (!database.commit(&transactionError)) {
|
||||
database.rollback();
|
||||
if (error) {
|
||||
*error = transactionError;
|
||||
}
|
||||
if (error) *error = transactionError;
|
||||
return false;
|
||||
}
|
||||
if (!touchedTrayIds.contains(slot.trayId)) {
|
||||
touchedTrayIds.append(slot.trayId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const int trayId : touchedTrayIds) {
|
||||
@@ -398,6 +450,50 @@ bool OrderService::generateWorkpiecesAndAssign(int orderId, QString *error)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OrderService::validateWorkpieceAllocation(int orderId, QString *error)
|
||||
{
|
||||
Order order;
|
||||
if (!OrderRepository().findOrder(orderId, &order, error)) return false;
|
||||
QString validationError;
|
||||
const QList<Tray> trays = TrayRepository().listTrays(&validationError);
|
||||
QList<Workpiece> workpieces = WorkpieceRepository().listByOrder(orderId, &validationError);
|
||||
if (!validationError.isEmpty()) { if (error) *error = validationError; return false; }
|
||||
if (!validateAllocationRange(order, trays, error)) return false;
|
||||
if (workpieces.size() != order.planNum) {
|
||||
if (error) *error = "订单工件数量与计划数量不一致";
|
||||
return false;
|
||||
}
|
||||
const int startTrayIndex = order.startTrayNo - 1;
|
||||
for (int index = 0; index < order.planNum; ++index) {
|
||||
const int offset = order.startSlotNo - 1 + index;
|
||||
const int trayIndex = startTrayIndex + offset / 49;
|
||||
const int position = offset % 49 + 1;
|
||||
const Tray &tray = trays.at(trayIndex);
|
||||
const QList<TraySlot> traySlots = TrayRepository().listSlots(tray.id, &validationError);
|
||||
if (!validationError.isEmpty() || traySlots.size() != 49) {
|
||||
if (error) *error = validationError.isEmpty() ? "托盘格位数据不完整" : validationError;
|
||||
return false;
|
||||
}
|
||||
const TraySlot &slot = traySlots.at(position - 1);
|
||||
bool matched = false;
|
||||
for (const Workpiece &workpiece : workpieces) {
|
||||
if (workpiece.trayId == tray.id && workpiece.trayPos == position
|
||||
&& workpiece.rowNo == slot.rowNo && workpiece.columnNo == slot.columnNo
|
||||
&& slot.workpieceId == workpiece.id && slot.state == "PENDING"
|
||||
&& workpiece.state == "PENDING") {
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matched) {
|
||||
if (error) *error = QString("托盘 %1 位置 %2 的工件分配不一致")
|
||||
.arg(trayIndex + 1).arg(position);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OrderService::startOrder(int id, QString *error)
|
||||
{
|
||||
return transition(id, QStringList() << "NOT_START", "RUNNING", "开始订单", error);
|
||||
|
||||
Reference in New Issue
Block a user