#include "SysDeptDao.h" SysDeptDao::SysDeptDao(QObject *parent) : BaseDao(parent) { } QVector<QVariantMap> SysDeptDao::findAllRecord() { // 新建查询 QSqlQuery query(ConnectionManager::getInstance()->getConnection()); // 查询语句 QString sql = "SELECT * FROM SYS_DEPT WHERE PID != '-1' ORDER BY PID, NUM"; // 执行查询 query.exec(sql); // 返回结果 QVector<QVariantMap> result; // 遍历查询结果 while (query.next()) { QVariantMap item; item.insert("id", query.value("id").toString()); item.insert("pid", query.value("pid").toString()); item.insert("pids", query.value("pids").toString()); item.insert("simplename", query.value("simplename").toString()); item.insert("fullname", query.value("fullname").toString()); } // LOG(TRACE) << QString("查询表[SYS_DEPT]的所有记录[%1][%2]").arg(result.size()).arg(sql).toStdString(); return result; } QVariantMap SysDeptDao::findRecordById(QString id) { QVariantMap item; return item; // 新建查询 QSqlQuery query(ConnectionManager::getInstance()->getConnection()); // 查询语句 QString sql = QString("SELECT * FROM SYS_DEPT WHERE SYS_DEPT.ID = '%1'").arg(id); // 执行查询 query.exec(sql); // 返回结果 QVariantMap result; // 获取结果集的大小 query.last(); int count = query.at() + 1; if (count >=1) { query.first(); result.insert("id", query.value("id").toString()); result.insert("pid", query.value("pid").toString()); result.insert("pids", query.value("pids").toString()); result.insert("simplename", query.value("simplename").toString()); result.insert("fullname", query.value("fullname").toString()); } // LOG(TRACE) << QString("根据id查询SYS_DEPT表的记录[ID=%1][%2]").arg(id).arg(sql).toStdString(); return result; } QVector<QVariantMap> SysDeptDao::findRecordsByProperty(QString properName, QVariant properValue) { // 新建查询 QSqlQuery query(ConnectionManager::getInstance()->getConnection()); // 查询语句 QString sql = "SELECT * FROM SYS_DEPT WHERE SYS_DEPT.%1 = '%2' ORDER BY PID, NUM"; 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").toString()); item.insert("pid", query.value("pid").toString()); item.insert("pids", query.value("pids").toString()); item.insert("simplename", query.value("simplename").toString()); item.insert("fullname", query.value("fullname").toString()); result.append(item); } // LOG(TRACE) << QString("根据属性值查询SYS_DEPT表的记录[%1][%2]").arg(count).arg(sql).toStdString(); return result; } QString SysDeptDao::save(QVariantMap object) { return "0"; } bool SysDeptDao::edit(QVariantMap newObject, QString id) { return true; } bool SysDeptDao::dele(QString id) { return true; } void SysDeptDao::appendChild(QList<QVariantMap> * deptTree, QVariantMap node) { // 1. 首先添加节点本身 deptTree->append(node); // 2. 查找节点的直接子节点 QVector<QVariantMap> children = findRecordsByProperty("pid", node.find("id")->toString()); if (children.isEmpty() == false) { // 3. 如果子节点不为空则递归添加 for (int i = 0; i < children.size(); i++) { appendChild(deptTree, children.at(i)); } } }