From b8c7a191a7f8427fe8d8e9b83743adaf70f6d0f5 Mon Sep 17 00:00:00 2001 From: baiYue Date: Fri, 14 Mar 2025 17:18:25 +0800 Subject: [PATCH] add modify project model function --- common/include/dataBase.h | 7 +- common/source/dataBase.cpp | 171 +++++++++++++- include/projectModelDlg.h | 18 +- include/renameModel.h | 10 +- source/projectModelDlg.cpp | 458 ++++++++++++++++++++++++++----------- source/renameModel.cpp | 69 +++++- ui/projectModelDlg.ui | 2 +- 7 files changed, 577 insertions(+), 158 deletions(-) diff --git a/common/include/dataBase.h b/common/include/dataBase.h index 937706a..f146922 100644 --- a/common/include/dataBase.h +++ b/common/include/dataBase.h @@ -185,10 +185,15 @@ public: //***********工程模 bool createProjectManager(); //生成记录表,包含工程模名称,属性组名,启用和关闭的属性字段(json类型)[一个属性组建一个表] bool insertProjectManager(const QString& name,const QString& tag,const QString& metaModel,const QString& groupName,int linkType,QJsonObject checkState); - QStringList getProjectFromManager(const QString& sMeta); //返回元模下已创建的工程名 + bool updateCheckState(const QString& tableName,QJsonObject checkState); //更新属性选中状态 + QMap getProjectFromManager(const QString& sMeta); //返回元模下已创建的工程名 QMap getCheckStateFromManager(const QString& sProject); //获取当前工程模型所有属性的选择状态 <属性名,选择状态> + QMap getProjectTableName(const QString& sProject); //获取当前工程模型下所有表信息 bool createDynamicTable(const QString&, const QStringList&); bool deleteProjectModel(const QString&); + bool deleteTable(const QString&); //删除表 + bool deleteRecordFromManager(const QString& sProject,const QString& sGroup); //删除某个模型下的组 + bool modifyProjectTable(QString sTable,QMap mOld,QMap mNew); private: QMap _attributeGroup; //属性组的组 QMap _dataType; //数据类型组 diff --git a/common/source/dataBase.cpp b/common/source/dataBase.cpp index 1200ad3..5fa4538 100644 --- a/common/source/dataBase.cpp +++ b/common/source/dataBase.cpp @@ -1126,14 +1126,35 @@ bool DataBase::insertProjectManager(const QString& name,const QString& tag,const return false; } -QStringList DataBase::getProjectFromManager(const QString& sMeta) +bool DataBase::updateCheckState(const QString& tableName,QJsonObject checkState) { - QSet projectSet; + if(db.open()) + { + QJsonDocument checkDoc(checkState); + QString strCheck = checkDoc.toJson(QJsonDocument::Compact); + + QSqlQuery query(db); + query.prepare("UPDATE project_manager SET check_state = ? WHERE name = ?"); + query.bindValue(0, strCheck); + query.bindValue(1, tableName); + + if (!query.exec()) { + qDebug() << "Update failed:" << query.lastError(); + return false; + } + return true; + } + return false; +} + +QMap DataBase::getProjectFromManager(const QString& sMeta) +{ + QMap map; if(db.open()) { QSqlQuery qry(db); - qry.prepare("SELECT tag FROM project_manager WHERE meta_model = ?"); + qry.prepare("SELECT tag,link_type FROM project_manager WHERE meta_model = ?"); qry.bindValue(0,sMeta); bool res = qry.exec(); @@ -1142,21 +1163,25 @@ QStringList DataBase::getProjectFromManager(const QString& sMeta) { qDebug()< DataBase::getCheckStateFromManager(const QString& sProject) @@ -1197,6 +1222,39 @@ QMap DataBase::getCheckStateFromManager(const QString& sPro return map; } +QMap DataBase::getProjectTableName(const QString& sProject) +{ + QMap map; + if(db.open()) + { + QSqlQuery qry(db); + + qry.prepare("SELECT group_name, name FROM project_manager WHERE tag = ?"); + qry.bindValue(0,sProject); + bool res = qry.exec(); + QString str = qry.lastQuery(); + if(!res) + { + qDebug()< mOld,QMap mNew) +{ + if(db.open()) + { + QSqlQuery query(db); + QStringList sqlBatch; + + for (auto &col : mOld.keys()) { + if (!mNew.contains(col)) { + sqlBatch << QString("ALTER TABLE %1 DROP COLUMN %2") + .arg(sTable, col); + } + } + + // 添加/修改列 + for (auto &col : mNew.keys()) { + const QString &newType = mNew[col]; + + // 新增列 + if (!mOld.contains(col)) { + sqlBatch << QString("ALTER TABLE %1 ADD COLUMN %2 %3") + .arg(sTable, col, newType); + } + // 修改列类型 + else if (mOld[col] != newType) { + sqlBatch << QString("ALTER TABLE %1 ALTER COLUMN %2 TYPE %3 USING %2::%3") + .arg(sTable, col, newType); + } + } + + // ================= 阶段4:执行变更事务 ================= + if (!db.transaction()) { + qDebug() << "开启事务失败:" << db.lastError().text(); + return false; + } + + for (const QString &sql : sqlBatch) { + if (!query.exec(sql)) { + qDebug() << "执行SQL失败:" << query.lastError().text() + << "\n语句:" << sql; + db.rollback(); + return false; + } + } + + if (!db.commit()) { + qDebug() << "提交事务失败:" << db.lastError().text(); + db.rollback(); + return false; + } + + return true; } else return false; diff --git a/include/projectModelDlg.h b/include/projectModelDlg.h index eab7fd0..b4cd357 100644 --- a/include/projectModelDlg.h +++ b/include/projectModelDlg.h @@ -9,11 +9,17 @@ QT_BEGIN_NAMESPACE namespace Ui { class projectModelDlg; } QT_END_NAMESPACE +struct PropertyState //每个属性的状态 +{ + bool checkState = false; + QString dataType; +}; + struct PropertyPage //属性列表信息 { QStandardItemModel* pBase; //基础属性 QStandardItemModel* pSelect; //已选择属性 - QMap mCheckState; //属性选择状态 + QMap mCheckState; //属性选择状态 }; typedef QMap MapProperty; //str为属性名,model1基础属性,model2已选择属性 @@ -42,7 +48,6 @@ public: void update(); void generate(const QString&); //根据输入名称生成表 - QString getProjectName() const; //返回当前选择项目的名称 public slots: void onSaveClicked(); void onCancelClicked(); @@ -60,10 +65,17 @@ public: QStringList getGroupList(const QString& model) const; //返回该元模下的属性组列表 QStringList getAttributeList(const QString& model,const QString& group) const; //根据元模名和组名返回属性列表 void setItemAttribute(const QString&,QStandardItem*); //设置item的属性(数据库表字段名) - QString combinePropertySql(const QStandardItem*); //根据item属性生成sql + QPair combinePropertySql(const QStandardItem*); //根据item属性生成sql + QString getProjectName() const; //返回当前选择项目的名称 + QString getMetaName() const; //返回当前元模型名 + bool ifProjectEqual(QMap); //根据每个属性组的勾选状态判断两个模型是否相同 + QString modifyProjectModel(QMap); //修改工程模 private: void updateIconList(); //选择工程模后刷新关联图标 void removeProjectData(const QString&,const QString&); //移除对应的project层级结构 + QString getItemDataType(const QStandardItem* pItem); //返回数据类型 + bool createPropertyTable(const QString& sProject,MapProperty::Iterator iter,int nLinkType); //创建属性组表并插入记录到管理表(工程名,当前项迭代器,关联图元类型) + QJsonObject getSelectedState(MapProperty::Iterator iter); //返回json格式的选中状态 private: Ui::projectModelDlg *ui; RenameModel* m_pRenameModel; diff --git a/include/renameModel.h b/include/renameModel.h index 9c685c9..040aadb 100644 --- a/include/renameModel.h +++ b/include/renameModel.h @@ -8,6 +8,14 @@ QT_BEGIN_NAMESPACE namespace Ui { class renameModel; } QT_END_NAMESPACE +enum projectState +{ + Err = -1, + NotExist = 0, + Exist, + Changed +}; + class projectModelDlg; class RenameModel : public QDialog @@ -27,7 +35,7 @@ public slots: void onCancelClicked(); private: void setShowName(); //获取当前名称并显示 - bool couldSave(); //判断当前名称是否可用 + projectState couldSave(); //判断当前名称是否可用 private: Ui::renameModel *ui; projectModelDlg* _pParent; diff --git a/source/projectModelDlg.cpp b/source/projectModelDlg.cpp index 1dde8b7..b06c4cd 100644 --- a/source/projectModelDlg.cpp +++ b/source/projectModelDlg.cpp @@ -49,67 +49,64 @@ void projectModelDlg::initial() update(); } -MapProperty projectModelDlg::addNewProject(const QString& sMeta,const QString& sProject) //todo:保存后生成新建 +MapProperty projectModelDlg::addNewProject(const QString& sMeta,const QString& sProject) { MapProperty mt; QStringList lstProperty = getGroupList(sMeta); //lstProperty< mapCheckState = DataBase::GetInstance()->getCheckStateFromManager(sProject); //获取选择状态 - if(mapCheckState.isEmpty()) //无返回值,是新建目标 + for(auto &property:lstProperty) { - for(auto &property:lstProperty) - { - //todo:读取属性信息 - QStringList lstName = getAttributeList(sMeta,property); - //lstName<appendRow(new QStandardItem(property)); //总览view - - for(auto &name:lstName) - { - QStandardItem* pItem = new QStandardItem(name); - setItemAttribute(name,pItem); - struProperty.pBase->appendRow(pItem); - struProperty.mCheckState.insert(name,0); //初始都是未选择状态 - } - mt.insert(property,struProperty); - } - } - else - { - for(auto &property:lstProperty) + PropertyPage struProperty; + struProperty.pBase = new QStandardItemModel(this); + struProperty.pSelect = new QStandardItemModel(this); + if(mapCheckState.contains(property)) //生成的模型中勾选了该属性组 { QJsonObject obj = mapCheckState[property]; QJsonArray nodesJsonArray = obj["checkState"].toArray(); - PropertyPage struProperty; - struProperty.pBase = new QStandardItemModel(this); - struProperty.pSelect = new QStandardItemModel(this); for (QJsonValueRef nodeJson : nodesJsonArray) { QJsonObject node = nodeJson.toObject(); QString propertyName = node["name"].toString(); int nState = node["checked"].toInt(); + QString dataType = node["type"].toString(); QStandardItem* pItem = new QStandardItem(propertyName); setItemAttribute(propertyName,pItem); + PropertyState sta; + sta.dataType = dataType; if(nState) { struProperty.pSelect->appendRow(pItem); - struProperty.mCheckState.insert(propertyName,1); + sta.checkState = true; + struProperty.mCheckState.insert(propertyName,sta); } else { struProperty.pBase->appendRow(pItem); - struProperty.mCheckState.insert(propertyName,0); + sta.checkState = false; + struProperty.mCheckState.insert(propertyName,sta); } } - mt.insert(property,struProperty); } + else //未勾选属性组或是新建 + { + QStringList lstName = getAttributeList(sMeta,property); + for(auto &name:lstName) + { + QStandardItem* pItem = new QStandardItem(name); + setItemAttribute(name,pItem); + struProperty.pBase->appendRow(pItem); + QString dataType = getItemDataType(pItem); + PropertyState sta; + sta.dataType = dataType; + sta.checkState = false; + struProperty.mCheckState.insert(name,sta); //初始都是未选择状态 + } + } + mt.insert(property,struProperty); } return mt; @@ -123,19 +120,22 @@ void projectModelDlg::initialModel() for(auto &model:lstModel) { MapProject mp; - QStringList lstProject = DataBase::GetInstance()->getProjectFromManager(model); - lstProject.prepend(QString::fromWCharArray(L"新建")); //每个类别都有未命名工程 + QMap mapProject = DataBase::GetInstance()->getProjectFromManager(model); + mapProject.insert(QString::fromWCharArray(L"新建"),0); //每个类别都有未命名工程 QStandardItem* modelItem = new QStandardItem(model); //总览view类型 - for(auto &proj:lstProject) + QMap::Iterator iter; + for(iter = mapProject.begin();iter != mapProject.end(); ++iter) { - QStandardItem* propertyItem = new QStandardItem(proj); + QStandardItem* propertyItem = new QStandardItem(iter.key()); modelItem->appendRow(propertyItem); //总览view名称 PropertyModel pm; - pm.mapProperty = addNewProject(model,proj); - mp.insert(proj,pm); + pm.mapProperty = addNewProject(model,iter.key()); + pm.nType = iter.value(); + mp.insert(iter.key(),pm); } + _viewModel->appendRow(modelItem); //todo:读取存储,按分类遍历名称 m_mapTotal.insert(model,mp); @@ -182,52 +182,13 @@ void projectModelDlg::generate(const QString& str) bool createRes = true; //动态表生成结果 for(MapProperty::Iterator it = mapProperty.begin();it != mapProperty.end();++it){ //每个属性组单独生成表 - - QString sName = _curMeta + QString("_") + str + QString("_")+it.key(); - - QStringList fields; - fields.append("id SERIAL NOT NULL PRIMARY KEY"); - fields.append("global_uuid uuid NOT NULL DEFAULT gen_random_uuid()"); - fields.append("attribute_group VARCHAR(64) NOT NULL"); - - QJsonObject objState; - QJsonArray arrState; - + int nType = ite.value().nType; QStandardItemModel* pSelectModel = it->pSelect; QStandardItem *rootItem = pSelectModel->invisibleRootItem(); - for (int row = 0; row < rootItem->rowCount(); ++row) { //遍历已选择列表 - QStandardItem *childItem = rootItem->child(row); - if (childItem) { - QString s = combinePropertySql(childItem); //拼接单句sql - fields.append(s); - - QJsonObject node; //保存已选择状态 - node["name"] = childItem->text(); - node["checked"] = 1; - arrState.append(node); - } - } - QStandardItemModel* pBaseModel = it->pBase; - rootItem = pBaseModel->invisibleRootItem(); - for (int row = 0; row < rootItem->rowCount(); ++row) { //遍历未选择列表 - QStandardItem *childItem = rootItem->child(row); - if (childItem) { - QJsonObject node; //保存未选择状态 - node["name"] = childItem->text(); - node["checked"] = 0; - arrState.append(node); - } - } - - if(!DataBase::GetInstance()->createDynamicTable(sName,fields)) - { - createRes = false; - } - else - { - objState["checkState"] = arrState; - DataBase::GetInstance()->insertProjectManager(sName,str,_curMeta,it.key(),ite.value().nType,objState); - } + if(rootItem->rowCount() == 0) //修改逻辑,未勾选的属性组不生成表 by/20250311 + continue; + bool res = createPropertyTable(str,it,nType); + createRes = createRes && res; } if(createRes) { @@ -237,7 +198,9 @@ void projectModelDlg::generate(const QString& str) if(_curProject == QString::fromWCharArray(L"新建")) { ui->cb_projectModel->addItem(str); - addNewProject(_curMeta,_curProject); + PropertyModel npm; //新建工程对象 + npm.mapProperty = addNewProject(_curMeta,_curProject); + m_mapTotal[_curMeta].insert(_curProject,npm); QList lst = _viewModel->findItems(_curMeta); if(lst.size() == 1) { @@ -257,11 +220,6 @@ void projectModelDlg::generate(const QString& str) } -QString projectModelDlg::getProjectName() const -{ - return _curProject; -} - void projectModelDlg::onSaveClicked() { if(_curProject.isEmpty()) @@ -270,12 +228,14 @@ void projectModelDlg::onSaveClicked() } else { - if(_curProject == QString::fromWCharArray(L"新建")) + if(m_mapTotal[_curMeta][_curProject].nType == 0) { - if(m_pRenameModel) - { - m_pRenameModel->showCenter(); - } + QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"请选择工程模类型")); + return; + } + if(m_pRenameModel) + { + m_pRenameModel->showCenter(); } } } @@ -308,22 +268,8 @@ void projectModelDlg::onApplyClicked() { property[_curProperty].pSelect->appendRow(item); property[_curProperty].pBase->removeRow(selected.row()); - property[_curProperty].mCheckState[item->text()] = 1; //选择状态设为1 + m_mapTotal[_curMeta][_curProject].mapProperty[_curProperty].mCheckState[item->text()].checkState = true; //选择状态设为1 } - /*MapProperty::Iterator ite = property.find(_curProperty); - if(ite != property.end()) - { - QStandardItemModel* pBase = ite.value().pBase; - QStandardItemModel* pSelect = ite.value().pSelect; - - QModelIndex selected = ui->treeView_base->currentIndex(); - QStandardItem* item = pBase->takeItem(selected.row()); // 根据index获取当前item - if(item) - { - pSelect->appendRow(item); - pBase->removeRow(selected.row()); - } - }*/ } } } @@ -351,22 +297,8 @@ void projectModelDlg::onRevokeClicked() { property[_curProperty].pBase->appendRow(item); property[_curProperty].pSelect->removeRow(selected.row()); - property[_curProperty].mCheckState[item->text()] = 0; //选择状态设为0 + m_mapTotal[_curMeta][_curProject].mapProperty[_curProperty].mCheckState[item->text()].checkState = false; //选择状态设为0 } - /*MapProperty::Iterator ite = property.find(_curProperty); - if(ite != property.end()) - { - QStandardItemModel* pBase = ite.value().pBase; - QStandardItemModel* pSelect = ite.value().pSelect; - - QModelIndex selected = ui->treeView_sub->currentIndex(); - QStandardItem* item = pSelect->takeItem(selected.row()); // 根据index获取当前item - if(item) - { - pBase->appendRow(item); - pSelect->removeRow(selected.row()); - } - }*/ } } } @@ -409,7 +341,6 @@ void projectModelDlg::onProjectIndexChanged(const QString& str) } else { - updateIconList(); if(_curProject == str) //选择未改变 { return; @@ -432,7 +363,11 @@ void projectModelDlg::onProjectIndexChanged(const QString& str) ui->cb_property->addItem(ite.key()); } } + + QString sFirst = ui->cb_property->itemText(0); //点选工程模后默认选中第一条属性 + onPropertyIndexChanged(sFirst); } + updateIconList(); } } @@ -499,7 +434,7 @@ void projectModelDlg::onIndexClicked(const QModelIndex &index) QString sProject = model->data(index, Qt::DisplayRole).toString(); ui->cb_baseModel->setCurrentText(sMeta); //无法编辑combobox的会跳转到该内容 - onBaseModelIndexChanged(sMeta); + onBaseModelIndexChanged(sMeta); //Base的改变会清空_project ui->cb_projectModel->setCurrentText(sProject); onProjectIndexChanged(sProject); @@ -567,14 +502,32 @@ void projectModelDlg::onDeleteProjectClicked() // 删除当前选中的项 QString meta = model->data(index.parent(), Qt::DisplayRole).toString(); QString text = model->data(index, Qt::DisplayRole).toString(); - bool val = DataBase::GetInstance()->deleteProjectModel(text); - if(val) + QMessageBox msgBox; + msgBox.setText(QString::fromWCharArray(L"工程模将被删除")); + msgBox.setInformativeText(QString::fromWCharArray(L"删除后不可恢复,确定删除?")); + msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Cancel); + int ret = msgBox.exec(); + switch (ret) { + case QMessageBox::Yes: { - //removeProjectData(meta,text); - model->removeRow(index.row(), index.parent()); + bool val = DataBase::GetInstance()->deleteProjectModel(text); + if(val) + { + removeProjectData(meta,text); + model->removeRow(index.row(), index.parent()); + } + else + QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"删除失败")); + break; + } + case QMessageBox::Cancel: + // Cancel was clicked + break; + default: + // should never be reached + break; } - else - QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"删除失败")); } else { // 如果是第一级节点,不执行删除操作 return; @@ -686,7 +639,7 @@ void projectModelDlg::setItemAttribute(const QString& name,QStandardItem* p) } } -QString projectModelDlg::combinePropertySql(const QStandardItem* pItem) +QPair projectModelDlg::combinePropertySql(const QStandardItem* pItem) { int id = pItem->data(Id).toInt(); QString attribute = pItem->data(Attribute).toString(); @@ -732,7 +685,135 @@ QString projectModelDlg::combinePropertySql(const QStandardItem* pItem) sql += " PRIMARY KEY"; }*/ - return sql; + return qMakePair(sql,dataTypePart); +} + +QString projectModelDlg::getProjectName() const +{ + return _curProject; +} + +QString projectModelDlg::getMetaName() const +{ + return _curMeta; +} + +bool projectModelDlg::ifProjectEqual(QMap map) +{ + //todo:判断关联的模型类型 + MapProperty curPro = m_mapTotal[_curMeta][_curProject].mapProperty; + + QMap::Iterator iter; + for(iter = curPro.begin();iter != curPro.end();++iter) + { + if(!map.contains(iter.key())) //已存在的模型中不包含该属性组 + { + QMap curCheckState = iter.value().mCheckState; + for(auto &val:curCheckState) + { + if(val.checkState) //该属性组不在模型中且被勾选 + return false; + } + continue; //该属性组不在模型中且未被勾选,不做判断 + } + else + { + QJsonObject dbCheckState = map[iter.key()]; //数据库中该属性组的勾选状态 + QMap curCheckState = iter.value().mCheckState; //当前程序中的勾选状态 + + QJsonArray nodesJsonArray = dbCheckState["checkState"].toArray(); + if(nodesJsonArray.size() != curCheckState.size()) //属性个数对不上,模型不同 + return false; + for (QJsonValueRef nodeJson : nodesJsonArray) + { + QJsonObject node = nodeJson.toObject(); + QString propertyName = node["name"].toString(); + int nState = node["checked"].toInt(); + if(curCheckState[propertyName].checkState != nState) //相同属性选中状态不同 + return false; + } + } + } + return true; +} + +QString projectModelDlg::modifyProjectModel(QMap mapOld) +{ + QString sRes; + MapProperty curPro = m_mapTotal[_curMeta][_curProject].mapProperty; + int nType = m_mapTotal[_curMeta][_curProject].nType; + + QMap::Iterator iter; + for(iter = curPro.begin();iter != curPro.end();++iter) //遍历当前模型所有属性组 + { + QMap curCheckState = iter.value().mCheckState; //当前程序中的属性勾选状态 + bool isNull = true; //当前属性组是否未空 + for(auto &val:curCheckState) + { + if(val.checkState){ + isNull = false; //当前程序模型有勾选,不为空 + break; + } + } + if(isNull){ + if(mapOld.contains(iter.key())){ //当前模型勾选为空且库模型中包含此属性组,移除库中该属性组表 + QMap map = DataBase::GetInstance()->getProjectTableName(_curProject); + bool res = DataBase::GetInstance()->deleteTable(map[iter.key()]); + if(res){ + DataBase::GetInstance()->deleteRecordFromManager(_curProject,iter.key()); + sRes = QString::fromWCharArray(L"修改模型成功"); + } + } + } + else{ + if(mapOld.contains(iter.key())){ //新旧模型中都存在,修改模型 + QMap oldSchema; //库中模型 属性名/数据类型 + QMap newSchema; //现有模型 + + QJsonObject obj = mapOld[iter.key()]; + QJsonArray nodesJsonArray = obj["checkState"].toArray(); + + for (QJsonValueRef nodeJson : nodesJsonArray) + { + QJsonObject node = nodeJson.toObject(); + QString propertyName = node["name"].toString(); + int nState = node["checked"].toInt(); + QString dataType = node["type"].toString(); + + if(nState) + oldSchema.insert(propertyName,dataType); + } + + QMap::Iterator it; + for(it = curCheckState.begin(); it != curCheckState.end();++it) + { + if(it->checkState){ + newSchema.insert(it.key(),it->dataType); + } + } + if(oldSchema == newSchema) + continue; + + QMap map = DataBase::GetInstance()->getProjectTableName(_curProject); + + bool res = DataBase::GetInstance()->modifyProjectTable(map[iter.key()],oldSchema,newSchema); + QJsonObject objState = getSelectedState(iter); + DataBase::GetInstance()->updateCheckState(map[iter.key()],objState); + if(res) + { + sRes = QString::fromWCharArray(L"修改模型成功"); + } + } + else{ //非空且库模型中不存在,新增 + bool res = createPropertyTable(_curProject,iter,nType); + if(res) + { + sRes = QString::fromWCharArray(L"修改模型成功"); + } + } + } + } + return sRes; } void projectModelDlg::updateIconList() @@ -765,3 +846,114 @@ void projectModelDlg::removeProjectData(const QString& sMeta,const QString& sPro { m_mapTotal[sMeta].remove(sProject); } + +QString projectModelDlg::getItemDataType(const QStandardItem* pItem) +{ + QString dataType = pItem->data(DataType).toString(); + int lengthPrecision = pItem->data(LengthPrecision).toInt(); + int scale = pItem->data(Scale).toInt(); + + QString dataTypePart = dataType; + if (lengthPrecision > 0) { + dataTypePart += QString("(%1").arg(lengthPrecision); + if (scale > 0) { + dataTypePart += QString(",%1").arg(scale); + } + dataTypePart += ")"; + } + return dataTypePart; +} + +bool projectModelDlg::createPropertyTable(const QString& sProject,MapProperty::Iterator iter,int nLinkType) +{ + QString sName = _curMeta + QString("_") + sProject + QString("_")+iter.key(); + + QStringList fields; + fields.append("id SERIAL NOT NULL PRIMARY KEY"); + fields.append("global_uuid uuid NOT NULL DEFAULT gen_random_uuid()"); + fields.append("attribute_group VARCHAR(64) NOT NULL"); + + QStandardItemModel* pSelectModel = iter->pSelect; + QStandardItem *rootItem = pSelectModel->invisibleRootItem(); + + for (int row = 0; row < rootItem->rowCount(); ++row) { //遍历已选择列表 + QStandardItem *childItem = rootItem->child(row); + if (childItem) { + QPair pair = combinePropertySql(childItem); //拼接单句sql + fields.append(pair.first); + } + } + + QJsonObject objState = getSelectedState(iter); + + if(!DataBase::GetInstance()->createDynamicTable(sName,fields)) + { + return false; + } + else + { + DataBase::GetInstance()->insertProjectManager(sName,sProject,_curMeta,iter.key(),nLinkType,objState); + return true; + } +} + +QJsonObject projectModelDlg::getSelectedState(MapProperty::Iterator iter) +{ + QJsonObject objState; + QJsonArray arrState; + + QStandardItemModel* pSelectModel = iter->pSelect; + QStandardItem *rootItem = pSelectModel->invisibleRootItem(); + + for (int row = 0; row < rootItem->rowCount(); ++row) { //遍历已选择列表 + QStandardItem *childItem = rootItem->child(row); + if (childItem) { + + QString dataType = childItem->data(DataType).toString(); + int lengthPrecision = childItem->data(LengthPrecision).toInt(); + int scale = childItem->data(Scale).toInt(); + + QString dataTypePart = dataType; //拼接数据类型 + if (lengthPrecision > 0) { + dataTypePart += QString("(%1").arg(lengthPrecision); + if (scale > 0) { + dataTypePart += QString(",%1").arg(scale); + } + dataTypePart += ")"; + } + + QJsonObject node; //保存已选择状态 + node["name"] = childItem->text(); + node["checked"] = 1; + node["type"] = dataTypePart; + arrState.append(node); + } + } + QStandardItemModel* pBaseModel = iter->pBase; + rootItem = pBaseModel->invisibleRootItem(); + for (int row = 0; row < rootItem->rowCount(); ++row) { //遍历未选择列表 + QStandardItem *childItem = rootItem->child(row); + if (childItem) { + QString dataType = childItem->data(DataType).toString(); + int lengthPrecision = childItem->data(LengthPrecision).toInt(); + int scale = childItem->data(Scale).toInt(); + + QString dataTypePart = dataType; //拼接数据类型 + if (lengthPrecision > 0) { + dataTypePart += QString("(%1").arg(lengthPrecision); + if (scale > 0) { + dataTypePart += QString(",%1").arg(scale); + } + dataTypePart += ")"; + } + + QJsonObject node; //保存未选择状态 + node["name"] = childItem->text(); + node["checked"] = 0; + node["type"] = dataTypePart; + arrState.append(node); + } + } + objState["checkState"] = arrState; + return objState; +} diff --git a/source/renameModel.cpp b/source/renameModel.cpp index 60c1fe1..10b8c66 100644 --- a/source/renameModel.cpp +++ b/source/renameModel.cpp @@ -1,5 +1,7 @@ +#include #include "renameModel.h" #include "projectModelDlg.h" +#include "dataBase.h" #include "ui_renameModel.h" RenameModel::RenameModel(QWidget *parent) @@ -61,21 +63,34 @@ void RenameModel::setShowName() } } -bool RenameModel::couldSave() +projectState RenameModel::couldSave() { if(_pParent) { + QString meta = _pParent->getMetaName(); QString str = ui->lineEdit_name->text(); - //QString str = _pParent->getProjectName(); if(str == QString::fromWCharArray(L"新建")) { - ui->label_info->setText(QString::fromWCharArray(L"请输入需保存的名称")); - return false; + return Err; } else { //todo:判断输入的名称是否存在 - return true; + QMap map = DataBase::GetInstance()->getCheckStateFromManager(str); + if(map.isEmpty()) + { + return NotExist; + } + else + { + bool val = _pParent->ifProjectEqual(map); + if(val){ + return Exist; + } + else{ + return Changed; + } + } } } } @@ -84,17 +99,47 @@ void RenameModel::onOkClicked() { if(_pParent) { - if(couldSave()) - { - //todo:保存 + projectState state = couldSave(); + switch(state){ + case Err: + ui->label_info->setText(QString::fromWCharArray(L"请输入需保存的名称")); + break; + case NotExist: _pParent->generate(ui->lineEdit_name->text()); + ui->label_info->clear(); hide(); - } - else - { + break; + case Exist: + ui->label_info->setText(QString::fromWCharArray(L"该模型已存在")); + break; + case Changed: + QMessageBox msgBox; + msgBox.setText(QString::fromWCharArray(L"提示")); + msgBox.setInformativeText(QString::fromWCharArray(L"该模型已存在且与同名模型不一致,是否替换库模型?")); + msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Cancel); + int ret = msgBox.exec(); + if(ret == QMessageBox::Yes) + { + QMap mapCheckState = DataBase::GetInstance()->getCheckStateFromManager(ui->lineEdit_name->text()); //获取选择状态 + QString sRes = _pParent->modifyProjectModel(mapCheckState); + if(!sRes.isEmpty()) + { + QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型成功")); + } + else + { + QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"修改模型失败")); + } + hide(); + } + else if(ret == QMessageBox::Cancel) + { + } + ui->label_info->clear(); + break; } - ui->label_info->clear(); } } diff --git a/ui/projectModelDlg.ui b/ui/projectModelDlg.ui index b1d2226..7073534 100644 --- a/ui/projectModelDlg.ui +++ b/ui/projectModelDlg.ui @@ -146,7 +146,7 @@ font: 12pt "Microsoft YaHei UI"; color: rgb(8, 8, 8); - 关联图元 + 工程模类型