#include "firingwidget.h"

#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QSqlQuery>
#include <QTimeZone>
#include <QUrl>
#include <QUrlQuery>

#include "../backendapimanager.h"
#include "../global.h"
#include "../logger.h"
#include "ui_firingwidget.h"

#define ANSI_COLOR_GREEN "\x1B[32m"
#define ANSI_COLOR_RESET "\x1B[0m"

#define MSG_DATA_TYPE_BLASTING 1
#define MSG_DATA_TYPE_RECORD 2

firingWidget::firingWidget(const int &row, const bool &select, const QString &uuid, QWidget *parent)
    : QWidget(parent),
      ui(new Ui::firingWidget),
      m_curProjUuid(uuid),
      m_row(row),
      m_select(select),
      daoProj(DatabaseManager::getInstance().getDatabase()),
      daoDet(DatabaseManager::getInstance().getDatabase()),
      daoReg(DatabaseManager::getInstance().getDatabase()) {
    ui->setupUi(this);

    ui->pushButton_2->setText("立即测试");
    ui->textEdit->setStyleSheet("border: none;background: transparent;");
    ui->frame_2->setStyleSheet("border: none;background: transparent;");
    QFrame *frame = ui->frame_2;  // 使用你为 QFrame 设置的对象名称
    navProgress = new NavProgress(frame);
    ui->verticalLayout->setContentsMargins(0, 30, 0, 0);
    ui->scrollArea->setStyleSheet("background: rgba(0, 0, 0, 22); border-radius: 20px;");
    ui->verticalLayout->addWidget(navProgress);
    m_topic = "hxgc/" + uuid + "/B";
    QStringList topics = {m_topic};
    m_connectionCheckTimer = new QTimer(this);

    m_subscription = mainMqttClient->subscribeToTopic(m_topic);
    connect(m_subscription, &QMqttSubscription::messageReceived, this, &firingWidget::handleProjectFiringMqttMessage);

    connect(this, &firingWidget::lastStageChanged, this, &firingWidget::handleFiringStageChanged);
    connect(this, &firingWidget::updateData, this, &firingWidget::onBlastSuccess);
}

firingWidget::~firingWidget() {
    m_subscription->unsubscribe();
    delete ui;
}

void firingWidget::sendMqttMessage(const QString &topic, const QByteArray &message) {
    qDebug() << ANSI_COLOR_GREEN << "XXX Sending MQTT message to topic:" << topic << "with message:" << message
             << ANSI_COLOR_RESET;
    if (mainMqttClient->isConnected()) {
        mainMqttClient->sendMessage(topic, message, quint8(2), false);
    } else {
        QMessageBox::warning(nullptr, "MQTT未连接", "MQTT未连接, 无法发送消息: " + topic + ", " + message);
        Logger::getInstance().error("MQTT未连接, 无法发送消息" + topic + ", " + message);
    }
    qDebug() << ANSI_COLOR_GREEN << "XXXy Sending MQTT message to topic:" << topic << "with message:" << message;
}

void firingWidget::startBlasting() {
    // 等待3s mqtt连接后再发送mqtt消息
    m_lastStage = FiringStages::Starting;
    qDebug() << ANSI_COLOR_GREEN << "Firing widget started with topic: 1" << m_topic << ANSI_COLOR_RESET;
    emit lastStageChanged(FiringStages::Starting);
    qDebug() << ANSI_COLOR_GREEN << "Firing widget started with topic: 2" << m_topic << ANSI_COLOR_RESET;
    checkBlasterConnection();
}

