DataCollectionWidget.cpp 21 KB

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