#include "blastrecord.h" #include "../components/loadingWidget.h" #include "../utils/backendapimanager.h" #include "ui_blastrecord.h" BlastRecord::BlastRecord(QWidget* parent) : QWidget(parent), ui(new Ui::BlastRecord), dao(DatabaseManager::getInstance().getDatabase()) { ui->setupUi(this); m_pPageWidget = new PageWidget; connect(m_pPageWidget, &PageWidget::currentPageChanged, this, &BlastRecord::PageChanged); connect(m_pPageWidget->getComboBox(), QOverload::of(&QComboBox::currentIndexChanged), this, &BlastRecord::onComboBoxIndexChanged); m_pageSize = 10; m_currentPage = 1; ui->verticalLayout_4->addWidget(m_pPageWidget); loadAndRefreshData(); } void BlastRecord::loadAndRefreshData() { LoadingWidget::showLoading(this, "正在加载数据,请稍候..."); QJsonObject params; QJsonObject blastData = backendAPIManager::getBlastRecords(m_currentPage, m_pageSize, params); LoadingWidget::hideLoading(); m_currentPage = blastData["pageIndex"].toInt(); m_pageSize = blastData["pageSize"].toInt(); m_totalCount = blastData["count"].toInt(); QList> blastRecords; for (const QJsonValue& value : blastData["list"].toArray()) { QJsonObject jsonObj = value.toObject(); QSharedPointer record(new HBlastRecord); record->setProjectName(jsonObj["projectName"].toString()); record->setIsOfflineBlast(jsonObj["isOfflineBlast"].toBool()); record->setAppVersion(jsonObj["appVersion"].toString()); record->setEquipmentSn(jsonObj["equipmentSn"].toString()); record->setRegDetCount(jsonObj["regDetCount"].toString().toInt()); record->setBlastAt(QDateTime::fromString(jsonObj["blastAt"].toString(), Qt::ISODateWithMs)); blastRecords.append(record); } drawTable(blastRecords); } void BlastRecord::drawTable(QList> blastRecordsList) { m_pPageWidget->setMaxPage(ceil(static_cast(m_totalCount) / m_pageSize)); QStandardItemModel* model; model = new QStandardItemModel(this); // 定义表头信息 QList headers = { {"工程名称", "projectName"}, {"是否离线", "isOfflineBlast"}, {"app版本", "appVersion"}, {"起爆器编号", "equipmentSn"}, {"注册雷管", "regDetCount"}, {"起爆时间", "blastAt"}, }; QStringList headerLabels; QMap propMap; for (int i = 0; i < headers.size(); ++i) { headerLabels << headers[i].label; propMap[i] = headers[i].prop; } model->setHorizontalHeaderLabels(headerLabels); for (int row = 0; row < blastRecordsList.size(); ++row) { HBlastRecord& HBlastRecord = *blastRecordsList.at(row).data(); for (int col = 0; col < headers.size(); ++col) { QString prop = propMap[col]; QStandardItem* item = nullptr; if (!prop.isEmpty()) { QMetaProperty metaProp = HBlastRecord.metaObject()->property(HBlastRecord.metaObject()->indexOfProperty(prop.toUtf8())); QVariant value = metaProp.read(&HBlastRecord); if (value.type() == QVariant::DateTime) { QDateTime dateTime = value.toDateTime(); item = new QStandardItem(dateTime.toString("yyyy-MM-dd HH:mm:ss")); } else if (prop == "isOfflineBlast") { item = new QStandardItem(value.toBool() ? "离线爆破" : "线上爆破"); } else { item = new QStandardItem(value.toString()); } } else { item = new QStandardItem("操作"); } if (item) { item->setTextAlignment(Qt::AlignCenter); model->setItem(row, col, item); } } } ui->tableView->setModel(model); ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); ui->tableView->setAlternatingRowColors(true); // 启用交替行颜色 ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); } void BlastRecord::PageChanged(int page) { m_currentPage = page; loadAndRefreshData(); } void BlastRecord::onComboBoxIndexChanged(int index) { QVariant variant = m_pPageWidget->getComboBox()->itemData(index); int value = variant.toInt(); m_pageSize = value; loadAndRefreshData(); } void BlastRecord::dataTable(const QJsonDocument& jsonDoc) { model = new QStandardItemModel(this); model->setHorizontalHeaderLabels( {"工程名称", "离线爆破", "app版本", "起爆器编号", "注册雷管", "起爆时间", "上报时间"}); for (const QJsonValue& jsonValue : dataArray) { QJsonObject jsonObj = jsonValue.toObject(); QList items = jsonToItem(jsonObj); model->appendRow(items); } ui->tableView->setModel(model); ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); ui->tableView->setAlternatingRowColors(true); } QList BlastRecord::jsonToItem(const QJsonObject& jsonObj) { QStandardItem* nameItem = new QStandardItem(jsonObj["name"].toString()); nameItem->setTextAlignment(Qt::AlignCenter); qDebug() << "jsonObj:" << jsonObj; QStandardItem* isOfflineBlastItem = new QStandardItem(jsonObj["isOfflineBlast"].toString()); QStandardItem* equipItem = new QStandardItem(jsonObj["equipmentSn"].toString()); QStandardItem* typeItem = new QStandardItem(jsonObj["equipmentTypeName"].toString()); QStandardItem* statusItem = new QStandardItem(jsonObj["statusName"].toString()); QStandardItem* onlineItem = new QStandardItem(jsonObj["onlineName"].toString()); QString createdAtStr = jsonObj["createdAt"].toString(); QDateTime createdAt = QDateTime::fromString(createdAtStr, Qt::ISODateWithMs); QString formattedCreatedAt = createdAt.toString("yyyy-MM-dd HH:mm:ss"); QStandardItem* createdItem = new QStandardItem(formattedCreatedAt); return QList() << nameItem << isOfflineBlastItem << equipItem << typeItem << statusItem << onlineItem; } BlastRecord::~BlastRecord() { delete ui; }