1157 lines
28 KiB
C++
1157 lines
28 KiB
C++
#pragma execution_character_set("utf-8")
|
||
|
||
#include <QCoreApplication>
|
||
#include <QFileDialog>
|
||
#include<Qtime>
|
||
#include<QProcess>
|
||
|
||
#include"SearchEditEventModule.h"
|
||
#include"DataManager.h"
|
||
#include"DataModelData.h"
|
||
#include"M_EntityModelDAO.h"
|
||
#include"M_EntityTableDAO.h"
|
||
#include"M_NormalColumnDAO.h"
|
||
#include"DataAttribute.h"
|
||
#include"DataClass.h"
|
||
#include"Connection.h"
|
||
#include"global.h"
|
||
#include"ProjectData.h"
|
||
#include"SqlcipherTool.h"
|
||
|
||
//#define QTDEBUG
|
||
|
||
#ifdef QTDEBUG
|
||
|
||
json ret16 = json::array();
|
||
json ret17 = json::array();
|
||
json ret18 = json::array();
|
||
json ret30 = json::array();
|
||
json ret31 = json::array();
|
||
|
||
QStringList head;
|
||
|
||
//测试数据生成
|
||
void buildTestData()
|
||
{
|
||
head << "id" << "proA" << "proB" << "proC" << "proD" << "proE";
|
||
for (int index = 1; index <= 5; index++)
|
||
{
|
||
//少量数据
|
||
for (int i = 0; i < index*1000; i++)
|
||
{
|
||
json col;
|
||
col["id"] = QString::number(i + 1).toStdString();
|
||
for (int j = 1; j < head.size(); j++)
|
||
{
|
||
col[head[j].toStdString()] = QString::number(qrand() % 10).toStdString();
|
||
}
|
||
switch (index)
|
||
{
|
||
case 1:
|
||
ret16.push_back(col);
|
||
break;
|
||
case 2:
|
||
ret17.push_back(col);
|
||
break;
|
||
case 3:
|
||
ret18.push_back(col);
|
||
break;
|
||
case 4:
|
||
ret30.push_back(col);
|
||
break;
|
||
case 5:
|
||
ret31.push_back(col);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
#endif
|
||
|
||
|
||
|
||
void getfile(json& parameter)
|
||
{
|
||
//打开文件夹选择
|
||
QString curPath = QCoreApplication::applicationDirPath();
|
||
QString selectDir = QFileDialog::getOpenFileName(nullptr, "选择文件", curPath, "");
|
||
if (!selectDir.isEmpty())
|
||
{
|
||
parameter["response"] = CommonHelper::qstringToUtf8(selectDir);
|
||
}
|
||
|
||
}
|
||
|
||
QString buildTmpFileName(QString type, int id, int colid)
|
||
{
|
||
QString tail = type.section('.', 1);
|
||
if (tail == "*")
|
||
{
|
||
return QString("%1@%2").arg(id).arg(colid);
|
||
}
|
||
else {
|
||
return QString("%1@%2.%3").arg(id).arg(colid).arg(tail);
|
||
}
|
||
}
|
||
|
||
QString checkFileType(QByteArray bytes)
|
||
{
|
||
QByteArray fileHead = bytes.left(50);
|
||
if (fileHead.startsWith("\xFF\xD8\xFF"))
|
||
{
|
||
return "*.jpg";
|
||
}
|
||
else if (fileHead.startsWith("\x42\x4D"))
|
||
{
|
||
return "*.bmp";
|
||
}
|
||
else if (fileHead.startsWith("\x89\x50\x4E\x47"))
|
||
{
|
||
return "*.png";
|
||
}
|
||
else if (fileHead.startsWith("\x47\x49\x46\x38"))
|
||
{
|
||
return "*.gif";
|
||
}
|
||
else if (fileHead.startsWith("\x3c\x3F\x78\x6D\x6C"))
|
||
{
|
||
return "*.xml";
|
||
}
|
||
else if (fileHead.startsWith("\x68\x74\x6D\x6C\x3E"))
|
||
{
|
||
return "*.html";
|
||
}
|
||
else
|
||
return NULL;
|
||
}
|
||
|
||
SearchEditEventModule::SearchEditEventModule(QObject* /*parent*/, DataManager* manager)
|
||
:BaseEventModule(manager, "SearchEditModule")
|
||
{
|
||
|
||
#ifdef QTDEBUG
|
||
buildTestData();
|
||
#endif // QTDEBUG
|
||
}
|
||
|
||
SearchEditEventModule::~SearchEditEventModule()
|
||
{
|
||
|
||
}
|
||
|
||
bool SearchEditEventModule::onMessage(const std::string& eventName, json& parameter)
|
||
{
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("SearchEditEventModule receive message.......");
|
||
LOG(INFO) << eventName;
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
|
||
//检查是否连接数据库
|
||
DataManager& dataRoot = GetDataRoot();
|
||
if (!dataRoot.pTheDBPlatform)
|
||
{
|
||
parameter["error"] = "请先连接数据库文件!";
|
||
return true;
|
||
}
|
||
|
||
//using namespace DBPlatformSpace;
|
||
//DBPlatformNew::setCurrentWorkDB("d:\\test1");
|
||
//M_EntityTableDAO* pTable = NULL;
|
||
//M_EntityTableDAO::FindByID(422, pTable);
|
||
//list<M_NormalColumnDAO*> columnList;
|
||
//list<string>valueList, valuelist2;
|
||
//ResultMsg rm3 = pTable->getData(columnList, valueList);
|
||
|
||
//valuelist2.push_back("-1@#@202@#@BBC大大小@#@101");
|
||
//valuelist2.push_back("-1@#@201@#@BB大大小多多少少@#@103");
|
||
//valuelist2.push_back("-1@#@203@#@BB大大小多少@#@101");
|
||
//ResultMsg rm4 = pTable->saveData(columnList, valuelist2);
|
||
//rm3 = pTable->getData(columnList, valueList);
|
||
|
||
TRY{
|
||
if (eventName == "open-file-dir")
|
||
{
|
||
////打开文件夹选择
|
||
//QString curPath = QCoreApplication::applicationDirPath();
|
||
//QString selectDir = QFileDialog::getOpenFileName(nullptr, "选择文件", curPath, "");
|
||
//if (!selectDir.isEmpty())
|
||
//{
|
||
// parameter["response"] = CommonHelper::qstringToUtf8(selectDir);
|
||
//}
|
||
getfile(parameter);
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("open-file-dir Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
else if (eventName == "open-data-file")
|
||
{
|
||
//连接指定的数据文件
|
||
return connectDBFile(parameter);
|
||
}
|
||
else if (eventName == "save-connection")
|
||
{
|
||
//保存连接信息
|
||
return saveConnection(parameter);
|
||
}
|
||
else if (eventName == "close-connection")
|
||
{//关闭连接
|
||
return closeConnection(parameter);
|
||
}
|
||
else if (eventName == "delete-connection")
|
||
{//删除连接
|
||
return delConnection(parameter);
|
||
}
|
||
else if (eventName == "get-table-stru")
|
||
{
|
||
//获取数据类结构
|
||
return getTableStru(parameter);
|
||
}
|
||
else if (eventName == "get-table-data")
|
||
{
|
||
//获取数据类数据
|
||
return getTableData(parameter);
|
||
}
|
||
else if (eventName == "save-table-item")
|
||
{
|
||
//添加和修改数据
|
||
return saveTableItem(parameter);
|
||
}
|
||
else if (eventName == "delete-table-item")
|
||
{
|
||
//删除
|
||
return delTableItem(parameter);
|
||
}
|
||
else if (eventName == "save-new-data-file")
|
||
{ //另存为
|
||
return saveNewFile(parameter);
|
||
}
|
||
else if (eventName == "filter-data")
|
||
{ //筛选数据
|
||
return filterData(parameter);
|
||
}
|
||
else if (eventName == "get-src-file")
|
||
{ //获取需要加/解密的数据文件
|
||
getfile(parameter);
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("get-src-file Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
else if (eventName == "set-new-file")
|
||
{
|
||
setNewFile(parameter);
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("seet-new-file Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
else if (eventName == "do-encryption")
|
||
{
|
||
//进行加解密
|
||
sqlcipherDo(parameter);
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("do-encryption Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
else if (eventName == "save-data-to-file")
|
||
{//保存blob数据到本地
|
||
saveBlobToFile(parameter);
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("save-data-to-file Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
else if (eventName == "copy-blob-data")
|
||
{
|
||
//复制blob
|
||
copyBlobData(parameter);
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("copy-blob-data Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
}
|
||
CATCH(parameter);
|
||
|
||
|
||
return EventModule::onMessage(eventName, parameter);
|
||
}
|
||
|
||
|
||
bool SearchEditEventModule::connectDBFile(json& parameter)
|
||
{
|
||
#ifdef QTDEBUG
|
||
json ret;
|
||
auto data = parameter["data"];
|
||
int id = data["id"];
|
||
int modelId = 0;
|
||
//查找连接
|
||
SysManager& sysMnr = GetSysManager();
|
||
Connection* pCon = sysMnr.findConnection(id);
|
||
if (!pCon)
|
||
{
|
||
//没找到连接
|
||
parameter["error"] = "找不到连接";
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("open-data-file Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
QString name = pCon->getPath().right(4);
|
||
if (name == "A.db")
|
||
{
|
||
//失败
|
||
parameter["response"]["status"] = false;
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("open-data-file Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
else if (name == "B.db")
|
||
{
|
||
modelId = 3;
|
||
}
|
||
else if (name == "C.db")
|
||
{
|
||
modelId = 6;
|
||
}
|
||
else if (name == "D.db")
|
||
{
|
||
modelId = 7;
|
||
}
|
||
|
||
|
||
//当前时间
|
||
QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm");
|
||
pCon->setTime(curTime);
|
||
pCon->setStatus(true);
|
||
pCon->toJson(ret);
|
||
|
||
DataModelData* pCurrentModel = qobject_cast<DataModelData*>(pDataManager->findObjectById(DataManager::DataType::dataModel, modelId));
|
||
ret["dataclass"] = json::array();
|
||
if (pCurrentModel)
|
||
{
|
||
//如果找到模型,获得所有的数据类
|
||
if (!pCurrentModel->_initChildren)
|
||
{
|
||
pCurrentModel->getAllChildren(pDataManager, false);
|
||
pCurrentModel->_initChildren = true;
|
||
}
|
||
|
||
|
||
//json
|
||
QMap<unsigned int, DPData*>::iterator it = pCurrentModel->_childrenMap.begin();
|
||
while (it != pCurrentModel->_childrenMap.end())
|
||
{
|
||
json pack;
|
||
(*it)->toJson(pack, true);
|
||
ret["dataclass"].push_back(pack);
|
||
it++;
|
||
}
|
||
}
|
||
parameter["response"] = ret;
|
||
#else
|
||
using namespace DBPlatformSpace;
|
||
json ret;
|
||
auto data = parameter["data"];
|
||
int id = data["id"];
|
||
QString modelName; //数据文件对应模型名,确认是 名称标识还是显示名称?
|
||
//查找连接
|
||
SysManager& sysMnr = GetSysManager();
|
||
Connection* pCon = sysMnr.findConnection(id);
|
||
//确认连接文件是否存在
|
||
QFile file(pCon->getPath());
|
||
if (file.exists())
|
||
{
|
||
ResultMsg rm = DBPlatformSpace::DBPlatformNew::setCurrentWorkDB(CommonHelper::qstringToStdString(pCon->getPath()),0);
|
||
if (rm.rCode == 0)
|
||
{
|
||
//获取对应的数据模型
|
||
M_EntityModelDAO* pMEntityModel = nullptr;
|
||
ResultMsg rm = M_EntityModelDAO::getInstanceInfo(CommonHelper::qstringToStdString(pCon->getPath()), pMEntityModel);
|
||
if (rm.rCode == 0)
|
||
{
|
||
//设置当前文件路径
|
||
currentFilePath = pCon->getPath();
|
||
//文件设置成功
|
||
pCon->setStatus(true);
|
||
//连接类型:置为 sqlite
|
||
pCon->setType("sqlite");
|
||
//当前时间
|
||
QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm");
|
||
pCon->setTime(curTime);
|
||
pCon->toJson(ret); //连接信息返回
|
||
ret["dataclass"] = json::array();
|
||
|
||
////先找项目
|
||
ProjectData* pPro = qobject_cast<ProjectData*>(pDataManager->findObjectById(DataManager::DataType::project, pMEntityModel->_rProjectID));
|
||
if (pPro)
|
||
{
|
||
if (!pPro->_initChildren)
|
||
pPro->getAllChildren(pDataManager);
|
||
//获取数据模型名
|
||
pCurrentModel = qobject_cast<DataModelData*>(pDataManager->findObjectById(DataManager::DataType::dataModel, pMEntityModel->_ID));
|
||
if (pCurrentModel)
|
||
{
|
||
//如果找到模型,获得所有的数据类
|
||
if (!pCurrentModel->_initChildren)
|
||
{
|
||
pCurrentModel->getAllChildren(pDataManager, false);
|
||
pCurrentModel->_initChildren = true;
|
||
}
|
||
|
||
parameter["response"] = json::array();
|
||
//json
|
||
QMap<unsigned int, DPData*>::iterator it = pCurrentModel->_childrenMap.begin();
|
||
while (it != pCurrentModel->_childrenMap.end())
|
||
{
|
||
json pack;
|
||
(*it)->toJson(pack, true);
|
||
ret["dataclass"].push_back(pack);
|
||
it++;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//找不到数据模型
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//找不到项目提示错误信息
|
||
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
//查询实例文件和项目信息失败
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("M_EntityModelDAO::getInstanceInfo failed ");
|
||
parameter["error"] = "打开连接失败,请检查文件是否有效";
|
||
LOG(ERROR) << rm.rMsg;
|
||
return true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("setCurrentWorkDB failed ");
|
||
parameter["error"] = "打开连接失败,请检查文件是否有效";
|
||
LOG(ERROR) << rm.rMsg;
|
||
return true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << "连接文件不存在";
|
||
parameter["error"] = "连接文件不存在,请检查。";
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("open-data-file Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
parameter["response"] = ret;
|
||
#endif
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("open-data-file Sendto web.......");
|
||
// LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
//前端新建连接与修改连接完保存后
|
||
bool SearchEditEventModule::saveConnection(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["id"];
|
||
Connection* pConn = nullptr;
|
||
SysManager& sysMnr = GetSysManager();
|
||
if (id == -1)
|
||
{
|
||
//新建连接
|
||
pConn = new Connection();
|
||
id = sysMnr.IDTool.new_id();
|
||
pConn->save(parameter);
|
||
pConn->setID(id);
|
||
sysMnr.addConnection(id, pConn);
|
||
}
|
||
else
|
||
{
|
||
pConn = sysMnr.findConnection(id);
|
||
if (pConn)
|
||
{
|
||
pConn->save(parameter);
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("save-connection cannot find connection");
|
||
parameter["response"]["status"] = false;
|
||
return true;
|
||
}
|
||
|
||
}
|
||
json j;
|
||
sysMnr.getConnectionJson(j);
|
||
parameter["response"] = j;
|
||
|
||
//sysMnr.saveConnectionProfile();
|
||
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("save-connection Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
|
||
//关闭数据连接
|
||
bool SearchEditEventModule::closeConnection(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["id"];
|
||
SysManager& sysMnr = GetSysManager();
|
||
Connection* pCon = sysMnr.findConnection(id);
|
||
if (!pCon)
|
||
{
|
||
parameter["error"] = "找不到连接";
|
||
return true;
|
||
}
|
||
pCon->setStatus(false);
|
||
// pDataManager->pTheDBPlatform->setCurrentWorkDB("");
|
||
parameter["response"]["status"] = true;
|
||
return true;
|
||
}
|
||
//删除数据连接
|
||
bool SearchEditEventModule::delConnection(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["id"];
|
||
SysManager& sysMnr = GetSysManager();
|
||
if (sysMnr.delConnection(id))
|
||
{
|
||
json j;
|
||
sysMnr.getConnectionJson(j);
|
||
parameter["response"] = j;
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("del-connection failed.......");
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
|
||
|
||
sysMnr.IDTool.recylce_id(id);
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
bool SearchEditEventModule::getTableStru(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["tableId"];
|
||
#ifdef QTDEBUG
|
||
DataClass* pTable = qobject_cast<DataClass*>(pDataManager->findObjectById(DataManager::DataType::dataClass, id));
|
||
if (!pTable)
|
||
{
|
||
parameter["error"] = "找不到数据类";
|
||
}
|
||
else
|
||
{
|
||
//json
|
||
QMap<unsigned int, DPData*>::iterator it = pTable->_childrenMap.begin();
|
||
while (it != pTable->_childrenMap.end())
|
||
{
|
||
json pack;
|
||
(*it)->toJson(pack, true);
|
||
parameter["response"].push_back(pack);
|
||
it++;
|
||
}
|
||
}
|
||
|
||
|
||
#else
|
||
DPData* pTable = pDataManager->findObjectById(DataManager::DataType::dataClass, id);
|
||
DBPlatformSpace::M_EntityTableDAO* pT = dynamic_cast<DBPlatformSpace::M_EntityTableDAO*>(pTable->_pDBDAO);
|
||
|
||
string currWorkDBFilePath = "";
|
||
currWorkDBFilePath = DBPlatformSpace::DBPlatformNew::getCurrentWorkDB();
|
||
DBPlatformSpace::M_EntityModelDAO* pMmodelOwner = NULL;
|
||
DBPlatformSpace::M_EntityModelDAO::getInstanceInfo(currWorkDBFilePath, pMmodelOwner);
|
||
//获取数据列信息
|
||
list<DBPlatformSpace::M_NormalColumnDAO*> colInDbList;
|
||
pT->getNormalColumnDAOList(pMmodelOwner, colInDbList);
|
||
|
||
for (auto el:colInDbList)
|
||
{
|
||
json j;
|
||
DataAttribute att;
|
||
att.loadData(el);
|
||
att.toJson(j);
|
||
parameter["response"].push_back(j);
|
||
}
|
||
|
||
|
||
#endif
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("getTableStru Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
bool SearchEditEventModule::getTableData(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["tableId"];
|
||
|
||
#ifdef QTDEBUG
|
||
json ret;
|
||
|
||
if (id == 16)
|
||
{
|
||
//少量数据
|
||
//for (int i = 0; i < 10; i++)
|
||
//{
|
||
// json col;
|
||
// for (int j = 0; j < head.size(); j++)
|
||
// {
|
||
// col[head[j].toStdString()] = qrand() % 10;
|
||
// }
|
||
// ret.push_back(col);
|
||
//}
|
||
ret = ret16;
|
||
}
|
||
else if (id == 17)
|
||
{
|
||
//大量数据
|
||
//for (int i = 0; i < 10000; i++)
|
||
//{
|
||
// json col;
|
||
// for (int j = 0; j < head.size(); j++)
|
||
// {
|
||
// col[head[j].toStdString()] = qrand() % 30;
|
||
// }
|
||
// ret.push_back(col);
|
||
//}
|
||
ret = ret17;
|
||
}
|
||
else if (id == 18)
|
||
{
|
||
ret = ret18;
|
||
}
|
||
else if (id == 30)
|
||
{
|
||
ret = ret30;
|
||
}
|
||
else if (id == 31)
|
||
{
|
||
ret = ret31;
|
||
}
|
||
parameter["response"] = ret;
|
||
#else
|
||
//查找该数据类
|
||
DPData* pTable = pDataManager->findObjectById(DataManager::DataType::dataClass,id);
|
||
|
||
if (pTable)
|
||
{
|
||
json j;
|
||
bool ret = qobject_cast<DataClass*>(pTable)->getAllDataToJson(j);
|
||
parameter["response"] = j;
|
||
if (ret == false)
|
||
{
|
||
parameter["error"] = "获取数据失败";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
parameter["error"] = "获取数据类数据失败";
|
||
}
|
||
|
||
#endif
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("getTableData Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
bool SearchEditEventModule::saveNewFile(json& parameter)
|
||
{
|
||
//打开文件夹选择
|
||
QString curPath = QCoreApplication::applicationDirPath();
|
||
QString selectDir = QFileDialog::getSaveFileName(nullptr, tr("文件另存为"), "", "", nullptr, QFileDialog::DontConfirmOverwrite);
|
||
|
||
auto data = parameter["data"];
|
||
int id = data["id"]; //当前连接 id
|
||
|
||
if (!selectDir.isEmpty())
|
||
{
|
||
QFileInfo selectFile(selectDir);
|
||
if (selectFile.isFile())
|
||
{
|
||
//文件存在
|
||
QString msg = "文件已存在,请重新命名!";
|
||
parameter["error"] = CommonHelper::qstringToUtf8(msg);
|
||
parameter["response"]["status"] = false;
|
||
return true;
|
||
}
|
||
|
||
parameter["response"] = CommonHelper::qstringToUtf8(selectDir);
|
||
#ifdef QTDEBUG
|
||
return true;
|
||
#endif // QTDEBUG
|
||
}
|
||
else
|
||
{
|
||
parameter["response"]["status"] = false;
|
||
return true;
|
||
}
|
||
//关闭连接
|
||
|
||
//复制
|
||
oldFile = currentFilePath;
|
||
bool ret = QFile::copy(currentFilePath, selectDir);
|
||
if (ret)
|
||
{
|
||
//切换到新文件
|
||
DBPlatformSpace::ResultMsg rm = DBPlatformSpace::DBPlatformNew::setCurrentWorkDB(CommonHelper::qstringToStdString(selectDir),0);
|
||
if (rm.rCode != 0)
|
||
{
|
||
//此时恢复
|
||
parameter["error"] = "无法另存为该数据文件";
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("复制文件成功,但设置新文件路径失败");
|
||
rm = DBPlatformSpace::DBPlatformNew::setCurrentWorkDB(CommonHelper::qstringToStdString(oldFile));
|
||
if (rm.rCode == 0)
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("恢复到原路径成功");
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("恢复到原路径失败");
|
||
}
|
||
bool ret = QFile::remove(selectDir);
|
||
if (ret == true)
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("删除新文件成功");
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("删除新文件失败");
|
||
}
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("copytoNewFile Sendto web.......");
|
||
LOG(INFO) << CommonHelper::stringToUtf8(rm.rMsg);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
currentFilePath = selectDir;
|
||
//更新连接信息
|
||
SysManager& sysMnr = GetSysManager();
|
||
Connection* pCon = sysMnr.findConnection(id);
|
||
//保存当前连接id
|
||
currentConnectID = id;
|
||
if (pCon)
|
||
{
|
||
pCon->setPath(currentFilePath);
|
||
}
|
||
json j;
|
||
pCon->toJson(j);
|
||
parameter["response"] = j;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
parameter["error"] = "另存数据文件失败";
|
||
}
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("copytoNewFile Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
//修改
|
||
//proA = 100时,返回修改失败
|
||
bool modify(json& j, int id, json& src)
|
||
{
|
||
for (auto& x : src)
|
||
{
|
||
if (x["id"] == id)
|
||
{
|
||
if (j["proA"] == "100") return false;
|
||
x["proA"] = j["proA"];
|
||
x["proB"] = j["proB"];
|
||
x["proC"] = j["proC"];
|
||
x["proD"] = j["proD"];
|
||
x["proE"] = j["proE"];
|
||
return true;
|
||
}
|
||
}
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("can not find modify data");
|
||
return false;
|
||
}
|
||
|
||
bool SearchEditEventModule::saveTableItem(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["tableId"];
|
||
auto saveas = data["saveas"];
|
||
|
||
|
||
#ifdef QTDEBUG
|
||
|
||
bool ret;
|
||
auto items = data["items"];
|
||
for (auto& x : items)
|
||
{
|
||
//每行数据
|
||
// map<string, string> mp = x;
|
||
json j = x;
|
||
if (id == 16)
|
||
{
|
||
if (j["id"] == "-1")
|
||
{
|
||
j["id"] = "200";
|
||
ret16.push_back(j);
|
||
ret = true;
|
||
}
|
||
else
|
||
{//修改
|
||
ret = modify(j, j["id"], ret16);
|
||
}
|
||
}
|
||
if (id == 17)
|
||
{
|
||
if (j["id"] == "-1")
|
||
{
|
||
j["id"] = "20000";
|
||
ret17.push_back(j);
|
||
ret = true;
|
||
}
|
||
else
|
||
{
|
||
//修改
|
||
ret = modify(j, j["id"], ret17);
|
||
}
|
||
}
|
||
|
||
}
|
||
parameter["response"]["status"] = ret;
|
||
#else
|
||
|
||
//list<string> valuelist;
|
||
|
||
//auto items = data["items"];
|
||
//for (auto& x : items)
|
||
//{
|
||
// QStringList qValue; //每行数据
|
||
// map<string, string> mp = x;
|
||
// for (auto& y : mp)
|
||
// {
|
||
// string v = y.second;
|
||
// qValue.push_back(QString::fromStdString(v));
|
||
// }
|
||
|
||
// string value = CommonHelper::qstringToStdString(qValue.join(","));
|
||
// valuelist.push_back(value);
|
||
//}
|
||
DPData* pTable = pDataManager->findObjectById(DataManager::DataType::dataClass,id);
|
||
if (pTable)
|
||
{
|
||
bool ret = qobject_cast<DataClass*>(pTable)->saveTableData(parameter, pCurrentModel);
|
||
parameter["response"]["status"] = ret;
|
||
if (ret == false && saveas == true)
|
||
{
|
||
//如果是另存为失败,删除新生成的另存文件
|
||
SysManager& sysMnr = GetSysManager();
|
||
Connection* pCon = sysMnr.findConnection(currentConnectID);
|
||
QString newfile = pCon->getPath();
|
||
//恢复连接信息
|
||
//切换到原文件
|
||
DBPlatformSpace::ResultMsg rm = DBPlatformSpace::DBPlatformNew::setCurrentWorkDB(CommonHelper::qstringToStdString(oldFile),0);
|
||
if (rm.rCode == 0)
|
||
{
|
||
//切换到原文件成功恢复连接信息
|
||
pCon->setPath(oldFile);
|
||
currentFilePath = oldFile;
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("另存失败,切换到原数据文件连接");
|
||
}
|
||
else
|
||
{
|
||
//切换到原文件失败
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("另存失败,切换原数据文件失败");
|
||
}
|
||
bool ret = QFile::remove(newfile);
|
||
if (ret == false)
|
||
{
|
||
//删除原文件失败
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("另存失败时,删除新文件失败");
|
||
}
|
||
else
|
||
{
|
||
//删除原文件失败
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("另存失败时,删除新文件成功");
|
||
}
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
parameter["error"] = "找不到数据类";
|
||
}
|
||
|
||
#endif
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("savetableItem Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
bool SearchEditEventModule::delTableItem(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["tableId"];
|
||
auto items = data["items"];
|
||
auto saveas = data["saveas"];
|
||
json ret;
|
||
#ifdef QTDEBUG
|
||
if (items.size() == 0)
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("clear the table");
|
||
}
|
||
else
|
||
{
|
||
if (id == 16)
|
||
{
|
||
for (auto& i : items)
|
||
{
|
||
for (json::iterator it = ret16.begin(); it != ret16.end(); ++it)
|
||
{
|
||
if ((*it)["id"] == i)
|
||
{
|
||
ret16.erase(it);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
parameter["response"]["status"] = true;
|
||
}
|
||
if (id == 17)
|
||
{
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
}
|
||
|
||
|
||
#else
|
||
|
||
DPData* pTable = pDataManager->findObjectById(DataManager::DataType::dataClass, id);
|
||
if (pTable)
|
||
{
|
||
bool ret = qobject_cast<DataClass*>(pTable)->delTableData(parameter);
|
||
parameter["response"]["status"] = ret;
|
||
if (ret == false && saveas == true)
|
||
{
|
||
//如果是删除(另存为)失败,删除新生成的另存文件
|
||
SysManager& sysMnr = GetSysManager();
|
||
Connection* pCon = sysMnr.findConnection(currentConnectID);
|
||
QString newfile = pCon->getPath();
|
||
bool ret = QFile::remove(newfile);
|
||
if (ret == false)
|
||
{
|
||
//删除原文件失败
|
||
LOG(INFO) << CommonHelper::utf8ToStdString( "(删除)另存失败时,删除新文件失败");
|
||
}
|
||
//恢复连接信息
|
||
//切换到原文件
|
||
DBPlatformSpace::ResultMsg rm = DBPlatformSpace::DBPlatformNew::setCurrentWorkDB(CommonHelper::qstringToStdString(oldFile),0);
|
||
if (rm.rCode == 0)
|
||
{
|
||
//恢复连接信息
|
||
pCon->setPath(oldFile);
|
||
currentFilePath = oldFile;
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("(删除)另存失败,恢复原数据文件连接");
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("(删除)另存失败,恢复原连接信息失败");
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
parameter["error"] = "找不到数据类";
|
||
}
|
||
|
||
#endif
|
||
return true;
|
||
|
||
}
|
||
|
||
bool SearchEditEventModule::filterData(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int id = data["tableId"]; //要做筛选的数据类id
|
||
QString filterStr = CommonHelper::utf8ToQString(data["filter"]);
|
||
|
||
DPData* pData = pDataManager->findObjectById(DataManager::DataType::dataClass, id);
|
||
DataClass* pTable = qobject_cast<DataClass*>(pData);
|
||
json result;
|
||
pTable->filterData(filterStr, result);
|
||
parameter["response"] = result;
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("filterData Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
return true;
|
||
}
|
||
|
||
void SearchEditEventModule::setNewFile(json& parameter)
|
||
{
|
||
//选择新文件全路径
|
||
QString file = QFileDialog::getSaveFileName(nullptr, tr("保存为文件"), "", "", nullptr, QFileDialog::DontConfirmOverwrite);
|
||
|
||
if (file.isEmpty())
|
||
{
|
||
// parameter["error"] = "未确定文件";
|
||
return ;
|
||
}
|
||
|
||
QFileInfo selectFile(file);
|
||
if (selectFile.isFile())
|
||
{
|
||
//文件存在
|
||
parameter["error"] = "文件已存在,请重新命名";
|
||
return ;
|
||
}
|
||
parameter["response"] = CommonHelper::qstringToUtf8(file);
|
||
}
|
||
|
||
void SearchEditEventModule::sqlcipherDo(json& parameter)
|
||
{
|
||
SqlcipherTool sqliteTool;
|
||
auto data = parameter["data"];
|
||
string srcFile = data["srcfile"];
|
||
string dstFile = data["dstfile"];
|
||
string key = data["key"];
|
||
auto type = data["type"];
|
||
if (type == 0)
|
||
{ //加密
|
||
string msg;
|
||
int ret = sqliteTool.encryption(srcFile, dstFile, key, msg);
|
||
if (ret != 0)
|
||
{
|
||
parameter["error"] = "加密失败!请检查文件是否有效";
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
else
|
||
{
|
||
parameter["response"]["status"] = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//解密
|
||
string msg;
|
||
int ret = sqliteTool.decrypt(srcFile, dstFile, key, msg);
|
||
if (ret != 0)
|
||
{
|
||
parameter["error"] = "解密失败!请检查文件或密码是否有效";
|
||
parameter["response"]["status"] = false;
|
||
}
|
||
else
|
||
{
|
||
parameter["response"]["status"] = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
//保存blob数据到本地文件
|
||
void SearchEditEventModule::saveBlobToFile(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int tableid = data["tableid"];
|
||
int id = (CommonHelper::utf8ToQString(data["id"])).toInt();
|
||
int colid = data["colid"]; //列的id
|
||
|
||
//读取该blob数据
|
||
using namespace DBPlatformSpace;
|
||
double* buf = NULL;
|
||
int size = 0;
|
||
M_NormalColumnDAO* curCol = NULL;
|
||
ResultMsg rm = M_NormalColumnDAO::FindByID(colid, curCol);
|
||
M_EntityTableDAO* curTable = NULL;
|
||
rm = M_EntityTableDAO::FindByID(tableid, curTable);
|
||
rm = curTable->getBlobData(curCol, id, buf, size);
|
||
|
||
QByteArray bytes = QByteArray::fromRawData((char*)buf, size);
|
||
//根据bytes内容判断所存文件数据为哪种类型
|
||
QString fileType = checkFileType(bytes);
|
||
|
||
|
||
//选择新文件全路径
|
||
QString file = QFileDialog::getSaveFileName(nullptr, tr("保存为文件"), "", fileType,NULL, QFileDialog::DontConfirmOverwrite);
|
||
|
||
if (!file.isEmpty())
|
||
{
|
||
QFile sFile(file);
|
||
|
||
//以重写、只写方式打开文件(运行到此时,文件已经存在目录中)
|
||
if (!sFile.open(QIODevice::Truncate | QIODevice::WriteOnly ))
|
||
printf("Open jsonfile failed!\n");
|
||
|
||
//写入文件内容
|
||
sFile.write((char*)buf, size);
|
||
//关闭文件
|
||
sFile.close();
|
||
|
||
parameter["response"] = { {"status",true} };
|
||
}
|
||
|
||
|
||
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("exportxmlFile Sendto web.......");
|
||
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
||
}
|
||
|
||
void SearchEditEventModule::copyBlobData(json& parameter)
|
||
{
|
||
auto data = parameter["data"];
|
||
int tableid = data["tableid"];
|
||
int srcid = data["srcID"];
|
||
vector<int> cols = data["srcCol"];
|
||
parameter["response"]["retFilePath"] = json::array();
|
||
|
||
using namespace DBPlatformSpace;
|
||
M_NormalColumnDAO* curCol = NULL; //当前列
|
||
M_EntityTableDAO* curTable = NULL;
|
||
ResultMsg rm = M_EntityTableDAO::FindByID(tableid, curTable);
|
||
|
||
for (int curColid : cols)
|
||
{
|
||
json j;
|
||
//读取该行该列的blob数据
|
||
rm = M_NormalColumnDAO::FindByID(curColid, curCol);
|
||
double* buf = NULL;
|
||
int size = 0;
|
||
QByteArray bytes;
|
||
if (srcid == -1)
|
||
{
|
||
}
|
||
else
|
||
{
|
||
rm = curTable->getBlobData(curCol, srcid, buf, size);
|
||
bytes = QByteArray::fromRawData((char*)buf, size);
|
||
}
|
||
|
||
|
||
QString tempFilePath = QCoreApplication::applicationDirPath() + "/temp/";
|
||
bool ret = CommonHelper::checkDir(tempFilePath);
|
||
if (ret)
|
||
{
|
||
QString filePath = tempFilePath + buildTmpFileName(checkFileType(bytes), srcid, curColid);
|
||
QFile sFile(filePath);
|
||
//以重写、只写方式打开文件(运行到此时,文件已经存在目录中)
|
||
if (!sFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
|
||
printf("Open jsonfile failed!\n");
|
||
|
||
//写入文件内容
|
||
sFile.write((char*)buf, size);
|
||
//关闭文件
|
||
sFile.close();
|
||
|
||
string name = curCol->NameToDAOAttriName(curCol->_name);
|
||
j["col"] = CommonHelper::stringToUtf8(name);
|
||
j["filePath"] = CommonHelper::qstringToUtf8(filePath);
|
||
parameter["response"]["retFilePath"].push_back(j);
|
||
}
|
||
|
||
}
|
||
} |