740 lines
23 KiB
C++
740 lines
23 KiB
C++
#include "orderservice.h"
|
||
|
||
#include "../db/databasemanager.h"
|
||
#include "../repositories/logrepository.h"
|
||
#include "../repositories/orderrepository.h"
|
||
#include "../repositories/productionrepository.h"
|
||
#include "../repositories/trayrepository.h"
|
||
#include "../repositories/workpiecerepository.h"
|
||
|
||
#include <QDateTime>
|
||
#include <QSet>
|
||
#include <QSqlError>
|
||
#include <QSqlQuery>
|
||
#include <QStringList>
|
||
|
||
#include <algorithm>
|
||
|
||
namespace {
|
||
|
||
bool recordLog(const QString &action, const QString &content, const QString &status,
|
||
QString *error)
|
||
{
|
||
LogRecord record;
|
||
record.type = "操作日志";
|
||
record.module = "订单管理";
|
||
record.action = action;
|
||
record.content = content;
|
||
record.status = status;
|
||
return LogRepository().insertLog(record, error);
|
||
}
|
||
|
||
bool containsState(const QStringList &states, const QString &state)
|
||
{
|
||
return states.contains(state);
|
||
}
|
||
|
||
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()
|
||
{
|
||
static OrderService service;
|
||
return service;
|
||
}
|
||
|
||
QList<Order> OrderService::listOrders(QString *error, int limit)
|
||
{
|
||
return OrderRepository().listOrders(error, limit);
|
||
}
|
||
|
||
bool OrderService::findOrder(int id, Order *order, QString *error)
|
||
{
|
||
return OrderRepository().findOrder(id, order, error);
|
||
}
|
||
|
||
bool OrderService::findUnfinishedOrder(Order *order, QString *error)
|
||
{
|
||
return OrderRepository().findUnfinishedOrder(order, error);
|
||
}
|
||
|
||
bool OrderService::createOrder(Order *order, QString *error)
|
||
{
|
||
if (!order) {
|
||
if (error) {
|
||
*error = "Order pointer is null";
|
||
}
|
||
return false;
|
||
}
|
||
if (order->no.trimmed().isEmpty()) {
|
||
if (error) {
|
||
*error = "Order number cannot be empty";
|
||
}
|
||
return false;
|
||
}
|
||
if (order->planNum <= 0) {
|
||
if (error) {
|
||
*error = "Order plan quantity must be greater than 0";
|
||
}
|
||
return false;
|
||
}
|
||
if (order->inspectionMode != "BEFORE_PLATING"
|
||
&& order->inspectionMode != "AFTER_PLATING") {
|
||
if (error) {
|
||
*error = "Inspection mode must be BEFORE_PLATING or AFTER_PLATING";
|
||
}
|
||
return false;
|
||
}
|
||
if (order->startTrayNo <= 0
|
||
|| order->startSlotNo < 1 || order->startSlotNo > 49) {
|
||
if (error) {
|
||
*error = "Start tray or slot is invalid";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
order->no = order->no.trimmed();
|
||
order->status = "NOT_START";
|
||
order->qualified = 0;
|
||
order->unqualified = 0;
|
||
|
||
DatabaseManager &database = DatabaseManager::instance();
|
||
QString transactionError;
|
||
if (!database.transaction(&transactionError)) {
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
OrderRepository repository;
|
||
Order unfinishedOrder;
|
||
if (!repository.findUnfinishedOrder(&unfinishedOrder, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (unfinishedOrder.id > 0) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = QString("Unfinished order already exists: %1")
|
||
.arg(unfinishedOrder.no);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
TrayRepository trayRepository;
|
||
ProductionRunSnapshot activeRun;
|
||
if (!ProductionRepository().findActiveRun(&activeRun, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
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;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
if (!repository.insertOrder(order, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "写入新订单失败:" + transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (releasedCount > 0
|
||
&& !recordLog("解除料位占用",
|
||
QString("新建订单前解除 %1 个料位占用,保留历史工件及检测数据")
|
||
.arg(releasedCount),
|
||
"成功", &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "记录清理日志失败:" + transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (!recordLog("新建订单", "创建订单:" + order->no,
|
||
"成功", &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "记录新建订单日志失败:" + transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (!database.commit(&transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "提交新建订单事务失败:" + transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool OrderService::generateWorkpiecesAndAssign(int orderId, QString *error)
|
||
{
|
||
if (orderId <= 0) {
|
||
if (error) {
|
||
*error = "Order id must be greater than 0";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
DatabaseManager &database = DatabaseManager::instance();
|
||
QString transactionError;
|
||
if (!database.transaction(&transactionError)) {
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
OrderRepository orderRepository;
|
||
Order order;
|
||
if (!orderRepository.findOrderForUpdate(orderId, &order, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (order.status != "NOT_START") {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "Only not-started orders can generate workpieces";
|
||
}
|
||
return false;
|
||
}
|
||
if (!ensureTrayPositionMigration(&transactionError)) {
|
||
database.rollback();
|
||
if (error) *error = transactionError;
|
||
return false;
|
||
}
|
||
|
||
WorkpieceRepository workpieceRepository;
|
||
TrayRepository trayRepository;
|
||
QList<Workpiece> workpieces =
|
||
workpieceRepository.listByOrder(orderId, &transactionError);
|
||
if (!transactionError.isEmpty()) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (workpieces.size() > order.planNum) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "Existing workpieces exceed order plan quantity";
|
||
}
|
||
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;
|
||
}
|
||
for (const Tray &tray : trays) {
|
||
if (!trayRepository.initializeSlots(tray.id, 7, 7, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
QSet<QString> existingBarcodes;
|
||
for (const Workpiece &workpiece : workpieces) {
|
||
existingBarcodes.insert(workpiece.barcode);
|
||
}
|
||
|
||
const int missingCount = order.planNum - workpieces.size();
|
||
int nextSequence = 1;
|
||
for (int index = 0; index < missingCount; ++index) {
|
||
while (existingBarcodes.contains(workpieceBarcode(order.no, nextSequence))) {
|
||
++nextSequence;
|
||
}
|
||
|
||
Workpiece workpiece;
|
||
workpiece.barcode = workpieceBarcode(order.no, nextSequence);
|
||
workpiece.orderId = order.id;
|
||
workpiece.state = "PENDING";
|
||
if (!workpieceRepository.insertWorkpiece(&workpiece, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
workpieces.append(workpiece);
|
||
existingBarcodes.insert(workpiece.barcode);
|
||
++nextSequence;
|
||
}
|
||
|
||
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 = transactionError.isEmpty() ? "托盘格位数据不完整" : transactionError;
|
||
return false;
|
||
}
|
||
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;
|
||
}
|
||
} 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.isEmpty()
|
||
? QString("托盘 %1 位置 %2 已被占用").arg(trayIndex + 1).arg(position)
|
||
: transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
allocationChanged = true;
|
||
if (!touchedTrayIds.contains(tray.id)) {
|
||
touchedTrayIds.append(tray.id);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!allocationChanged) {
|
||
if (!database.commit(&transactionError)) {
|
||
database.rollback();
|
||
if (error) *error = transactionError;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
for (const int trayId : touchedTrayIds) {
|
||
if (!trayRepository.refreshStats(trayId, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
if (!orderRepository.refreshProgress(orderId, &transactionError)
|
||
|| !recordLog("生成工件", QString("订单 %1 生成并分配 %2 个工件")
|
||
.arg(order.no).arg(workpieces.size()), "成功",
|
||
&transactionError)
|
||
|| !database.commit(&transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
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);
|
||
}
|
||
|
||
bool OrderService::pauseOrder(int id, QString *error)
|
||
{
|
||
return transition(id, QStringList() << "RUNNING", "PAUSED", "暂停订单", error);
|
||
}
|
||
|
||
bool OrderService::resumeOrder(int id, QString *error)
|
||
{
|
||
return transition(id, QStringList() << "PAUSED", "RUNNING", "恢复订单", error);
|
||
}
|
||
|
||
bool OrderService::completeOrder(int id, QString *error)
|
||
{
|
||
DatabaseManager &database = DatabaseManager::instance();
|
||
QString transactionError;
|
||
if (!database.transaction(&transactionError)) {
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
OrderRepository repository;
|
||
if (!repository.refreshProgress(id, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
Order order;
|
||
if (!repository.findOrder(id, &order, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (!containsState(QStringList() << "RUNNING" << "PAUSED", order.status)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "Only running or paused orders can be completed";
|
||
}
|
||
return false;
|
||
}
|
||
if (order.qualified + order.unqualified < order.planNum) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "Order is not fully processed";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
if (!repository.updateOrderLifecycle(id, "FINISHED", order.beginTime,
|
||
QDateTime::currentDateTime(), &transactionError)
|
||
|| !recordLog("完成订单", "完成订单:" + order.no, "成功", &transactionError)
|
||
|| !database.commit(&transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool OrderService::cancelOrder(int id, QString *error)
|
||
{
|
||
return transition(id, QStringList() << "NOT_START" << "PAUSED",
|
||
"CANCELED", "取消订单", error);
|
||
}
|
||
|
||
bool OrderService::terminateOrder(int id, QString *error)
|
||
{
|
||
return transition(id, QStringList() << "NOT_START" << "RUNNING" << "PAUSED",
|
||
"FINISHED", "终止订单(剩余工件不再执行)", error);
|
||
}
|
||
|
||
bool OrderService::deleteOrder(int id, QString *error)
|
||
{
|
||
DatabaseManager &database = DatabaseManager::instance();
|
||
QString transactionError;
|
||
if (!database.transaction(&transactionError)) {
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
OrderRepository repository;
|
||
Order order;
|
||
if (!repository.findOrderForUpdate(id, &order, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
QString latestRunState;
|
||
bool hasActiveRun = false;
|
||
if (!ProductionRepository().lockOrderRuns(
|
||
id, &latestRunState, &hasActiveRun, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (hasActiveRun) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "订单存在活动生产任务,请先在控制面板终止订单;终止完成后,无测量记录的订单会自动删除";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
const bool manuallyStopped = order.status == "FINISHED"
|
||
&& latestRunState == "STOPPED";
|
||
if (order.status == "COMPLETED"
|
||
|| (order.status == "FINISHED" && !manuallyStopped)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "已完成订单不能删除";
|
||
}
|
||
return false;
|
||
}
|
||
if (order.status != "NOT_START" && order.status != "CANCELED"
|
||
&& !manuallyStopped) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "当前订单不能直接删除,请先在控制面板安全终止订单";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
WorkpieceRepository workpieceRepository;
|
||
bool hasInspectionRecords = false;
|
||
if (!workpieceRepository.orderHasInspectionRecords(
|
||
id, &hasInspectionRecords, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (hasInspectionRecords) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = "订单已有测量记录,必须保留检测追溯,不能删除";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
QList<int> affectedTrayIds;
|
||
int removedCount = 0;
|
||
if (!workpieceRepository.removeUninspected(
|
||
id, &affectedTrayIds, &removedCount, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
TrayRepository trayRepository;
|
||
for (const int trayId : affectedTrayIds) {
|
||
if (!trayRepository.refreshStats(trayId, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
if (!repository.removeOrder(id, &transactionError)
|
||
|| !recordLog("删除订单",
|
||
QString("删除订单:%1,释放 %2 个未测量工件")
|
||
.arg(order.no).arg(removedCount),
|
||
"成功", &transactionError)
|
||
|| !database.commit(&transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool OrderService::transition(int id, const QStringList &allowedStates,
|
||
const QString &targetState, const QString &action,
|
||
QString *error)
|
||
{
|
||
DatabaseManager &database = DatabaseManager::instance();
|
||
QString transactionError;
|
||
if (!database.transaction(&transactionError)) {
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
OrderRepository repository;
|
||
Order order;
|
||
if (!repository.findOrder(id, &order, &transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
if (!containsState(allowedStates, order.status)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = QString("Invalid order state transition: %1 -> %2")
|
||
.arg(order.status, targetState);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
const QDateTime beginTime = targetState == "RUNNING" && !order.beginTime.isValid()
|
||
? QDateTime::currentDateTime() : order.beginTime;
|
||
const QDateTime endTime = (targetState == "CANCELED" || targetState == "FINISHED")
|
||
? QDateTime::currentDateTime() : order.endTime;
|
||
|
||
if (!repository.updateOrderLifecycle(id, targetState, beginTime, endTime, &transactionError)
|
||
|| !recordLog(action, QString("%1:%2").arg(action, order.no),
|
||
"成功", &transactionError)
|
||
|| !database.commit(&transactionError)) {
|
||
database.rollback();
|
||
if (error) {
|
||
*error = transactionError;
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|