#include "SystemDao.h"

#include <QCryptographicHash>
#include "utils/ProMemory.h"

SystemDao::SystemDao()
{

}

void SystemDao::getUserRoleIdAndName(QString userId, QMap<QString, QVariant> &user)
{
    QSqlDatabase dbLocal = CDbConnectionPool::instance().openConnection("local");
    QSqlQuery query(dbLocal);

    // 查询用户
    QString sql = "SELECT r.ID, r.NAME FROM "
                  "sys_role r "
                  "JOIN sys_ru_relation rr ON r.ID = rr.ROLEID "
                  "JOIN sys_user u ON u.ID = rr.USERID "
                  "WHERE u.ID = :userId";
    sql = appendSchemaName(sql, QStringList() << "sys_role" << "sys_user" << "sys_ru_relation");

    qDebug() << sql;

    query.prepare(sql);
    query.bindValue(":userId", userId);

    QStringList loginUserRole;
    QStringList loginUserRoleIndex;

    // TODO-LIST 修改为不使用全局变量
    if (query.exec()) {
        while (query.next()) {
            loginUserRole.append(query.value("NAME").toString());
            loginUserRoleIndex.append(query.value("ID").toString());
        }

        user.insert("userRole", loginUserRole);
        user.insert("userRoleIndex", loginUserRoleIndex);
    } else {
        LogUtil::PrintLog("ERROR", QString("查询用户权限失败[userId=%1][%2]").arg(userId).arg(query.lastError().text()));
    }
}

QString SystemDao::getStaffDeptIdByUserId(QString userId)
{
    QString result = 0;

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

    // 查询语句
    QString sql = "SELECT brsi.dept_id AS deptId "
                  "FROM biz_resource_staff_info brsi "
                  "JOIN sys_user ON sys_user.ACCOUNT = brsi.account "
                  "WHERE sys_user.id = :userId";

    query.prepare(sql);
    query.bindValue(":userId", userId);

    if (query.exec() && query.next()) {
        result = query.value("deptId").toString();
    } else {
        LogUtil::PrintLog("ERROR", QString("查询计量人员所在部门失败[userId=%1][%2]").arg(userId).arg(query.lastError().text()));
    }

//    qDebug() << sql << userId << result;

    return result;
}

QMap<QString, QVariant> SystemDao::getSysUserByAccount(QString account)
{
    QMap<QString, QVariant> result;

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

    // 查询用户
    QString sql = "SELECT u.ID, u.ACCOUNT, u.PASSWORD, u.SALT, u.NAME, u.ROLE_ID, u.DEPT_ID, "
                  "r.biz_lab_code, r.group_no "
                  "FROM sys_user u "
                  "LEFT JOIN biz_lab_rel_dept r ON r.dept_id = u.DEPT_ID "
                  "WHERE DEL_FLAG = '0' AND ACCOUNT = :account";
    sql = appendSchemaName(sql, QStringList() << "sys_user");

//    qDebug() << sql;

    query.prepare(sql);
    query.bindValue(":account", account);

    bool ok = query.exec();
    if (ok == false) {
        LogUtil::PrintLog("ERROR", QString("%1登录失败[%2]").arg(account).arg(query.lastError().text()));
        return result;
    }

    if (query.next()) {
        result.insert("id", query.value("ID").toString());
        result.insert("account", query.value("ACCOUNT").toString());
        result.insert("password", query.value("PASSWORD").toString());
        result.insert("salt", query.value("SALT").toString());
        result.insert("deptId", query.value("DEPT_ID").toString());
        result.insert("roleId", query.value("ROLE_ID").toString());
        result.insert("name", query.value("NAME").toString());
        result.insert("labCode", query.value("biz_lab_code").toString());
        result.insert("groupCode", query.value("group_no").toString());
    } else {
        // 没有找到用户
        LogUtil::PrintLog("ERROR", QString("%1登录失败[%2]").arg(account).arg("没有找到用户"));
    }

    return result;
}


QMap<QString, QString> SystemDao::getSysDictByCode(QString dictCode)
{
    QMap<QString, QString> result;

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

    QString sql = "SELECT * FROM sys_dict WHERE PID = (SELECT ID FROM sys_dict WHERE CODE = :code);";
    sql = appendSchemaName(sql, QStringList() << "sys_dict");

    query.prepare(sql);
    query.bindValue(":code", dictCode);

    if (query.exec()) {
        while (query.next()) {
            QString key = QString("%1-%2").arg(dictCode).arg(query.value("CODE").toString());
            QString value = query.value("NAME").toString();

            result.insert(key, value);
        }
    } else {
        LogUtil::PrintLog("ERROR", QString("查询字典值失败[dictCode=%1][%2]").arg(dictCode).arg(query.lastError().text()));
    }

    return result;
}
QMap<QString, QString> SystemDao::getLabCodeDict()
{
    QMap<QString, QString> labCodeDict = getSysDictByCode("bizLabCode");
    ProMemory::getInstance().setLabCodeDict(labCodeDict);
    return labCodeDict;
}
QMap<QString, QString> SystemDao::getGroupCodeDict()
{
    QMap<QString, QString> groupCodeDict = getSysDictByCode("bizGroupCode");
    ProMemory::getInstance().setLabCodeDict(groupCodeDict);
    return groupCodeDict;
}
QMap<QString, QString> SystemDao::getEqptApprovalStatusDict()
{
    QMap<QString, QString> dict = getSysDictByCode("eqptApprovalStatus");
    return dict;
}
QMap<QString, QString> SystemDao::getTaskMeasureStatusDict()
{
    QMap<QString, QString> measureStatusDict = getSysDictByCode("measureStatus");
    ProMemory::getInstance().setMeasureStatusDict(measureStatusDict);
    return measureStatusDict;
}
