592 lines
18 KiB
C++
592 lines
18 KiB
C++
#ifndef CCSDATABIND_H
|
|
#define CCSDATABIND_H
|
|
#include <QList>
|
|
#include <QMetaProperty>
|
|
#include "reportdataset.h"
|
|
|
|
#if(QT_VERSION_MAJOR == 6)
|
|
#include<QRegularExpression>
|
|
#endif
|
|
namespace CCS_Report {
|
|
//extern QHash<QString,QList<QObject*>> DataSets;
|
|
|
|
class CCSDataBind
|
|
{
|
|
public:
|
|
|
|
static QString bingdataT(QString bindSource,QList<QPair<QString,QString>> filter)
|
|
{
|
|
if (IsDataSourceField(bindSource))
|
|
{
|
|
QPair<QString,QString> pairField = ParserField(bindSource);
|
|
auto find_index = DataSets.find(pairField.first);
|
|
QList<QObject*> a = DataSets[pairField.first];
|
|
if(find_index != DataSets.end())
|
|
{
|
|
foreach (QObject* item ,find_index.value())
|
|
{
|
|
if (Filter(item,filter))
|
|
{
|
|
int index = item->metaObject()->indexOfProperty(pairField.second.toStdString().c_str());
|
|
if (index >=0)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
return metaProp.read(item).toString();
|
|
/*QString strValue = metaProp.read(item).toString();
|
|
qDebug() << strValue.isEmpty() << strValue.isNull();
|
|
return strValue;*/
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
/*
|
|
* 实现简单的动态表格,根据表和字段获取某个字段下的多条记录,
|
|
*/
|
|
template<typename T>
|
|
static QList<QString> bingdataDataset(QString bindSource,QList<T*> t,QPair<QString,QString> key)
|
|
{
|
|
QList<QString> listRtn;
|
|
if (IsDataSourceField(bindSource))
|
|
{
|
|
QList<QString> listName = ParserField(bindSource);
|
|
|
|
foreach (T* item,t) {
|
|
int index = item->metaObject()->indexOfProperty(listName[1].toStdString().c_str());
|
|
if (index >=0)
|
|
{
|
|
if (key.first != "")
|
|
{
|
|
int index1 = item->metaObject()->indexOfProperty(key.first.toStdString().c_str());
|
|
if (index1 >=0)
|
|
{
|
|
QMetaProperty metaProp1 = item->metaObject()->property(index1);
|
|
QString strCmp = metaProp1.read(item).toString();
|
|
if (!(strCmp == key.second))
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
listRtn.append(metaProp.read(item).toString());
|
|
}
|
|
}
|
|
}
|
|
return listRtn;
|
|
}
|
|
|
|
static bool IsDataSourceField(QString fieldS)
|
|
{
|
|
#if(QT_VERSION_MAJOR == 6)
|
|
QRegularExpression rx("\\$[F]\\{*.*\\}");
|
|
QRegularExpressionMatch match = rx.match(fieldS);
|
|
if (match.hasMatch())
|
|
{
|
|
return true;
|
|
}
|
|
#else
|
|
QRegExp rx("\\$[F]\\{*.*\\}");
|
|
int rtn = rx.indexIn(fieldS);
|
|
if (rtn >= 0)
|
|
{
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
static bool IsKeyField(QString fieldS)
|
|
{
|
|
#if(QT_VERSION_MAJOR == 6)
|
|
QRegularExpression rx("\\$[D]\\{.*\\}");
|
|
QRegularExpressionMatch match = rx.match(fieldS);
|
|
if (match.hasMatch())
|
|
{
|
|
return true;
|
|
}
|
|
#else
|
|
QRegExp rx("\\$[D]\\{.*\\}");
|
|
int rtn = rx.indexIn(fieldS);
|
|
if (rtn >= 0)
|
|
{
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
static QString ParserKeyField(QString KeyString,int& iStart,int& iEnd)
|
|
{
|
|
QString left = "{";
|
|
QString right = "}";
|
|
QString field = "";
|
|
|
|
iStart = KeyString.indexOf(left);
|
|
iEnd = KeyString.indexOf(right);
|
|
if ((iStart > 0) && (iEnd >0) && (iEnd > iStart))
|
|
{
|
|
field = KeyString.mid(iStart+1,iEnd-iStart-1);
|
|
}
|
|
|
|
return field;
|
|
}
|
|
static void ParserKeyFields(QString KeyString,QList<QString>&listFields, QList<QPair<int,int>>& listPostion)
|
|
{
|
|
QString left = "{";
|
|
QString right = "}";
|
|
QString strValue = KeyString;
|
|
|
|
int iInialEnd = 0;
|
|
while (true)
|
|
{
|
|
int iStart = strValue.indexOf(left);
|
|
int iEnd = strValue.indexOf(right);
|
|
if ((iStart > 0) && (iEnd > 0) && (iEnd > iStart))
|
|
{
|
|
QString field = strValue.mid(iStart + 1, iEnd - iStart - 1);
|
|
if (field.length() > 0)
|
|
{
|
|
QPair<int, int> pairPositon;
|
|
pairPositon.first = iStart + iInialEnd; pairPositon.second = iEnd+iInialEnd;
|
|
listPostion.append(pairPositon);
|
|
listFields.append(field);
|
|
iInialEnd += iEnd + 1;
|
|
}
|
|
strValue = strValue.mid(iEnd+1);
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
static QPair<QString,QString> ParserField(QString fieldS)
|
|
{
|
|
QPair<QString,QString> pairFieldInfo;
|
|
QString left = "{";
|
|
QString right = "}";
|
|
|
|
int first = fieldS.indexOf(left);
|
|
int end = fieldS.indexOf(right);
|
|
//if ((first + 1) > (end - first - 1))
|
|
//{
|
|
// int i=0;
|
|
//}
|
|
QString field = fieldS.mid(first+1,end-first-1);
|
|
int dot = field.indexOf(".");
|
|
if (dot > 0){
|
|
pairFieldInfo.first = field.mid(0,dot);
|
|
pairFieldInfo.second = field.mid(dot+1);
|
|
}
|
|
return pairFieldInfo;
|
|
}
|
|
template<typename T>
|
|
static bool Filter(T* item,QList<QPair<QString,QString>>& filter)
|
|
{
|
|
bool bRecord = true;
|
|
for(int i=0;i<filter.count();i++){
|
|
QPair<QString,QString> pairfilter = filter.at(i);
|
|
int index = item->metaObject()->indexOfProperty(pairfilter.first.toStdString().c_str());
|
|
if (index >=0)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
QString strMatchValue =metaProp.read(item).toString();
|
|
if (strMatchValue != pairfilter.second)
|
|
{
|
|
bRecord = false;
|
|
break;
|
|
}
|
|
}else{
|
|
bRecord = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return bRecord;
|
|
}
|
|
|
|
template<typename T>
|
|
static QList<QString> GroupByT(QString field,QList<T*> t,QList<QPair<QString,QString>> filter)
|
|
{
|
|
QList<QString> listRtn;
|
|
QHash<QString,int> hashValue;
|
|
bool bRecord;
|
|
|
|
foreach (T* item, t) {
|
|
bRecord = Filter(item,filter);
|
|
if (bRecord)
|
|
{
|
|
int index = item->metaObject()->indexOfProperty(field.toStdString().c_str());
|
|
if (index >=0)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
QString tmp = metaProp.read(item).toString();
|
|
|
|
if (!(hashValue.contains(tmp)))
|
|
{
|
|
|
|
listRtn.append(tmp);
|
|
hashValue.insert(tmp,0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return listRtn;
|
|
}
|
|
template<typename T>
|
|
static QList<QStringList> GroupByT(QStringList field, QList<T*> t, QList<QPair<QString, QString>> filter)
|
|
{
|
|
QList<QStringList> listRtn;
|
|
QList<QString> listFieldValue;
|
|
QHash<QString, int> hashValue;
|
|
bool bRecord;
|
|
QString strValue = "";
|
|
QList<int> listIndex;
|
|
QStringList listValue;
|
|
|
|
foreach(T * item, t) {
|
|
bRecord = Filter(item, filter);
|
|
if (bRecord)
|
|
{
|
|
strValue = "";
|
|
listValue.clear();
|
|
listIndex.clear();
|
|
for (auto f : field)
|
|
{
|
|
int index = item->metaObject()->indexOfProperty(f.toStdString().c_str());
|
|
if (index >= 0)
|
|
{
|
|
listIndex.append(index);
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
QString strValue1 = metaProp.read(item).toString();
|
|
strValue += strValue1;
|
|
strValue += ",";
|
|
listValue.append(strValue1);
|
|
}
|
|
}
|
|
if (listIndex.count() != field.count())
|
|
continue;
|
|
|
|
if ((!listFieldValue.contains(strValue)))
|
|
{
|
|
listFieldValue.append(strValue);
|
|
listRtn.append(listValue);
|
|
}
|
|
}
|
|
}
|
|
return listRtn;
|
|
}
|
|
//此方法实现动态列的数据,从三维变成二维展示的过程
|
|
/*
|
|
* A B C D A B c1 c2 c3
|
|
* a1 b1 c1 d1 a1 b1 d1 d2 d3
|
|
* a1 b1 c2 d2 ====>
|
|
* a1 b1 c3 d3
|
|
*
|
|
*/
|
|
|
|
///
|
|
/// dynamicField 动态表头列 dynamicValue动态内容列 dyanmicValue非动态列 t 表 key groupID
|
|
///
|
|
template<typename T>
|
|
static QMap<QString,QMap<QString,QString>> TransDynamicColumnDataT(QString dynamicField,QString dynamicValue,
|
|
QList<QString> noDynamicField,QList<T*> t,QList<QPair<QString,QString>> filter)
|
|
{
|
|
//if (transField == "" || varField =="") return NULL;
|
|
|
|
int iCount = 1;
|
|
QString compareRecord;
|
|
QString tranField;
|
|
QString value;
|
|
QHash<QString,QString> hashRecordIdentify;
|
|
QMap<QString,QMap<QString,QString>> listTranslatedData;
|
|
|
|
QString strRecordIdentify =GetFormatName(iCount++,5); //考虑QMap中的排序的问题
|
|
|
|
foreach (T* item, t) {
|
|
bool bRecord = Filter(item,filter);
|
|
if (!(bRecord))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
tranField="";
|
|
value="";
|
|
compareRecord="";
|
|
QMap<QString,QString> hashRecord;
|
|
for (int i = 0; i < item->metaObject()->propertyCount(); ++i) {
|
|
QMetaProperty metaProp = item->metaObject()->property(i);
|
|
QString strField = metaProp.name(); //字段名
|
|
QString strValue = metaProp.read(item).toString(); //字段值
|
|
//判断此字段是否在非动态内容中
|
|
if (noDynamicField.contains(strField))
|
|
{
|
|
//判断非动态行中是否有此记录,作为唯一标识
|
|
if (!(hashRecord.contains(strField)))
|
|
{
|
|
hashRecord.insert(strField,strValue);
|
|
compareRecord += strValue+"&";
|
|
}
|
|
}else if (strField==dynamicField)
|
|
{
|
|
//因为不能确定哪个字段先出现,所以先获取此值,等读取完这条记录后,才进行确定
|
|
tranField = strValue;
|
|
}else if (strField==dynamicValue)
|
|
{
|
|
value = strValue;
|
|
}
|
|
}
|
|
|
|
if (!(hashRecord.contains(tranField)))
|
|
{
|
|
hashRecord.insert(tranField,value);
|
|
}
|
|
if (!(hashRecordIdentify.contains(compareRecord)))
|
|
{
|
|
hashRecordIdentify.insert(compareRecord,strRecordIdentify);
|
|
strRecordIdentify =GetFormatName(iCount++,5);
|
|
}
|
|
|
|
QString strKey = strRecordIdentify+compareRecord;
|
|
if (listTranslatedData.contains(strKey))
|
|
{
|
|
listTranslatedData[strKey].insert(tranField,value);
|
|
}else
|
|
{
|
|
listTranslatedData.insert(strKey,hashRecord);
|
|
}
|
|
}
|
|
return listTranslatedData;
|
|
}
|
|
///
|
|
/// 根据父表的键值获取对应子表的数据
|
|
///
|
|
template<typename T>
|
|
static QList<T*> ChildrenDataByParent(QList<QPair<QString,QString>> listParent,QList<T*> t,QList<QPair<QString,QString>> filter)
|
|
{
|
|
QList<T*> listFilterData;
|
|
bool bFlag = true;
|
|
QList<QPair<QString,QString>> listFilter;
|
|
listFilter = listParent;
|
|
listFilter.append(filter);
|
|
foreach (T* item,t) {
|
|
bFlag = Filter(item,listFilter);
|
|
|
|
if (bFlag)
|
|
{
|
|
//有对应的记录
|
|
listFilterData.append(item);
|
|
}
|
|
}
|
|
return listFilterData;
|
|
}
|
|
template<typename T>
|
|
static QMap<int,QList<T*>> ChildrenDataByParent(QList<QString> listParentValue,QString subKey, QList<T*> t, QList<QPair<QString, QString>> filter)
|
|
{
|
|
QMap<int, QList<T*>> mapSubData;
|
|
QList<QPair<QString, QString>> listKeyValues;
|
|
QPair<QString, QString> pairKeyValue;
|
|
for(int i=0;i<listParentValue.count();i++)
|
|
{
|
|
listKeyValues.clear();
|
|
|
|
pairKeyValue.first = subKey;
|
|
pairKeyValue.second = listParentValue.at(i);
|
|
listKeyValues.append(pairKeyValue);
|
|
|
|
QList<QObject*> listSubTableData = ChildrenDataByParent(listKeyValues, t, filter);
|
|
mapSubData.insert(i, listSubTableData);
|
|
}
|
|
return mapSubData;
|
|
}
|
|
template<typename T>
|
|
static QMap<int, QList<T*>> ChildrenDataByParent(QList<QList<QString>> listParentValue, QList<QString> subKey, QList<T*> t, QList<QPair<QString, QString>> filter)
|
|
{
|
|
QMap<int, QList<T*>> mapSubData;
|
|
QList<QPair<QString, QString>> listKeyValues;
|
|
QPair<QString, QString> pairKeyValue;
|
|
for(int i=0;i<listParentValue.count();i++)
|
|
{
|
|
listKeyValues.clear();
|
|
for (int j = 0; j < listParentValue.at(i).count(); j++)
|
|
{
|
|
pairKeyValue.first = subKey.at(j);
|
|
pairKeyValue.second = listParentValue.at(i).at(j);
|
|
listKeyValues.append(pairKeyValue);
|
|
}
|
|
QList<QObject*> listSubTableData = ChildrenDataByParent(listKeyValues, t, filter);
|
|
mapSubData.insert(i, listSubTableData);
|
|
}
|
|
|
|
return mapSubData;
|
|
}
|
|
|
|
///
|
|
/// 根据父表的键值获取对应子表的数据
|
|
///
|
|
template<typename T>
|
|
static int GetRecordNum(QList<T*> t,QPair<QString,QString> key)
|
|
{
|
|
int iCount = 0;
|
|
if (key.first == "")
|
|
{
|
|
return t.count();
|
|
}
|
|
foreach (T* item,t) {
|
|
int index = item->metaObject()->indexOfProperty(key.first.toStdString().c_str());
|
|
if (index >=0)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
QString strValue = metaProp.read(item).toString();
|
|
if ((strValue == key.second))
|
|
{
|
|
iCount++;
|
|
}
|
|
}
|
|
}
|
|
return iCount;
|
|
}
|
|
|
|
|
|
static QString GetFormatName(int num,int digital)
|
|
{
|
|
QString format= "%0"+QString::number(digital)+"d";
|
|
#if(QT_VERSION_MAJOR == 6)
|
|
QString strFormat = QString::number(num).asprintf(format.toStdString().c_str(), num); //考虑QMap中的排序的问题
|
|
return strFormat;
|
|
#else
|
|
QString strFormat = QString::number(num).sprintf(format.toStdString().c_str(), num); //考虑QMap中的排序的问题
|
|
return strFormat;
|
|
#endif
|
|
}
|
|
template<typename T>
|
|
static bool BelongRecord(T* item,QPair<QString,QString> key)
|
|
{
|
|
QString strMatchValue ;
|
|
if (key.first != "")
|
|
{
|
|
int iMatchindex = item->metaObject()->indexOfProperty(key.first.toStdString().c_str());
|
|
if (iMatchindex >=0)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(iMatchindex);
|
|
strMatchValue = metaProp.read(item).toString();
|
|
if (!(strMatchValue == key.second))
|
|
{
|
|
return false;
|
|
}
|
|
}else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
template<typename T>
|
|
static QList<T*> FilterDataT(QList<T*> t,QList<QPair<QString,QString>> filter)
|
|
{
|
|
QList<T*> filteredTable;
|
|
bool bRecord;
|
|
if (filter.count() == 0) return t;
|
|
|
|
foreach (T* item,t) {
|
|
bRecord = Filter(item,filter);
|
|
|
|
if (bRecord)
|
|
{
|
|
filteredTable.append(item);
|
|
}
|
|
}
|
|
return filteredTable;
|
|
}
|
|
//template<typename T>
|
|
//static QList<QString> GetDataByFieldT(QList<T*> t,QString field,QList<QPair<QString,QString>> filter)
|
|
//{
|
|
// QList<QString> fieldValue;
|
|
// bool bRecord;
|
|
// foreach (T* item,t) {
|
|
// bRecord = true;
|
|
// foreach (QPair<QString,QString> item1, filter) {
|
|
// int index = item->metaObject()->indexOfProperty(item1.first.toStdString().c_str());
|
|
// if (index >=0)
|
|
// {
|
|
// QMetaProperty metaProp = item->metaObject()->property(index);
|
|
// QString strMatchValue =metaProp.read(item).toString();
|
|
// if (strMatchValue != item1.second)
|
|
// {
|
|
// bRecord = false;
|
|
// }
|
|
// }else{bRecord = false;}
|
|
// }
|
|
// if (bRecord)
|
|
// {
|
|
// int index = item->metaObject()->indexOfProperty(field.toStdString().c_str());
|
|
// if (index >=0)
|
|
// {
|
|
// QMetaProperty metaProp = item->metaObject()->property(index);
|
|
// QString strValue =metaProp.read(item).toString();
|
|
// fieldValue.append(strValue);
|
|
// }
|
|
// }
|
|
// }
|
|
// return fieldValue;
|
|
//}
|
|
template<typename T>
|
|
static QList<QString> GetDataByFieldT(QList<T*> t,QString field)
|
|
{
|
|
QList<QString> fieldValue;
|
|
foreach (T* item,t) {
|
|
QString strValue = GetValueByFieldT(item,field);
|
|
fieldValue.append(strValue);
|
|
}
|
|
return fieldValue;
|
|
}
|
|
template<typename T>
|
|
static QString GetValueByFieldT(T* item,QString field)
|
|
{
|
|
QString fieldValue="";
|
|
int index = item->metaObject()->indexOfProperty(field.toStdString().c_str());
|
|
if (index >=0)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(index);
|
|
fieldValue =metaProp.read(item).toString();
|
|
}
|
|
return fieldValue;
|
|
}
|
|
template<typename T>
|
|
static QList<QList<QString>> GetDataByFieldT(QList<T*> t, QList<QString> fields)
|
|
{
|
|
QList<QList<QString>> fieldValues;
|
|
foreach(T * item, t) {
|
|
|
|
QList<QString> listValues = GetValueByFieldT(item, fields);
|
|
if (listValues.count() > 0)
|
|
fieldValues.append(GetValueByFieldT(item, fields));
|
|
}
|
|
return fieldValues;
|
|
}
|
|
template<typename T>
|
|
static QList<QString> GetValueByFieldT(T* item, QList<QString> fields)
|
|
{
|
|
QList<QString> listFieldValue;
|
|
QList<int> listIndex;
|
|
for (auto o : fields)
|
|
{
|
|
int index = item->metaObject()->indexOfProperty(o.toStdString().c_str());
|
|
if (index <= 0) return listFieldValue;
|
|
listIndex.append(index);
|
|
}
|
|
for (auto o : listIndex)
|
|
{
|
|
QMetaProperty metaProp = item->metaObject()->property(o);
|
|
listFieldValue.append(metaProp.read(item).toString());
|
|
}
|
|
return listFieldValue;
|
|
}
|
|
};
|
|
}
|
|
#endif // CCSDATABIND_H
|