Newer
Older
CasicBioRecNew / dao / RecognitionRecordDao.cpp
tanyue on 1 Feb 2023 3 KB 20230201 更新日志組件庫
#include "RecognitionRecordDao.h"

RecognitionRecordDao::RecognitionRecordDao(QObject *parent) : BaseDao(parent)
{

}


QVector<QVariantMap> RecognitionRecordDao::findAllRecord()
{
    // 新建查询
    QSqlQuery query(ConnectionManager::getInstance()->getConnection());

    // 查询语句
    QString sql = "SELECT * FROM RECOGNITION_RECORDS";

    // 执行查询
    query.exec(sql);

    // 返回结果
    QVector<QVariantMap> result;

    // 遍历查询结果
    while (query.next()) {
        QVariantMap item;

        item.insert("id", query.value("id").toLongLong());
        result.append(item);
    }

//    LOG(DEBUG) << QString("查询RECOGNITION_RECORDS表的所有记录[%1]").arg(result.size()).toLocal8Bit().data();
    LOG_DEBUG(QString("查询RECOGNITION_RECORDS表的所有记录[%1]").arg(result.size()).toLocal8Bit().data());
    return result;
}

QVariantMap RecognitionRecordDao::findRecordById(QString id)
{
    QVariantMap item;
    return item;
}

QVector<QVariantMap> RecognitionRecordDao::findRecordsByProperty(QString properName, QVariant properValue)
{
    // 新建查询
    QSqlQuery query(ConnectionManager::getInstance()->getConnection());

    // 查询语句
    QString sql = "SELECT * FROM RECOGNITION_RECORDS WHERE %1 = '%2'  ORDER BY DATETIME DESC";
    sql = sql.arg(properName).arg(properValue.toString());


    // 执行查询
    query.exec(sql);

    // 获取结果集的大小
    int count = 0;

    // 返回结果
    QVector<QVariantMap> result;

    // 遍历查询结果
    while (query.next()) {
        QVariantMap item;

        count++;

        item.insert("id", query.value("id").toLongLong());
        item.insert("datetime", query.value("datetime").toString());
        item.insert("type", query.value("type").toInt());

        result.append(item);
    }

//    LOG(DEBUG) << sql.toStdString();
//    LOG(DEBUG) << QString("根据属性值查询RECOGNITION_RECORDS表的记录[%1]").arg(count).toLocal8Bit().data();
    LOG_DEBUG(sql.toStdString());
    LOG_DEBUG(QString("根据属性值查询RECOGNITION_RECORDS表的记录[%1]").arg(count).toLocal8Bit().data());

    return result;
}

QString RecognitionRecordDao::save(QVariantMap object)
{
    // 新建查询
    QSqlQuery query(ConnectionManager::getInstance()->getConnection());

    qulonglong id = ConnectionManager::getInstance()->generateId();

    // INSERT语句
    QString sql = QString("INSERT INTO RECOGNITION_RECORDS "
                  "(ID, PERSON_ID, DATETIME, TYPE, DEV_CODE, DOOR_CODE, INOUT_TYPE) "
                  "VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7')")
            .arg(id).arg(object.value("person_id").toString())
            .arg(object.value("date_time").toString())
            .arg(object.value("type").toString())
            .arg(object.value("dev_code").toString())
            .arg(object.value("door_code").toString())
            .arg(object.value("inout_type").toString())
            .arg(object.value("debug_info").toString());

    query.prepare(sql);
//    LOG(DEBUG) << sql.toStdString();
    LOG_DEBUG(sql.toStdString());


    // 插入识别的log日志
    QString logStr = QString("INSERT INTO RECOGNITION_LOGS (ID, LOG_INFO) VALUES ('%1', '%2')")
            .arg(id).arg(object.value("debug_info").toString());
    QSqlQuery logQuery(ConnectionManager::getInstance()->getConnection());
    logQuery.prepare(logStr);

    // 开启事务
    ConnectionManager::getInstance()->getConnection().transaction();

    // 执行插入
    bool success = query.exec();
    logQuery.exec();

    // 结束事务
    ConnectionManager::getInstance()->getConnection().commit();

    // 返回结果
    if (success == true)
    {
        return QString("%1").arg(id);
    }
    else
    {
        return "-1";
    }
}

bool RecognitionRecordDao::edit(QVariantMap newObject, QString id)
{
    return false;
}

bool RecognitionRecordDao::dele(QString id)
{
    return false;
}