#include "hblastrecorddao.h"

HBlastRecordDao::HBlastRecordDao(QSqlDatabase db) : database(db) {}

PaginatedHBlastRecordResult HBlastRecordDao::getAllHRecords(int page, int pageSize) {
    QList<QSharedPointer<HBlastRecord>> BlastRecords;
    QSqlQuery query(database);
    int offset = (page - 1) * pageSize;
    query.prepare(
        "SELECT * FROM h_blast_record ORDER BY created_at desc LIMIT :pageSize OFFSET :offset");
    query.bindValue(":pageSize", pageSize);
    query.bindValue(":offset", offset);
    if (query.exec()) {
        while (query.next()) {
            BlastRecords.append(recordToBlastRecord(query.record()));
        }
    } else {
        qWarning() << "Query execution failed: " << query.lastError().text();
    }

    query.prepare("SELECT COUNT(*) FROM h_blast_record");
    int totalCount = 0;
    if (query.exec() && query.next()) {
        totalCount = query.value(0).toInt();
    }
    return {BlastRecords, totalCount};
}
bool HBlastRecordDao::addHBlastRecord(const HBlastRecord &record) {
    QSqlQuery query;
    query.prepare(
        "INSERT INTO h_blast_record (uuid, project_name, project_htid, project_xmbh, "
        "operator_name, phone, operator_identity, equipment_sn, company_code, app_version, "
        "longitude, latitude, equipment_count, reg_det_count, error_det_count, blast_at, "
        "created_at, updated_at, deleted_at, create_by, update_by, is_offline_blast) "
        "VALUES (:uuid, :project_name, :project_htid, :project_xmbh, :operator_name, :phone, "
        ":operator_identity, :equipment_sn, :company_code, :app_version, :longitude, :latitude, "
        ":equipment_count, :reg_det_count, :error_det_count, :blast_at, :created_at, :updated_at, "
        ":deleted_at, :create_by, :update_by, :is_offline_blast)");
    query.bindValue(":uuid", record.getUuid());
    query.bindValue(":project_name", record.getProjectName());
    query.bindValue(":project_htid", record.getProjectHtid());
    query.bindValue(":project_xmbh", record.getProjectXmbh());
    query.bindValue(":operator_name", record.getOperatorName());
    query.bindValue(":phone", record.getPhone());
    query.bindValue(":operator_identity", record.getOperatorIdentity());
    query.bindValue(":equipment_sn", record.getEquipmentSn());
    query.bindValue(":company_code", record.getCompanyCode());
    query.bindValue(":app_version", record.getAppVersion());
    query.bindValue(":longitude", record.getLongitude());
    query.bindValue(":latitude", record.getLatitude());
    query.bindValue(":equipment_count", record.getEquipmentCount());
    query.bindValue(":reg_det_count", record.getRegDetCount());
    query.bindValue(":error_det_count", record.getErrorDetCount());
    query.bindValue(":is_offline_blast", record.getIsOfflineBlast());
    query.bindValue(":blast_at", record.getBlastAt().toString(Qt::ISODateWithMs));
    query.bindValue(":created_at", record.getCreatedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":create_by", record.getCreateBy());
    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Failed to insert blast record:" << query.lastError().text();
        return false;
    }
}

// HBlastRecord HBlastRecordDao::getById(qint64 id)
// {
//     HBlastRecord record;
//     if (!db.isOpen()) {
//         return record;
//     }

//     QSqlQuery query(db);
//     query.prepare("SELECT * FROM h_blast_record WHERE id = :id");
//     query.bindValue(":id", id);

//     if (query.exec() && query.next()) {
//         record.setId(query.value("id").toLongLong());
//         record.setUuid(query.value("uuid").toString());
//         record.setProjectName(query.value("project_name").toString());
//         record.setProjectHtid(query.value("project_htid").toString());
//         record.setProjectXmbh(query.value("project_xmbh").toString());
//         record.setOperatorName(query.value("operator_name").toString());
//         record.setPhone(query.value("phone").toString());
//         record.setOperatorIdentity(query.value("operator_identity").toString());
//         record.setEquipmentSn(query.value("equipment_sn").toString());
//         record.setCompanyCode(query.value("company_code").toString());
//         record.setAppVersion(query.value("app_version").toString());
//         record.setLongitude(query.value("longitude").toDouble());
//         record.setLatitude(query.value("latitude").toDouble());
//         record.setEquipmentCount(query.value("equipment_count").toInt());
//         record.setRegDetCount(query.value("reg_det_count").toInt());
//         record.setErrorDetCount(query.value("error_det_count").toInt());
//         record.setBlastAt(query.value("blast_at").toDateTime());
//         record.setCreatedAt(query.value("created_at").toDateTime());
//         record.setUpdatedAt(query.value("updated_at").toDateTime());
//         record.setDeletedAt(query.value("deleted_at").toDateTime());
//         record.setCreateBy(query.value("create_by").toLongLong());
//         record.setUpdateBy(query.value("update_by").toLongLong());
//     }

