1659 lines
42 KiB
C++
1659 lines
42 KiB
C++
#pragma execution_character_set("utf-8")
|
||
|
||
#include <QStack>
|
||
#include "TransformExcel.h"
|
||
#include "DataModelData.h"
|
||
#include "DataPack.h"
|
||
#include "DataClass.h"
|
||
#include "DataAttribute.h"
|
||
#include "DataManager.h"
|
||
#include "M_NormalColumnDAO.h"
|
||
#include "global.h"
|
||
#include "common.h"
|
||
#include <Qdir>
|
||
#include <QFile>
|
||
#include <QApplication>
|
||
|
||
|
||
static const int PROCESSED = 1;
|
||
|
||
TransformExcel::TransformExcel(DataManager* pManager, int datamodeld) :_pDataManager_ref(pManager), _dataModelId(datamodeld)
|
||
{
|
||
attributeHead << "上层数据类名称标识" << "名称标识" << "显示名称" << "描述"
|
||
<< "默认值" << "数据类型" << "是否索引" << "是否唯一" << "是否自增" << "最大长度" << "最小长度" << "数值位数"
|
||
<< "数值精度" << "是否为查找序列" << "是否为主键" << "属性类型" << "关联数据类名称";
|
||
|
||
dataClassHead << "上层模块名称标识" << "名称标识" << "显示名称" << "数据类类型" << "描述" << "数量类型";
|
||
packHead << "上层数据名称标识" << "名称标识" << "显示名称" << "模块类型" << "描述" << "是否生成数据表";
|
||
}
|
||
|
||
void TransformExcel::freeNode(DPData* pData)
|
||
{
|
||
if (pData == nullptr)
|
||
{
|
||
return;
|
||
}
|
||
|
||
qDeleteAll(pData->_childrenMap);
|
||
pData->_childrenMap.clear();
|
||
}
|
||
|
||
TransformExcel::~TransformExcel()
|
||
{
|
||
if (newTreeRoot._childrenMap.size() == 0)
|
||
{
|
||
return;
|
||
}
|
||
//把树剩余结点清空
|
||
DPData* pData = &newTreeRoot;
|
||
freeNode(pData);
|
||
}
|
||
|
||
bool TransformExcel::importData(QString& errMsg)
|
||
{
|
||
bool hasEmptyCell;
|
||
//生成树结构---------
|
||
QList<QStringList> result;
|
||
//处理数据模块表读取的数据
|
||
if (!excel.readSheetData("数据模块", result, hasEmptyCell))
|
||
{
|
||
errMsg = "导入文件数据模块表: 没有模块sheet页或读取数据模块表出错";
|
||
return false;
|
||
}
|
||
if (!addDataPackToTree(result))
|
||
{
|
||
//数据有问题
|
||
errMsg = "导入文件数据模块表有误";
|
||
return false;
|
||
}
|
||
//读取处理数据类表
|
||
result.clear();
|
||
if (!excel.readSheetData("数据类", result, hasEmptyCell))
|
||
{
|
||
errMsg = "导入文件数据类表:没有数据类sheet页或读取数据模块表出错";
|
||
return false;
|
||
}
|
||
if (!addDataClassToTree(result))
|
||
{
|
||
errMsg = "导入文件数据类表有错";
|
||
return false;
|
||
}
|
||
//读取处理数据属性表
|
||
result.clear();
|
||
if (!excel.readSheetData("属性", result, hasEmptyCell))
|
||
{
|
||
errMsg = "导入文件属性表:没有属性sheet页或读取属性表出错";
|
||
return false;
|
||
}
|
||
if (!addDataAttributetoTree(result))
|
||
{
|
||
errMsg = "导入文件属性表有错";
|
||
return false;
|
||
}
|
||
|
||
//开始导入前备份数据库
|
||
backupDBFile();
|
||
//开始导入-------------
|
||
//1. 从底层开始处理
|
||
levelNodeOrder.push_back(&newTreeRoot);
|
||
if (!processDataImport(&newTreeRoot))
|
||
{
|
||
if (rollBackDBFile())
|
||
{
|
||
errMsg = "模型导入失败";
|
||
}
|
||
else
|
||
{
|
||
QString tempFile = QCoreApplication::applicationDirPath() + "/rollback.txt";
|
||
QFile file(tempFile);
|
||
if (file.exists())
|
||
{
|
||
QFile::remove(tempFile);
|
||
}
|
||
//生成临时文件
|
||
//文件不存在,生成一个空文件
|
||
file.open(QIODevice::ReadWrite | QIODevice::Text);
|
||
QTextStream stream(&file);
|
||
stream << _srcFile;
|
||
file.close();
|
||
//数据回滚失败,重启程序
|
||
LOG(ERROR) << CommonHelper::utf8ToStdString("数据回滚失败,重启程序");
|
||
errMsg = "数据回滚失败,重启程序";
|
||
CommonHelper::message("数据回滚失败,重启程序", 2);
|
||
}
|
||
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
QFileInfo fileInfo(_bakFile);
|
||
if (fileInfo.isFile())
|
||
{
|
||
/* bool ret = */QFile::remove(_bakFile);
|
||
}
|
||
return true;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool TransformExcel::processDataImport(DPData* /*pData*/)
|
||
{
|
||
bool ret = false;
|
||
int index_ = 0;
|
||
while (index_ < levelNodeOrder.size())
|
||
{
|
||
DPData* pdata = levelNodeOrder.at(index_);
|
||
for (auto i : pdata->_childrenMap)
|
||
{
|
||
if ((int)i->_baseType == g_TYPE_DATAATTRUBUTE)
|
||
{
|
||
continue;
|
||
}
|
||
levelNodeOrder.push_back(i);
|
||
}
|
||
index_++;
|
||
}
|
||
//除第一个为根结点(模型),其它结点都要处理
|
||
for (int i = levelNodeOrder.size() - 1; i > 0; i--)
|
||
{
|
||
DPData* pdata = levelNodeOrder.at(i);
|
||
if (dataMark[pdata->_name] == PROCESSED)
|
||
{
|
||
continue;
|
||
}
|
||
ret = processCurrentNode(pdata);
|
||
if (!ret)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//处理关系属性
|
||
for (int i = 0; i < relAttList.size(); i++)
|
||
{
|
||
DataAttribute* pAtt = relAttList.at(i);
|
||
DPData* pClass = _pDataManager_ref->findobjectByNameInDatamodel(g_TYPE_DATACLASS, "name", pAtt->_fkname, _dataModelId);
|
||
if (pClass)
|
||
{
|
||
//查找目标模型中的真实属性
|
||
DPData* pRealAtt = _pDataManager_ref->findObjectById(DataManager::DataType::dataAttribute, pAtt->_id);
|
||
if (pRealAtt)
|
||
{
|
||
qobject_cast<DataAttribute*>(pRealAtt)->_wFkTableID = pClass->_id;
|
||
if (!pRealAtt->saveSelf())
|
||
{
|
||
QString msg = "关系属性:" + pRealAtt->_name + "--" + "class ID:" + QString::number(pRealAtt->_parent->_id);
|
||
LOG(INFO) << CommonHelper::qstringToString(msg);
|
||
return false;
|
||
}
|
||
|
||
QString msg = "关系属性:" + pRealAtt->_name + "--" + "fkTableID:" + QString::number(qobject_cast<DataAttribute*>(pRealAtt)->_wFkTableID);
|
||
LOG(INFO) << CommonHelper::qstringToString(msg);
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::qstringToString("找关系属性失败");
|
||
QString msg = "在目标模型中查找不到关系属性 " + pAtt->_name;
|
||
CommonHelper::message(msg,2);
|
||
return false;
|
||
}
|
||
}
|
||
else {
|
||
QString msg = "查找不到关联数据类 " + pAtt->_fkname;
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//处理结点
|
||
bool TransformExcel::processCurrentNode(DPData* pData)
|
||
{
|
||
static DPData* pdst = nullptr;
|
||
int ret = findAndProcessSameNode(pData);
|
||
if (ret == 1)
|
||
{
|
||
return false;//导入失败
|
||
}
|
||
if (ret == 0)
|
||
{
|
||
//如已按同名处理
|
||
dataMark.insert(pData->_name, PROCESSED);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
if ((int)pData->_baseType == g_TYPE_DATAMODEL)
|
||
{
|
||
//当前为根模块
|
||
return processImportStack(pdst);
|
||
}
|
||
if (processNoSameNode(pData))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//查找同名并处理同名数据
|
||
//返回值:
|
||
// 0: 同名处理成功,数据类属性都新加成功
|
||
// 1:新加属性失败
|
||
// 2: 没找到同名
|
||
int TransformExcel::findAndProcessSameNode(DPData* pData)
|
||
{
|
||
|
||
DPData* pfindData = nullptr;
|
||
pfindData = _pDataManager_ref->findobjectByNameInDatamodel(pData->_baseType, "name", pData->_name, _dataModelId);
|
||
if (pfindData)
|
||
{
|
||
if ((int)pData->_baseType == g_TYPE_DATACLASS)
|
||
{
|
||
DataClass* pNew = qobject_cast<DataClass*>(pData);
|
||
DataClass* pOld = qobject_cast<DataClass*>(pfindData);
|
||
importExcelData(pNew, pOld);
|
||
_importClassCount++;
|
||
//数据类信息替换完毕,增加数据属性
|
||
if (proccessAttribute(pNew, pOld))
|
||
{
|
||
//将数据类结点移除
|
||
pNew->_parent->deleteChild(pNew->_id);
|
||
delete pNew;
|
||
pNew = nullptr;
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
return 1;
|
||
}
|
||
}
|
||
else if ((int)pData->_baseType == g_TYPE_DATAPACK)
|
||
{
|
||
//模块同名,信息覆盖,并挂上child
|
||
DataPack* pNew = qobject_cast<DataPack*>(pData);
|
||
DataPack* pOld = qobject_cast<DataPack*>(pfindData);
|
||
importExcelData(pNew, pOld);
|
||
if (!processImportStack(pOld))
|
||
{
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
return 0;
|
||
}
|
||
return 2;
|
||
}
|
||
|
||
//处理非同名情况
|
||
bool TransformExcel::processNoSameNode(DPData* pData)
|
||
{
|
||
importStack.push(pData);
|
||
DPData* pCur = pData;
|
||
|
||
while (pCur->_parent)
|
||
{
|
||
pCur = pCur->_parent;
|
||
return processCurrentNode(pCur);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
// 非同名数据类
|
||
// bool TransformExcel::ProcessNoSameDataClass(DataClass*& pNew, QMap<int,DPData*>& sameNamePack)
|
||
// {
|
||
// QStack<DPData*> importStack; //需要导入到数据库的数据
|
||
// importStack.push(pNew);
|
||
// DPData* pOldDst = nullptr; //目标模块
|
||
// DPData* pCur = pNew->_parent;
|
||
// while (pCur->_parent)
|
||
// {
|
||
// //先查找同名模块
|
||
// pOldDst = _pDataManager_ref->findobjectByNameInDatamodel(pCur->_baseType, "name", pCur->_name, _dataModelId);
|
||
// if (pOldDst)
|
||
// {
|
||
// //找到同名模块,合并信息,跳出循环,添加
|
||
// DataPack* pNew = qobject_cast<DataPack*>(pCur);
|
||
// DataPack* pOld = qobject_cast<DataPack*>(pOldDst);
|
||
// importExcelData(pNew, pOld);
|
||
// // sameNamePack.insert(pCur->_id, pCur);
|
||
// break;
|
||
// }
|
||
// else
|
||
// {
|
||
// //没有同名模块,当前数据模块入栈,再往上层找
|
||
// importStack.push(pCur);
|
||
// pCur = pCur->_parent;
|
||
// }
|
||
// }
|
||
//
|
||
// return processImportStack(/*importStack, */pOldDst);
|
||
// }
|
||
|
||
bool TransformExcel::processImportStack(/*QStack<DPData*>& importStack,*/ DPData* pOldDst)
|
||
{
|
||
while (!importStack.isEmpty())
|
||
{
|
||
DPData* pData = importStack.pop();
|
||
if (dataMark.value(pData->_name) == PROCESSED)
|
||
{
|
||
return true;
|
||
}
|
||
if ((int)pData->_baseType == g_TYPE_DATACLASS)
|
||
{
|
||
DataClass* pClass = qobject_cast<DataClass*>(pData);
|
||
//如果是数据类,直接往目标加
|
||
if (pOldDst)
|
||
{
|
||
int oldMapId = pClass->_id;
|
||
//是否有相同名称标识的模块,
|
||
int id = 0;
|
||
if (_pDataManager_ref->findobjectByNameInDatamodel(g_TYPE_DATAPACK, "name", pClass->_name, _dataModelId))
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("导入新类与原有模块名称标识相同");
|
||
return false;
|
||
}
|
||
//看是否有相同显示名称的类,若相同,加序号
|
||
id = 0;
|
||
QString oldname = pClass->_strDisplayName;
|
||
while (_pDataManager_ref->findobjectByNameInDatamodel(pClass->_baseType, "displayName", pClass->_strDisplayName, _dataModelId))
|
||
{
|
||
id++;
|
||
pClass->_strDisplayName = oldname + QString::number(id);
|
||
}
|
||
oldname = pClass->_strDisplayName;
|
||
//是否有相同显示名称模块,若相同加序号
|
||
id = 0;
|
||
if (_pDataManager_ref->findobjectByNameInDatamodel(g_TYPE_DATAPACK, "displayName", pClass->_strDisplayName, _dataModelId))
|
||
{
|
||
id++;
|
||
pClass->_strDisplayName = oldname + QString::number(id);
|
||
}
|
||
pClass->_id = -1;
|
||
// if (pClass->_type != g_RELATED_CLASS)
|
||
// {
|
||
// pClass->reName();
|
||
// }
|
||
DPData* pOldParent = pClass->_parent;
|
||
if (pOldDst->addChild(pClass))
|
||
{
|
||
pOldParent->_childrenMap.remove(oldMapId);//从原父结点删除
|
||
//导入成功
|
||
_pDataManager_ref->insertDataMap(DataManager::DataType::dataClass, pClass->_id, pClass);
|
||
_importClassCount++;
|
||
dataMark.insert(pClass->_name, PROCESSED);
|
||
QMap<unsigned int, DPData*> tmpMap = pClass->_childrenMap;
|
||
pClass->_childrenMap.clear();
|
||
QMap<unsigned int, DPData*>::iterator it = tmpMap.begin();
|
||
while (it != tmpMap.end())
|
||
{
|
||
|
||
//查找相同名称标识的属性
|
||
DPData* pData_ = pClass->hasSameNameChild("name", (*it)->_name);
|
||
if (pData_)
|
||
{//相同名称标识属性
|
||
DataAttribute* pNew = qobject_cast<DataAttribute*>(*it);
|
||
importExcelData(pNew, qobject_cast<DataAttribute*>(pData_));
|
||
pNew->_id = pData_->_id; //更新id,关系属性查找关系类时使用
|
||
}
|
||
else
|
||
{
|
||
(*it)->_id = -1;
|
||
DataAttribute* pNew = qobject_cast<DataAttribute*>(*it);
|
||
pNew->checkImportAttributeName(pClass);
|
||
if (pClass->addChild(*it))//添加属性
|
||
{
|
||
_pDataManager_ref->insertDataMap(DataManager::DataType::dataAttribute, (*it)->_id, (*it));
|
||
_importAttributeCount++;
|
||
}
|
||
else
|
||
{
|
||
//导入失败,删除已导入的数据
|
||
return false;
|
||
}
|
||
}
|
||
it++;
|
||
// /*新加入属性*/
|
||
// //看属性是否有显示名称相同的,有相同则加序号
|
||
// int id = 0;
|
||
// QString newName = pAtt->_strDisplayName;
|
||
// while (pClass->hasSameNameChild("displayName", newName, true))
|
||
// {
|
||
// LOG(INFO) << CommonHelper::utf8ToStdString("导入新属性显示名称与原有属性相同,加序号");
|
||
// id += 1;
|
||
// newName = pAtt->_strDisplayName + QString::number(id);
|
||
// }
|
||
// pAtt->_strDisplayName = newName;
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//保存数据类信息失败,需将已导入信息删除
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//为数据模块
|
||
DataPack* pPack = qobject_cast<DataPack*>(pData);
|
||
//若有相同显示名称模块
|
||
int id = 0;
|
||
while (_pDataManager_ref->findobjectByNameInDatamodel(pPack->_baseType, "displayName", pPack->_strDisplayName, _dataModelId) != nullptr)
|
||
{
|
||
id += 1;
|
||
pPack->_strDisplayName = pPack->_strDisplayName + QString::number(id);
|
||
}
|
||
if (pPack->_bIsChildPack)
|
||
{
|
||
//如果子模块
|
||
int oldId = pPack->_id;
|
||
pPack->_parent->_childrenMap.remove(oldId);
|
||
pPack->_id = -1;
|
||
//子模块
|
||
if (pOldDst->addChild(pPack))
|
||
{
|
||
pPack->_parent = pOldDst;
|
||
_pDataManager_ref->insertDataMap(DataManager::DataType::dataPack, pPack->_id, pPack);
|
||
pOldDst = pPack;
|
||
_importPackCount++;
|
||
dataMark.insert(pPack->_name, PROCESSED);
|
||
}
|
||
else
|
||
{
|
||
//回滚到最初
|
||
return false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//根模块,直接挂到目的模型上
|
||
DPData* pdatamodel = _pDataManager_ref->findObjectById(DataManager::DataType::dataModel, _dataModelId);
|
||
pPack->_parent->_childrenMap.remove(pPack->_id);
|
||
pPack->_parent = pdatamodel;
|
||
pPack->_id = -1;
|
||
if (pdatamodel->addChild(pPack))
|
||
{
|
||
_pDataManager_ref->insertDataMap(DataManager::DataType::dataPack, pPack->_id, pPack);
|
||
pOldDst = pPack;
|
||
_importPackCount++;
|
||
dataMark.insert(pPack->_name, PROCESSED);
|
||
}
|
||
else
|
||
{
|
||
//向数据库添加失败,将添加成功的数据都删除
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/*处理数据模块表中的数据,生成树结构*/
|
||
bool TransformExcel::addDataPackToTree(QList<QStringList>& result)
|
||
{
|
||
if (CommonHelper::compareQStringlist(result[0], packHead))
|
||
{
|
||
//表头与预设一样
|
||
for (int i = 1; i < result.size(); i++)
|
||
{
|
||
//新生成数据模块
|
||
DataPack* pNewPack = new DataPack();
|
||
if (!loadDataPack(result[i], pNewPack))
|
||
{
|
||
delete pNewPack;
|
||
pNewPack = nullptr;
|
||
return false;
|
||
}
|
||
if (result[i][0] == "数据模型")
|
||
{
|
||
//直接挂数据模型下
|
||
pNewPack->_bIsChildPack = false;
|
||
pNewPack->_parent = &newTreeRoot;
|
||
pNewPack->_id = index; //临时id
|
||
newTreeRoot._childrenMap.insert(index, pNewPack);
|
||
}
|
||
else
|
||
{
|
||
QString name = result[i][0];
|
||
//查找上层模块并下挂
|
||
if (mapDataPack.contains(name))
|
||
{
|
||
pNewPack->_bIsChildPack = true;
|
||
pNewPack->_parent = mapDataPack[name];
|
||
pNewPack->_id = index;//临时id
|
||
mapDataPack[name]->_childrenMap.insert(index, pNewPack);
|
||
|
||
}
|
||
else
|
||
{
|
||
//找不到上层模块,报错或记录
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("导入表数据有误,找不到上层模块");
|
||
CommonHelper::message("导入表数据有误,找不到上层模块", 2);
|
||
delete pNewPack;
|
||
pNewPack = nullptr;
|
||
return false;
|
||
}
|
||
}
|
||
dataMark.insert(pNewPack->_name, 0);
|
||
mapDataPack.insert(pNewPack->_name, pNewPack);
|
||
index++;
|
||
}
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
//读入excel表的表头与预设不一样,提示问题
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("导入表数据有误,表头数据有错");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/*处理数据类的数据,生成树结构*/
|
||
bool TransformExcel::addDataClassToTree(QList<QStringList>& result)
|
||
{
|
||
if (CommonHelper::compareQStringlist(result[0], dataClassHead))
|
||
{
|
||
//与预设一样
|
||
for (int i = 1; i < result.size(); i++)
|
||
{
|
||
//新生成数据类
|
||
DataClass* pNewClass = new DataClass();
|
||
if (!loadDataClass(result[i], pNewClass))
|
||
{
|
||
delete pNewClass;
|
||
pNewClass = nullptr;
|
||
return false;
|
||
}
|
||
QString parentName = result[i][0];
|
||
//查找上层模块并下挂
|
||
if (mapDataPack.contains(parentName))
|
||
{
|
||
pNewClass->_parent = mapDataPack[parentName];
|
||
pNewClass->_id = index; //临时 id
|
||
mapDataPack[parentName]->_childrenMap.insert(index, pNewClass);
|
||
mapDataClass.insert(pNewClass->_name, pNewClass);
|
||
dataMark.insert(pNewClass->_name, 0);
|
||
index++;
|
||
}
|
||
else
|
||
{
|
||
QString msg = "数据类 " + pNewClass->_name + "上层模块 "+ parentName +"找不到";
|
||
//找不到上层模块,报错或记录
|
||
LOG(INFO) << CommonHelper::qstringToStdString(msg);
|
||
CommonHelper::message(msg, 2);
|
||
delete pNewClass;
|
||
pNewClass = nullptr;
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
//与预设不一样,提示问题
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("数据类表表头错误");
|
||
CommonHelper::message("导入表:数据类表头有错", 2);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/*处理属性表中数据,建立树结构*/
|
||
bool TransformExcel::addDataAttributetoTree(QList<QStringList>& result)
|
||
{
|
||
if (CommonHelper::compareQStringlist(result[0], attributeHead))
|
||
{
|
||
//与预设一样
|
||
for (int i = 1; i < result.size(); i++)
|
||
{
|
||
//新生成属性
|
||
DataAttribute* pNewAttribute = new DataAttribute();
|
||
if (!loadDataAttribute(result[i], pNewAttribute))
|
||
{
|
||
delete pNewAttribute;
|
||
pNewAttribute = nullptr;
|
||
return false;
|
||
}
|
||
QString parentName = result[i][0];
|
||
//查找上层模块并下挂
|
||
if (mapDataClass.contains(parentName))
|
||
{
|
||
pNewAttribute->_parent = mapDataClass[parentName];
|
||
pNewAttribute->_id = index; //临时id
|
||
mapDataClass[parentName]->_childrenMap.insert(index, pNewAttribute);
|
||
// dataMark.insert(pNewAttribute->_name, 0);
|
||
}
|
||
else
|
||
{
|
||
//找不到上层模块,报错或记录
|
||
delete pNewAttribute;
|
||
pNewAttribute = nullptr;
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("属性找不到上层数据类");
|
||
QString msg = "导入表:属性找不到上层数据类" + parentName;
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
// loadDataAttribute(result[i], pNewAttribute);
|
||
index++;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//与预设不一样,提示问题
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("属性表表头错误");
|
||
CommonHelper::message("导入表:属性表表头错误", 2);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//excel表里每行数据生成一个模块对象
|
||
bool TransformExcel::loadDataPack(const QStringList& data, DataPack* pData)
|
||
{
|
||
//上层数据名称标识
|
||
//名称标识
|
||
pData->_name = data[1];
|
||
if (pData->_name == " ")
|
||
{
|
||
CommonHelper::message("数据模块名称标识为空", 2);
|
||
return false;
|
||
}
|
||
QString msg;
|
||
if (pData->checkNameillegal(msg))
|
||
{
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
//显示名称
|
||
pData->setDisplayName(data[2]);
|
||
//模块类型
|
||
pData->setType((data[3]).toInt());
|
||
if (data[3] == "父数据类")
|
||
{
|
||
pData->setType(1);
|
||
}
|
||
else if (data[3] == "分类容器")
|
||
{
|
||
pData->setType(0);
|
||
}
|
||
else if (data[3] == "默认模块")
|
||
{
|
||
pData->setType(2);
|
||
// LOG(INFO) << CommonHelper::utf8ToStdString("默认模块");
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("importDatamodel: 模块类型 值错误");
|
||
CommonHelper::message("导入:模块类型值错误",2);
|
||
return false;
|
||
}
|
||
//描述
|
||
pData->setDes(data[4]);
|
||
|
||
//是否生成数据表,将“是、否”转成1.0
|
||
if (data[5] == "是")
|
||
{
|
||
pData->setIsGenerateTable(1);
|
||
}
|
||
else if (data[5] == "否")
|
||
{
|
||
pData->setIsGenerateTable(0);
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("importDatamodel: 是否生成数据表 值错误");
|
||
CommonHelper::message("导入:模块 是否生成数据表 值错误 ", 2);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//每行数据生成一个数据类对象
|
||
bool TransformExcel::loadDataClass(const QStringList& data, DataClass* pData)
|
||
{
|
||
//上层模块名称标识
|
||
//名称标识
|
||
pData->_name = data[1];
|
||
if (pData->_name == " ")
|
||
{
|
||
CommonHelper::message("数据类名称标识为空", 2);
|
||
return false;
|
||
}
|
||
QString msg;
|
||
if ( !pData->checkNameillegal(msg) || !pData->checkLetter(msg, 0) )
|
||
{
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
//显示名称
|
||
pData->_strDisplayName = data[2];
|
||
//数据类类型
|
||
if (data[3] == "普通数据类")
|
||
{
|
||
pData->_type = 1;
|
||
//普通数据类,先rename
|
||
pData->reName();
|
||
if (!pData->checkNameSize(3,128,msg) || !pData->checkLetter(msg,2))
|
||
{
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
}
|
||
else if (data[3] == "关系数据类")
|
||
{
|
||
pData->_type = 2;
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("importDatamodel: 数据类类型 值错误");
|
||
CommonHelper::message("导入:数据类类型错误 ", 2);
|
||
return false;
|
||
}
|
||
//描述
|
||
pData->_strDescription = data[4];
|
||
//数量类型
|
||
if (data[5] == "n")
|
||
{
|
||
pData->_wCountType = 0;
|
||
}
|
||
else if (data[5] == "1")
|
||
{
|
||
pData->_wCountType = 1;
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("importDatamodel: 数据类表数量类型 值错误");
|
||
CommonHelper::message("导入:数据类表数量类型值错误 ", 2);
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//每行excel数据生成一个属性对象
|
||
bool TransformExcel::loadDataAttribute(const QStringList& data, DataAttribute* pData)
|
||
{
|
||
QString errorhead = " 名称标识(" + data[1] + ") 所在行" ;
|
||
QString error;
|
||
//上层数据类名称标识
|
||
//名称标识
|
||
pData->_name = data[1];
|
||
QString msg;
|
||
if (pData->_name == " ")
|
||
{
|
||
CommonHelper::message("属性名称标识为空", 2);
|
||
return false;
|
||
}
|
||
|
||
if ( !pData->checkNameillegal(msg) || !pData->checkLetter(msg, 0))
|
||
{
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
pData->reName();
|
||
if (!pData->checkNameSize(2, 128, msg) || !pData->checkLetter(msg, 1))
|
||
{
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//显示名称
|
||
pData->_strDisplayName = data[2];
|
||
//描述
|
||
pData->_strDescription = data[3];
|
||
//默认值
|
||
pData->_strDefaultValue = data[4];
|
||
//数据类型:整型1 实型2 字符串型3 char* double *
|
||
QString type = data[5].trimmed();
|
||
if (type == "整型")
|
||
{
|
||
pData->_wDataType = MNORMALCOL_TYPE_INT;
|
||
}
|
||
else if (type == "实型")
|
||
{
|
||
pData->_wDataType = MNORMALCOL_TYPE_DOUBLE;
|
||
}
|
||
else if (type == "字符串")
|
||
{
|
||
pData->_wDataType = MNORMALCOL_TYPE_STRING;
|
||
}
|
||
else if (type == "char*")
|
||
{
|
||
pData->_wDataType = MNORMALCOL_TYPE_CHARPTR;
|
||
}
|
||
else if (type == "double*")
|
||
{
|
||
pData->_wDataType = MNORMALCOL_TYPE_DOUBLEPTR;
|
||
}
|
||
else
|
||
{
|
||
error = errorhead + "数据类型 值错误";
|
||
LOG(INFO) << CommonHelper::qstringToStdString("importDatamodel:"+error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
//是否索引
|
||
if (!CommonHelper::convertExcelboolValue(data[6].trimmed(), pData->_wIsIndex))
|
||
{
|
||
error = errorhead + " 是否索引 值错误";
|
||
LOG(INFO) << CommonHelper::qstringToStdString("importDatamodel:"+ error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
//是否唯一
|
||
if (!CommonHelper::convertExcelboolValue(data[7].trimmed(), pData->_wIsOnly))
|
||
{
|
||
error = errorhead + " 是否唯一 值错误";
|
||
LOG(INFO) << CommonHelper::qstringToStdString("importDatamodel:" + error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
//是否自增
|
||
if (!CommonHelper::convertExcelboolValue(data[8].trimmed(), pData->_wIsAutoincrement))
|
||
{
|
||
error = errorhead + " 是否自增 值错误";
|
||
LOG(INFO) << CommonHelper::qstringToStdString("importDatamodel: " + error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
//最大长度
|
||
CommonHelper::convertExcelInt(data[9].trimmed(), pData->_wMaxLength);
|
||
//最小长度
|
||
CommonHelper::convertExcelInt(data[10].trimmed(), pData->_wMinLength);
|
||
//数值位数
|
||
CommonHelper::convertExcelInt(data[11].trimmed(), pData->_wNumLength);
|
||
//数值精度
|
||
CommonHelper::convertExcelInt(data[12].trimmed(), pData->_wNumPresision);
|
||
//是否为查找序列
|
||
CommonHelper::convertExcelboolValue(data[13].trimmed(), pData->_wIsFindKey);
|
||
//是否为主键
|
||
if (!CommonHelper::convertExcelboolValue(data[14].trimmed(), pData->_isPrimaryKey))
|
||
{
|
||
error = errorhead + " 是否主键 值错误";
|
||
LOG(INFO) << CommonHelper::qstringToStdString("importDatamodel: "+ error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
|
||
//属性关系类型 :0 :普通属性; 1为1:1;2为1:n
|
||
if (data[15] == "普通属性")
|
||
{
|
||
//检查普通属性的名称
|
||
if (pData->illegalName(msg))
|
||
{
|
||
CommonHelper::message(msg, 2);
|
||
return false;
|
||
}
|
||
|
||
pData->_FKType = 0;
|
||
if (data[16] != " ")
|
||
{
|
||
error = errorhead + " 不应有关联数据类名";
|
||
//如果为普通属性,不应该有关联数据类
|
||
LOG(INFO) << CommonHelper:: qstringToStdString("importDatamodel: " + error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
//无需再读取 关联数据类名称
|
||
return true;
|
||
}
|
||
else if (data[15] == "1:1")
|
||
{
|
||
pData->_FKType = 1;
|
||
if (data[16] == " ")
|
||
{
|
||
error = errorhead + " 缺少关联数据类名";
|
||
//有关联数据类
|
||
LOG(INFO) << CommonHelper::qstringToStdString("importDatamodel: " + error);
|
||
CommonHelper::message(error, 2);
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
checkImportRelateAttribute(pData, data[16]);
|
||
}
|
||
|
||
}
|
||
else if (data[15] == "1:n")
|
||
{
|
||
pData->_FKType = 2;
|
||
if (data[16] == " ")
|
||
{
|
||
//有关联数据类
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("importDatamodel: 属性表 关联数据类名称 值错误");
|
||
CommonHelper::message("导入:属性表 关联数据类名称 值错误 ", 2);
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
checkImportRelateAttribute(pData, data[16]);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("importDatamodel: 属性表 属性类型 值错误");
|
||
CommonHelper::message("导入:属性表 属性类型 值错误 ", 2);
|
||
return false;
|
||
}
|
||
//关联数据类名称 转数据类id
|
||
//先在目标模型中查找关联数据类名称+数据模型id,得到数据类id
|
||
// QString name = data[16];
|
||
// DPData* pClass = _pDataManager_ref->findobjectByNameInDatamodel(g_TYPE_DATACLASS,"name", name, _dataModelId);
|
||
// if (pClass)
|
||
// {
|
||
// pData->_wFkTableID = pClass->_id;
|
||
// }
|
||
// else
|
||
// {
|
||
// pData->_fkname = name;
|
||
// }
|
||
return true;
|
||
}
|
||
|
||
void TransformExcel::checkImportRelateAttribute(DataAttribute* pData, const QString name)
|
||
{
|
||
//关联数据类名称 转数据类id
|
||
//先在目标模型中查找关联数据类名称+数据模型id,得到数据类id
|
||
DPData* pClass = _pDataManager_ref->findobjectByNameInDatamodel(g_TYPE_DATACLASS, "name", name, _dataModelId);
|
||
if (pClass)
|
||
{
|
||
pData->_wFkTableID = pClass->_id;
|
||
}
|
||
else
|
||
{
|
||
pData->_fkname = name;
|
||
relAttList.append(pData);
|
||
}
|
||
}
|
||
|
||
|
||
void TransformExcel::importExcelData(DataPack* pNew, DataPack* pOld)
|
||
{
|
||
//显示名称
|
||
if (pNew->_strDisplayName != "")
|
||
{
|
||
pOld->setDisplayName(pNew->getDisplayName());
|
||
//检查是否有重复的显示名,有就加序号
|
||
checkCoverName(pOld, pOld->_strDisplayName);
|
||
}
|
||
//模块类型
|
||
if (pNew->_folderType != -1)
|
||
{
|
||
pOld->_folderType = pNew->_folderType;
|
||
}
|
||
//描述
|
||
if (pNew->_strDescription != "")
|
||
{
|
||
pOld->setDes(pNew->getDes());
|
||
}
|
||
//是否生成数据表
|
||
if (pNew->_bIsGenerateTable != -1)
|
||
{
|
||
pOld->_bIsGenerateTable = pNew->_bIsGenerateTable;
|
||
}
|
||
pOld->saveSelf();
|
||
}
|
||
|
||
void TransformExcel::importExcelData(DataClass* pNew, DataClass* pOld)
|
||
{
|
||
//将new数据中有的内容更新到old,名称标识相同
|
||
if (pNew->getdisplayName() != "")
|
||
{
|
||
pOld->setdisplayName(pNew->getdisplayName());
|
||
//检查是否有重复的显示名,有就加序号
|
||
checkCoverName(pOld, pOld->_strDisplayName);
|
||
}
|
||
if (pNew->getType() != 0)
|
||
{
|
||
pOld->setType(pNew->getType());
|
||
}
|
||
if (pNew->getDescription() != "")
|
||
{
|
||
pOld->setDescription(pNew->getDescription());
|
||
}
|
||
if (pNew->getCountType() != 0)
|
||
{
|
||
pOld->setCountType(pNew->getCountType());
|
||
}
|
||
|
||
pOld->saveSelf();
|
||
//将该数据类从树中删除
|
||
//pNew->_parent->_childrenMap.remove(pNew->_id);
|
||
}
|
||
|
||
|
||
//将new属性导入旧属性,合并数据项
|
||
void TransformExcel::importExcelData(DataAttribute* pNew, DataAttribute* pOld)
|
||
{
|
||
//显示名称
|
||
if (pNew->getDisplayName() != "")
|
||
{
|
||
pOld->setDisplayName(pNew->getDisplayName());
|
||
//检查是否有重复的显示名,有就加序号
|
||
DPData* sameData = nullptr;
|
||
int id = 0;
|
||
QString oldname = pOld->_strDisplayName;
|
||
while (sameData = pOld->_parent->hasSameNameChild("displayName", pOld->_strDisplayName))
|
||
{
|
||
if (sameData != pOld)
|
||
{
|
||
//如果找到不是自己
|
||
id++;
|
||
pOld->_strDisplayName = oldname + QString::number(id);
|
||
sameData = nullptr;
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//描述
|
||
if (pNew->getDes() != "")
|
||
{
|
||
pOld->setDes(pNew->getDes());
|
||
}
|
||
//默认值
|
||
if (pNew->_strDefaultValue != "")
|
||
{
|
||
pOld->setDefaultValue(pNew->_strDefaultValue);
|
||
}
|
||
//数据类型
|
||
if (pNew->_wDataType != 0)
|
||
{
|
||
pOld->setDataType(pNew->_wDataType);
|
||
}
|
||
//是否索引
|
||
if (pNew->_wIsIndex != -1)
|
||
{
|
||
pOld->_wIsIndex = pNew->_wIsIndex;
|
||
}
|
||
//是否唯一
|
||
if (pNew->_wIsOnly != -1)
|
||
{
|
||
pOld->_wIsOnly = pNew->_wIsOnly;
|
||
}
|
||
//是否自增
|
||
if (pNew->_wIsAutoincrement != -1)
|
||
{
|
||
pOld->_wIsAutoincrement = pNew->_wIsAutoincrement;
|
||
}
|
||
//最大长度
|
||
if (pNew->_wMaxLength != -1)
|
||
{
|
||
pOld->_wMaxLength = pNew->_wMaxLength;
|
||
}
|
||
//最小长度
|
||
if (pNew->_wMinLength != -1)
|
||
{
|
||
pOld->_wMinLength = pNew->_wMinLength;
|
||
}
|
||
//数值位数
|
||
if (pNew->_wNumLength != -1)
|
||
{
|
||
pOld->_wNumLength = pNew->_wNumLength;
|
||
}
|
||
//数值精度
|
||
if (pNew->_wNumPresision != -1)
|
||
{
|
||
pOld->_wNumPresision = pNew->_wNumPresision;
|
||
}
|
||
//是否为查找序列
|
||
if (pNew->_wIsFindKey != -1)
|
||
{
|
||
pOld->_wIsFindKey = pNew->_wIsFindKey;
|
||
}
|
||
//是否为主键
|
||
if (pNew->_isPrimaryKey != -1)
|
||
{
|
||
pOld->_isPrimaryKey = pNew->_isPrimaryKey;
|
||
}
|
||
//属性类型
|
||
if (pNew->_FKType != -1)
|
||
{
|
||
pOld->_FKType = pNew->_FKType;
|
||
}
|
||
//关联数据类
|
||
if (pNew->_wFkTableID != -1)
|
||
{
|
||
pOld->_wFkTableID = pNew->_wFkTableID;
|
||
}
|
||
pOld->saveSelf();
|
||
}
|
||
|
||
|
||
void TransformExcel::checkCoverName(DPData* pOld,QString& name)
|
||
{
|
||
//检查是否有重复的显示名,有就加序号
|
||
int id = 0;
|
||
QString oldname = name;//覆盖完后的名字
|
||
DPData* sameData = nullptr;
|
||
while (sameData = _pDataManager_ref->findobjectByNameInDatamodel(pOld->_baseType, "displayName", name, _dataModelId))
|
||
{
|
||
if (sameData != pOld)
|
||
{
|
||
//同名的不是自己
|
||
id++;
|
||
name = oldname + QString::number(id);
|
||
sameData = nullptr;
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
bool TransformExcel::proccessAttribute(DataClass* pNewClass, DataClass* pOldClass)
|
||
{
|
||
QMap<unsigned int, DPData*>::iterator it = pNewClass->_childrenMap.begin();
|
||
while (it != pNewClass->_childrenMap.end())
|
||
{
|
||
//查找相同名称标识的属性
|
||
DPData* pData = pOldClass->hasSameNameChild("name", (*it)->_name);
|
||
DataAttribute* pNew = qobject_cast<DataAttribute*>(*it);
|
||
if (pData)
|
||
{
|
||
DataAttribute* pOld = qobject_cast<DataAttribute*>(pData);
|
||
importExcelData(pNew, pOld);
|
||
pNew->_id = pOld->_id; //更新id,关系属性查找关系类时使用
|
||
_importAttributeCount++;
|
||
}
|
||
else
|
||
{
|
||
/*新加入属性*/
|
||
//看属性是否有显示名称相同的,有相同则加序号
|
||
pNew->checkImportAttributeName( pOldClass);
|
||
|
||
//保存原id
|
||
// int oldId = (*it)->_id;
|
||
//无同名属性,则新加入数据类中
|
||
(*it)->_parent = pOldClass;
|
||
if (pOldClass->addChild(*it))
|
||
{ //成功
|
||
//属性已挂入新的树中,从导入树中删除
|
||
// pNewClass->_childrenMap.remove(oldId);
|
||
_pDataManager_ref->insertDataMap(DataManager::DataType::dataAttribute, (*it)->_id, (*it)); //1008test
|
||
_importAttributeCount++;
|
||
}
|
||
else
|
||
{
|
||
CommonHelper::message("导入:添加属性失败",2);
|
||
//失败
|
||
return false;
|
||
}
|
||
}
|
||
it++;
|
||
}
|
||
pNewClass->_childrenMap.clear();
|
||
return true;
|
||
}
|
||
|
||
// void TransformExcel::accessJsonNode(json& para)
|
||
// {
|
||
// auto child = para["children"];
|
||
// if (child.is_null())
|
||
// {
|
||
// return;
|
||
// }
|
||
// for (auto& subChild: child)
|
||
// {
|
||
// int id = subChild["id"];
|
||
// int classType = subChild["classType"];
|
||
// if (classType == g_TYPE_DATACLASS)
|
||
// {
|
||
// DPData* pdata = _pDataManager_ref->findObjectById(DataManager::DataType::dataClass, id);
|
||
// classList.append(qobject_cast<DataClass*>(pdata));
|
||
// //再把下挂属性加入
|
||
// QMap<unsigned int, DPData*>::iterator it = pdata->_childrenMap.begin();
|
||
// while (it!= pdata->_childrenMap.end())
|
||
// {
|
||
// attributeList.append(qobject_cast<DataAttribute*>(*it));
|
||
// it++;
|
||
// }
|
||
// }
|
||
// else if (classType == g_TYPE_DATAPACK)
|
||
// {
|
||
// DPData* pData = _pDataManager_ref->findObjectById(DataManager::DataType::dataPack, id);
|
||
// DataPack* pPack = qobject_cast<DataPack*>(pData);
|
||
// //加入packlist (需要先加根模块)
|
||
// packList.append(pPack);
|
||
// }
|
||
//
|
||
// accessJsonNode(subChild);
|
||
// }
|
||
// }
|
||
|
||
|
||
bool TransformExcel::exportData(json& parameter)
|
||
{
|
||
//清除sheet
|
||
excel.clearAllSheet();
|
||
//datamodel只可能会有一个
|
||
// auto data = parameter["data"];
|
||
// _dataModelId = data["id"];
|
||
// auto packarray = data["children"];
|
||
// for (auto& pack : packarray)
|
||
// { //根模块
|
||
// int id = pack["id"];
|
||
// DPData* pData = _pDataManager_ref->findObjectById(DataManager::DataType::dataPack, id);
|
||
// if (pData)
|
||
// {
|
||
// DataPack* pPack = qobject_cast<DataPack*>(pData);
|
||
// //加入packlist (需要先加根模块)
|
||
// packList.append(pPack);
|
||
// accessJsonNode(pack);
|
||
// }
|
||
// else
|
||
// {
|
||
// //找不到该数据模块
|
||
// }
|
||
//
|
||
// }
|
||
|
||
auto data = parameter["data"];
|
||
auto delRelAttr = data["delRelAttr"];//true 为删除关系属性
|
||
auto dataArray = data["data"];
|
||
for (auto& datanode : dataArray)
|
||
{
|
||
int id = datanode["id"];
|
||
int classtype = datanode["classType"];
|
||
insertImportList(classtype, id);
|
||
int parentId = datanode["parentId"];
|
||
int parentType = datanode["parentClassType"];
|
||
insertImportList(parentType, parentId);
|
||
}
|
||
insertImportAtt(delRelAttr);
|
||
writeDataPack(packMapEx);
|
||
writeDataClass(classMapEx);
|
||
writeDataAttribute(attributeMapEx);
|
||
|
||
return true;
|
||
}
|
||
|
||
void TransformExcel::insertImportAtt(bool delRelAttr)
|
||
{
|
||
QList<int> keylist = classMapEx.keys();
|
||
for (int i = 0; i < keylist.size(); i++)
|
||
{
|
||
int key = keylist[i];
|
||
DataClass* pClass = classMapEx[key];
|
||
if (pClass->_childrenMap.size() > 0)
|
||
{
|
||
QMap<unsigned int, DPData*>::iterator it = pClass->_childrenMap.begin();
|
||
while (it != pClass->_childrenMap.end())
|
||
{
|
||
DataAttribute* pAtt = qobject_cast<DataAttribute*>(*it);
|
||
if (pAtt->_FKType != 0)
|
||
{
|
||
//为关系属性
|
||
if (delRelAttr && !classMapEx.contains(pAtt->_wFkTableID))
|
||
{
|
||
//则该关系属性不需要导出
|
||
}
|
||
else
|
||
{
|
||
attributeMapEx.insert(pAtt->_id, pAtt);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
attributeMapEx.insert(pAtt->_id, pAtt);
|
||
}
|
||
it++;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
void TransformExcel::insertImportList(int type, int id)
|
||
{
|
||
if (type == g_TYPE_DATAPACK)
|
||
{
|
||
if (!packMapEx.contains(id))
|
||
{
|
||
DataPack* pData = qobject_cast<DataPack*>(_pDataManager_ref->findObjectById(DataManager::DataType::dataPack, id));
|
||
packMapEx.insert(id, pData);
|
||
}
|
||
}
|
||
else if (type == g_TYPE_DATACLASS)
|
||
{
|
||
if (!classMapEx.contains(id))
|
||
{
|
||
DataClass* pData = qobject_cast<DataClass*>(_pDataManager_ref->findObjectById(DataManager::DataType::dataClass, id));
|
||
classMapEx.insert(id, pData);
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
//写入模块表
|
||
void TransformExcel::writeDataPack(QMap<int, DataPack*>& mapdata)
|
||
{
|
||
QList<QStringList> input;
|
||
input.append(packHead);
|
||
QList<int> keylist = mapdata.keys();
|
||
//所有数据
|
||
for (int i = 0; i < keylist.size(); i++)
|
||
{
|
||
QStringList dataValue;
|
||
int key = keylist[i];
|
||
DataPack* pCurData = mapdata[key];
|
||
//上层数据名称标识,需判断是否为子模块
|
||
if (pCurData->_bIsChildPack)
|
||
{
|
||
dataValue.append(pCurData->_parent->_name);
|
||
}
|
||
else
|
||
{
|
||
//为数据模型根模块
|
||
dataValue.append("数据模型");
|
||
}
|
||
//名称标识
|
||
dataValue.append(pCurData->_name);
|
||
//显示名称
|
||
dataValue.append(pCurData->_strDisplayName);
|
||
//模块类型
|
||
if (pCurData->_folderType == 1)
|
||
{
|
||
dataValue.append("父数据类");
|
||
}
|
||
else if (pCurData->_folderType == 0)
|
||
{
|
||
dataValue.append("分类容器");
|
||
}
|
||
else if (pCurData->_folderType == 2)
|
||
{
|
||
dataValue.append("默认模块");
|
||
}
|
||
//描述
|
||
dataValue.append(pCurData->getDes());
|
||
//是否生成数据表
|
||
if (pCurData->_bIsGenerateTable)
|
||
{
|
||
dataValue.append("是");
|
||
}
|
||
else
|
||
{
|
||
dataValue.append("否");
|
||
}
|
||
input.append(dataValue);
|
||
}
|
||
//写入excel
|
||
excel.writeSheetData("数据模块", input);
|
||
}
|
||
|
||
//写入数据类表
|
||
void TransformExcel::writeDataClass(QMap<int, DataClass*>& mapdata)
|
||
{
|
||
QList<QStringList> input;
|
||
input.append(dataClassHead);
|
||
QList<int> keylist = mapdata.keys();
|
||
//所有
|
||
for (int i = 0; i < keylist.size(); i++)
|
||
{
|
||
QStringList dataValue;
|
||
int key = keylist[i];
|
||
DataClass* pCurData = mapdata[key];
|
||
//上层模块名称标识
|
||
dataValue.append(pCurData->_parent->_name);
|
||
//名称标识
|
||
dataValue.append(pCurData->_name);
|
||
//显示名称
|
||
dataValue.append(pCurData->_strDisplayName);
|
||
//数据类类型
|
||
if (pCurData->_type == 1)
|
||
{
|
||
dataValue.append("普通数据类");
|
||
}
|
||
else
|
||
{
|
||
dataValue.append("关系数据类");
|
||
}
|
||
//描述
|
||
dataValue.append(pCurData->_strDescription);
|
||
//数量类型
|
||
if (pCurData->_wCountType == 0)
|
||
{
|
||
dataValue.append("n");
|
||
}
|
||
else
|
||
{
|
||
dataValue.append("1");
|
||
}
|
||
input.append(dataValue);
|
||
}
|
||
//写入excel
|
||
excel.writeSheetData("数据类", input);
|
||
}
|
||
|
||
//写入属性表
|
||
void TransformExcel::writeDataAttribute(QMap<int, DataAttribute*>& mapdata)
|
||
{
|
||
QList<QStringList> input;
|
||
input.append(attributeHead);
|
||
QList<int> keylist = mapdata.keys();
|
||
//所有属性数据
|
||
for (int i = 0; i < keylist.size(); i++)
|
||
{
|
||
QStringList dataValue;
|
||
int key = keylist[i];
|
||
DataAttribute* pCurData = mapdata[key];
|
||
//上层数据类名称标识
|
||
dataValue.append(pCurData->_parent->_name);
|
||
//名称标识
|
||
dataValue.append(pCurData->_name);
|
||
//显示名称
|
||
dataValue.append(pCurData->_strDisplayName);
|
||
//描述
|
||
dataValue.append(pCurData->_strDescription);
|
||
//默认值
|
||
dataValue.append(pCurData->_strDefaultValue);
|
||
//数据类型
|
||
if (pCurData->_wDataType == MNORMALCOL_TYPE_INT)
|
||
{
|
||
dataValue.append("整型");
|
||
}
|
||
else if (pCurData->_wDataType == MNORMALCOL_TYPE_STRING)
|
||
{
|
||
dataValue.append("字符串");
|
||
}
|
||
else if (pCurData->_wDataType == MNORMALCOL_TYPE_DOUBLE)
|
||
{
|
||
dataValue.append("实型");
|
||
}
|
||
else if (pCurData->_wDataType == MNORMALCOL_TYPE_CHARPTR)
|
||
{
|
||
dataValue.append("char*");
|
||
}
|
||
else if (pCurData->_wDataType == MNORMALCOL_TYPE_DOUBLEPTR)
|
||
{
|
||
dataValue.append("double*");
|
||
}
|
||
|
||
//是否索引
|
||
dataValue.append(CommonHelper::convertPropertyValue(pCurData->_wIsIndex));
|
||
//是否唯一
|
||
dataValue.append(CommonHelper::convertPropertyValue(pCurData->_wIsOnly));
|
||
//是否自增
|
||
dataValue.append(CommonHelper::convertPropertyValue(pCurData->_wIsAutoincrement));
|
||
//最大长度
|
||
dataValue.append(QString::number(pCurData->_wMaxLength));
|
||
//最小长度
|
||
dataValue.append(QString::number(pCurData->_wMinLength));
|
||
//数值位数
|
||
dataValue.append(QString::number(pCurData->_wNumLength));
|
||
//数值精度
|
||
dataValue.append(QString::number(pCurData->_wNumPresision));
|
||
//是否为查找序列
|
||
dataValue.append(CommonHelper::convertPropertyValue(pCurData->_wIsFindKey));
|
||
//是否为主键
|
||
dataValue.append(CommonHelper::convertPropertyValue(pCurData->_isPrimaryKey));
|
||
//属性类型
|
||
if (pCurData->_FKType == 0)
|
||
{
|
||
dataValue.append("普通属性");
|
||
}
|
||
else if (pCurData->_FKType == 1)
|
||
{
|
||
dataValue.append("1:1");
|
||
}
|
||
else if (pCurData->_FKType == 2)
|
||
{
|
||
dataValue.append("1:n");
|
||
}
|
||
|
||
//查找关联数据类名称标识
|
||
DPData* pData = _pDataManager_ref->findObjectById(DataManager::DataType::dataClass, pCurData->_wFkTableID);
|
||
if (pData)
|
||
{
|
||
dataValue.append(pData->_name);
|
||
}
|
||
|
||
input.append(dataValue);
|
||
}
|
||
//写入excel
|
||
excel.writeSheetData("属性", input);
|
||
}
|
||
////////////////////////////////////////下面与excel相关
|
||
|
||
// 打开excel文件
|
||
bool TransformExcel::openFile(QString filePath)
|
||
{
|
||
return excel.OpenFile(filePath);
|
||
}
|
||
// 保存excel文件
|
||
void TransformExcel::closeFile(QString filePath)
|
||
{
|
||
excel.saveFile(filePath);
|
||
}
|
||
|
||
//备份一个数据库文件
|
||
void TransformExcel::backupDBFile()
|
||
{
|
||
SysManager& sysMgr = GetSysManager();
|
||
_srcFile = sysMgr._sysDBPath;
|
||
_midFile = sysMgr._sysDBPath.left(sysMgr._sysDBPath.lastIndexOf(".")+1) + "mid.dbp";
|
||
_bakFile = sysMgr._sysDBPath + ".bak";
|
||
QFileInfo dstfile(_bakFile);
|
||
if (dstfile.isFile())
|
||
{
|
||
QFile::remove(_bakFile);
|
||
}
|
||
/* bool ret = */ QFile::copy(_srcFile, _bakFile);
|
||
}
|
||
//恢复原来的数据库文件;
|
||
bool TransformExcel::rollBackDBFile()
|
||
{
|
||
|
||
// bool ret = QFile::remove(_srcFile);
|
||
QFile srcfile(_srcFile);
|
||
QFile dstfile(_bakFile);
|
||
QFileInfo midfile(_midFile);
|
||
|
||
if (_pDataManager_ref->pTheDBPlatform)
|
||
{
|
||
delete _pDataManager_ref->pTheDBPlatform;
|
||
_pDataManager_ref->pTheDBPlatform = nullptr;
|
||
|
||
if (!srcfile.remove())
|
||
{
|
||
QString msg = "DB remove " + _srcFile + " failed";
|
||
LOG(ERROR) << CommonHelper::qstringToStdString(msg);
|
||
LOG(ERROR) << CommonHelper::qstringToStdString(srcfile.errorString());
|
||
CommonHelper::message("数据回滚失败,将重启程序", 2);
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
if (!dstfile.rename(_srcFile))
|
||
{
|
||
LOG(ERROR) << CommonHelper::utf8ToStdString("DB rollback failed ");
|
||
LOG(ERROR) << CommonHelper::qstringToStdString(dstfile.errorString());
|
||
CommonHelper::message("数据回滚失败,将重启程序", 2);
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
if (_pDataManager_ref->pTheDBPlatform)
|
||
{
|
||
delete _pDataManager_ref->pTheDBPlatform;
|
||
_pDataManager_ref->pTheDBPlatform = nullptr;
|
||
}
|
||
_pDataManager_ref->pTheDBPlatform = new DBPlatformSpace::DBPlatformNew(CommonHelper::qstringToStdString(_srcFile));
|
||
if ("OK" == DBPlatformSpace::DBPlatformNew::getCurrPlatformStatus())
|
||
{
|
||
LOG(INFO) << CommonHelper::utf8ToStdString("link bakDB success");
|
||
_pDataManager_ref->_dbConnected = true;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
|
||
|
||
// if (midfile.isFile())
|
||
// {
|
||
// QFile::remove(_midFile);
|
||
// }
|
||
// bool ret = QFile::copy(_bakFile, _midFile);
|
||
// _pDataManager_ref->pTheDBPlatform = new DBPlatformSpace::DBPlatformNew(CommonHelper::qstringToStdString(_midFile));
|
||
|
||
// if ("OK" == DBPlatformSpace::DBPlatformNew::getCurrPlatformStatus())
|
||
// {
|
||
// LOG(INFO) << CommonHelper::utf8ToStdString("link midDB success");
|
||
// if (!srcfile.remove())
|
||
// {
|
||
// QString msg = "DB remove " + _srcFile + " failed";
|
||
// LOG(ERROR) << CommonHelper::qstringToStdString(msg);
|
||
// LOG(ERROR) << CommonHelper::qstringToStdString(srcfile.errorString());
|
||
// }
|
||
// else
|
||
// {
|
||
// if( !dstfile.rename(_srcFile))
|
||
// {
|
||
// LOG(ERROR) << CommonHelper::utf8ToStdString("DB rollback failed ");
|
||
// LOG(ERROR) << CommonHelper::qstringToStdString(dstfile.errorString());
|
||
// return false;
|
||
// }
|
||
// else
|
||
// {
|
||
// if (_pDataManager_ref->pTheDBPlatform)
|
||
// {
|
||
// delete _pDataManager_ref->pTheDBPlatform;
|
||
// _pDataManager_ref->pTheDBPlatform = nullptr;
|
||
// }
|
||
// _pDataManager_ref->pTheDBPlatform = new DBPlatformSpace::DBPlatformNew(CommonHelper::qstringToStdString(_srcFile));
|
||
// if ("OK" == DBPlatformSpace::DBPlatformNew::getCurrPlatformStatus())
|
||
// {
|
||
// LOG(INFO) << CommonHelper::utf8ToStdString("link bakDB success");
|
||
// _pDataManager_ref->_dbConnected = true;
|
||
// //删除中间数据库
|
||
// if (midfile.isFile())
|
||
// {
|
||
// QFile::remove(_midFile);
|
||
// }
|
||
// }
|
||
// return true;
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|