#include "CheckDao.h"

CheckDao::CheckDao()
{

}

QStringList CheckDao::getMeasureItemCategoryByModels(QStringList modelIds)
{
    QStringList result;

    QSqlDatabase dbLocal = CDbConnectionPool::instance().openConnection("local");
    QSqlQuery query(dbLocal);

    QString sql = "SELECT bbdmii.item_category_id FROM biz_business_device_measure_item_info bbdmii "
                  "WHERE ";
    for (int i = 0; i < modelIds.size() - 1; i++) {
        sql += "bbdmii.device_model_id = ? AND ";
    }
    sql += "bbdmii.device_model_id = ?;";
    query.prepare(sql);

    for (int i = 0; i < modelIds.size(); i++) {
        query.bindValue(i, modelIds.at(i));
    }

    bool isSuccess = query.exec();
    if (isSuccess) {
        while (query.next()) {
            result << query.value("item_category_id").toString();
        }
    } else {
        qDebug() << "Query execution failed: " << query.lastError().text();
    }

    return result;
}

QStringList CheckDao::getSelectedTaskMeasureItemCategory(QStringList taskSampleIds)
{
    QStringList resultList;

    QSqlDatabase dbLocal = CDbConnectionPool::instance().openConnection("local");
    QSqlQuery query(dbLocal);

    QString placeholders = QString("?,").repeated(taskSampleIds.size()).chopped(1);

    QString sql = QString("SELECT bbdmii.item_category_id FROM biz_business_device_measure_item_info bbdmii "
                  "LEFT JOIN eqpt_equipment_model_info eemi ON eemi.id = bbdmii.device_model_id "
                  "LEFT JOIN eqpt_equipment_info eei ON eei.model_id = eemi.id "
                  "WHERE eei.id in (%1);").arg(placeholders);

    qDebug() << sql << taskSampleIds;

    query.prepare(sql);
    for (QString sampleId : taskSampleIds) {
        query.addBindValue(sampleId);
    }

    if (query.exec()) {
        while (query.next()) {
            resultList << query.value(0).toString();
        }
    } else {
        LogUtil::PrintLog("ERROR", QString("查询所选待检设备的检定项分类失败[%1]").arg(query.lastError().text()));
    }

    return resultList;
}

DeviceMeasureCategoryDto CheckDao::getDeviceMeasureCategoryById(QString categoryId)
{
    DeviceMeasureCategoryDto result;

    QSqlDatabase dbLocal = CDbConnectionPool::instance().openConnection("local");
    QSqlQuery query(dbLocal);

    QString sql = "SELECT bbdmic.* FROM biz_business_device_measure_item_category bbdmic "
                  "WHERE bbdmic.id = :id";

    query.prepare(sql);
    query.bindValue(":id", categoryId);

    bool isSuccess = query.exec();
    if (isSuccess) {
        if (query.first()) {
            result.id = query.value("id").toString();
            result.categoryNo = query.value("category_no").toString();
            result.categoryName = query.value("category_name").toString();
            result.deviceType = query.value("device_type").toString();
            result.belongStandardEquipment = query.value("belong_standard_equipment").toString();
            result.measureCategory = query.value("measure_category").toString();
            result.technologyFile = query.value("technology_file").toString();
        }
    } else {
        LogUtil::PrintLog("ERROR", QString("查询标准设备的检定项分类失败[%1][%2]").arg(categoryId).arg(query.lastError().text()));
    }

    qDebug() << "CheckDao::getDeviceMeasureCategoryById: " << query.lastQuery() << categoryId;

    return result;
}

QList<ZdDeviceRoleDto> CheckDao::getDeviceRoleByCategory(QString categoryId)
{
    QList<ZdDeviceRoleDto> result;

    DeviceMeasureCategoryDto category = getDeviceMeasureCategoryById(categoryId);

    QSqlDatabase dbLocal = CDbConnectionPool::instance().openConnection("local");
    QSqlQuery query(dbLocal);

    QString sql = "SELECT * FROM zd_device_role WHERE cateory_id = :cateory or cateory_name = :categoryName;";
    query.prepare(sql);

    query.bindValue(":cateory", categoryId);
    query.bindValue(":categoryName", category.categoryName);
    bool isSuccess = query.exec();
    if (isSuccess) {
        while (query.next()) {
            ZdDeviceRoleDto role;

            role.id = query.value("id").toString();
            role.belongVername = query.value("belong_vername").toString();
            role.belongCheckname = query.value("belong_checkname").toString();
            role.categoryId = query.value("cateory_id").toString();
            role.categoryName = query.value("cateory_name").toString();
            role.deviceId = query.value("device_id").toString();
            role.deviceType = query.value("device_type").toString();
            role.role = query.value("role").toString();

            result.append(role);
        }
    } else {
        LogUtil::PrintLog("ERROR", QString("查询标准设备的角色分类失败[%1][%2][%3]").arg(categoryId).arg(category.categoryName).arg(query.lastError().text()));
    }

    qDebug() << "CheckDao::getDeviceRoleByCategory: " << query.lastQuery() << categoryId << category.categoryName;

    return result;
}