// 定间隔检查有没有收到爆破器返回信息
void firingWidget::checkBlasterConnection() {
    m_lastMsgTime = QDateTime::currentDateTime();

    connect(m_subscription, &QMqttSubscription::messageReceived, this,
            [&]() { m_lastMsgTime = QDateTime::currentDateTime(); });
    connect(m_connectionCheckTimer, &QTimer::timeout, this, [this]() {
        if (m_lastStage == FiringStages::CancelConfirmed || m_lastStage == FiringStages::BlastFinished) {
            return;
        }
        if (m_lastMsgTime.secsTo(QDateTime::currentDateTime()) > 3) {
            m_connectionCheckTimer->stop();
            QMessageBox msgBox;
            msgBox.setWindowTitle("起爆检测测试错误");
            msgBox.setText("超时未收到爆破器消息，是否继续等待");
            QPushButton *yesButton = msgBox.addButton("继续等待", QMessageBox::YesRole);
            QPushButton *noButton = msgBox.addButton("取消", QMessageBox::NoRole);
            yesButton->setStyleSheet(
                "QPushButton { background-color:rgb(5, 58, 156); color: white; padding: 5px 15px; "
                "border-radius: 4px; }");
            noButton->setStyleSheet(
                "QPushButton { background-color: #f44336; color: white; padding: 5px 15px; "
                "border-radius: 4px; }");
            msgBox.exec();
            if (msgBox.clickedButton() == noButton) {
                sendCancelFiringMsg();
                emit closeFiring(m_curProjUuid, m_row);
                return;
            }
            m_connectionCheckTimer->start(1000);
        }
    });

    qDebug() << ANSI_COLOR_GREEN << "Firing widget initialized with topic: 6" << m_connectionCheckTimer
             << ANSI_COLOR_RESET;
    m_connectionCheckTimer->start(1000);
}
void firingWidget::on_pushButton_2_clicked() { emit countdown(m_curProjUuid, m_topic, "message"); }

// NOTE: aborted, changed to upload in the smartmine server side.
bool firingWidget::uploadToDanLing(const QJsonObject &jsonObj) {
    QString htid = jsonObj["project_htid"].toString();
    QString htm = "";  //
    QString xmbh = jsonObj["project_xmbh"].toString();
    QString sbbh = jsonObj["equipment_sn"].toString();
    QString bprysfz = jsonObj["operator_identity"].toString();
    QString dwdm = jsonObj["company_code"].toString();
    QString xtm = "";  //
    QString jd = jsonObj["blast_longitude"].toString();
    QString bpsj = jsonObj["blast_time"].toString();
    QString wd = jsonObj["blast_latitude"].toString();
    QStringList uidList;
    QJsonArray regs = jsonObj["regs"].toArray();
    for (const auto &reg : regs) {
        QJsonObject regObj = reg.toObject();
        QJsonArray dets = regObj["dets"].toArray();
        for (const auto &det : dets) {
            QJsonObject detObj = det.toObject();
            uidList.append(detObj["uid"].toString());
        }
    }
    QString uid = uidList.join(",");

    QString plainText = QString(
                            "{\"htid\":\"%1\",\"htm\":\"%2\",\"xmbh\":\"%3\",\"sbbh\":\"%4\","
                            "\"bprysfz\":\"%5\",\"dwdm\":\"%6\",\"xtm\":\"%7\",\"jd\":\"%8\","
                            "\"bpsj\":\"%9\",\"wd\":\"%10\",\"uid\":\"%11\"}")
                            .arg(htid)
                            .arg(htm)
                            .arg(xmbh)
                            .arg(sbbh)
                            .arg(bprysfz)
                            .arg(dwdm)
                            .arg(xtm)
                            .arg(jd)
                            .arg(bpsj)
                            .arg(wd)
                            .arg(uid);
    Des3Encryption des3;
    QByteArray key = "jadl12345678912345678912";
    QByteArray encryptedData = des3.des3Encrypt(plainText.toUtf8(), key);
    QByteArray base64Encoded = encryptedData.toBase64();
    QString encodedJson = QUrl::toPercentEncoding(base64Encoded);
    QString baseUrl = "http://test.mbdzlg.com/mbdzlgtxzx/servlet/DzlgSysbJsonServlert";
    QUrl url(baseUrl);
    QUrlQuery query;
    query.addQueryItem("param", encodedJson);
    url.setQuery(query);

    QNetworkAccessManager manager;
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QByteArray postData;
    QNetworkReply *reply = manager.post(request, postData);
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
    if (reply->error() == QNetworkReply::NoError) {
        QByteArray responseData = reply->readAll();
        qDebug() << "Response:" << QString(responseData);
    } else {
        qDebug() << "Error:" << reply->errorString();
    }
    reply->deleteLater();
    return true;
}