//     return record;
// }

bool HBlastRecordDao::updateHBlastRecord(const HBlastRecord &record) {
    QSqlQuery query;
    query.prepare(
        "UPDATE h_blast_record SET uuid = :uuid,"
        " project_name = :project_name, "
        "project_htid = :project_htid,"
        "project_xmbh = :project_xmbh,"
        "operator_name = :operator_name,"
        "phone = :phone,"
        "operator_identity = :operator_identity,"
        "equipment_sn = :equipment_sn,"
        "company_code = :company_code,"
        " app_version = :app_version,"
        "longitude = :longitude,"
        "latitude = :latitude,"
        "equipment_count = :equipment_count,"
        "reg_det_count = :reg_det_count,"
        "error_det_count = :error_det_count,"
        "blast_at = :blast_at,"
        "created_at = :created_at,"
        "updated_at = :updated_at,"
        "deleted_at = :deleted_at,"
        "create_by = :create_by,"
        "update_by = :update_by"
        "WHERE id = :id");
    query.bindValue(":id", record.getId());
    query.bindValue(":uuid", record.getUuid());
    query.bindValue(":project_name", record.getProjectName());
    query.bindValue(":project_htid", record.getProjectHtid());
    query.bindValue(":project_xmbh", record.getProjectXmbh());
    query.bindValue(":operator_name", record.getOperatorName());
    query.bindValue(":phone", record.getPhone());
    query.bindValue(":operator_identity", record.getOperatorIdentity());
    query.bindValue(":equipment_sn", record.getEquipmentSn());
    query.bindValue(":company_code", record.getCompanyCode());
    query.bindValue(":app_version", record.getAppVersion());
    query.bindValue(":longitude", record.getLongitude());
    query.bindValue(":latitude", record.getLatitude());
    query.bindValue(":equipment_count", record.getEquipmentCount());
    query.bindValue(":reg_det_count", record.getRegDetCount());
    query.bindValue(":error_det_count", record.getErrorDetCount());
    query.bindValue(":blast_at", record.getBlastAt());
    query.bindValue(":created_at", record.getCreatedAt());
    query.bindValue(":updated_at", record.getUpdatedAt());
    query.bindValue(":deleted_at", record.getDeletedAt());
    query.bindValue(":create_by", record.getCreateBy());
    query.bindValue(":update_by", record.getUpdateBy());

    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Failed to update record:" << query.lastError().text();
        return false;
    }
}

bool HBlastRecordDao::deleteHBlastRecord(const HBlastRecord &record) {
    QSqlQuery query;
    query.prepare("DELETE FROM h_blast_record WHERE id = :id");
    query.bindValue(":id", record.getId());

    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Failed to delete record:" << query.lastError().text();
        return false;
    }
}

QSharedPointer<HBlastRecord> HBlastRecordDao::recordToBlastRecord(const QSqlRecord &record) {
    QSharedPointer<HBlastRecord> blastRecord = QSharedPointer<HBlastRecord>::create();
    blastRecord->setId(record.value("id").toLongLong());
    blastRecord->setUuid(record.value("uuid").toString());
    blastRecord->setProjectName(record.value("project_name").toString());
    blastRecord->setIsOfflineBlast(record.value("is_offline_blast").toBool());
    blastRecord->setProjectHtid(record.value("project_htid").toString());
    blastRecord->setProjectXmbh(record.value("project_xmbh").toString());
    blastRecord->setOperatorName(record.value("operator_name").toString());
    blastRecord->setPhone(record.value("phone").toString());
    blastRecord->setOperatorIdentity(record.value("operator_identity").toString());
    blastRecord->setEquipmentSn(record.value("equipment_sn").toString());
    blastRecord->setCompanyCode(record.value("company_code").toString());
    blastRecord->setAppVersion(record.value("app_version").toString());
    blastRecord->setLongitude(record.value("longitude").toString());
    blastRecord->setLatitude(record.value("latitude").toString());
    blastRecord->setEquipmentCount(record.value("equipment_count").toInt());
    blastRecord->setRegDetCount(record.value("reg_det_count").toInt());
    blastRecord->setErrorDetCount(record.value("error_det_count").toInt());
    blastRecord->setBlastAt(
        QDateTime::fromString(record.value("blast_at").toString(), Qt::ISODateWithMs));
    blastRecord->setCreatedAt(
        QDateTime::fromString(record.value("created_at").toString(), Qt::ISODateWithMs));
    blastRecord->setUpdatedAt(
        QDateTime::fromString(record.value("updated_at").toString(), Qt::ISODateWithMs));
    blastRecord->setDeletedAt(
        QDateTime::fromString(record.value("deleted_at").toString(), Qt::ISODateWithMs));
    blastRecord->setCreateBy(record.value("create_by").toLongLong());
    blastRecord->setUpdateBy(record.value("update_by").toLongLong());
    return blastRecord;
}
