#include "hblastregrecorddao.h"

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

bool HBlastRegRecordDao::saveHBlastRegRecord(const HBlastEquipmentRecord &record) {
    QSqlQuery query;
    query.prepare(
        "INSERT INTO h_blast_equipment_record (uuid, equip_sn, equip_version, reg_deto_count, "
        "error_deto_count, after_test_bus_v, after_test_bus_i, bus_leakage_current_i, "
        "net_charged_v, net_charged_i, before_blasting_v, before_blasting_i, record_uuid, "
        "created_at, updated_at, deleted_at, create_by, update_by) "
        "VALUES (:uuid, :equip_sn, :equip_version, :reg_deto_count, :error_deto_count, "
        ":after_test_bus_v, :after_test_bus_i, :bus_leakage_current_i, :net_charged_v, "
        ":net_charged_i, :before_blasting_v, :before_blasting_i, :record_uuid, :created_at, "
        ":updated_at, :deleted_at, :create_by, :update_by)");
    query.bindValue(":uuid", record.getUuid());
    query.bindValue(":equip_sn", record.getEquipSn());
    query.bindValue(":equip_version", record.getEquipVersion());
    query.bindValue(":reg_deto_count", record.getRegDetoCount());
    query.bindValue(":error_deto_count", record.getErrorDetoCount());
    query.bindValue(":after_test_bus_v", record.getAfterTestBusV());
    query.bindValue(":after_test_bus_i", record.getAfterTestBusI());
    query.bindValue(":bus_leakage_current_i", record.getBusLeakageCurrentI());
    query.bindValue(":net_charged_v", record.getNetChargedV());
    query.bindValue(":net_charged_i", record.getNetChargedI());
    query.bindValue(":before_blasting_v", record.getBeforeBlastingV());
    query.bindValue(":before_blasting_i", record.getBeforeBlastingI());
    query.bindValue(":record_uuid", record.getRecordUUID());
    query.bindValue(":created_at", record.getCreatedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":updated_at", record.getUpdatedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":deleted_at", record.getDeletedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":create_by", record.getCreateBy());
    query.bindValue(":update_by", record.getUpdateBy());

    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Insert failed:" << query.lastError().text();
        return false;
    }
}

bool HBlastRegRecordDao::updateHBlastEquipmentRecord(const HBlastEquipmentRecord &record) {
    QSqlQuery query;
    query.prepare(
        "UPDATE h_blast_equipment_record SET uuid = :uuid, equip_sn = :equip_sn, equip_version = "
        ":equip_version, reg_deto_count = :reg_deto_count, error_deto_count = :error_deto_count, "
        "after_test_bus_v = :after_test_bus_v, after_test_bus_i = :after_test_bus_i, "
        "bus_leakage_current_i = :bus_leakage_current_i, net_charged_v = :net_charged_v, "
        "net_charged_i = :net_charged_i, before_blasting_v = :before_blasting_v, before_blasting_i "
        "= :before_blasting_i, record_uuid = :record_uuid, 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(":equip_sn", record.getEquipSn());
    query.bindValue(":equip_version", record.getEquipVersion());
    query.bindValue(":reg_deto_count", record.getRegDetoCount());
    query.bindValue(":error_deto_count", record.getErrorDetoCount());
    query.bindValue(":after_test_bus_v", record.getAfterTestBusV());
    query.bindValue(":after_test_bus_i", record.getAfterTestBusI());
    query.bindValue(":bus_leakage_current_i", record.getBusLeakageCurrentI());
    query.bindValue(":net_charged_v", record.getNetChargedV());
    query.bindValue(":net_charged_i", record.getNetChargedI());
    query.bindValue(":before_blasting_v", record.getBeforeBlastingV());
    query.bindValue(":before_blasting_i", record.getBeforeBlastingI());
    query.bindValue(":record_uuid", record.getRecordUUID());
    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() << "Update failed:" << query.lastError().text();
        return false;
    }
}

bool HBlastRegRecordDao::deleteHBlastRegRecord(qint64 id) {
    QSqlQuery query;
    query.prepare("DELETE FROM h_blast_equipment_record WHERE id = :id");
    query.bindValue(":id", id);

    if (query.exec()) {
        return true;
    } else {
        qDebug() << "Delete failed:" << query.lastError().text();
        return false;
    }
}

// HBlastRegRecord HBlastRegRecordDao::getHBlastRegRecordById(qint64 id) {
//     HBlastRegRecord record;
//     QSqlQuery query;
//     query.prepare("SELECT * FROM h_blast_equipment_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.setEquipSn(query.value("equip_sn").toString());
//         record.setEquipVersion(query.value("equip_version").toString());
//         record.setRegDetoCount(query.value("reg_deto_count").toInt());
//         record.setErrorDetoCount(query.value("error_deto_count").toInt());
//         record.setAfterTestBusV(query.value("after_test_bus_v").toString());
//         record.setAfterTestBusI(query.value("after_test_bus_i").toString());
//         record.setBusLeakageCurrentI(query.value("bus_leakage_current_i").toString());
//         record.setNetChargedV(query.value("net_charged_v").toString());
//         record.setNetChargedI(query.value("net_charged_i").toString());
//         record.setBeforeBlastingV(query.value("before_blasting_v").toString());
//         record.setBeforeBlastingI(query.value("before_blasting_i").toString());
//         record.setRecordUuid(query.value("record_uuid").toString());
//         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;
// }