void firingWidget::handleProjectFiringMqttMessage(const QMqttMessage &message) {
    QString msgText = QString("Topic: %1, Message: %2").arg(message.topic().name(), QString(message.payload()));
    QTextEdit *textEdit = ui->scrollAreaWidgetContents->findChild<QTextEdit *>("textEdit");
    if (textEdit) {
        textEdit->append(msgText);
    }
    int stage = 0;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(message.payload());
    if (!jsonDoc.isNull() && jsonDoc.isObject()) {
        QJsonObject jsonObj = jsonDoc.object();

        if (jsonObj.contains("type") && jsonObj["type"].toInt() == MSG_DATA_TYPE_BLASTING) {
            if (jsonObj.contains("data") && jsonObj["data"].isObject()) {
                QJsonObject relayInfo = jsonObj["data"].toObject();
                int status = relayInfo["status"].toInt();
                if (ErrorBlastStatus::isErrorStatus(status)) {
                    Logger::getInstance().error(QString("爆破器返回异常. 工程uuid: %1; 错误code: %2")
                                                    .arg(m_curProjUuid, ErrorBlastStatus::getErrorMessage(status)));
                    QMessageBox::critical(nullptr, "爆破器报错",
                                          QString("错误错误信息: %1").arg(ErrorBlastStatus::getErrorMessage(status)));
                    sendCancelFiringMsg();
                    return;
                }

                stage = relayInfo["stage"].toInt();
                if (stage != m_lastStage) {
                    m_lastStage = stage;
                    switch (stage) {
                        case FiringStages::Starting:
                            navProgress->setState(FiringStages::Starting);
                            ui->pushButton_2->setText("立即测试");
                            ui->pushButton_2->setEnabled(false);
                            break;
                        case FiringStages::QuickTesting:
                            navProgress->setState(FiringStages::QuickTesting);
                            emit updateBlastStatus(FiringStages::QuickTesting, m_row);
                            ui->pushButton_2->setText("立即测试");
                            ui->pushButton_2->setEnabled(false);
                            break;
                        case FiringStages::QuickTestFinished:
                            // 起爆检测测试检查报错雷管数量，如果有错误，提示用户确认是否继续
                            if (relayInfo["errorCount"].toInt() > 0) {
                                QMessageBox msgBox;
                                msgBox.setWindowTitle("起爆检测测试错误");
                                msgBox.setText(QString("起爆检测测试发现 %1 个报错雷管，是否继续起爆？")
                                                   .arg(relayInfo["errorCount"].toInt()));
                                QPushButton *yesButton = msgBox.addButton("确定继续", QMessageBox::YesRole);
                                QPushButton *noButton = msgBox.addButton("取消", QMessageBox::NoRole);
                                yesButton->setStyleSheet(
                                    "QPushButton { background-color:rgb(5, 58, 156); color: white; padding: 5px 15px; "
                                    "border-radius: 4px; }");
                                noButton->setStyleSheet(
                                    "QPushButton { background-color: #f44336; color: white; padding: 5px 15px; "
                                    "border-radius: 4px; }");
                                msgBox.exec();
                                if (msgBox.clickedButton() == noButton) {
                                    sendCancelFiringMsg();
                                    return;
                                }
                            }

                            ui->pushButton_2->setEnabled(true);
                            ui->pushButton_2->setText("充电");
                            navProgress->setState(FiringStages::QuickTestFinished);

                            emit progressChanged(FiringStages::QuickTestFinished, m_row);
                            emit updateBlastStatus(FiringStages::QuickTestFinished, m_row);
                            break;
                        case FiringStages::NetCharging:
                            ui->pushButton_2->setEnabled(false);
                            emit progressChanged(FiringStages::NetCharging, m_row);
                            emit updateBlastStatus(FiringStages::NetCharging, m_row);
                            ui->pushButton_2->setText("充电");
                            navProgress->setState(FiringStages::NetCharging);
                            break;
                        case FiringStages::NetChargingFinished:
                            ui->pushButton_2->setEnabled(true);
                            emit progressChanged(FiringStages::NetChargingFinished, m_row);
                            emit updateBlastStatus(FiringStages::NetChargingFinished, m_row);
                            ui->pushButton_2->setText("起爆");
                            navProgress->setState(FiringStages::NetChargingFinished);
                            break;
                        case FiringStages::Blasting:
                            ui->pushButton_2->setEnabled(false);
                            emit progressChanged(FiringStages::Blasting, m_row);
                            emit updateBlastStatus(FiringStages::Blasting, m_row);
                            ui->pushButton_2->setText("起爆");
                            navProgress->setState(FiringStages::Blasting);
                            break;
                        case FiringStages::BlastFinished:
                            ui->pushButton_2->setEnabled(false);
                            emit progressChanged(FiringStages::BlastFinished, m_row);
                            emit updateBlastStatus(FiringStages::BlastFinished, m_row);
                            emit updateProjectStatus(m_curProjUuid, BlastStatus::Blasted);
                            ui->pushButton_2->setText("已完成起爆");
                            navProgress->setState(FiringStages::BlastFinished);
                            emit closeFiring(m_curProjUuid, m_row);
                            break;
                        case FiringStages::CancelConfirmed:
                            navProgress->setState(FiringStages::BlastFinished);
                            ui->pushButton_2->setEnabled(false);
                            ui->pushButton_2->setText("已确认取消");

                            emit progressChanged(FiringStages::Starting, m_row);
                            emit updateBlastStatus(FiringStages::Starting, m_row);
                            emit updateProjectStatus(m_curProjUuid, BlastStatus::Created);
                            emit closeFiring(m_curProjUuid, m_row);

                            break;
                        default:
                            break;
                    }

                    emit lastStageChanged(m_lastStage);
                }
            }
        }
        if (jsonObj.contains("type") && jsonObj["type"].toInt() == MSG_DATA_TYPE_RECORD) {
            if (jsonObj.contains("data") && jsonObj["data"].isObject()) {
                QJsonObject dataObject = jsonObj["data"].toObject();
                emit updateData(dataObject);
                emit closeFiring(m_curProjUuid, m_row);
            }
        }
    }
}

