#include "hblastrecorddetdao.h"

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

bool HBlastRecordDetDao::addHBlastRecordDet(const HBlastRecordDet& recordDet) {
    QSqlQuery query;
    query.prepare(
        "INSERT INTO h_blast_record_det (id, uuid, out_code, inner_code, uid, status, freq, "
        "delay_time, tag, blast_record_id, reg_id, created_at, updated_at, deleted_at, create_by, "
        "update_by) "
        "VALUES (:id, :uuid, :outCode, :innerCode, :uid, :status, :freq, :delayTime, :tag, "
        ":blastRecordId, :regId, :createdAt, :updatedAt, :deletedAt, :createBy, :updateBy)");
    query.bindValue(":id", recordDet.getId());
    query.bindValue(":uuid", recordDet.getUuid());
    query.bindValue(":outCode", recordDet.getOutCode());
    query.bindValue(":innerCode", recordDet.getInnerCode());
    query.bindValue(":uid", recordDet.getUid());
    query.bindValue(":status", recordDet.getStatus());
    query.bindValue(":freq", recordDet.getFreq());
    query.bindValue(":delayTime", recordDet.getDelayTime());
    query.bindValue(":tag", recordDet.getTag());
    query.bindValue(":blastRecordId", recordDet.getBlastRecordUuid());
    query.bindValue(":regId", recordDet.getEquipmentRecordUuid());
    query.bindValue(":createdAt", recordDet.getCreatedAt());
    query.bindValue(":updatedAt", recordDet.getUpdatedAt());
    query.bindValue(":deletedAt", recordDet.getDeletedAt());
    query.bindValue(":createBy", recordDet.getCreateBy());
    query.bindValue(":updateBy", recordDet.getUpdateBy());

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

// HBlastRecordDet HBlastRecordDetDao::getById(qint64 id)
// {
//     HBlastRecordDet recordDet;
//     QSqlQuery query(db);
//     query.prepare("SELECT * FROM HBlastRecordDet WHERE id = :id");
//     query.bindValue(":id", id);

//     if (query.exec() && query.next()) {
//         recordDet.setId(query.value("id").toLongLong());
//         recordDet.setUuid(query.value("uuid").toString());
//         recordDet.setOutCode(query.value("outCode").toString());
//         recordDet.setInnerCode(query.value("innerCode").toString());
//         recordDet.setUid(query.value("uid").toString());
//         recordDet.setStatus(query.value("status").toString());
//         recordDet.setFreq(query.value("freq").toString());
//         recordDet.setDelayTime(query.value("delayTime").toString());
//         recordDet.setTag(query.value("tag").toString());
//         recordDet.setBlastRecordId(query.value("blastRecordId").toInt());
//         recordDet.setRegId(query.value("regId").toInt());
//         recordDet.setCreatedAt(query.value("createdAt").toDateTime());
//         recordDet.setUpdatedAt(query.value("updatedAt").toDateTime());
//         recordDet.setDeletedAt(query.value("deletedAt").toDateTime());
//         recordDet.setCreateBy(query.value("createBy").toLongLong());
//         recordDet.setUpdateBy(query.value("updateBy").toLongLong());
//     }

//     return recordDet;
// }

bool HBlastRecordDetDao::updateHBlastRecordDet(const HBlastRecordDet& recordDet) {
    QSqlQuery query;
    query.prepare(
        "UPDATE HBlastRecordDet SET uuid = :uuid, outCode = :outCode, innerCode = :innerCode, uid "
        "= :uid, status = :status, freq = :freq, delayTime = :delayTime, tag = :tag, blastRecordId "
        "= :blastRecordId, regId = :regId, createdAt = :createdAt, updatedAt = :updatedAt, "
        "deletedAt = :deletedAt, createBy = :createBy, updateBy = :updateBy "
        "WHERE id = :id");
    query.bindValue(":id", recordDet.getId());
    query.bindValue(":uuid", recordDet.getUuid());
    query.bindValue(":outCode", recordDet.getOutCode());
    query.bindValue(":innerCode", recordDet.getInnerCode());
    query.bindValue(":uid", recordDet.getUid());
    query.bindValue(":status", recordDet.getStatus());
    query.bindValue(":freq", recordDet.getFreq());
    query.bindValue(":delayTime", recordDet.getDelayTime());
    query.bindValue(":tag", recordDet.getTag());
    query.bindValue(":blastRecordId", recordDet.getBlastRecordUuid());
    query.bindValue(":regId", recordDet.getEquipmentRecordUuid());
    query.bindValue(":createdAt", recordDet.getCreatedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":updatedAt", recordDet.getUpdatedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":deletedAt", recordDet.getDeletedAt().toString(Qt::ISODateWithMs));
    query.bindValue(":createBy", recordDet.getCreateBy());
    query.bindValue(":updateBy", recordDet.getUpdateBy());

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

// bool HBlastRecordDetDao::deleteById(qint64 id)
// {
//     QSqlQuery query(db);
//     query.prepare("DELETE FROM HBlastRecordDet WHERE id = :id");
//     query.bindValue(":id", id);

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