DataCollectionWidget.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include "DataCollectionWidget.h"
  2. #include "DataTableWidget.h"
  3. #include "ConfigExpertDataWidget.h"
  4. #include "ConfigMeasureDataWidget.h"
  5. #include "ConfigSchemeDataWidget.h"
  6. #include "CreateSchemeWidget.h"
  7. #include "EvalDataManager.h"
  8. #include "algorithm/HierarchicalAnalysis.h"
  9. #include "MindEvaluation.h"
  10. #include "GreyClusteringConfigWidget.h" // 灰色聚类配置
  11. #include "GreyClusteringSampleTable.h" // 灰色聚类评估
  12. #include "MatterElementConfigWidget.h" // 物元分析配置
  13. #include "dbService/SchemeProcessService.h"
  14. #include "dbService/ClassSet.h"
  15. #include "dbService/CNodeDataService.h"
  16. #include "dbService/NodeMatrixService.h"
  17. #include "dbService/UserService.h"
  18. #include "dbService/SchemeInfoService.h"
  19. #include <xlsxdocument.h>
  20. #include <CNode.h>
  21. #include <Widgets/Button.h>
  22. #include <Widgets/ComboBox.h>
  23. #include <QTabWidget>
  24. #include <QBoxLayout>
  25. #include <QMap>
  26. #include <QFileDialog>
  27. #include <QMessageBox>
  28. #include <QComboBox>
  29. #include <QDebug>
  30. DataCollectionWidget::DataCollectionWidget(ProjectInfo *proj, QWidget *parent) : EvalWidget(proj, parent)
  31. {
  32. setTitle("评估数据采集");
  33. m_configExpert = new ConfigExpertDataWidget(this);
  34. m_configMeasure = new ConfigMeasureDataWidget(this);
  35. m_configScheme = new ConfigSchemeDataWidget(this);
  36. m_configMeasure->setFixedWidth(256);
  37. m_configScheme->setFixedWidth(350);
  38. m_contentLayout->addWidget(m_configExpert);
  39. m_contentLayout->addWidget(m_configMeasure);
  40. m_contentLayout->addWidget(m_configScheme);
  41. m_configExpert->setHidden(true);
  42. m_configMeasure->setHidden(true);
  43. m_configScheme->setHidden(true);
  44. m_comboBox = new QComboBox(this);
  45. m_comboBox->addItems({ "结果集结", "矩阵集结" });
  46. m_comboBox->resize(50, 35);
  47. m_calcBtn = new PushButton("更新数据");
  48. m_topLayout->addStretch();
  49. m_topLayout->addWidget(m_comboBox);
  50. m_topLayout->addSpacing(10);
  51. m_topLayout->addWidget(m_calcBtn);
  52. m_addSchemeWidget = new CreateSchemeWidget(this);
  53. connect(m_calcBtn, &PushButton::clicked, this, &DataCollectionWidget::slotCalc);
  54. connect(m_tab, &QTabWidget::currentChanged, this, &DataCollectionWidget::slotTabCurrentChanged);
  55. connect(m_configExpert, &ConfigExpertDataWidget::sigImportData, this, &DataCollectionWidget::slotImportData);
  56. connect(m_configExpert, &ConfigExpertDataWidget::sigConfigSelected, this,
  57. &DataCollectionWidget::slotConfigSelected);
  58. connect(m_configScheme, &ConfigSchemeDataWidget::sigAddScheme, this, &DataCollectionWidget::slotAddScheme);
  59. connect(m_configMeasure, &ConfigMeasureDataWidget::sigAddData, this, &DataCollectionWidget::slotAddMeasureData);
  60. connect(m_configMeasure, &ConfigMeasureDataWidget::sigCurrentRowChanged, this,
  61. &DataCollectionWidget::slotCurrentMeasureDataSelected);
  62. connect(m_addSchemeWidget, &CreateSchemeWidget::sigSchemeInfoConfirmed, this,
  63. &DataCollectionWidget::slotAddSchemeInfo);
  64. }
  65. void DataCollectionWidget::setType(int type)
  66. {
  67. EvalWidget::setType(type);
  68. setupTabWidget();
  69. }
  70. ///
  71. /// \brief DataCollectionWidget::setupTabWidget
  72. /// 根据评估类型, 评估方案, 评估算法, 加载合适的数据导入界面
  73. void DataCollectionWidget::setupTabWidget()
  74. {
  75. m_tab->clear();
  76. QMap<int, QList<CNodeData>> nodeListMap;
  77. for (int i : indexList()) {
  78. // 获取指标体系数据
  79. QList<CNodeData> nodeList;
  80. bool ret = CNodeDataService().QueryAll(nodeList, m_proj->id, i);
  81. if (ret) {
  82. nodeListMap[i] = nodeList;
  83. } else {
  84. return;
  85. }
  86. // 获取方案规划数据
  87. QList<SchemePlanManager::SchemeProcessInfo> processList;
  88. ret = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_proj->id, i);
  89. if (ret == false) {
  90. return;
  91. }
  92. ProjectManager::IndexType t = (ProjectManager::IndexType)i;
  93. QString indexName = ProjectManager::nameOfIndexType(t);
  94. // 效能评估方案中导入评估数据的步骤, 选择物元分析法时使用
  95. SchemePlanManager::SchemeProcessInfo importEffiEvalDataProcess;
  96. for (SchemePlanManager::SchemeProcessInfo process : processList) {
  97. // 综合效能评估 - 灰色聚类法: 效能等级配置页面, 导入评估数据页面
  98. if (process.algorithm == SchemePlanManager::GCE && process.indexType == ProjectManager::EfficiencyIndex) {
  99. CMind *mind = new CMind(this);
  100. mind->setNodeList(nodeListMap[i]);
  101. GreyClusteringConfigWidget *gc = new GreyClusteringConfigWidget(mind, process.efficiencyGrades);
  102. m_tab->addTab(gc, indexName + " - " + "灰色聚类法效能等级配置");
  103. GreyClusteringSampleTable *gs = new GreyClusteringSampleTable(mind, process.efficiencyGrades);
  104. m_tab->addTab(gs, indexName + " - " + "收集效能评估数据");
  105. }
  106. // 综合效能评估 - 物元分析法: 效能等级配置页面, 导入评估数据页面
  107. if (process.algorithm == SchemePlanManager::MEA && process.indexType == ProjectManager::EfficiencyIndex) {
  108. QList<MEConfigItem> items;
  109. CMind *mind = new CMind(this);
  110. mind->setNodeList(nodeListMap[i]);
  111. MatterElementConfigWidget *mec = new MatterElementConfigWidget(mind, process.efficiencyGrades);
  112. m_tab->addTab(mec, indexName + "-" + "物元分析法效能等级配置");
  113. DataTableWidget *table = new DataTableWidget(importEffiEvalDataProcess, this);
  114. table->mind1()->setNodeList(nodeListMap[i]);
  115. table->setupModels();
  116. m_tab->addTab(table, indexName + " - " + "收集效能评估数据");
  117. connect(table, &DataTableWidget::sigMeasureDataEdited, this,
  118. &DataCollectionWidget::slotMeasureDataEdited);
  119. }
  120. // 导入效能评估的权重分析数据
  121. // 导入其他评估的权重分析数据和评估数据
  122. if (process.dSource >= 0) {
  123. if (process.type == SchemePlanManager::ImportEvalData
  124. && process.indexType == ProjectManager::EfficiencyIndex) {
  125. importEffiEvalDataProcess = process;
  126. continue;
  127. }
  128. DataTableWidget *table = new DataTableWidget(process, this);
  129. table->mind1()->setNodeList(nodeListMap[i]);
  130. if (i == ProjectManager::TechIndex) {
  131. table->mind2()->setNodeList(nodeListMap[ProjectManager::AbilityIndex]);
  132. }
  133. table->setupModels();
  134. QString processName = SchemePlanManager::processName(process);
  135. m_tab->addTab(table, indexName + " - " + processName);
  136. connect(table, &DataTableWidget::sigMeasureDataEdited, this,
  137. &DataCollectionWidget::slotMeasureDataEdited);
  138. }
  139. }
  140. }
  141. }
  142. void DataCollectionWidget::refreshSchemeData()
  143. {
  144. m_configScheme->loadData();
  145. }
  146. void DataCollectionWidget::slotTabCurrentChanged(int index)
  147. {
  148. DataTableWidget *table = dynamic_cast<DataTableWidget *>(m_tab->widget(index));
  149. if (index >= 0 && table != nullptr) {
  150. bool expert = (table->process().dSource == SchemePlanManager::FromExpert);
  151. if (expert) {
  152. m_configExpert->setProcess(table->process());
  153. }
  154. m_configExpert->setVisible(expert);
  155. bool scheme = (table->process().indexType != ProjectManager::TechIndex
  156. && table->process().type == SchemePlanManager::ImportEvalData);
  157. if (scheme) {
  158. m_configScheme->setProcess(table->process());
  159. }
  160. m_configScheme->setVisible(scheme);
  161. bool meaure = (table->process().dSource == SchemePlanManager::FromMeasurement) && !scheme;
  162. if (meaure) {
  163. m_configMeasure->setProcess(table->process());
  164. m_configMeasure->selectFirst();
  165. }
  166. m_configMeasure->setVisible(meaure);
  167. }
  168. if (table == nullptr) {
  169. m_configExpert->setHidden(true);
  170. m_configMeasure->setHidden(true);
  171. m_configScheme->setHidden(true);
  172. }
  173. }
  174. void DataCollectionWidget::slotCalc()
  175. {
  176. DataTableWidget *table = dynamic_cast<DataTableWidget *>(m_tab->currentWidget());
  177. if (table != nullptr) {
  178. QTableView *tableView = (QTableView *)table->tabWidget()->currentWidget();
  179. DataTableItemModel *model = (DataTableItemModel *)tableView->model();
  180. int schemeStart = -1; // 表中方案数据开始列
  181. if (table->process().indexType == ProjectManager::OptimalIndex) {
  182. schemeStart = 2;
  183. } else if (table->process().indexType == ProjectManager::EfficiencyIndex) {
  184. schemeStart = 1;
  185. }
  186. qDebug() << schemeStart;
  187. /// 存储方案数据
  188. /// @attention 缺少数据库更新接口
  189. if (model->columnCount() > schemeStart && schemeStart > 0) {
  190. QList<SchemaEval *> schemeList = m_configScheme->schemeList();
  191. for (SchemaEval *scheme : schemeList) {
  192. for (int i = schemeStart; i < model->columnCount(); i++) {
  193. QStandardItem *hHeader = model->horizontalHeaderItem(i);
  194. if (hHeader->text() != scheme->name) {
  195. continue;
  196. }
  197. QStringList list;
  198. for (int j = 0; j < model->rowCount(); j++) {
  199. QStandardItem *vHeader = model->verticalHeaderItem(j);
  200. QStandardItem *item = model->item(j, i);
  201. if (item != nullptr && item->text().trimmed().length() > 0) {
  202. QString value = QString("%1:%2").arg(vHeader->text()).arg(item->text());
  203. list.append(value);
  204. }
  205. }
  206. QString str = list.join(";");
  207. scheme->valueStr = str;
  208. qDebug() << scheme->id << scheme->name << str;
  209. }
  210. }
  211. }
  212. }
  213. GreyClusteringSampleTable *gcSample = dynamic_cast<GreyClusteringSampleTable *>(m_tab->currentWidget());
  214. if (gcSample != nullptr) {
  215. // gcSample->compute();
  216. }
  217. GreyClusteringConfigWidget *gcConfig = dynamic_cast<GreyClusteringConfigWidget *>(m_tab->currentWidget());
  218. if (gcConfig != nullptr) {
  219. qDebug() << __FUNCTION__ << __LINE__ << "gc" << endl;
  220. }
  221. }
  222. void DataCollectionWidget::slotImportData(UserConfig *config)
  223. {
  224. QFileDialog::Options options;
  225. options |= QFileDialog::DontUseNativeDialog;
  226. QString selectedFilter;
  227. QString fileName =
  228. QFileDialog::getOpenFileName(this, "选择文件", "", "文件(*.csv *.xls *.xlsx)", &selectedFilter, options);
  229. bool fileValid = true;
  230. if (fileName.isEmpty() || fileName.size() <= 0) {
  231. fileValid = false;
  232. return;
  233. }
  234. // 校验提醒
  235. QStringList strList = fileName.split("/");
  236. QStringList names = strList.at(strList.size() - 1).split("-");
  237. if (names.at(0) != config->userName) {
  238. QMessageBox::warning(this, tr("失败"), tr("请确认专家名称"));
  239. fileValid = false;
  240. return;
  241. }
  242. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  243. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  244. if (names.at(1) != indexName) {
  245. QMessageBox::warning(this, tr("失败"), tr("请确认工程类型"));
  246. fileValid = false;
  247. return;
  248. }
  249. if (names.at(2).split(".").at(0) != m_proj->projectName) {
  250. QMessageBox::warning(this, tr("失败"), tr("请确认工程名称"));
  251. fileValid = false;
  252. return;
  253. }
  254. // 读取文件内容
  255. QXlsx::Document xlsxR(fileName);
  256. QStringList str = xlsxR.sheetNames();
  257. qDebug() << __FUNCTION__ << __LINE__ << fileValid << str << endl;
  258. int size = 0;
  259. bool userinfo = false;
  260. if (str.contains("专家信息")) {
  261. size = str.size() - 1;
  262. userinfo = true;
  263. } else {
  264. size = str.size();
  265. }
  266. // 遍历sheet页
  267. QList<NodeMatrixInfo *> nodeMatrxInfoList;
  268. for (int i = 0; i < size; i++) {
  269. QString mark = "1";
  270. if (i != 0) {
  271. mark = "2";
  272. }
  273. xlsxR.selectSheet(str.at(i));
  274. if (xlsxR.load()) {
  275. int row = xlsxR.dimension().lastRow();
  276. int col = xlsxR.dimension().lastColumn();
  277. for (int r = 2; r <= row; r++) {
  278. for (int c = 2; c <= col; c++) {
  279. QString abscissa;
  280. QString ordinate;
  281. QString nodeValue;
  282. QXlsx::Cell *cellAbscissa = xlsxR.cellAt(r, 1);
  283. if (cellAbscissa != NULL) {
  284. QVariant var = cellAbscissa->readValue();
  285. ordinate = var.toString();
  286. }
  287. QXlsx::Cell *cellOrdinate = xlsxR.cellAt(1, c);
  288. if (cellOrdinate != NULL) {
  289. QVariant var = cellOrdinate->readValue();
  290. abscissa = var.toString();
  291. }
  292. QXlsx::Cell *value = xlsxR.cellAt(r, c);
  293. if (value != NULL) {
  294. QVariant var = value->readValue();
  295. nodeValue = var.toString();
  296. }
  297. NodeMatrixInfo *nodeInfo = new NodeMatrixInfo();
  298. nodeInfo->expertId = QString::number(config->userId);
  299. nodeInfo->expertName = config->userName;
  300. nodeInfo->abscissa = abscissa;
  301. nodeInfo->ordinate = ordinate;
  302. nodeInfo->nodeValue = nodeValue;
  303. nodeInfo->engineerId = config->engineerId;
  304. nodeInfo->mark = mark;
  305. nodeInfo->tableMsg = indexName;
  306. nodeInfo->mindId = table->process().dSource;
  307. nodeInfo->writeDate = QDateTime::currentDateTime();
  308. if (i != 0) {
  309. nodeInfo->node = "1." + QString::number(i) + "." + QString::number(r - 1);
  310. } else {
  311. nodeInfo->node = "1." + QString::number(r - 1);
  312. }
  313. nodeMatrxInfoList.append(nodeInfo);
  314. }
  315. }
  316. }
  317. }
  318. // 更新数据库
  319. if (NodeMatrixService().QueryNodeMatrixListByExpertIdAndEngineerId(QString::number(config->userId),
  320. config->engineerId, indexName)) {
  321. bool ret = NodeMatrixService().UpdateNodeMatrixNodeValueList(nodeMatrxInfoList);
  322. qDebug() << __FUNCTION__ << __LINE__ << "import data update" << ret << endl;
  323. } else {
  324. bool ret = NodeMatrixService().AddNodeMatrixInfoList(nodeMatrxInfoList);
  325. qDebug() << __FUNCTION__ << __LINE__ << "import data add" << ret << endl;
  326. }
  327. // 如果有专家信息更新用户
  328. if (userinfo) {
  329. xlsxR.selectSheet(str[size]);
  330. QFUser user;
  331. if (UserService().QueryUserInfoById(&user, xlsxR.cellAt(2, 1)->readValue().toInt())) {
  332. user.workPosition = xlsxR.cellAt(2, 3)->readValue().toString();
  333. user.post = xlsxR.cellAt(2, 4)->readValue().toString();
  334. user.major = xlsxR.cellAt(2, 5)->readValue().toString();
  335. user.phone = xlsxR.cellAt(2, 6)->readValue().toString();
  336. user.writeTime = xlsxR.cellAt(2, 7)->readValue().toString();
  337. user.remark = xlsxR.cellAt(2, 8)->readValue().isNull() ? "" : xlsxR.cellAt(2, 8)->readValue().toString();
  338. UserService().UpdateUserInfo(user);
  339. }
  340. }
  341. QMessageBox::information(this, tr("成功"), tr("数据保存成功"));
  342. }
  343. void DataCollectionWidget::slotConfigSelected(UserConfig *config)
  344. {
  345. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  346. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  347. // MindEvaluation *eval =
  348. // new MindEvaluation(table->mind1(), MindEvaluation::NoMerge, indexName, table->process().dSource);
  349. // eval->updateSeqNodes();
  350. // eval->computeWeights();
  351. // for (MindNodeItem item : eval->mindNodeWeights) {
  352. // qDebug() << __FUNCTION__ << __LINE__ << item.name << item.value << item.score << endl;
  353. // }
  354. QList<NodeMatrixInfo *> data;
  355. NodeMatrixService().QueryNodeMatrixListByExpertIdAndEngineerId(&data, config->userId, config->engineerId,
  356. indexName);
  357. table->setNodeMatrixData(data, true);
  358. }
  359. void DataCollectionWidget::slotAddScheme()
  360. {
  361. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  362. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  363. QString processName = SchemePlanManager::processName(table->process());
  364. // 判断是否效能评估数据导入
  365. bool isScheme = table->process().indexType == ProjectManager::EfficiencyIndex
  366. || table->process().indexType == ProjectManager::OptimalIndex;
  367. if (isScheme) {
  368. m_addSchemeWidget->show();
  369. }
  370. }
  371. void DataCollectionWidget::slotAddSchemeInfo(const QString name, const QString remark, const QString path)
  372. {
  373. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  374. int type = 0;
  375. if (table->process().indexType == ProjectManager::EfficiencyIndex) {
  376. type = 1;
  377. }
  378. SchemaEval *scheme = new SchemaEval();
  379. scheme->engineerId = m_proj->id;
  380. scheme->name = name;
  381. scheme->remark = remark;
  382. scheme->filePath = path;
  383. scheme->type = type;
  384. bool ret = SchemeInfoService().AddSchemeInfoList({ scheme });
  385. if (ret) {
  386. refreshSchemeData();
  387. table->addScheme(scheme);
  388. }
  389. }
  390. /// 添加实测数据
  391. void DataCollectionWidget::slotAddMeasureData()
  392. {
  393. static bool isBusing = false;
  394. if (isBusing) {
  395. return; // 添加完成后再响应下一次添加
  396. }
  397. isBusing = true;
  398. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  399. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  400. QString processName = SchemePlanManager::processName(table->process());
  401. QDateTime time = QDateTime::currentDateTime();
  402. QString uuid = QString("%1").arg(time.toMSecsSinceEpoch());
  403. QList<NodeMatrixInfo *> dataList;
  404. /// 权重数据
  405. /// 一种指标: 能力重要度评估指标, 或方案评估指标, 或效能评估指标
  406. /// 除根节点外, 每个指标一个权重值
  407. if (table->process().type == SchemePlanManager::ImportWeightData) {
  408. for (CNodeData n : table->mind1()->nodeList()) {
  409. if (n.number <= 0) {
  410. continue;
  411. }
  412. NodeMatrixInfo *info = new NodeMatrixInfo();
  413. info->engineerId = m_proj->id;
  414. info->mindId = 1;
  415. info->tableMsg = indexName;
  416. info->writeDate = time;
  417. info->strUuid = uuid;
  418. info->abscissa = n.name;
  419. info->nodeValue = "1";
  420. dataList.append(info);
  421. }
  422. }
  423. /// 评估数据
  424. /// 两种指标: 能力重要度评估指标, 技术措施重要度评估指标
  425. /// 每个技术措施指标, 对应能力重要度指标体系最后一层指标下的数据
  426. if (table->process().type == SchemePlanManager::ImportEvalData
  427. && table->process().indexType == ProjectManager::TechIndex) {
  428. QList<CNodeData> leaves = table->mind2()->leaves();
  429. for (CNodeData n1 : table->mind1()->nodeList()) {
  430. if (n1.number <= 0) {
  431. continue;
  432. }
  433. for (CNodeData n2 : leaves) {
  434. NodeMatrixInfo *info = new NodeMatrixInfo();
  435. info->engineerId = m_proj->id;
  436. info->mindId = 1;
  437. info->tableMsg = indexName;
  438. info->writeDate = time;
  439. info->strUuid = uuid;
  440. info->abscissa = n1.name;
  441. info->ordinate = n2.name;
  442. info->nodeValue = "1";
  443. dataList.append(info);
  444. }
  445. }
  446. }
  447. if (dataList.size() > 0) {
  448. bool ret = NodeMatrixService().AddNodeMatrixInfoList(dataList);
  449. qDebug() << __FUNCTION__ << __LINE__ << ret << dataList.size() << endl;
  450. if (ret) {
  451. m_configMeasure->reloadData();
  452. m_configMeasure->selectLast();
  453. }
  454. }
  455. isBusing = false;
  456. }
  457. void DataCollectionWidget::slotCurrentMeasureDataSelected()
  458. {
  459. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  460. table->setNodeMatrixData(m_configMeasure->selectedData(), false);
  461. }
  462. void DataCollectionWidget::slotMeasureDataEdited(NodeMatrixInfo *info)
  463. {
  464. m_configMeasure->updateNodeValue(info);
  465. }