DataCollectionWidget.cpp 20 KB

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