void firingWidget::onCountdownFinished(const QString &topic, const QString &message) {
    qDebug() << "Countdown finished for topic:" << topic << ", message:" << message;
    sendMqttMessage(topic, message.toUtf8());
}

void firingWidget::onMqttDisconnected() {
    Logger::getInstance().info(QString("Project(%1) firing MQTT disconncted.").arg(m_curProjUuid));
}

void firingWidget::onMqttConnected() {
    Logger::getInstance().info(QString("Project(%1) firing MQTT connected.").arg(m_curProjUuid));
}

void firingWidget::onButtonPressedReceived(const QString &topic, const QString &message) {
    QByteArray data = "\r\nDISABLE_BUTTON\r\n";
    // 调用发送数据的方法
    bool success = serialTool->sendData(data);
    if (!success) {
        Logger::getInstance().error(QString("onButtonPressedReceived: Failed to send data: %1").arg(data));
        return;
    }
    emit countdown(m_curProjUuid, topic, message);
    qDebug() << "Data sent successfully";
}

void firingWidget::onSafeChecked(const QString projectUuid) {
    if (projectUuid == m_curProjUuid) {
        m_lastStage = 3;  // 设置为待安全确认状态
        emit projSafeCheckSuccess(m_curProjUuid);
        emit lastStageChanged(m_lastStage);
        qDebug() << ANSI_COLOR_GREEN << "安全确认成功，进入待起爆状态" << ANSI_COLOR_RESET;
    } else {
        qDebug() << "Project UUID mismatch, expected: " << m_curProjUuid << ", got: " << projectUuid;
    }
}

