DPS/DataPlatform/DataAttribute.cpp

382 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma execution_character_set("utf-8")
#include "DataAttribute.h"
#include "M_NormalColumnDAO.h"
#include "M_EntityModelDAO.h"
#include "global.h"
#include "DataClass.h"
#include "DataManager.h"
#include < regex >
DataAttribute::DataAttribute()
{
_baseType = g_TYPE_DATAATTRUBUTE;
}
DataAttribute::~DataAttribute()
{
}
void static backupDAOData(DBPlatformSpace::M_NormalColumnDAO& src, DBPlatformSpace::M_NormalColumnDAO& dst)
{
}
void static restoreData(DBPlatformSpace::M_NormalColumnDAO& src, DataAttribute& dst)
{
//主要恢复界面属性值
}
void DataAttribute::checkImportAttributeName(DPData* pParent)
{
DataClass* pClass = qobject_cast<DataClass*>(pParent);
/*新加入属性*/
//看属性是否有显示名称相同的,有相同则加序号
int id = 0;
QString newName = _strDisplayName;
while (pClass->hasSameNameChild("displayName", newName))
{
LOG(INFO) << CommonHelper::utf8ToStdString("新加属性显示名称与原有属性相同, 加序号");
id += 1;
newName = _strDisplayName + QString::number(id);
}
_strDisplayName = newName;
}
//属性名第一个字母大写C,第二个字母大写
void DataAttribute::reName( )
{
QString oldName = _name;
QString newName;
//首字母变成大写
if (oldName.at(0) == "C" || oldName.at(0) == "c")
{
if (oldName.size() >= 2)
{
_name = 'C' + oldName.at(1).toUpper() + oldName.right(oldName.size() - 2);
}
else
{
_name = 'C' ;
}
}
else
{
newName = oldName.at(0).toUpper() + oldName.right(oldName.size() - 1);
_name = "C" + newName;
}
}
void DataAttribute::toJson(json& jsonObj, bool)
{
jsonObj["id"] = _id;
jsonObj["label"] = CommonHelper::qstringToUtf8(_strDisplayName);
jsonObj["name"] = CommonHelper::qstringToUtf8(_name);
jsonObj["description"] = CommonHelper::qstringToUtf8(_strDescription);
jsonObj["createVersion"] = _wCreateVersion;
jsonObj["delVersion"] = _wDeleteVersion;
jsonObj["dataType"] = _wDataType;
jsonObj["defaultValue"] = CommonHelper::qstringToUtf8(_strDefaultValue);
jsonObj["isIndex"] = CommonHelper::intTobool(_wIsIndex);
jsonObj["isOnly"] = CommonHelper::intTobool(_wIsOnly);
jsonObj["isAutoincrement"] = CommonHelper::intTobool(_wIsAutoincrement);
jsonObj["maxLength"] = _wMaxLength;
jsonObj["minLength"] = _wMinLength;
jsonObj["numLength"] = _wNumLength;
jsonObj["numPresision"] = _wNumPresision;
jsonObj["isFindKey"] = CommonHelper::intTobool(_wIsFindKey);
jsonObj["fkType"] = _FKType;
jsonObj["fkTableID"] = _wFkTableID;
jsonObj["classType"] = g_TYPE_DATAATTRUBUTE;
jsonObj["innerName"] = CommonHelper::qstringToUtf8(_strInternalName);
//是否为主键
jsonObj["isPk"] = CommonHelper::intTobool(_isPrimaryKey);
if (_displayfield == "")
{
using namespace DBPlatformSpace;
M_NormalColumnDAO* pAtt = static_cast<M_NormalColumnDAO*>(_pDBDAO);
string name = pAtt->NameToDAOAttriName(pAtt->_name);
_displayfield = QString::fromStdString(name);
}
//[20230914]
jsonObj["field"] = CommonHelper::qstringToUtf8(_displayfield);
}
bool DataAttribute::saveSelf()
{
using namespace DBPlatformSpace;
ResultMsg rm;
/*将这条项目数据写入数据库*/
M_NormalColumnDAO* pDao = dynamic_cast<M_NormalColumnDAO*>(_pDBDAO);
/*备份DAO数据*/
M_NormalColumnDAO oldDaoData;
backupDAOData(*pDao, oldDaoData);
if (pDao == nullptr)
{
pDao = new M_NormalColumnDAO();
}
saveToDao();
rm = pDao->save();
if (rm.rCode == 0)
{
LOG(INFO) << CommonHelper::utf8ToStdString("M_NormalColumnDAO.save success ");
return true;
}
else
{
LOG(INFO) << CommonHelper::utf8ToStdString("M_NormalColumnDAO.save failed ");
LOG(ERROR) << rm.rMsg;
restoreData(oldDaoData, *this);
return false;
}
}
bool DataAttribute::deleteSelf()
{
using namespace DBPlatformSpace;
if (_pDBDAO)
{
DPData* pDataModel = getDataModelOwner();
M_EntityModelDAO* pModelDAO = dynamic_cast<M_EntityModelDAO*>(pDataModel->_pDBDAO);
M_NormalColumnDAO* pAttributeDao = dynamic_cast<M_NormalColumnDAO*>(_pDBDAO);
ResultMsg rm = pAttributeDao->delFromEntityModel(pModelDAO);
if (rm.rCode == 0)
{
LOG(INFO) << CommonHelper::utf8ToStdString("M_NormalColumnDAO.delFromEntityModel success ");
QString msg = "NormalColumn:" + QString::number(_id) + "--" + _name + "删除成功";
LOG(INFO) << CommonHelper::qstringToString(msg);
if (_parent)
{
_parent->deleteChild(_id);
return true;
}
else
{
LOG(INFO) << CommonHelper::utf8ToStdString("属性_parent = null");
//_parent为空
return false;
}
}
else
{
LOG(INFO) << CommonHelper::utf8ToStdString("M_NormalColumnDAO.delFromEntityModel failed ");
LOG(ERROR) << rm.rMsg;
//数据库删除失败
return false;
}
}
else
{
LOG(ERROR) << CommonHelper::utf8ToStdString("DataAttribute._pDBDAO = null ");
//_pDBDAO为空
return false;
}
}
void DataAttribute::loadData(DBPlatformSpace::DAO* pDao)
{
_pDBDAO = pDao;
DBPlatformSpace::M_NormalColumnDAO* pDAOData = dynamic_cast<DBPlatformSpace::M_NormalColumnDAO*>(_pDBDAO);
_id = pDAOData->_ID;
_name = CommonHelper::stringToQstring(pDAOData->_name);
_wCreateVersion = pDAOData->_addVersion;
_wDataType = pDAOData->_dataType;
_strDefaultValue = CommonHelper::stringToQstring(pDAOData->_defaultValue);
_wDeleteVersion = pDAOData->_delVersion;
_strDescription = CommonHelper::stringToQstring(pDAOData->_description);
_strDisplayName = CommonHelper::stringToQstring(pDAOData->_displayName);
_wFkTableID = pDAOData->_fkTableID;
_FKType = pDAOData->_fkType;
_strInternalName = CommonHelper::stringToQstring(pDAOData->_innerName);
// int _interfaceAttrTyp; ///<接口属性(需要添加专属接口的属性)类型
_wIsAutoincrement = pDAOData->_isAutoIncrement;
// _isDeleted 是否逻辑删除
_wIsFindKey = pDAOData->_isFindKey;
_wIsIndex = pDAOData->_isIndex;
_wIsOnly = pDAOData->_isOnly;
_wMaxLength = pDAOData->_maxLength;
_wMinLength = pDAOData->_minLength;
_wNumLength = pDAOData->_numLength;
_wNumPresision = pDAOData->_numPresision;
_isPrimaryKey = pDAOData->_isPk;
//_t_M_EntityTableID 所属数据类id
}
void DataAttribute::saveToDao()
{
using namespace DBPlatformSpace;
M_NormalColumnDAO* pDao = dynamic_cast<M_NormalColumnDAO*>(_pDBDAO);
if (pDao == nullptr)
{
pDao = new M_NormalColumnDAO();
_pDBDAO = pDao;
}
// pDao->_ID = getID();
//名称标识
pDao->_name = CommonHelper::qstringToStdString(getName());
//数据类型
pDao->_dataType = _wDataType;
pDao->_fkTableID = _wFkTableID;
pDao->_fkType = _FKType;
//默认值
pDao->_defaultValue = CommonHelper::qstringToStdString(_strDefaultValue);
//描述
pDao->_description = CommonHelper::qstringToStdString(_strDescription);
//显示名称
pDao->_displayName = CommonHelper::qstringToStdString(_strDisplayName);
//是否索引
pDao->_isIndex = _wIsIndex;
//是否唯一
pDao->_isOnly = _wIsOnly;
//是否自增
pDao->_isAutoIncrement = _wIsAutoincrement;
//最大长度
pDao->_maxLength = _wMaxLength;
//最小长度
pDao->_minLength = _wMinLength;
//数值位数
pDao->_numLength = _wNumLength;
//数值精度
pDao->_numPresision = _wNumPresision;
//是否为查找序列
pDao->_isFindKey = _wIsFindKey;
//是否为主键
pDao->_isPk = _isPrimaryKey;
}
//名称标识是否合法 不合法返回true
bool DataAttribute::illegalName( QString& errMsg)
{
//CR开头ID结尾给出不合理提示
if (_name.left(2) =="CR" && _name.right(2) == "ID" && _name.size() > 4)
{
errMsg = "因(规范化后)名称与关系属性命名规则冲突,建议重新定义: " + _name;
return true;
}
return false;
}
void DataAttribute::getNewProperty(json& parameter, QVariantMap& valueMap)
{
auto data = parameter["data"];
//显示名称
valueMap.insert("displayName", CommonHelper::utf8ToQString(data["label"]));
//名称标识
if (!data["name"].is_null())
{
valueMap.insert("name", CommonHelper::utf8ToQString(data["name"]));
}
//描述
valueMap.insert("description", CommonHelper::utf8ToQString(data["description"]));
//默认值
valueMap.insert("defaultValue", CommonHelper::utf8ToQString(data["defaultValue"]));
//数据类型
if (!data["dataType"].is_null())
{
valueMap.insert("dataType", (int)data["dataType"]);
}
//是否索引
valueMap.insert("isIndex", (int)data["isIndex"]);
//是否唯一
valueMap.insert("isOnly", (int)data["isOnly"]);
//是否自增
if (!data["isAutoincrement"].is_null())
{
valueMap.insert("isAutoIncrement", (int)data["isAutoincrement"]);
}
//最大长度
valueMap.insert("maxLen", (int)data["maxLength"]);
//最小长度
valueMap.insert("minLen", (int)data["minLength"]);
//数值位数
valueMap.insert("numLen", (int)data["numLength"]);
//数值精度
valueMap.insert("numPresision", (int)data["numPresision"]);
//是否为查找序列
valueMap.insert("isFindKey", (int)data["isFindKey"]);
//是否主键
valueMap.insert("isPK", (int)data["isPk"]);
}
void DataAttribute::setNewData(json& parameter)
{
QVariantMap newValues;
getNewProperty(parameter, newValues);
setProperties(newValues);
}
void DataAttribute::setFileData(FileGenerate* generator, QStringList flag)
{
if (flag.size() == 2)
{
generator->AttTableData.append(new CCS_Report::CCSModelDataSet(flag[0], flag[1], _strDisplayName, _strInternalName, CommonHelper::convertAttributeTypeTpString(_wDataType)));
}
else if (flag.size() == 3)
{
generator->AttTableData111.append(new CCS_Report::CCSModelDataSet(flag[0], flag[1], flag[2], _strDisplayName, _strInternalName, CommonHelper::convertAttributeTypeTpString(_wDataType)));
}
}
//合法 true. 不合法 false
bool DataAttribute::checkNameSize(int min, int max, QString& errMsg)
{
if (_name.size() >= min && _name.size() <= max)
{
return true;
}
errMsg = "数据类名称标识长度不合法:" + _name;
return false;
}
//判断第index+1位 是不是字母是返回true, 否返回false
bool DataAttribute::checkLetter(QString& errmsg, int index)
{
if (_name.at(index).isLetter())
{
return true;
}
errmsg = "数据类名称标识第" + QString::number(index + 1) + "位需为字母:" +_name;
return false;
}
//判断其它合法性, 全部为合法字符返回true,
bool DataAttribute::checkNameillegal(QString& errMsg)
{
bool foundmatch = false;
try {
std::regex re(R"(^[\w\(\)\[\]\\]+$)");
foundmatch = std::regex_search(_name.toStdString(), re);
}
catch (std::regex_error& e) {
// Syntax error in the regular expression
}
if (foundmatch)
{//合法
return true;
}
else
{
errMsg = "名称标识 " + _name + " 含无效字符";
return false;
}
return true; //合法
}