#include "ExportReportManager.h" #include "ProjectManager.h" #include "SchemePlanManager.h" #include "CMindView.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include ExportReportManager::ExportReportManager(QObject *parent) : QObject(parent) { m_word = new QWord(this); } ExportReportManager::~ExportReportManager() { delete m_word; } ProjectInfo *ExportReportManager::proj() const { return m_proj; } void ExportReportManager::setProj(ProjectInfo *proj) { m_proj = proj; } int ExportReportManager::evalType() const { return m_evalType; } void ExportReportManager::setEvalType(int type) { m_evalType = type; } bool ExportReportManager::exportReport() { bool createRet = createWord(); if (createRet == false) { qDebug() << __FUNCTION__ << __LINE__ << m_word->getStrErrorInfo() << endl; return false; } insertProjectInfo(); if (m_evalType == ProjectManager::DemandEval) { insertDemandAlg(); insertDemandIndex(); insertDemandTechIndex(); insertDemandWeight(); insertDemandEval(); } if (m_evalType == ProjectManager::OptimalEval) { insertSchemeDesignInfo(); insertSchemeIndex(); insertSchemeAlgInfo(); insertSchemeData(); insertSchemeWeightData(); } if (m_evalType == ProjectManager::EfficiencyEval) { insertEffiIndex(); insertEffiAlgInfo(); insertEffiConfigData(); insertEffiSchemeData(); insertEffiWeightData(); insertEffiResult(); } m_word->setVisible(true); QAxObject *doc = m_word->getWordApp()->querySubObject("ActiveDocument"); doc->dynamicCall("SaveAs(const QString&))", QDir::toNativeSeparators(m_word->getFilePath())); m_word->close(); return true; } bool ExportReportManager::createWord() { delete m_word; m_word = new QWord(this); // 报告文件夹路径 QString curPath = QDir::currentPath(); QString dateStr = QDate::currentDate().toString("yyyy-MM-dd"); QString reportDir = curPath + "/reports/" + dateStr + "/"; QDir dir(reportDir); if (!dir.exists()) { dir.mkpath(reportDir); } // 根据模板创建 word 文档 QString typeStr = ProjectManager::reportNameOfEvalType((ProjectManager::EvalType)m_evalType); QString tempPath = curPath + "/report_templates/" + typeStr + ".docx"; if (!QFile::exists(tempPath)) { qDebug() << __FUNCTION__ << __LINE__ << "Report template not found" << endl; return false; } QString fileName = m_proj->projectName + "_" + typeStr + ".docx"; QString filePath = reportDir + fileName; if (QFile::exists(filePath)) { QFile::remove(filePath); } bool ret = QFile::copy(tempPath, filePath); if (ret) { ret = m_word->open(filePath, false); } else { qDebug() << __FUNCTION__ << __LINE__ << "Copy report template failed" << endl; } return ret; } void ExportReportManager::insertProjectInfo() { insertText("bmProjName", m_proj->projectName); insertText("bmProjUnit", m_proj->estimateDept); QString dateStr = QDate::currentDate().toString("yyyy年MM月"); insertText("bmProjDate", dateStr); insertText("bmEvalPurpose", m_proj->estimateObjective); } void ExportReportManager::insertDemandAlg() { // bmWeightAlg bmEvalAlg // bmWeightAlgDscpt bmEvalAlgDscpt QList processList; bool processRet = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_proj->id, 1); if (processRet == false) { return; } for (int i = 0; i < processList.size(); ++i) { if (processList[i].type == SchemePlanManager::CalculateWeight) { SchemePlanManager::Algorithm alg = processList[i].algorithm; QString algStr = SchemePlanManager::nameOfAlgorithm(alg); insertText("bmWeightAlg", algStr); QString dscrpt = SchemePlanManager::descriptionOfAlgorithm(alg); insertText("bmWeightAlgDscpt", dscrpt); } } processRet = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_proj->id, 2); if (processRet == false) { return; } for (int i = 0; i < processList.size(); ++i) { if (processList[i].type == SchemePlanManager::RunEvaluate) { SchemePlanManager::Algorithm alg = processList[i].algorithm; QString algStr = SchemePlanManager::nameOfAlgorithm(alg); insertText("bmEvalAlg", algStr); QString dscrpt = SchemePlanManager::descriptionOfAlgorithm(alg); insertText("bmEvalAlgDscpt", dscrpt); } } } void ExportReportManager::insertDemandIndex() { insertText("bmProjName21", m_proj->projectName); insertText("bmProjName22", m_proj->projectName); QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 1); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmIndexTable QAxObject *table = insertTable("bmIndexTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("量纲")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; setCellString(table, i + 2, columnCount, node.dimension); } delete mind; CMindView *m = new CMindView(); m->setNodeList(nodeList); m->refreshItems(); int sceneWidth = m->sceneRect().width(); int sceneHeight = m->sceneRect().height(); QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight); QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight); QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20)); pixmap.fill(QColor(244, 244, 255)); QPainter painter(&pixmap); painter.setRenderHints(QPainter::Antialiasing); m->scene()->render(&painter, targetRect, sourceRect); delete m; QString imgName = QString("%1_%2_index").arg(m_proj->id).arg(1); QString path = saveImage(imgName, pixmap); QImageWriter writer(path); writer.setFormat("PNG"); ret = writer.write(pixmap.toImage()); if (ret) { QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmIndexPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { int w = pixmap.width(); int h = pixmap.height(); if (w > 400) { h = h * 400 / w; w = 400; } shape->setProperty("Width", w); shape->setProperty("Height", h); } } } } void ExportReportManager::insertDemandTechIndex() { insertText("bmProjName31", m_proj->projectName); insertText("bmProjName32", m_proj->projectName); QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 2); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels() - 1; // bmIndexTable QAxObject *table = insertTable("bmTechTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } delete mind; CMindView *m = new CMindView(); m->setNodeList(nodeList); m->refreshItems(); int sceneWidth = m->sceneRect().width(); int sceneHeight = m->sceneRect().height(); QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight); QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight); QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20)); pixmap.fill(QColor(244, 244, 255)); QPainter painter(&pixmap); painter.setRenderHints(QPainter::Antialiasing); m->scene()->render(&painter, targetRect, sourceRect); delete m; QString imgName = QString("%1_%2_index").arg(m_proj->id).arg(2); QString path = saveImage(imgName, pixmap); QImageWriter writer(path); writer.setFormat("PNG"); ret = writer.write(pixmap.toImage()); if (ret) { QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmTechPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { int w = pixmap.width(); int h = pixmap.height(); if (w > 400) { h = h * 400 / w; w = 400; } shape->setProperty("Width", w); shape->setProperty("Height", h); } } } } void ExportReportManager::insertDemandWeight() { // 指标权重表:bmWeightTable,指标权重图:bmWeightPic QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 1); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmIndexTable QAxObject *table = insertTable("bmWeightTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("权重值")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } delete mind; MindWeightInfo info; ret = MindWeightService().queryWeightData(&info, m_proj->id, 1); if (ret == false) { return; } if (info.id < 0) { return; } QMap weightData; QStringList weightList = info.weight.split(";"); for (QString keyValueStr : weightList) { QStringList keyValue = keyValueStr.split(":"); if (keyValue.size() == 2) { weightData[keyValue.first()] = keyValue.last().toDouble(); } } QVector values; for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; QString value = QString("%1").arg(weightData[node.name]); setCellString(table, i + 2, columnCount, value); PlotView::Data data { node.name, weightData[node.name] }; values.append(data); } PlotView *plot = new PlotView(PlotView::HistogramHorizontal, values, ""); plot->setFixedSize(QSize(600, 600)); plot->plot(); QPixmap pixmap = plot->grab(); plot->deleteLater(); if (pixmap.isNull()) { return; } QString imgName = QString("%1_%2_weight_plot").arg(m_proj->id).arg(1); QString path = saveImage(imgName, pixmap); QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmWeightPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { shape->setProperty("Width", 400); shape->setProperty("Height", 400); } } } void ExportReportManager::insertDemandEval() { MindScoreInfo info; bool ret = MindScoreService().queryScoreData(&info, m_proj->id); if (ret == false || info.id < 0) { return; } QMap scoreData; QStringList keyValueStringList = info.score.split(";"); for (QString keyValueString : keyValueStringList) { QStringList keyValue = keyValueString.split(":"); if (keyValue.size() == 2) { scoreData[keyValue.first()] = keyValue.last(); } } QList nodeList; ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 2); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmEvalTable QAxObject *table = insertTable("bmEvalTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("重要度")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } delete mind; QVector values; for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; QString value = scoreData[node.name]; setCellString(table, i + 2, columnCount, value); PlotView::Data data { node.name, value.toDouble() }; values.append(data); } PlotView *plot = new PlotView(PlotView::HistogramHorizontal, values, ""); plot->setFixedSize(QSize(600, 600)); plot->plot(); QPixmap pixmap = plot->grab(); plot->deleteLater(); if (pixmap.isNull()) { return; } QString imgName = QString("%1_%2_priority_plot").arg(m_proj->id).arg(1); QString path = saveImage(imgName, pixmap); QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmEvalPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { shape->setProperty("Width", 400); shape->setProperty("Height", 400); } } QString viTech; double score = 0; for (QString key : scoreData.keys()) { if (scoreData[key].toDouble() > score) { score = scoreData[key].toDouble(); viTech = key; } } insertText("bmVITech1", viTech); insertText("bmVITech2", viTech); } void ExportReportManager::insertSchemeDesignInfo() { QList schemeList; bool ret = SchemeInfoService().QuerySchemeInfoByEngineerId(&schemeList, m_proj->id, 0); if (ret == false) { qDebug() << __FUNCTION__ << __LINE__ << "query" << endl; return; } for (int i = schemeList.size() - 1; i >= 0; --i) { // 插入图片 QAxObject *bmSchemes = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmSchemes"); if (bmSchemes != nullptr) { bmSchemes->dynamicCall("Select(void)"); QAxObject *range = bmSchemes->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", schemeList[i]->filePath); if (shape != nullptr) { shape->setProperty("Width", 200); shape->setProperty("Height", 200); } } m_word->insertMoveDown(); // 插入图片标题 QString remark = schemeList[i]->remark; int picTtlIndex = remark.lastIndexOf("(图"); QString subStr = remark.mid(picTtlIndex + 1, 8); QString picTtl = ""; if (subStr.contains(")")) { picTtl = subStr.split(")").first(); } m_word->setFontName(QString::fromLocal8Bit("黑体")); m_word->setFontSize(9); m_word->setParagraphAlignment(0); m_word->insertText(picTtl + " " + "阵地位置图"); m_word->insertMoveDown(); // 插入方案描述 insertText("bmSchemes", schemeList[i]->name + ":" + schemeList[i]->remark); if (i != 0) { m_word->insertMoveDown(); } } } void ExportReportManager::insertSchemeIndex() { insertText("bmProjName31", m_proj->projectName); insertText("bmProjName32", m_proj->projectName); QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 4); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmIndexTable QAxObject *table = insertTable("bmIndexTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标\n(能力项)").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("量纲")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; setCellString(table, i + 2, columnCount, node.dimension); } delete mind; CMindView *m = new CMindView(); m->setNodeList(nodeList); m->refreshItems(); int sceneWidth = m->sceneRect().width(); int sceneHeight = m->sceneRect().height(); QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight); QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight); QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20)); pixmap.fill(QColor(244, 244, 255)); QPainter painter(&pixmap); painter.setRenderHints(QPainter::Antialiasing); m->scene()->render(&painter, targetRect, sourceRect); delete m; QString imgName = QString("%1_%2_index").arg(m_proj->id).arg(4); QString path = saveImage(imgName, pixmap); QImageWriter writer(path); writer.setFormat("PNG"); ret = writer.write(pixmap.toImage()); if (ret) { QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmIndexPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { int w = pixmap.width(); int h = pixmap.height(); if (w > 400) { h = h * 400 / w; w = 400; } shape->setProperty("Width", w); shape->setProperty("Height", h); } } } } void ExportReportManager::insertSchemeData() { insertText("bmProjName41", m_proj->projectName); QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 4); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QMap> schemeData; QMap schemeNames; QList schemeList; ret = SchemeInfoService().QuerySchemeInfoByEngineerId(&schemeList, m_proj->id, m_schemeType); if (ret == false || schemeList.size() <= 0) { delete mind; return; } for (SchemaEval *scheme : schemeList) { if (schemeData.keys().contains(scheme->id) == false) { schemeData[scheme->id] = QMap(); } QStringList keyValueStringList = scheme->valueStr.split(";"); for (QString keyValueStr : keyValueStringList) { QStringList keyValue = keyValueStr.split(":"); if (keyValue.size() == 2) { schemeData[scheme->id][keyValue.first()] = keyValue.last().toDouble(); } } schemeNames[scheme->id] = scheme->name; } QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = schemeNames.keys().size() + 1; delete mind; // bmSchemeDataTable QAxObject *table = insertTable("bmSchemeDataTable", rowCount, columnCount); if (table == nullptr) { return; } QList schemeIds = schemeNames.keys(); for (int i = 0; i < schemeIds.size() + 1; ++i) { if (i == 0) { setCellString(table, 1, i + 1, "指标"); } else { setCellString(table, 1, i + 1, schemeNames[schemeIds[i - 1]]); } } for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; setCellString(table, i + 2, 1, node.name); for (int j = 0; j < schemeIds.size(); ++j) { QString value = QString("%1").arg(schemeData[schemeIds[j]][node.name]); setCellString(table, i + 2, j + 2, value); } } // 指标权重表:bmEvalTable,指标权重图:bmEvalPic QAxObject *scoreTable = insertTable("bmEvalTable", schemeList.size() + 1, 2); if (scoreTable == nullptr) { return; } QVector values; setCellString(scoreTable, 1, 2, "得分"); for (int i = 0; i < schemeList.size(); ++i) { setCellString(scoreTable, i + 2, 1, schemeList[i]->name); QString str = QString::number(schemeList[i]->score, 'f', 2); setCellString(scoreTable, i + 2, 2, str); PlotView::Data data { schemeList[i]->name, schemeList[i]->score }; values.append(data); } PlotView *plot = new PlotView(PlotView::HistogramHorizontal, values, ""); plot->setFixedSize(QSize(600, 600)); plot->plot(); QPixmap pixmap = plot->grab(); plot->deleteLater(); if (pixmap.isNull()) { return; } QString imgName = QString("%1_%2_eval").arg(m_proj->id).arg(4); QString path = saveImage(imgName, pixmap); QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmEvalPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { shape->setProperty("Width", 400); shape->setProperty("Height", 400); } } // bmSchemeConclusion QString scores; int bestIndex = 0; for (int i = 0; i < schemeList.size(); ++i) { QString str = QString::number(schemeList[i]->score, 'f', 2); scores += QString("%1%2,").arg(schemeList[i]->name).arg(str); if (schemeList[i]->score > schemeList[bestIndex]->score) { bestIndex = i; } } QString conclusion = QString("经评估分析,%1个方案得分分别为:%2%3为最优方案。") .arg(schemeList.size()) .arg(scores) .arg(schemeList[bestIndex]->name); insertText("bmSchemeConclusion", conclusion); } void ExportReportManager::insertSchemeAlgInfo() { // bmWeightAlg bmEvalAlg // bmWeightAlgDscpt bmEvalAlgDscpt QList processList; bool processRet = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_proj->id, 4); if (processRet == false) { return; } for (int i = 0; i < processList.size(); ++i) { if (processList[i].type == SchemePlanManager::CalculateWeight) { SchemePlanManager::Algorithm alg = processList[i].algorithm; QString algStr = SchemePlanManager::nameOfAlgorithm(alg); insertText("bmWeightAlg", algStr); QString dscrpt = SchemePlanManager::descriptionOfAlgorithm(alg); insertText("bmWeightAlgDscpt", dscrpt); } if (processList[i].type == SchemePlanManager::RunEvaluate) { SchemePlanManager::Algorithm alg = processList[i].algorithm; QString algStr = SchemePlanManager::nameOfAlgorithm(alg); insertText("bmEvalAlg", algStr); QString dscrpt = SchemePlanManager::descriptionOfAlgorithm(alg); insertText("bmEvalAlgDscpt", dscrpt); } } } void ExportReportManager::insertSchemeWeightData() { // 指标权重表:bmWeightTable,指标权重图:bmWeightPic QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 4); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmIndexTable QAxObject *table = insertTable("bmWeightTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标\n(能力项)").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("权重值")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } delete mind; MindWeightInfo info; ret = MindWeightService().queryWeightData(&info, m_proj->id, 4); if (ret == false) { return; } if (info.id < 0) { return; } QMap weightData; QStringList weightList = info.weight.split(";"); for (QString keyValueStr : weightList) { QStringList keyValue = keyValueStr.split(":"); if (keyValue.size() == 2) { weightData[keyValue.first()] = keyValue.last().toDouble(); } } QVector values; for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; QString value = QString("%1").arg(weightData[node.name]); setCellString(table, i + 2, columnCount, value); PlotView::Data data { node.name, weightData[node.name] }; values.append(data); } PlotView *plot = new PlotView(PlotView::HistogramHorizontal, values, ""); plot->setFixedSize(QSize(600, 600)); plot->plot(); QPixmap pixmap = plot->grab(); plot->deleteLater(); if (pixmap.isNull()) { return; } QString imgName = QString("%1_%2_weight_plot").arg(m_proj->id).arg(4); QString path = saveImage(imgName, pixmap); QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmWeightPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { shape->setProperty("Width", 400); shape->setProperty("Height", 400); } } } void ExportReportManager::insertEffiIndex() { insertText("bmProjName21", m_proj->projectName); insertText("bmProjName22", m_proj->projectName); QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 8); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmIndexTable QAxObject *table = insertTable("bmIndexTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标\n(能力项)").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("量纲")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; setCellString(table, i + 2, columnCount, node.dimension); } delete mind; CMindView *m = new CMindView(); m->setNodeList(nodeList); m->refreshItems(); int sceneWidth = m->sceneRect().width(); int sceneHeight = m->sceneRect().height(); QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight); QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight); QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20)); pixmap.fill(QColor(244, 244, 255)); QPainter painter(&pixmap); painter.setRenderHints(QPainter::Antialiasing); m->scene()->render(&painter, targetRect, sourceRect); delete m; QString imgName = QString("%1_%2_index").arg(m_proj->id).arg(8); QString path = saveImage(imgName, pixmap); QImageWriter writer(path); writer.setFormat("PNG"); ret = writer.write(pixmap.toImage()); if (ret) { QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmIndexPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { int w = pixmap.width(); int h = pixmap.height(); if (w > 400) { h = h * 400 / w; w = 400; } shape->setProperty("Width", w); shape->setProperty("Height", h); } } } } void ExportReportManager::insertEffiAlgInfo() { // bmWeightAlg bmEvalAlg // bmWeightAlgDscpt bmEvalAlgDscpt QList processList; bool processRet = SchemeProcessService().QueryAllByProjectIdAndIndexType(processList, m_proj->id, 8); if (processRet == false) { return; } for (int i = 0; i < processList.size(); ++i) { if (processList[i].type == SchemePlanManager::CalculateWeight) { SchemePlanManager::Algorithm alg = processList[i].algorithm; QString algStr = SchemePlanManager::nameOfAlgorithm(alg); insertText("bmWeightAlg", algStr); QString dscrpt = SchemePlanManager::descriptionOfAlgorithm(alg); insertText("bmWeightAlgDscpt", dscrpt); } if (processList[i].type == SchemePlanManager::RunEvaluate) { SchemePlanManager::Algorithm alg = processList[i].algorithm; if (alg == SchemePlanManager::MEA) { m_schemeType = 1; } else { m_schemeType = 2; } QString algStr = SchemePlanManager::nameOfAlgorithm(alg); insertText("bmEvalAlg", algStr); QString dscrpt = SchemePlanManager::descriptionOfAlgorithm(alg); insertText("bmEvalAlgDscpt", dscrpt); } } } void ExportReportManager::insertEffiConfigData() { // bmProjName31, bmProjName32, bmConfigTable insertText("bmProjName31", m_proj->projectName); insertText("bmProjName32", m_proj->projectName); QList gradeInfoList; int type = m_schemeType == 1 ? 0 : 1; bool ret = GradeInfoService().QueryGradeByProjectIdAndType(&gradeInfoList, m_proj->id, type); if (ret == false || gradeInfoList.size() <= 0) { return; } QStringList gradeNames; for (int i = 0; i < gradeInfoList.size(); ++i) { gradeNames.append(gradeInfoList[i]->gradeValue); } QMap gradeInfoData; if (m_schemeType == 1) { QList gradeIndexInfoList; ret = GradeIndexInfoService().QueryGradeIndexInfoByProjectId(&gradeIndexInfoList, m_proj->id); if (ret == false || gradeIndexInfoList.size() <= 0) { return; } for (int i = gradeIndexInfoList.size() - 1; i >= 0; --i) { QString name = gradeIndexInfoList[i]->gradeIndexName; QString value = gradeInfoData[name]; if (gradeNames.size() > gradeIndexInfoList[i]->gradeLevel - 1) { QString grade = gradeNames[gradeIndexInfoList[i]->gradeLevel - 1]; if (value.contains(grade)) { continue; } else { value += grade; } } value += ":"; value += gradeIndexInfoList[i]->gradeIndexValue; if (gradeIndexInfoList[i]->gradeLevel > 1) { value += "\n"; } gradeInfoData[name] = value; } } else { QList effectIndexInfoList; ret = EffectIndexInfoService().QueryEffectIndexInfoByProjectId(&effectIndexInfoList, m_proj->id); if (ret == false) { return; } for (EffectIndexInfo *info : effectIndexInfoList) { QStringList valueList = info->effectIndexValue.split(","); QString value; for (int i = valueList.size() - 1; i >= 0; --i) { if (gradeNames.size() > i) { value += gradeNames[i]; } value += ":"; value += (valueList[i].replace(":", ",")); if (i > 0) { value += "\n"; } gradeInfoData[info->effectIndexName] = value; } } } QList nodeList; ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 8); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmConfigTable QAxObject *table = insertTable("bmConfigTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标\n(能力项)").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("效能分级")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; QString value = QString("%1").arg(gradeInfoData[node.name]); setCellString(table, i + 2, columnCount, value); } delete mind; } void ExportReportManager::insertEffiSchemeData() { QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 8); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QMap> schemeData; QMap schemeNames; QList schemeList; ret = SchemeInfoService().QuerySchemeInfoByEngineerId(&schemeList, m_proj->id, m_schemeType); if (ret == false || schemeList.size() <= 0) { delete mind; return; } for (SchemaEval *scheme : schemeList) { if (m_schemeType == 2 && scheme->name != "建设前" && scheme->name != "建设后") { continue; } if (schemeData.keys().contains(scheme->id) == false) { schemeData[scheme->id] = QMap(); } QStringList keyValueStringList = scheme->valueStr.split(";"); for (QString keyValueStr : keyValueStringList) { QStringList keyValue = keyValueStr.split(":"); if (keyValue.size() == 2) { schemeData[scheme->id][keyValue.first()] = keyValue.last().toDouble(); } } schemeNames[scheme->id] = scheme->name; } QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = schemeNames.keys().size() + 1; delete mind; // bmSchemeDataTable QAxObject *table = insertTable("bmEffiDataTable", rowCount, columnCount); if (table == nullptr) { return; } QList schemeIds = schemeNames.keys(); for (int i = 0; i < schemeIds.size() + 1; ++i) { if (i == 0) { setCellString(table, 1, i + 1, "指标"); } else { setCellString(table, 1, i + 1, schemeNames[schemeIds[i - 1]]); } } for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; setCellString(table, i + 2, 1, node.name); for (int j = 0; j < schemeIds.size(); ++j) { QString value = QString("%1").arg(schemeData[schemeIds[j]][node.name]); setCellString(table, i + 2, j + 2, value); } } } void ExportReportManager::insertEffiWeightData() { // 指标权重表:bmWeightTable,指标权重图:bmWeightPic QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 8); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels(); // bmIndexTable QAxObject *table = insertTable("bmWeightTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标\n(能力项)").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); setCellString(table, 1, i, QString("权重值")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } delete mind; MindWeightInfo info; ret = MindWeightService().queryWeightData(&info, m_proj->id, 8); if (ret == false) { return; } if (info.id < 0) { return; } QMap weightData; QStringList weightList = info.weight.split(";"); for (QString keyValueStr : weightList) { QStringList keyValue = keyValueStr.split(":"); if (keyValue.size() == 2) { weightData[keyValue.first()] = keyValue.last().toDouble(); } } QVector values; for (int i = 0; i < leaveNodes.size(); ++i) { CNodeData node = leaveNodes[i]; QString value = QString("%1").arg(weightData[node.name]); setCellString(table, i + 2, columnCount, value); PlotView::Data data { node.name, weightData[node.name] }; values.append(data); } PlotView *plot = new PlotView(PlotView::HistogramHorizontal, values, ""); plot->setFixedSize(QSize(600, 600)); plot->plot(); QPixmap pixmap = plot->grab(); plot->deleteLater(); if (pixmap.isNull()) { return; } QString imgName = QString("%1_%2_weight_plot").arg(m_proj->id).arg(8); QString path = saveImage(imgName, pixmap); QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmWeightPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { shape->setProperty("Width", 400); shape->setProperty("Height", 400); } } } void ExportReportManager::insertEffiResult() { QList gradeInfoList; int type = m_schemeType == 1 ? 0 : 1; bool gradeRet = GradeInfoService().QueryGradeByProjectIdAndType(&gradeInfoList, m_proj->id, type); if (gradeRet == false) { return; } QStringList gradeNames; for (int i = 0; i < gradeInfoList.size(); ++i) { gradeNames.append(gradeInfoList[i]->gradeValue); } QList effList; bool effRet = EffectResultService().queryDataList(effList, m_proj->id); if (effRet == false) { return; } QMap>> effData; QMap schemeNames; int levels = 0; for (EffectResult effect : effList) { if (effData.keys().contains(effect.schemeId) == false) { effData[effect.schemeId] = QMap>(); schemeNames[effect.schemeId] = effect.schemeName; } QStringList values = effect.value.split(","); levels = values.size(); QList valueList; for (int v = 0; v < values.size(); ++v) { valueList.append(values[v].toDouble()); } effData[effect.schemeId][effect.indexName] = valueList; } QList nodeList; bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 8); if (ret == false) { return; } CMind *mind = new CMind(); mind->setNodeList(nodeList); QList leaveNodes = mind->leaves(); int rowCount = leaveNodes.size() + 1; int columnCount = mind->levels() + schemeNames.size() - 1; // bmEvalTable QAxObject *table = insertTable("bmEvalTable", rowCount, columnCount); if (table == nullptr) { delete mind; return; } for (int i = 2; i <= mind->levels(); ++i) { if (i < mind->levels()) { setCellString(table, 1, i - 1, QString("%1级指标\n(能力项)").arg(i - 1)); } else { setCellString(table, 1, i - 1, QString("指标")); } QList nodes = mind->nodesInLevel(i); int rowPos = 2; for (int j = 0; j < nodes.size(); ++j) { CNodeData node = nodes[j]; int leaves = mind->leavesCountOfNode(node); int textPos = (rowPos + leaves / 2); setCellString(table, textPos, i - 1, node.name); if (leaves > 1) { mergeCells(table, rowPos, i - 1, leaves, 1); } rowPos += leaves; } } for (int i = 0; i < schemeNames.size(); ++i) { setCellString(table, 1, i + mind->levels(), schemeNames[schemeNames.keys()[i]] + "\n(" + gradeNames.join(",") + ")"); for (int j = 0; j < leaveNodes.size(); ++j) { CNodeData node = leaveNodes[j]; QList values = effData[schemeNames.keys()[i]][node.name]; int bestIndex = 0; QString score; for (int k = 0; k < values.size(); ++k) { if (values[k] > values[bestIndex]) { bestIndex = k; } score += QString::number(values[k], 'f', 1).replace(".0", "").replace("nan", "0"); if (k < values.size() - 1) { score += ","; } } QString rand = gradeNames[bestIndex]; QString value = rand + "(" + score + ")"; setCellString(table, j + 2, i + mind->levels(), value); } } // 指标权重图:bmEvalPic for (int i = schemeNames.size() - 1; i >= 0; --i) { QString schemeName = schemeNames[schemeNames.keys()[i]]; QList values = effData[schemeNames.keys()[i]]["综合"]; QVector plotValues; for (int j = 0; j < values.size(); ++j) { PlotView::Data data { schemeName + "-" + gradeNames[j], values[j] }; plotValues.append(data); } PlotView *plot = new PlotView(PlotView::HistogramHorizontal, plotValues, ""); plot->setFixedSize(QSize(600, 600)); plot->plot(); QPixmap pixmap = plot->grab(); plot->deleteLater(); if (pixmap.isNull()) { return; } QString imgName = QString("%1_%2_%3_eval").arg(m_proj->id).arg(8).arg(schemeName); QString path = saveImage(imgName, pixmap); QAxObject *bmWeightPic = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", "bmEvalPic"); if (bmWeightPic != nullptr) { bmWeightPic->dynamicCall("Select(void)"); QAxObject *range = bmWeightPic->querySubObject("Range"); QAxObject *inlineShapes = range->querySubObject("InlineShapes"); QAxObject *shape = inlineShapes->querySubObject("AddPicture(const QString&)", path); if (shape != nullptr) { shape->setProperty("Width", 400); shape->setProperty("Height", 400); } } } QStringList retList; int bestGrade = 0; int bestSchemeIndex = 0; for (int i = 0; i < schemeNames.size(); ++i) { int grade = 0; QList values = effData[schemeNames.keys()[i]]["综合"]; for (int j = 0; j < values.size(); ++j) { if (values[j] > values[grade]) { grade = j; } } QString ret = schemeNames[schemeNames.keys()[i]] + "的综合评价等级为" + gradeNames[grade]; retList.append(ret); if (grade > bestGrade) { bestGrade = grade; bestSchemeIndex = i; } } QString bestScheme = schemeNames[schemeNames.keys()[bestSchemeIndex]]; QString conclusion = "根据评估结果," + retList.join(",") + "。所以" + bestScheme + "更优。"; insertText("bmEffiConclusion", conclusion); delete mind; } bool ExportReportManager::insertText(const QString &bookmark, const QString &text) { bool ret = false; QAxObject *bmProjName = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", bookmark); if (bmProjName != nullptr) { bmProjName->dynamicCall("Select(void)"); QAxObject *range = bmProjName->querySubObject("Range"); ret = range->setProperty("Text", text); } return ret; } QAxObject *ExportReportManager::insertTable(QString bookmark, int row, int column) { QAxObject *bmTable = m_word->getDocument()->querySubObject("Bookmarks(QVariant)", bookmark); if (bmTable) { bmTable->dynamicCall("Select(void)"); QAxObject *selection = m_word->getWordApp()->querySubObject("Selection"); if (!selection) return nullptr; selection->dynamicCall("InsertAfter(QString&)", "\n"); // selection->dynamicCall("MoveLeft(int)", 1); selection->querySubObject("ParagraphFormat")->dynamicCall("Alignment", "wdAlignParagraphCenter"); // selection->dynamicCall("TypeText(QString&)", "Table Test"); //设置标题 QAxObject *range = selection->querySubObject("Range"); QAxObject *tables = m_word->getDocument()->querySubObject("Tables"); QAxObject *table = tables->querySubObject("Add(QVariant,int,int)", range->asVariant(), row, column); if (!tables) return nullptr; for (int i = 1; i <= 6; i++) { QString str = QString("Borders(-%1)").arg(i); QAxObject *borders = table->querySubObject(str.toLocal8Bit().constData()); borders->dynamicCall("SetLineStyle(int)", 1); } return table; } return nullptr; } void ExportReportManager::setCellString(QAxObject *table, int row, int column, const QString &text) { if (table == nullptr) { return; } table->querySubObject("Cell(int,int)", row, column)->querySubObject("Range")->dynamicCall("SetText(QString)", text); } void ExportReportManager::mergeCells(QAxObject *table, int row, int column, int rowSpan, int columnSpan) { if (table == nullptr) { return; } QAxObject *startCell = table->querySubObject("Cell(int, int)", row, column); QAxObject *endCell = table->querySubObject("Cell(int, int)", row + rowSpan - 1, column + columnSpan - 1); if (nullptr == startCell) { return; } if (nullptr == endCell) { return; } startCell->querySubObject("Merge(LPDISPATCH)", endCell->asVariant()); } QString ExportReportManager::saveImage(const QString &name, const QPixmap &pixmap) { QString curPath = QDir::currentPath(); QString dateStr = QDate::currentDate().toString("yyyy-MM-dd"); QString picDir = curPath + "/report_pics/" + dateStr + "/"; QDir dir(picDir); if (!dir.exists()) { dir.mkpath(picDir); } QString filePath = picDir + name + ".png"; QImageWriter writer(filePath); writer.setFormat("PNG"); bool ret = writer.write(pixmap.toImage()); if (ret) { return filePath; } else { return ""; } }