DataCollectionWidget.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. /// 运行评估算法
  192. m_evaluator->setProcess(table->process());
  193. m_evaluator->setGatherType((DataEvaluator::GatherType)m_comboBox->currentIndex());
  194. m_evaluator->evaluate();
  195. }
  196. GreyClusteringSampleTable *gcSample = dynamic_cast<GreyClusteringSampleTable *>(m_tab->currentWidget());
  197. if (gcSample != nullptr) {
  198. gcSample->compute();
  199. }
  200. GreyClusteringConfigWidget *gcConfig = dynamic_cast<GreyClusteringConfigWidget *>(m_tab->currentWidget());
  201. if (gcConfig != nullptr) {
  202. qDebug() << __FUNCTION__ << __LINE__ << "gc" << endl;
  203. }
  204. }
  205. void DataCollectionWidget::slotImportData(UserConfig *config)
  206. {
  207. QFileDialog::Options options;
  208. options |= QFileDialog::DontUseNativeDialog;
  209. QString selectedFilter;
  210. QString fileName =
  211. QFileDialog::getOpenFileName(this, "选择文件", "", "文件(*.csv *.xls *.xlsx)", &selectedFilter, options);
  212. bool fileValid = true;
  213. if (fileName.isEmpty() || fileName.size() <= 0) {
  214. fileValid = false;
  215. return;
  216. }
  217. // 校验提醒
  218. QStringList strList = fileName.split("/");
  219. QStringList names = strList.at(strList.size() - 1).split("-");
  220. if (names.at(0) != config->userName) {
  221. QMessageBox::warning(this, tr("失败"), tr("请确认专家名称"));
  222. fileValid = false;
  223. return;
  224. }
  225. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  226. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  227. if (names.at(1) != indexName) {
  228. QMessageBox::warning(this, tr("失败"), tr("请确认工程类型"));
  229. fileValid = false;
  230. return;
  231. }
  232. if (names.at(2).split(".").at(0) != m_proj->projectName) {
  233. QMessageBox::warning(this, tr("失败"), tr("请确认工程名称"));
  234. fileValid = false;
  235. return;
  236. }
  237. // 读取文件内容
  238. QXlsx::Document xlsxR(fileName);
  239. QStringList str = xlsxR.sheetNames();
  240. qDebug() << __FUNCTION__ << __LINE__ << fileValid << str << endl;
  241. int size = 0;
  242. bool userinfo = false;
  243. if (str.contains("专家信息")) {
  244. size = str.size() - 1;
  245. userinfo = true;
  246. } else {
  247. size = str.size();
  248. }
  249. // 遍历sheet页
  250. QList<NodeMatrixInfo *> nodeMatrxInfoList;
  251. for (int i = 0; i < size; i++) {
  252. QString mark = "1";
  253. if (i != 0) {
  254. mark = "2";
  255. }
  256. xlsxR.selectSheet(str.at(i));
  257. if (xlsxR.load()) {
  258. int row = xlsxR.dimension().lastRow();
  259. int col = xlsxR.dimension().lastColumn();
  260. for (int r = 2; r <= row; r++) {
  261. for (int c = 2; c <= col; c++) {
  262. QString abscissa;
  263. QString ordinate;
  264. QString nodeValue;
  265. QXlsx::Cell *cellAbscissa = xlsxR.cellAt(r, 1);
  266. if (cellAbscissa != NULL) {
  267. QVariant var = cellAbscissa->readValue();
  268. ordinate = var.toString();
  269. }
  270. QXlsx::Cell *cellOrdinate = xlsxR.cellAt(1, c);
  271. if (cellOrdinate != NULL) {
  272. QVariant var = cellOrdinate->readValue();
  273. abscissa = var.toString();
  274. }
  275. QXlsx::Cell *value = xlsxR.cellAt(r, c);
  276. if (value != NULL) {
  277. QVariant var = value->readValue();
  278. nodeValue = var.toString();
  279. }
  280. NodeMatrixInfo *nodeInfo = new NodeMatrixInfo();
  281. nodeInfo->expertId = QString::number(config->userId);
  282. nodeInfo->expertName = config->userName;
  283. nodeInfo->abscissa = abscissa;
  284. nodeInfo->ordinate = ordinate;
  285. nodeInfo->nodeValue = nodeValue;
  286. nodeInfo->engineerId = config->engineerId;
  287. nodeInfo->mark = mark;
  288. nodeInfo->tableMsg = indexName;
  289. nodeInfo->mindId = table->process().dSource;
  290. nodeInfo->writeDate = QDateTime::currentDateTime();
  291. if (i != 0) {
  292. nodeInfo->node = "1." + QString::number(i) + "." + QString::number(r - 1);
  293. } else {
  294. nodeInfo->node = "1." + QString::number(r - 1);
  295. }
  296. nodeMatrxInfoList.append(nodeInfo);
  297. }
  298. }
  299. }
  300. }
  301. // 更新数据库
  302. if (NodeMatrixService().QueryNodeMatrixListByExpertIdAndEngineerId(QString::number(config->userId),
  303. config->engineerId, indexName)) {
  304. bool ret = NodeMatrixService().UpdateNodeMatrixNodeValueList(nodeMatrxInfoList);
  305. qDebug() << __FUNCTION__ << __LINE__ << "import data update" << ret << endl;
  306. } else {
  307. bool ret = NodeMatrixService().AddNodeMatrixInfoList(nodeMatrxInfoList);
  308. qDebug() << __FUNCTION__ << __LINE__ << "import data add" << ret << endl;
  309. }
  310. // 如果有专家信息更新用户
  311. if (userinfo) {
  312. xlsxR.selectSheet(str[size]);
  313. QFUser user;
  314. if (UserService().QueryUserInfoById(&user, xlsxR.cellAt(2, 1)->readValue().toInt())) {
  315. user.workPosition = xlsxR.cellAt(2, 3)->readValue().toString();
  316. user.post = xlsxR.cellAt(2, 4)->readValue().toString();
  317. user.major = xlsxR.cellAt(2, 5)->readValue().toString();
  318. user.phone = xlsxR.cellAt(2, 6)->readValue().toString();
  319. user.writeTime = xlsxR.cellAt(2, 7)->readValue().toString();
  320. user.remark = xlsxR.cellAt(2, 8)->readValue().isNull() ? "" : xlsxR.cellAt(2, 8)->readValue().toString();
  321. UserService().UpdateUserInfo(user);
  322. }
  323. }
  324. QMessageBox::information(this, tr("成功"), tr("数据保存成功"));
  325. m_configExpert->loadNodeData();
  326. m_configExpert->selectConfig(config);
  327. }
  328. void DataCollectionWidget::slotConfigSelectionChanged()
  329. {
  330. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  331. table->setNodeMatrixData(m_configExpert->selectedData(), true);
  332. }
  333. void DataCollectionWidget::slotAddScheme()
  334. {
  335. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  336. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  337. QString processName = SchemePlanManager::processName(table->process());
  338. // 判断是否效能评估数据导入
  339. bool isScheme = table->process().indexType == ProjectManager::EfficiencyIndex
  340. || table->process().indexType == ProjectManager::OptimalIndex;
  341. if (isScheme) {
  342. m_addSchemeWidget->show();
  343. }
  344. }
  345. void DataCollectionWidget::slotDeleteScheme()
  346. {
  347. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  348. table->setupModels();
  349. }
  350. void DataCollectionWidget::slotAddSchemeInfo(const QString name, const QString remark, const QString path)
  351. {
  352. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  353. int type = 0;
  354. if (table->process().indexType == ProjectManager::EfficiencyIndex) {
  355. type = 1;
  356. }
  357. SchemaEval *scheme = new SchemaEval();
  358. scheme->engineerId = m_proj->id;
  359. scheme->name = name;
  360. scheme->remark = remark;
  361. scheme->filePath = path;
  362. scheme->type = type;
  363. bool ret = SchemeInfoService().AddSchemeInfoList({ scheme });
  364. if (ret) {
  365. m_configScheme->loadData();
  366. table->setupModels();
  367. m_configScheme->selectLast();
  368. }
  369. }
  370. /// 添加实测数据
  371. void DataCollectionWidget::slotAddMeasureData()
  372. {
  373. static bool isBusing = false;
  374. if (isBusing) {
  375. return; // 添加完成后再响应下一次添加
  376. }
  377. isBusing = true;
  378. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  379. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)table->process().indexType);
  380. QString processName = SchemePlanManager::processName(table->process());
  381. QDateTime time = QDateTime::currentDateTime();
  382. QString uuid = QString("%1").arg(time.toMSecsSinceEpoch());
  383. QList<NodeMatrixInfo *> dataList;
  384. /// 权重数据
  385. /// 一种指标: 能力重要度评估指标, 或方案评估指标, 或效能评估指标
  386. /// 除根节点外, 每个指标一个权重值
  387. if (table->process().type == SchemePlanManager::ImportWeightData) {
  388. for (CNodeData n : table->mind1()->nodeList()) {
  389. if (n.number <= 0) {
  390. continue;
  391. }
  392. NodeMatrixInfo *info = new NodeMatrixInfo();
  393. info->engineerId = m_proj->id;
  394. info->mindId = 1;
  395. info->tableMsg = indexName;
  396. info->writeDate = time;
  397. info->strUuid = uuid;
  398. info->abscissa = n.name;
  399. info->nodeValue = "0";
  400. dataList.append(info);
  401. }
  402. }
  403. /// 评估数据
  404. /// 两种指标: 能力重要度评估指标, 技术措施重要度评估指标
  405. /// 每个技术措施指标, 对应能力重要度指标体系最后一层指标下的数据
  406. if (table->process().type == SchemePlanManager::ImportEvalData
  407. && table->process().indexType == ProjectManager::TechIndex) {
  408. QList<CNodeData> leaves = table->mind2()->leaves();
  409. for (CNodeData n1 : table->mind1()->nodeList()) {
  410. if (n1.number <= 0) {
  411. continue;
  412. }
  413. for (CNodeData n2 : leaves) {
  414. NodeMatrixInfo *info = new NodeMatrixInfo();
  415. info->engineerId = m_proj->id;
  416. info->mindId = 1;
  417. info->tableMsg = indexName;
  418. info->writeDate = time;
  419. info->strUuid = uuid;
  420. info->abscissa = n1.name;
  421. info->ordinate = n2.name;
  422. info->nodeValue = "1";
  423. dataList.append(info);
  424. }
  425. }
  426. }
  427. if (dataList.size() > 0) {
  428. bool ret = NodeMatrixService().AddNodeMatrixInfoList(dataList);
  429. qDebug() << __FUNCTION__ << __LINE__ << ret << dataList.size() << endl;
  430. if (ret) {
  431. m_configMeasure->reloadData();
  432. m_configMeasure->selectLast();
  433. }
  434. }
  435. isBusing = false;
  436. }
  437. void DataCollectionWidget::slotCurrentMeasureDataSelected()
  438. {
  439. DataTableWidget *table = (DataTableWidget *)m_tab->currentWidget();
  440. table->setNodeMatrixData(m_configMeasure->selectedData(), false);
  441. }
  442. void DataCollectionWidget::slotMeasureDataEdited(NodeMatrixInfo *info)
  443. {
  444. m_configMeasure->updateNodeValue(info);
  445. }