// 状态改变
void firingWidget::handleFiringStageChanged(int newStage) {
    qDebug() << ANSI_COLOR_GREEN << "Stage 值变为: " << newStage << ANSI_COLOR_RESET;
    QString topic = "hxgc/" + m_curProjUuid + "/P";
    QString message;
    QString buttonText;
    qDebug() << ANSI_COLOR_GREEN << "Stage 值变为: i1" << newStage << ANSI_COLOR_RESET;
    switch (newStage) {
        case FiringStages::Starting:
            message = "起爆测试";
            buttonText = "立即测试";
            sendMqttMessage(topic, message.toUtf8());
            emit updateBlastStatus(FiringStages::Starting, m_row);
            break;
        case FiringStages::QuickTestFinished:
            message = "开始充电";
            buttonText = "充电";
            sendMqttMessage(topic, message.toUtf8());
            break;
        case FiringStages::NetChargingFinished:
            message = "起爆";
            buttonText = "起爆";
            break;
        case FiringStages::Blasting:
            if (!m_select) {
                if (connection) {
                    disconnect(connection);
                    qDebug() << "Connection disconnected.";
                }
                if (connectionPress) {
                    disconnect(connectionPress);
                    qDebug() << "connectionPress disconnected.";
                }
                serialTool->releaseInstance();
            }

            qDebug() << ANSI_COLOR_GREEN << "释放按键 5" << ANSI_COLOR_RESET;

            break;
        default:

            return;
    }
    qDebug() << ANSI_COLOR_GREEN << "Stage 值变为: i2" << newStage << ANSI_COLOR_RESET;

    if (!message.isEmpty()) {
        if (m_lastStage == FiringStages::NetChargingFinished && !m_select) {
            bool success2;
            serialTool = SerialTool::getInstance(nullptr, &success2);
            if (serialTool) {
                QByteArray data = "\r\nENABLE_BUTTON\r\n";
                bool success = serialTool->sendData(data);
                if (success) {
                    qDebug() << "Data sent successfully";
                } else {
                    qDebug() << "Failed to send data";
                }
                connection = connect(serialTool, &SerialTool::enableButtonReceived,
                                     [this]() { emit updateBlastStatus(10, m_row); });
                connectionPress = connect(serialTool, &SerialTool::buttonPressedReceived,
                                          [this, topic, message]() { this->onButtonPressedReceived(topic, message); });

            } else {
                qDebug() << "serialTool Not found.";
                // 在准备起爆状态下 但是没有获取串口
                m_lastStage = FiringStages::PendingTriggerButtonClick;
            }
        } else if (m_lastStage == FiringStages::NetChargingFinished && m_select) {
            qDebug() << "多台起爆：待起爆状态";
            emit batchFiringSignal(m_curProjUuid);
        } else {
            qDebug() << "Stage 值变为: " << newStage << "发送消息" << message.toUtf8();
        }
    }
}

void firingWidget::sendCancelFiringMsg() {
    QString message = "取消流程";
    QString topic = "hxgc/" + m_curProjUuid + "/P";
    sendMqttMessage(topic, message.toUtf8());
}
void firingWidget::on_sendTest_4_clicked() {}

void firingWidget::testOnBlastSuccess(const QJsonObject &jsonObj) { onBlastSuccess(jsonObj); }
// 处理爆破成功，安全验证装置通过MQTT传回的数据
void firingWidget::onBlastSuccess(const QJsonObject &jsonObj) {
    Logger::getInstance("blastRecord.log").info(QJsonDocument(jsonObj).toJson(QJsonDocument::Indented));
    saveAndUploadRecord(jsonObj);
}

