448 lines
11 KiB
C++
448 lines
11 KiB
C++
#pragma execution_character_set("utf-8")
|
||
|
||
#include <QDateTime>
|
||
#include <QCoreApplication>
|
||
#include <QFileDialog>
|
||
#include "DataModelEventModule.h"
|
||
#include "DataModelData.h"
|
||
#include "DataManager.h"
|
||
#include "ProjectData.h"
|
||
|
||
#include "DataAccessModule.h"
|
||
|
||
|
||
DataModelEventModule::DataModelEventModule(QObject* /*parent*/, DataManager* manager)
|
||
:BaseEventModule(manager, "DataModelListView")
|
||
{
|
||
addShareEventModule(new DataAccessModule(manager));
|
||
}
|
||
|
||
DataModelEventModule::~DataModelEventModule()
|
||
{
|
||
|
||
}
|
||
|
||
bool DataModelEventModule::onMessage(const std::string& eventName, json& parameter)
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("DataModelEventModule receive message.......");
|
||
LOG(INFO) << eventName;
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
TRY{
|
||
if (eventName == "get-project") {
|
||
//获取项目信息
|
||
return getProject(parameter);
|
||
}
|
||
//else if (eventName == "get-project-datamodel")
|
||
//{
|
||
// return getAllDataModel(parameter);
|
||
//}
|
||
else if (eventName == "get-datamodel-by-identification")
|
||
{
|
||
return getDataModelByIdentification(parameter);
|
||
}
|
||
else if (eventName == "add-datamodel")
|
||
{
|
||
//新建数据模型
|
||
return addDataModel(parameter);
|
||
}
|
||
else if (eventName == "has-datamodel-child")
|
||
{
|
||
return hasDatamodelChild(parameter);
|
||
}
|
||
else if (eventName == "save-datamodel")
|
||
{
|
||
//修改数据模型
|
||
return upateDataModel(parameter);
|
||
}
|
||
else if (eventName == "delete-datamodel")
|
||
{
|
||
//删除数据模型
|
||
return deleteDataModel(parameter);
|
||
}
|
||
else if (eventName == "copy-datamodel")
|
||
{
|
||
//复制数据模型
|
||
return copyDataModel(parameter);
|
||
}
|
||
else if (eventName == "add-datamodel-version")
|
||
{
|
||
//数据模型升版
|
||
return upgradeVersion(parameter);
|
||
}
|
||
else if (eventName == "create-instance")
|
||
{
|
||
//生成实例文件
|
||
return generateInstanceFile(parameter);
|
||
}
|
||
}
|
||
CATCH(parameter);
|
||
|
||
return EventModule::onMessage(eventName, parameter);
|
||
}
|
||
|
||
//////////////////////////////////////////////////////////////////////////
|
||
//以下为消息响应函数
|
||
bool DataModelEventModule::getProject(json& parameter)
|
||
{
|
||
//获得当前项目id
|
||
auto data = parameter["data"];
|
||
int projectId = data["id"];
|
||
DPData* pData = pDataManager->findObjectById(DataManager::DataType::project, projectId);
|
||
ProjectData* pProject = dynamic_cast<ProjectData*>(pData);
|
||
if (pProject)
|
||
{
|
||
pProject->setCurrentProject();
|
||
json project;
|
||
pProject->toJson(project);
|
||
|
||
parameter["response"] = project;
|
||
}
|
||
else
|
||
{
|
||
parameter["error"] = "找不到该项目";
|
||
}
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("getProject Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
///*获取所有数据模型*/
|
||
//bool DataModelEventModule::getAllDataModel(json& parameter)
|
||
//{
|
||
// //获得当前项目id
|
||
// auto data = parameter["data"];
|
||
// int projectId = data["id"];
|
||
//
|
||
// DPData* pData = pDataManager->findObjectById(DataManager::DataType::project, projectId);
|
||
// ProjectData* pProject = dynamic_cast<ProjectData*>(pData);
|
||
//
|
||
// if (pProject)
|
||
// {
|
||
// //找到该项目
|
||
// if (!pProject->_initChildren)
|
||
// {
|
||
// pProject->getAllChildren(pDataManager);
|
||
// pProject->_initChildren = true;
|
||
// }
|
||
//
|
||
// if (pProject->_childrenMap.isEmpty())
|
||
// {
|
||
// parameter["error"] = "该项目没有数据模型";
|
||
// LOG(INFO) << CommonHelper::utf8ToStdString("getAllDataModel Sendto web.......");
|
||
// LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
// return true;
|
||
// }
|
||
// //读取的数据给parameter,返回给界面
|
||
// QMap<unsigned int, DPData*>::iterator it = pProject->_childrenMap.begin();
|
||
// while (it != pProject->_childrenMap.end())
|
||
// {
|
||
// DataModelData* pData = dynamic_cast<DataModelData*>(*it);
|
||
// //将对象信息转json
|
||
// if (pData) {
|
||
// json j;
|
||
// pData->toJson(j);
|
||
// parameter["response"].push_back(j);
|
||
// }
|
||
// it++;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// parameter["error"] = "找不到该项目";
|
||
// }
|
||
//
|
||
// LOG(INFO) << CommonHelper::utf8ToStdString("getAllDataModel Sendto web.......");
|
||
// LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
//
|
||
// return true;
|
||
//}
|
||
|
||
/*新建数据模型*/
|
||
bool DataModelEventModule::addDataModel(json& parameter)
|
||
{
|
||
|
||
//获得当前项目id
|
||
auto data = parameter["data"];
|
||
auto projectId = data["parentid"];
|
||
//查找项目,上层对象
|
||
DPData* pCurrentProject = pDataManager->findObjectById(DataManager::DataType::project, projectId);
|
||
if (!pCurrentProject) {
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("addDataModel Sendto web.......");
|
||
parameter["error"] = "找不到数据模型的父类项目";
|
||
|
||
return true;
|
||
}
|
||
//新建时输入name type description
|
||
DataModelData* pNewData = new DataModelData();
|
||
//设置name type des updateTime createTime
|
||
pNewData->setNewData(parameter);
|
||
|
||
//设置上层数据
|
||
pNewData->_parent = pCurrentProject;
|
||
//添加数据模型
|
||
if (pCurrentProject->addChild(pNewData))
|
||
{
|
||
//添加到总map
|
||
pDataManager->insertDataMap(DataManager::DataType::dataModel, pNewData->_id, pNewData);
|
||
//若新建数据模型成功,将新建数据模型信息返回给web端
|
||
pNewData->toJson(data);
|
||
parameter["response"] = data;
|
||
}
|
||
else
|
||
{
|
||
//失败
|
||
delete pNewData;
|
||
pNewData = nullptr;
|
||
parameter["error"] = "新建数据模型失败";
|
||
}
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("addDataModel Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
/*是否有下层数据*/
|
||
bool DataModelEventModule::hasDatamodelChild(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
auto dataModelId = data["id"];
|
||
DPData* pData = pDataManager->findObjectById(DataManager::DataType::dataModel, dataModelId);
|
||
if (!pData) {
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("hasDatamodelChild Sendto web.......");
|
||
parameter["error"] = "找不到该数据模型";
|
||
|
||
return true;
|
||
}
|
||
if (pData->_childrenMap.size() > 0)
|
||
{
|
||
parameter["response"]["status"] = true;
|
||
}
|
||
else
|
||
{
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("hasDatamodelChild Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
/*更新数据模型*/
|
||
bool DataModelEventModule::upateDataModel(json& parameter)
|
||
{
|
||
QVariantMap valueMap;
|
||
//修改name type desciption
|
||
auto data = parameter["data"];
|
||
auto dataModelId = data["id"];
|
||
//查找当前数据模型
|
||
DataModelData* pDataModel = (DataModelData*)pDataManager->findObjectById(DataManager::DataType::dataModel, dataModelId);
|
||
if (!pDataModel) {
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("upateDataModel Sendto web.......");
|
||
parameter["error"] = "找不到该数据模型";
|
||
|
||
return true;
|
||
}
|
||
//如果锁定,则不能修改
|
||
if (pDataModel->_isVerLocked)
|
||
{
|
||
parameter["error"] = "该版本已锁定,无法修改";
|
||
}
|
||
else
|
||
{
|
||
pDataModel->setEditData(parameter);
|
||
if (pDataModel->saveSelf())
|
||
{
|
||
//更新数据成功,返回给web端,更新时间
|
||
data["updateTime"] = CommonHelper::qstringToUtf8(pDataModel->getUpdateTime());
|
||
parameter["response"] = data;
|
||
}
|
||
else
|
||
{
|
||
parameter["error"] = "修改数据模型失败";
|
||
}
|
||
}
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("upateDataModel Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
|
||
/*删除数据模型*/
|
||
bool DataModelEventModule::deleteDataModel(json& parameter)
|
||
{
|
||
//获得id
|
||
auto data = parameter["data"];
|
||
auto dataModelId = data["id"];
|
||
|
||
DPData* pData = pDataManager->findObjectById(DataManager::DataType::dataModel, dataModelId);
|
||
if (!pData) {
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("deleteDataModel Sendto web.......");
|
||
parameter["error"] = "找不到该数据模型";
|
||
|
||
return true;
|
||
}
|
||
if (pData->deleteSelf())
|
||
{
|
||
//更新内存数据
|
||
pDataManager->deleteInDataMap(DataManager::DataType::dataModel, dataModelId);
|
||
delete pData;
|
||
pData = nullptr;
|
||
//删除成功
|
||
parameter["response"]["deleted"] = true;
|
||
}
|
||
else
|
||
{
|
||
parameter["response"]["deleted"] = false;
|
||
}
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("deleteDataModel Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
|
||
}
|
||
|
||
/*复制数据模型*/
|
||
bool DataModelEventModule::copyDataModel(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int srcDataModelId = data["id"];
|
||
//查找选定的数据模型
|
||
DataModelData* pData = (DataModelData*)pDataManager->findObjectById(DataManager::DataType::dataModel, srcDataModelId);
|
||
if (pData)
|
||
{
|
||
DataModelData* pNewModel = new DataModelData();
|
||
if (pData->copyData(pNewModel, parameter))
|
||
{
|
||
pNewModel->_parent = pData->_parent;
|
||
pDataManager->insertDataMap(DataManager::DataType::dataModel, pNewModel->_id, pNewModel);
|
||
//将新数据模型返回给web端
|
||
json j;
|
||
pNewModel->toJson(j);
|
||
parameter["response"] = j;
|
||
}
|
||
else
|
||
{
|
||
//失败
|
||
delete pNewModel;
|
||
pNewModel = nullptr;
|
||
parameter["error"] = "复制数据模型失败";
|
||
}
|
||
|
||
}
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("copyDataModel Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
/*生成实例文件*/
|
||
bool DataModelEventModule::generateInstanceFile(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int srcDataModelId = data["id"];
|
||
//查找选定的数据模型
|
||
DataModelData* pData = (DataModelData*)pDataManager->findObjectById(DataManager::DataType::dataModel, srcDataModelId);
|
||
QString msg;
|
||
try
|
||
{
|
||
if (pData)
|
||
{
|
||
if (!pData->createInstance(msg))
|
||
{
|
||
if (msg != "未选择实例文件路径")
|
||
{
|
||
parameter["error"] = CommonHelper::qstringToUtf8(msg);
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
parameter["response"]["status"] = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//未找到该模型
|
||
parameter["error"] = "未找到该数据模型";
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
}
|
||
catch (exception& e)
|
||
{
|
||
parameter["error"] = "创建实例失败";
|
||
|
||
LOG(ERROR) << e.what();
|
||
return true;
|
||
}
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("generateInstanceFile Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
|
||
}
|
||
|
||
/*数据模型升版*/
|
||
bool DataModelEventModule::upgradeVersion(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int srcDataModelId = data["id"];
|
||
//查找选定的数据模型
|
||
DataModelData* pData = qobject_cast<DataModelData*>(pDataManager->findObjectById(DataManager::DataType::dataModel, srcDataModelId));
|
||
if (pData)
|
||
{
|
||
DataModelData* pNewModel = new DataModelData();
|
||
if (pData->upgradeVersion(pNewModel))
|
||
{
|
||
//加入所属项目
|
||
pNewModel->_parent = pData->_parent;
|
||
pData->_parent->_childrenMap.insert(pNewModel->_id, pNewModel);
|
||
pDataManager->insertDataMap(DataManager::DataType::dataModel, pNewModel->_id, pNewModel);
|
||
//将新数据模型返回给web端
|
||
json j;
|
||
pNewModel->toJson(j);
|
||
parameter["response"] = j;
|
||
|
||
}
|
||
else
|
||
{
|
||
//升版失败
|
||
parameter["error"] = "该模型升版失败";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//未找到该模型
|
||
parameter["error"] = "未找到该模型";
|
||
}
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("upgradeVersion Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
bool DataModelEventModule::getDataModelByIdentification(json& parameter)
|
||
{
|
||
//获取名称标识相同的数据模型
|
||
auto data = parameter["data"];
|
||
int projectId = data["id"];
|
||
ProjectData* pProject = qobject_cast<ProjectData*>(pDataManager->findObjectById(DataManager::DataType::project, projectId));
|
||
if (!pProject) {
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("getDataModelByIdentification Sendto web.......");
|
||
|
||
parameter["error"] = "未找到该项目";
|
||
return true;
|
||
}
|
||
QString name = CommonHelper::utf8ToQString(data["identification"]);
|
||
QList<DPData*> modelList;
|
||
pProject->findDatamodelByName(name, modelList);
|
||
parameter["response"] = json::array();
|
||
for (int i = 0; i < modelList.size(); i++)
|
||
{
|
||
json j;
|
||
qobject_cast<DataModelData*>(modelList.at(i))->toJson(j);
|
||
parameter["response"].push_back(j);
|
||
}
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("getDataModelByIdentification Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
} |