#include "ProjectMindRelationService.h" #include "SqlDBHelper.h" #include ProjectMindRelationService::ProjectMindRelationService(QObject *parent) { } bool ProjectMindRelationService::AddOrUpdateRelation(const ProjectMindRelation &relation) { bool ret = false; try { QString nowDate = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); Transaction t(SqlDBHelper::getDatabase()); int id = relation.id; if (id == -1) { t.update("t_project_mind_ralation") .set("mind_id", relation.mindId) .set("type", relation.type) .set("projectId", relation.projectId) .set("updateTime", nowDate) .where("id = ?", id); } else { InsertQuery query = t.insertInto("t_project_mind_ralation( `mind_id`, `project_id`, `type` `create_time`)"); NonQueryResult result = query.values(relation.mindId, relation.projectId, relation.type, nowDate).exec(); } t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool ProjectMindRelationService::QueryByProjectIdAndType(ProjectMindRelation *relation, int projectId, int type) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("SELECT id, mind_id,project_id,type FROM t_project_mind_ralation WHERE type = %1 and " "project_id = %2 ") .arg(type) .arg(projectId); if (query.exec(selectSql)) { if (query.isNull(0) == false) { relation->id = query.value(0).toInt(); relation->mindId = query.value(1).toInt(); relation->projectId = query.value(0).toInt(); relation->type = query.value(0).toInt(); } ret = true; } else { qDebug() << query.lastError(); } return ret; } bool ProjectMindRelationService::QueryByProjectId(QList *relationList, int projectId) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("SELECT id, mind_id,project_id,type FROM t_project_mind_ralation WHERE " "project_id = %1 ") .arg(projectId); if (query.exec(selectSql)) { while (query.next()) { if (query.isNull(0) == false) { ProjectMindRelation *relation = new ProjectMindRelation(); relation->id = query.value(0).toInt(); relation->mindId = query.value(1).toInt(); relation->projectId = query.value(0).toInt(); relation->type = query.value(0).toInt(); relationList->append(relation); } } ret = true; } else { qDebug() << query.lastError(); } return ret; }