2025-06-23 10:41:33 +08:00
|
|
|
|
#pragma execution_character_set("utf-8")
|
|
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include "ProjectEventModule.h"
|
|
|
|
|
#include "Session.h"
|
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
#include "ProjectData.h"
|
|
|
|
|
#include "DataAccessModule.h"
|
|
|
|
|
#include "DataManager.h"
|
|
|
|
|
#include "M_ProjectDAO.h"
|
|
|
|
|
#include "FileGenerate.h"
|
|
|
|
|
#include "QWaiting.h"
|
|
|
|
|
|
|
|
|
|
ProjectEventModule::ProjectEventModule(QObject* /*parent*/, DataManager* manager) :BaseEventModule(manager, "ProjectManager")
|
|
|
|
|
{
|
|
|
|
|
addShareEventModule(new DataAccessModule(manager));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProjectEventModule::~ProjectEventModule()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
void ProjectEventModule::contextMenu(const json& /*request*/, QMenu* menu)
|
|
|
|
|
{
|
|
|
|
|
QAction* exportAction = new QAction(this);
|
2025-06-23 18:01:09 +08:00
|
|
|
|
exportAction->setText(QStringLiteral("导出"));//给动作设置文本
|
|
|
|
|
menu->addAction(exportAction); //把动作添加到菜单
|
2025-06-23 10:41:33 +08:00
|
|
|
|
|
|
|
|
|
QAction* instanceAction = new QAction(this);
|
2025-06-23 18:01:09 +08:00
|
|
|
|
instanceAction->setText(QStringLiteral("文档生成"));//给动作设置文本
|
|
|
|
|
menu->addAction(instanceAction); //把动作添加到菜单
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::onMessage(const std::string& eventName, json& parameter)
|
|
|
|
|
{
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("ProjectEventModule receive message.......");
|
|
|
|
|
LOG(INFO) << eventName;
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
TRY{
|
|
|
|
|
if (!pDataManager->_dbConnected)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
parameter["error"] = "数据库连接失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("ProjectEventModule::onMessage...DB connect failed");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (eventName == "add-project")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//新建项目
|
|
|
|
|
//项目添加失败需返回错误信息
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return addProject(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "save-project")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//修改项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return updateProject(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "delete-project")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//删除项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return deleteProject(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "change-project-focus")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//修改关注状态
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return changeFocusStatus(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "preimport-project")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//获取导入项目列表
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return getImportProjectList(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "import-project")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//导入项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return importProject(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "export-project")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//导出项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return exportProject(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "export-document")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//生成文档
|
2025-06-23 10:41:33 +08:00
|
|
|
|
return generateFile(parameter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CATCH(parameter);
|
|
|
|
|
|
|
|
|
|
return EventModule::onMessage(eventName, parameter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::addProject(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//新建name,type,author
|
2025-06-23 10:41:33 +08:00
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
QString name = CommonHelper::utf8ToQString(data["name"]);
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//先判断是否有重名项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
if (ProjectData::hasRepetitiveName(name, pDataManager))
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
parameter["error"] = "项目名称不能重复";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ProjectData* newProject = new ProjectData();
|
|
|
|
|
newProject->setNewData(parameter);
|
|
|
|
|
newProject->_parent = pDataManager;
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//将新建项目数据保存进数据库,存入pDataManager下层结点
|
2025-06-23 10:41:33 +08:00
|
|
|
|
if (pDataManager->addChild(newProject))
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//若保存成功,将新建项目返回给web端
|
2025-06-23 10:41:33 +08:00
|
|
|
|
data["id"] = newProject->_id;
|
|
|
|
|
data["createTime"] = CommonHelper::qstringToUtf8(newProject->getCreateTime());
|
|
|
|
|
data["updateTime"] = CommonHelper::qstringToUtf8(newProject->getUpdateTime());
|
|
|
|
|
parameter["response"] = data;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
delete newProject;
|
|
|
|
|
newProject = nullptr;
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//parameter错误提示
|
|
|
|
|
parameter["error"] = "新建项目失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("addProject Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//编辑项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
bool ProjectEventModule::updateProject(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//参数 id, name, type,author
|
|
|
|
|
//可以修改name,type,author
|
2025-06-23 10:41:33 +08:00
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
auto projectId = data["id"];
|
|
|
|
|
QString name = CommonHelper::utf8ToQString(data["name"]);
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//查找当前项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
DPData* pData = pDataManager->findObjectById(DataManager::DataType::project, projectId);
|
|
|
|
|
ProjectData* pProject = qobject_cast<ProjectData*>(pData);
|
|
|
|
|
if (pProject) {
|
|
|
|
|
pProject->setEditData(parameter);
|
|
|
|
|
if (pProject->saveSelf())
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//更新数据成功,冲掉web端的更新时间
|
2025-06-23 10:41:33 +08:00
|
|
|
|
data["updateTime"] = CommonHelper::qstringToUtf8(pProject->getUpdateTime());
|
|
|
|
|
parameter["response"] = data;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//编辑失败
|
|
|
|
|
parameter["error"] = "编辑项目失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2025-06-23 18:01:09 +08:00
|
|
|
|
parameter["error"] = "查询项目失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("updateProject Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::deleteProject(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//需要删除的项目编号
|
2025-06-23 10:41:33 +08:00
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
auto projectId = data["id"];
|
|
|
|
|
DPData* pData = pDataManager->findObjectById(DataManager::DataType::project, projectId);
|
|
|
|
|
|
|
|
|
|
if (pData)
|
|
|
|
|
{
|
|
|
|
|
if (pData->deleteSelf())
|
2025-06-23 18:01:09 +08:00
|
|
|
|
{//更新总map
|
2025-06-23 10:41:33 +08:00
|
|
|
|
pDataManager->deleteInDataMap(DataManager::DataType::project, projectId);
|
|
|
|
|
delete pData;
|
|
|
|
|
pData = nullptr;
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//删除成功
|
2025-06-23 10:41:33 +08:00
|
|
|
|
parameter["response"]["deleted"] = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parameter["response"]["deleted"] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
parameter["error"] = "找不到该项目";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("deleteProject Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::changeFocusStatus(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//获取项目id和关注状态
|
2025-06-23 10:41:33 +08:00
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
auto projectId = data["id"];
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//查找当前项目
|
2025-06-23 10:41:33 +08:00
|
|
|
|
DPData* pData = pDataManager->findObjectById(DataManager::DataType::project, projectId);
|
|
|
|
|
ProjectData* pProject = qobject_cast<ProjectData*>(pData);
|
|
|
|
|
if (pProject) {
|
|
|
|
|
pProject->setFocus(data["focus"]);
|
|
|
|
|
if (pProject->saveSelf())
|
|
|
|
|
{
|
|
|
|
|
data["focused"] = pProject->getFocus();
|
|
|
|
|
parameter["response"] = data;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//失败
|
|
|
|
|
parameter["error"] = "修改关注状态失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2025-06-23 18:01:09 +08:00
|
|
|
|
parameter["error"] = "查询项目失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("changeFocusStatus Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::getImportProjectList(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//选择导入项目文件,返回项目列表
|
|
|
|
|
QString selectDir = QFileDialog::getOpenFileName(nullptr, "选择导入文件", "", "*.dbp");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
SysManager& sysMgr = GetSysManager();
|
|
|
|
|
sysMgr._importFilePath = selectDir;
|
|
|
|
|
QList<ProjectData*> projectList;
|
|
|
|
|
parameter["response"] = json::array();
|
|
|
|
|
if (selectDir.isEmpty())
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//未选择导入文件
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("未选择导入文件");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ProjectData::getImportProjectList(projectList, selectDir))
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < projectList.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
json j;
|
|
|
|
|
projectList.at(i)->toJson(j);
|
|
|
|
|
parameter["response"].push_back(j);
|
|
|
|
|
}
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//释放内存
|
2025-06-23 10:41:33 +08:00
|
|
|
|
qDeleteAll(projectList);
|
|
|
|
|
projectList.clear();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
parameter["error"] = "获取导入项目列表失败";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("getImportProjectList Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::importProject(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
QWaiting waiting(Session::getSession()->parent(), "项目导入中...");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
waiting.working(std::bind([&](json& parameter) {
|
|
|
|
|
parameter["response"] = json::array();
|
|
|
|
|
QList<int> projectIdList;
|
|
|
|
|
if (ProjectData::importData(parameter, projectIdList))
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//从数据库取
|
2025-06-23 10:41:33 +08:00
|
|
|
|
pDataManager->getAllChildren();
|
|
|
|
|
ProjectData* pData = nullptr;
|
|
|
|
|
for (int i = 0; i < projectIdList.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
pData = dynamic_cast<ProjectData*>(pDataManager->findObjectById(DataManager::DataType::project, projectIdList.at(i)));
|
|
|
|
|
if (pData)
|
|
|
|
|
{
|
|
|
|
|
json j;
|
|
|
|
|
pData->toJson(j);
|
|
|
|
|
parameter["response"].push_back(j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("ProjectData::importData failed ");
|
|
|
|
|
}
|
|
|
|
|
return true; }, std::ref(parameter)));
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("importProject Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProjectEventModule::exportProject(json& parameter)
|
|
|
|
|
{
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
QString filePath = QFileDialog::getSaveFileName(nullptr, "保存导出文件", "", tr("*.dbp"));
|
2025-06-23 10:41:33 +08:00
|
|
|
|
|
|
|
|
|
if (filePath.isEmpty())
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("未选择导出路径");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//调用多线程处理耗时的任务
|
|
|
|
|
QWaiting waiting(Session::getSession()->parent(), "项目导出中...");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
waiting.working(std::bind([](json& parameter, QString filePath) {
|
|
|
|
|
|
|
|
|
|
if (ProjectData::exportData(parameter, filePath))
|
|
|
|
|
{
|
|
|
|
|
parameter["response"] = { {"status",true} };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("exportProject failed");
|
|
|
|
|
parameter["response"] = { {"status",false} };
|
|
|
|
|
}
|
|
|
|
|
return true; }, std::ref(parameter), filePath));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("exportProject Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//生成需求文档
|
2025-06-23 10:41:33 +08:00
|
|
|
|
bool ProjectEventModule::generateFile(json& parameter)
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//选择生成实例文件全路径
|
|
|
|
|
// QString file = QFileDialog::getSaveFileName(nullptr, tr("生成需求文件"), "", ".doc");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
QString path = QFileDialog::getExistingDirectory(nullptr, "生成需求文件", "");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
if (path.isEmpty())
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("未选择生成路径");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
parameter["response"] = { {"status",false} };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
QWaiting waiting(Session::getSession()->parent(), "文档生成中...");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
waiting.working(std::bind([¶meter, path]() {
|
|
|
|
|
FileGenerate generator;
|
|
|
|
|
if (generator.generate(path + "/", parameter))
|
|
|
|
|
{
|
|
|
|
|
parameter["response"] = { {"status",true} };
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
parameter["response"] = { {"status",false} };
|
|
|
|
|
};
|
|
|
|
|
return true;
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("generateFile Sendto web.......");
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|