feat(production): 修改订单加工位置不一致问题
-创建订单时解除所有占用 -修改数据库位置唯一约束 -严格按照订单起始位置分配 -订单启动时重新初始化位置
This commit is contained in:
@@ -175,6 +175,24 @@ bool TrayRepository::assignSlot(int trayId, int rowNo, int columnNo, int workpie
|
||||
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)) {
|
||||
@@ -239,18 +257,21 @@ bool TrayRepository::updateSlotState(int trayId, int rowNo, int columnNo, const
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("insert into tray_slots (tray_id, row_no, column_no, workpiece_id, state) "
|
||||
"values (:tray_id, :row_no, :column_no, :workpiece_id, :state) "
|
||||
"on conflict (tray_id, row_no, column_no) do update set "
|
||||
"workpiece_id = excluded.workpiece_id, state = excluded.state");
|
||||
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 > 0 ? QVariant(workpieceId) : QVariant(QVariant::Int));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ public:
|
||||
QString *error = nullptr);
|
||||
bool assignSlot(int trayId, int rowNo, int columnNo, int workpieceId,
|
||||
const QString &state, QString *error = nullptr);
|
||||
bool clearAllSlots(int *releasedCount = nullptr,
|
||||
QString *error = nullptr);
|
||||
bool refreshStats(int trayId, QString *error = nullptr);
|
||||
bool upsertTray(const Tray &tray, QString *error = nullptr);
|
||||
bool updateSlotState(int trayId, int rowNo, int columnNo, const QString &state,
|
||||
|
||||
@@ -105,10 +105,15 @@ bool WorkpieceRepository::claimNextPending(int orderId, Workpiece *workpiece,
|
||||
*workpiece = Workpiece();
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("with candidate as ("
|
||||
"select id from workpieces "
|
||||
"where order_id = :order_id and state = 'PENDING' "
|
||||
"order by tray_id nulls last, tray_pos, id "
|
||||
"for update skip locked limit 1"
|
||||
"select w.id from workpieces w "
|
||||
"join trays t on t.id = w.tray_id "
|
||||
"join tray_slots ts on ts.tray_id = w.tray_id "
|
||||
"and ts.row_no = w.row_no and ts.column_no = w.column_no "
|
||||
"and ts.workpiece_id = w.id and ts.state = 'PENDING' "
|
||||
"where w.order_id = :order_id and w.state = 'PENDING' "
|
||||
"order by t.rack_row nulls last, t.rack_column nulls last, "
|
||||
"w.tray_pos, w.id "
|
||||
"for update of w skip locked limit 1"
|
||||
") "
|
||||
"update workpieces w set state = 'PROCESSING', "
|
||||
"process_step = 'WAITING_DEVICE_READY', updated_at = now() "
|
||||
@@ -120,6 +125,18 @@ bool WorkpieceRepository::claimNextPending(int orderId, Workpiece *workpiece,
|
||||
return false;
|
||||
}
|
||||
if (!query.next()) {
|
||||
QSqlQuery pending(DatabaseManager::instance().database());
|
||||
pending.prepare("select exists (select 1 from workpieces "
|
||||
"where order_id = :order_id and state = 'PENDING')");
|
||||
pending.bindValue(":order_id", orderId);
|
||||
if (!pending.exec() || !pending.next()) {
|
||||
setError(pending, error);
|
||||
return false;
|
||||
}
|
||||
if (pending.value(0).toBool()) {
|
||||
if (error) *error = "待加工工件与当前料位绑定不一致,不能继续生产";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user