void firingWidget::saveAndUploadRecord(const QJsonObject &jsonObj) {
    return;  // changed to use server to save the record
    QSqlDatabase db = DatabaseManager::getInstance().getDatabase();
    if (!db.transaction()) {
        Logger::getInstance().critical(QString("Failed to start DB transaction. Error: %1").arg(db.lastError().text()));
        return;
    }

    qDebug() << "Starting transaction for blast record with uuid:" << jsonObj;
    HBlastRecord *blastRecord = recordBlastProject(jsonObj);
    if (blastRecord == nullptr) {
        Logger::getInstance("error_blast_records.log")
            .error(QString("Failed to create blast record. projectName: %1, ProjectID: %2; "
                           "rollback transaction: %3")
                       .arg(jsonObj["project_name"].toString(), jsonObj["project_htid"].toString(),
                            db.rollback() ? "success" : "failed"));
        return;
    }
    QList<HBlastEquipmentRecord *> blastEquipmentRecords;
    for (const auto &regValue : jsonObj["regs"].toArray()) {
        QJsonObject equipmentJsonObject = regValue.toObject();
        HBlastEquipmentRecord *hBlastEquipmentRecord =
            recordBlastEquipment(blastRecord->getUuid(), equipmentJsonObject);
        if (hBlastEquipmentRecord != nullptr) {
            blastEquipmentRecords.append(hBlastEquipmentRecord);
        }

        QList<HBlastRecordDet *> hBlastRecordDets = recordBlastProjectDets(
            blastRecord->getUuid(), hBlastEquipmentRecord->getUuid(), equipmentJsonObject["dets"].toArray());
        hBlastEquipmentRecord->setDetRecords(hBlastRecordDets);
    }

    blastRecord->setEquipmentRecords(blastEquipmentRecords);

    if (db.commit()) {
        Logger::getInstance().info(
            QString("Transaction committed successfully. blast_uuid: %1").arg(blastRecord->getUuid()));
    } else {
        Logger::getInstance().error(QString("Failed to commit transaction: %1. try to rollback: %2")
                                        .arg(db.lastError().text(), db.rollback() ? "success" : "failed"));
        return;
    }
    uploadBlastRecordToServer(blastRecord);
}

void firingWidget::uploadBlastRecordToServer(HBlastRecord *blastRecord) {
    if (blastRecord == nullptr) {
        return;
    }
    if (!backendAPIManager::uploadBlastProjectFull(blastRecord->ToJson())) {
        Logger::getInstance().error(QString("Failed to upload blast project. data: %1")
                                        .arg(QJsonDocument(blastRecord->ToJson()).toJson(QJsonDocument::Indented)));
    }
    Logger::getInstance().info(
        QString("Blast project uploaded to server successfully. blast_uuid: %1").arg(blastRecord->getUuid()));
}

