Browse Source

1. 插入算法信息;
2. 插入权重结果;

Ulricy 1 year ago
parent
commit
3ef55e7362
3 changed files with 239 additions and 4 deletions
  1. 225 3
      QFD/common/ExportReportManager.cpp
  2. 12 1
      QFD/common/ExportReportManager.h
  3. 2 0
      QFD/view/PlotView.cpp

+ 225 - 3
QFD/common/ExportReportManager.cpp

@@ -1,10 +1,16 @@
 #include "ExportReportManager.h"
 
 #include "ProjectManager.h"
+#include "SchemePlanManager.h"
+
+#include <view/PlotView.h>
 
 #include <dbService/ClassSet.h>
 #include <dbService/SchemeInfoService.h>
 #include <dbService/CNodeDataService.h>
+#include <dbService/SchemeInfoService.h>
+#include <dbService/SchemeProcessService.h>
+#include <dbService/MindWeightService.h>
 
 #include <QWord/QWord.h>
 #include <QWord/QWordDemo.h>
@@ -12,6 +18,7 @@
 #include <QDir>
 #include <QDate>
 #include <QLabel>
+#include <QImageWriter>
 
 #include <QDebug>
 
@@ -53,11 +60,13 @@ bool ExportReportManager::exportReport()
         return false;
     }
 
-    insertCommonInfo();
+    insertProjectInfo();
     if (m_evalType == ProjectManager::OptimalEval) {
         insertSchemeDesignInfo();
         insertSchemeIndex();
         insertSchemeData();
+        insertSchemeAlgInfo();
+        insertSchemeWeightData();
     }
 
     m_word->setVisible(true);
@@ -83,7 +92,7 @@ bool ExportReportManager::createWord()
 
     // 根据模板创建 word 文档
     QString typeStr  = ProjectManager::reportNameOfEvalType((ProjectManager::EvalType)m_evalType);
-    QString tempPath = "E:/qfd/code/QFD2/bin/report_templates/" + typeStr + ".docx";
+    QString tempPath = curPath + "/report_templates/" + typeStr + ".docx";
     if (!QFile::exists(tempPath)) {
         qDebug() << __FUNCTION__ << __LINE__ << "Report template not found" << endl;
         return false;
@@ -104,7 +113,7 @@ bool ExportReportManager::createWord()
     return ret;
 }
 
-void ExportReportManager::insertCommonInfo()
+void ExportReportManager::insertProjectInfo()
 {
     insertText("bmProjName", m_proj->projectName);
     insertText("bmProjUnit", m_proj->estimateDept);
@@ -222,6 +231,198 @@ void ExportReportManager::insertSchemeIndex()
 void ExportReportManager::insertSchemeData()
 {
     insertText("bmProjName41", m_proj->projectName);
+
+    QList<CNodeData> nodeList;
+    bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 4);
+    if (ret == false) {
+        return;
+    }
+
+    CMind *mind = new CMind();
+    mind->setNodeList(nodeList);
+
+    QMap<int, QMap<QString, double>> schemeData;
+    QMap<int, QString> schemeNames;
+    QList<SchemaEval *> schemeList;
+    ret = SchemeInfoService().QuerySchemeInfoByEngineerId(&schemeList, m_proj->id, 0);
+    if (ret == false) {
+        return;
+    }
+
+    if (schemeList.size() <= 0) {
+        return;
+    }
+
+    for (SchemaEval *scheme : schemeList) {
+        if (schemeData.keys().contains(scheme->id) == false) {
+            schemeData[scheme->id] = QMap<QString, double>();
+        }
+        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<CNodeData> leaveNodes = mind->leaves();
+    int rowCount                = leaveNodes.size() + 1;
+    int columnCount             = schemeNames.keys().size() + 1;
+
+    //    bmSchemeDataTable
+    QAxObject *table = insertTable("bmSchemeDataTable", rowCount, columnCount);
+    if (table == nullptr) {
+        return;
+    }
+
+    QList<int> 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);
+        }
+    }
+
+    delete mind;
+}
+
+void ExportReportManager::insertSchemeAlgInfo()
+{
+    // bmWeightAlg bmEvalAlg
+    QList<SchemePlanManager::SchemeProcessInfo> 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::stringFromAlgorithm(alg);
+            insertText("bmWeightAlg", algStr);
+        }
+        if (processList[i].type == SchemePlanManager::RunEvaluate) {
+            SchemePlanManager::Algorithm alg = processList[i].algorithm;
+            QString algStr                   = SchemePlanManager::stringFromAlgorithm(alg);
+            insertText("bmEvalAlg", algStr);
+        }
+    }
+}
+
+void ExportReportManager::insertSchemeWeightData()
+{
+    // 指标权重表:bmWeightTable,指标权重图:bmWeightPic
+
+    QList<CNodeData> nodeList;
+    bool ret = CNodeDataService().QueryAllValid(nodeList, m_proj->id, 4);
+    if (ret == false) {
+        return;
+    }
+
+    CMind *mind = new CMind();
+    mind->setNodeList(nodeList);
+
+    QList<CNodeData> 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<CNodeData> 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<QString, double> 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<PlotView::Data> 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").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);
+        }
+    }
 }
 
 bool ExportReportManager::insertText(const QString &bookmark, const QString &text)
@@ -293,3 +494,24 @@ void ExportReportManager::mergeCells(QAxObject *table, int row, int column, int
 
     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 "";
+    }
+}

+ 12 - 1
QFD/common/ExportReportManager.h

@@ -28,7 +28,7 @@ private:
     bool createWord();
 
     // 插入不同评估类型下共同的信息
-    void insertCommonInfo();
+    void insertProjectInfo();
 
     // 填充方案评估方案设计信息
     void insertSchemeDesignInfo();
@@ -36,6 +36,10 @@ private:
     void insertSchemeIndex();
     // 填充方案数据
     void insertSchemeData();
+    // 填充方案算法信息
+    void insertSchemeAlgInfo();
+    // 填充方案评估权重数据
+    void insertSchemeWeightData();
 
     // 在一个书签位置插入文字
     bool insertText(const QString &bookmark, const QString &text);
@@ -45,6 +49,8 @@ private:
     void setCellString(QAxObject *table, int row, int column, const QString &text);
     void mergeCells(QAxObject *table, int row, int column, int rowSpan, int columnSpan);
 
+    QString saveImage(const QString &name, const QPixmap &pixmap);
+
 signals:
 
 private:
@@ -93,6 +99,11 @@ private:
  *
  * 方案数据设置
  * 表格项目名称:bmProjName41
+ * 表格:bmSchemeDataTable
+ *
+ * 计算
+ * 权重算法:bmWeightAlg,指标权重表:bmWeightTable,指标权重图:bmWeightPic
+ * 评估算法:bmEvalAlg,指标权重表:bmEvalTable,指标权重图:bmEvalPic
  *
  * 2023-12-13 by chengxr
  */

+ 2 - 0
QFD/view/PlotView.cpp

@@ -3,6 +3,8 @@
 #include "qcustomplot/CustomHistogramBars.h"
 #include "qcustomplot/SmoothCurveGraph.h"
 
+#include <QPainter>
+
 PlotView::PlotView(PlotType t, const QVector<Data> &data, const QString &title, QWidget *parent)
     : QCustomPlot(parent), m_plotType(t), m_sourceData(data), m_title(title)
 {