HBlastRecord *firingWidget::recordBlastProject(const QJsonObject &jsonObj) {
    HBlastRecord *record = new HBlastRecord();

    record->setUuid(QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-")));
    record->setIsOfflineBlast(jsonObj["is_offline_blast"].toString() == "true");
    record->setProjectName(jsonObj["project_name"].toString());
    record->setProjectHtid(jsonObj["project_htid"].toString());
    record->setProjectXmbh(jsonObj["project_xmbh"].toString());
    record->setOperatorName(jsonObj["operator_name"].toString());
    record->setPhone(jsonObj["phone"].toString());
    record->setOperatorIdentity(jsonObj["operator_identity"].toString());
    record->setEquipmentSn(jsonObj["equipment_sn"].toString());
    record->setCompanyCode(jsonObj["company_code"].toString());
    record->setAppVersion(jsonObj["app_version"].toString());

    // TODO: 如何获得坐标
    record->setLongitude(jsonObj["blast_longitude"].toString().left(10));
    record->setLatitude(jsonObj["blast_latitude"].toString().left(10));

    record->setRegDetCount(jsonObj["reg_deto_count"].toString().toInt());
    record->setErrorDetCount(jsonObj["error_deto_count"].toString().toInt());
    QDateTime blastTime = QDateTime::fromString(jsonObj["blast_time"].toString(), "yyyy-MM-dd hh:mm:ss");
    record->setBlastAt(blastTime);
    record->setCreatedAt(QDateTime::currentDateTime());

    if (daoProj.addHBlastRecord(*record)) {
        qDebug() << "Record inserted successfully.";
        return record;
    } else {
        qDebug() << "Failed to insert record.";
        delete record;
        return nullptr;
    }
}

HBlastEquipmentRecord *firingWidget::recordBlastEquipment(const QString blastProjectUuid, const QJsonObject &regObj) {
    HBlastEquipmentRecord *recordReg = new HBlastEquipmentRecord();
    recordReg->setUuid(QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-")));
    recordReg->setEquipSn(regObj["equipment_sn"].toString());
    recordReg->setEquipVersion(regObj["equipment_version"].toString());
    recordReg->setRegDetoCount(regObj["reg_deto_count"].toString().toInt());
    recordReg->setErrorDetoCount(regObj["error_deto_count"].toString().toInt());
    recordReg->setAfterTestBusV(regObj["after_test_bus_v"].toString());
    recordReg->setAfterTestBusI(regObj["after_test_bus_i"].toString());
    recordReg->setBusLeakageCurrentI(regObj["bus_leakage_current_i"].toString());
    recordReg->setNetChargedV(regObj["net_charged_v"].toString());
    recordReg->setNetChargedI(regObj["net_charged_i"].toString());
    recordReg->setBeforeBlastingV(regObj["before_blasting_v"].toString());
    recordReg->setBeforeBlastingI(regObj["before_blasting_i"].toString());
    recordReg->setRecordUuid(blastProjectUuid);
    recordReg->setCreatedAt(QDateTime::currentDateTime());

    if (!daoReg.saveHBlastRegRecord(*recordReg)) {
        Logger::getInstance().error(QString("Failed to save HBlastRegRecord: %1").arg(recordReg->getEquipSn()));
        delete recordReg;
        return nullptr;
    }
    return recordReg;
}

QList<HBlastRecordDet *> firingWidget::recordBlastProjectDets(const QString projectUuid, const QString equipmentUuid,
                                                              const QJsonArray detsArray) {
    QList<HBlastRecordDet *> recordDets;

    for (const auto &detValue : detsArray) {
        QJsonObject detObj = detValue.toObject();
        HBlastRecordDet *recordDet = new HBlastRecordDet();

        recordDet->setId(0);
        recordDet->setUuid(QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-")));
        recordDet->setOutCode(detObj["out_code"].toString());
        recordDet->setInnerCode(detObj["in_code"].toString());
        recordDet->setUid(detObj["uid"].toString());
        recordDet->setStatus(detObj["status"].toString());
        recordDet->setFreq(detObj["freq"].toString());
        recordDet->setDelayTime(detObj["delay_time"].toString());
        recordDet->setTag(detObj["tag"].toString());
        recordDet->setBlastRecordUuid(projectUuid);
        recordDet->setEquipmentRecordUuid(equipmentUuid);
        recordDet->setCreatedAt(QDateTime::currentDateTime());

        if (daoDet.addHBlastRecordDet(*recordDet)) {
            recordDets.append(recordDet);
        } else {
            Logger::getInstance().error(QString("Failed to insert record det. data %1")
                                            .arg(QJsonDocument(recordDet->ToJson()).toJson(QJsonDocument::Compact)));
            delete recordDet;
            continue;
        }
    }
    return recordDets;
}
