From 6d3efe7ff2afba811dd5f3323527e9fc3342b388 Mon Sep 17 00:00:00 2001
From: wumu <mayi@mayi.com>
Date: 星期一, 08 一月 2024 22:33:46 +0800
Subject: [PATCH] 0108

---
 internal_system_v1/searchinfo.cpp |  268 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 257 insertions(+), 11 deletions(-)

diff --git a/internal_system_v1/searchinfo.cpp b/internal_system_v1/searchinfo.cpp
index b1036aa..95fb771 100644
--- a/internal_system_v1/searchinfo.cpp
+++ b/internal_system_v1/searchinfo.cpp
@@ -1,8 +1,12 @@
 #include "searchinfo.h"
 #include "ui_searchinfo.h"
+#include <QAxObject>
 #include <QDebug>
+#include <QDir>
 #include <QSqlError>
 #include <QSqlQuery>
+#include <QMessageBox>
+#include <QFileDialog>
 
 SearchInfo::SearchInfo(QWidget *parent) :
     QMainWindow(parent),
@@ -14,10 +18,15 @@
 //    ui->dateTimeEdit->hide();
 //    ui->dateTimeEdit_2->hide();
 
+    ui->dateEdit_end->setDate(QDate::currentDate());
+
     m_model = new QStandardItemModel(this);
     ui->tableView->setModel(m_model);
     ui->tableView->verticalHeader()->setVisible(false); // 闅愯棌琛屽ご
-
+    m_model->setColumnCount(5);
+    QStringList labels;
+    labels<<"ID"<<"鎸囨爣鍚嶇О"<<"浠g爜"<<"鏁伴噺"<<"鍏徃缂栧彿"<<"瀹¤鏃堕棿";
+    m_model->setHorizontalHeaderLabels(labels);
     if(QSqlDatabase::contains("qt_sql_default_connection")){
         m_db = QSqlDatabase::addDatabase("qt_sql_default_connection");
     }else{
@@ -32,7 +41,8 @@
     }else{
         qDebug()<<"db open fail";
     }
-
+    // 璁剧疆琛ㄦ牸鍒楁爣绛�
+    setLabels();
 
 }
 
@@ -135,7 +145,8 @@
             code      TEXT,\
             num       INTEGER,\
             evidences TEXT,\
-            com_id  INTEGER\
+            com_id  INTEGER,\
+            time     TEXT\
         )").arg(tableName);
     QSqlQuery query;
     if(query.exec(sql))
@@ -153,7 +164,8 @@
             kpi_name TEXT,\
             code     TEXT,\
             num      INTEGER,\
-            com_id  INTEGER\
+            com_id  INTEGER,\
+            time     TEXT\
         )").arg(tableName);
     QSqlQuery query;
     if(query.exec(sql))
@@ -172,7 +184,8 @@
             code     TEXT,\
             unit     TEXT,\
             num      INTEGER,\
-            com_id  INTEGER\
+            com_id  INTEGER,\
+            time     TEXT\
         )").arg(tableName);
     QSqlQuery query;
     if(query.exec(sql))
@@ -186,17 +199,52 @@
 void SearchInfo::searchComName()
 {
     QSqlQuery query;
-    QString sql = QString("select id,name from company_info");
+    QString sql = QString("select id,name,code from company_info");
     if(query.exec(sql)){
         qDebug()<<"size:"<<query.numRowsAffected();
         while(query.next()){
             int id = query.value(0).toInt();
             QString name = query.value(1).toString();
+            QString code = query.value(2).toString();
             qDebug()<<"com select:"<<id<<name;
             m_comInfo[name] = id;
+            m_comCode[name] = code;
             ui->comboBox_company->addItem(name);
         }
     }
+}
+
+QVector<QVector<QString>> SearchInfo::getResult(QString &sql,int colCnt)
+{
+    QVector<QVector<QString>> resultSet;
+    QSqlQuery query;
+    qDebug()<<sql;
+    if(query.exec(sql)){
+        while (query.next()) {
+            QVector<QString> tmp;
+            for(int i=0;i<colCnt;++i){
+                tmp.push_back(query.value(i).toString());
+            }
+            resultSet.append(tmp);
+        }
+    }
+    return resultSet;
+}
+
+void SearchInfo::setLabels()
+{
+    QStringList labels,labels2,labels3;
+    labels<<"ID"<<"鎸囨爣鍚嶇О"<<"浠g爜"<<"鏁伴噺"<<"鍏徃缂栧彿"<<"瀹¤鏃堕棿"<<" ";
+    labels2<<"ID"<<"鎸囨爣鍚嶇О"<<"浠g爜"<<"鍗曚綅"<<"鏁伴噺"<<"鍏徃缂栧彿"<<"瀹¤鏃堕棿";
+    labels3<<"ID"<<"鎸囨爣鍚嶇О"<<"浠g爜"<<"鏁伴噺"<<"浣愯瘉"<<"鍏徃缂栧彿"<<"瀹¤鏃堕棿";
+    m_labels["瀹¤椤圭洰"] = labels;
+    m_labels["鍐呭宸ヤ綔閲�"] = labels;
+    m_labels["闂閲戦"] = labels3;
+    m_labels["闂涓暟"] = labels3;
+    m_labels["闂鏁存敼"] = labels3;
+    m_labels["闂鏁存敼--闈為噾棰�"] = labels3;
+    m_labels["澶勫垎"] = labels;
+    m_labels["妗堜欢绾跨储"] = labels2;
 }
 
 void SearchInfo::on_pushButton_search_clicked()
@@ -205,6 +253,11 @@
     QString item = ui->comboBox->currentText();
     QString comName = ui->comboBox_company->currentText();
     QString sql = QString("select * from %1 where com_id=%2").arg(m_itemAndTable[item]).arg(m_comInfo[comName]);
+    if(ui->checkBox->isChecked()){ // 鍕鹃�変箣鍚�
+        sql = QString("select * from %1 where com_id=%2 and time between '%3' and '%4'").arg(m_itemAndTable[item]).arg(m_comInfo[comName])
+                .arg(ui->dateEdit_begin->date().toString("yyyy-MM-dd"))
+                .arg(ui->dateEdit_end->date().toString("yyyy-MM-dd"));
+    }
     qDebug()<<__FUNCTION__<<sql;
     QSqlQuery query;
     if(query.exec(sql)){
@@ -215,12 +268,205 @@
             rowCnt++;
             m_model->setRowCount(rowCnt);
             qDebug()<<query.value(0).toInt();
-            m_model->setItem(rowCnt-1,0,new QStandardItem(query.value(0).toString()));
-            m_model->setItem(rowCnt-1,1,new QStandardItem(query.value(1).toString()));
-            m_model->setItem(rowCnt-1,2,new QStandardItem(query.value(2).toString()));
-            m_model->setItem(rowCnt-1,3,new QStandardItem(query.value(3).toString()));
-            m_model->setItem(rowCnt-1,4,new QStandardItem(query.value(4).toString()));
+            for(int i=0;i<7;++i){
+                if(query.value(i).isValid()){
+                    m_model->setItem(rowCnt-1,i,new QStandardItem(query.value(i).toString()));
+                }
+            }
+
         }
     }
 
 }
+
+void SearchInfo::on_pushButton_export_clicked()
+{
+    if(ui->comboBox_company->currentText() == "鍏徃鍚嶇О"){
+        QMessageBox::information(this,"瀵煎嚭閿欒","璇烽�夋嫨涓�涓叕鍙稿悕鍐嶅鍑�");
+        return;
+    }
+    if(ui->label_excel_path->text().size() < 5){
+        QMessageBox::information(this,"瀵煎嚭璁剧疆閿欒","璇峰厛鍦ㄢ�滃鍑鸿缃�濋�夋嫨妯℃澘鍙婁繚瀛樻枃浠剁殑璺緞淇℃伅");
+        return;
+    }
+    // 姣忎竴寮犺〃鎷挎渶鏂扮殑閭d竴鎵规暟鎹嵆鍙紝鐒跺悗淇濆瓨鍒扮數瀛愯〃鏍间腑
+    int comId = m_comInfo[ui->comboBox_company->currentText()];
+    QString audit_project = QString("select kpi_name,code,num from %2 where com_id=%1 limit 10").arg(comId).arg("audit_project");
+    QString audit_workload = QString("select kpi_name,code,num from %2 where com_id=%1 limit 1").arg(comId).arg("audit_workload");
+    QString case_clue = QString("select kpi_name,code,unit,num from %2 where com_id=%1 limit 2").arg(comId).arg("case_clue");
+    QString problem_count = QString("select kpi_name,code,num,evidences from %2 where com_id=%1 limit 7").arg(comId).arg("problem_count");
+    QString problem_money = QString("select kpi_name,code,num,evidences from %2 where com_id=%1 limit 11").arg(comId).arg("problem_money");
+    QString problem_no_money = QString("select kpi_name,code,num,evidences from %2 where com_id=%1 limit 5").arg(comId).arg("problem_no_money");
+    QString problem_rectification = QString("select kpi_name,code,num,evidences from %2 where com_id=%1 limit 7").arg(comId).arg("problem_rectification");
+    QString punish = QString("select kpi_name,code,num from %2 where com_id=%1 limit 4").arg(comId).arg("punish");
+
+    QVector<QVector<QString>> audit_project_res = getResult(audit_project,3);
+    QVector<QVector<QString>> audit_workload_res = getResult(audit_workload,3);
+    QVector<QVector<QString>> case_clue_res = getResult(case_clue,4);
+    QVector<QVector<QString>> problem_count_res = getResult(problem_count,4);
+    QVector<QVector<QString>> problem_money_res = getResult(problem_money,4);
+    QVector<QVector<QString>> problem_no_money_res = getResult(problem_no_money,4);
+    QVector<QVector<QString>> problem_rectification_res = getResult(problem_rectification,4);
+    QVector<QVector<QString>> punish_res = getResult(punish,3);
+    qDebug()<<audit_project_res.size()<<punish_res.size();
+
+    // 鎿嶄綔excel
+    // 鍒涘缓鏂扮殑鐢靛瓙琛ㄦ牸
+//    QAxObject* excel = new QAxObject("Excel.Application");  // 鍒涘缓Excel瀵硅薄
+//    excel->setProperty("Visible", false);  // 璁剧疆Excel涓嶅彲瑙�
+//    QAxObject* workbooks = excel->querySubObject("Workbooks");  // 鑾峰彇鎵�鏈夊伐浣滅翱
+//    QAxObject* workbook = workbooks->querySubObject("Add");  // 娣诲姞鏂板伐浣滅翱
+//    QAxObject* worksheets = workbook->querySubObject("Worksheets");  // 鑾峰彇鎵�鏈夊伐浣滆〃
+//    QAxObject* worksheet = worksheets->querySubObject("Item(int)", 1);  // 鑾峰彇绗竴涓伐浣滆〃
+ //   for (int i = 1; i <= 61; i++) {
+
+//        for (int j = 1; j <= 5; j++) {
+//            QString cellValue = "Value";  // 璁剧疆瑕佸啓鍏ョ殑鍗曞厓鏍煎��
+//            worksheet->querySubObject("Cells(int, int)", i, j)->dynamicCall("SetValue(const QVariant&)", cellValue);  // 鍐欏叆鍗曞厓鏍煎��
+//        }
+    //}
+
+
+    // 鏋勫缓涓�寮犺〃锛�
+//    QString cellValue = "鍗曚綅鍐呴儴瀹¤涓氬姟鎯呭喌琛�";  // 璁剧疆瑕佸啓鍏ョ殑鍗曞厓鏍煎��
+//    worksheet->querySubObject("Cells(int, int)", 1, 1)->dynamicCall("SetValue(const QVariant&)", cellValue);  // 鍐欏叆鍗曞厓鏍煎��
+//    worksheet->querySubObject("Cells(int, int)", 3, 4)->dynamicCall("SetValue(const QVariant&)", "琛�  鍙�:");
+//    worksheet->querySubObject("Cells(int, int)", 3, 5)->dynamicCall("SetValue(const QVariant&)", "鍐呭缁�02琛�");
+
+//    QString path = QDir::currentPath();
+//    qDebug()<<"path:"<<path;
+//    path += "/debug/Data/save2.xls";
+//    qDebug()<<"path:"<<path;
+//    workbook->dynamicCall("SaveAs(const QString&)", path);  // 淇濆瓨宸ヤ綔绨�
+//    workbook->dynamicCall("Close()");  // 鍏抽棴宸ヤ綔绨�
+//    excel->dynamicCall("Quit()");  // 閫�鍑篍xcel搴旂敤绋嬪簭
+//    QMessageBox::information(this,"瀵煎嚭璺緞淇℃伅",path);
+
+    // 鎵撳紑宸插瓨鍦ㄧ殑鐢靛瓙琛ㄦ牸锛岀劧鍚庡線閲屽啓鍏ユ暟鎹�
+    // :/mayi/闄勪欢3_鍐呭缁�02琛╛鍗曚綅鍐呴儴瀹¤涓氬姟鎯呭喌琛�.xls
+
+    QAxObject excel("Excel.Application");
+    excel.setProperty("Visible",false);
+    excel.setProperty("DisplayAlerts",false);
+    //QString str = "D:/Qt/QtP/build-internal_system_v1-Desktop_Qt_5_9_4_MinGW_32bit-Debug/debug/Data/闄勪欢3_鍐呭缁�02琛╛鍗曚綅鍐呴儴瀹¤涓氬姟鎯呭喌琛�.xls";
+    QString str = ui->label_excel_path->text();
+    qDebug()<<"open:"<<str;
+    qDebug()<<"excel:"<<excel.className();
+    QAxObject * wbs = excel.querySubObject("WorkBooks");
+    QAxObject * wb = wbs->querySubObject("Open(QString&)",str);
+    QAxObject * shs = wb->querySubObject("Sheets"); // WorkSheets 涔熷彲浠�
+    int cnt = shs->property("Count").toInt();
+    qDebug()<<"cnt="<<cnt;
+    for(int i=0;i<cnt;++i){
+        QAxObject * sheet = shs->querySubObject("Item(int)", i+1);
+        QString sheetName = sheet->property("Name").toString();
+        qDebug()<<"sh name:"<<sheetName;
+        // 鐒跺悗鍐嶆搷浣滆〃鍐呯殑鏁版嵁
+        QAxObject *usedRange = sheet->querySubObject("UsedRange");
+        int rows = usedRange->querySubObject("Rows")->property("Count").toInt();
+        int columns = usedRange->querySubObject("Columns")->property("Count").toInt();
+        qDebug()<<"r--c:"<<rows<<columns;
+        QVariant  val = usedRange->dynamicCall("Value"); // 鎷垮埌浜岀淮琛ㄦ牸
+        QVariantList valRows = val.toList();
+        qDebug()<<valRows[11].toList().at(4);
+        sheet->querySubObject("Cells(int, int)", 7, 2)->dynamicCall("SetValue(const QVariant&)", ui->comboBox_company->currentText());
+        sheet->querySubObject("Cells(int, int)", 6, 2)->dynamicCall("SetValue(const QVariant&)", m_comCode[ui->comboBox_company->currentText()]);
+
+        // 鍐欏叆瀹¤椤圭洰 涓�
+        qDebug()<<"size:"<<audit_project_res.size();
+        QString it = audit_project_res[0][2];
+        //sheet->querySubObject("Cells(int, int)", 13, 5)->dynamicCall("SetValue(doubule)", 4.5);
+        for(int i=0;i<audit_project_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 11+i, 5)->dynamicCall("SetValue(const QVariant&)", audit_project_res[i][2].toDouble());
+        }
+
+        // 鍐欏叆瀹¤宸ヤ綔閲� 浜�
+        for(int i=0;i<audit_workload_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 21+i, 5)->dynamicCall("SetValue(const QVariant&)", audit_workload_res[i][2].toDouble());
+        }
+        // 鍐欏叆闂閲戦 涓�
+        for(int i=0;i<problem_money_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 22+i, 5)->dynamicCall("SetValue(const QVariant&)", problem_money_res[i][2].toDouble());
+        }
+        // 鍐欏叆闂涓暟 鍥�
+        for(int i=0;i<problem_count_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 33+i, 5)->dynamicCall("SetValue(const QVariant&)", problem_count_res[i][2].toDouble());
+        }
+        // 鍐欏叆闂鏁存敼(閲戦) 浜�
+        for(int i=0;i<problem_rectification_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 40+i, 5)->dynamicCall("SetValue(const QVariant&)", problem_rectification_res[i][2].toDouble());
+        }
+        // 鍐欏叆闂鏁存敼(闈為噾棰�) 鍏�
+        for(int i=0;i<problem_no_money_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 47+i, 5)->dynamicCall("SetValue(const QVariant&)", problem_no_money_res[i][2].toDouble());
+        }
+        // 鍐欏叆澶勫垎 涓�
+        for(int i=0;i<punish_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 52+i, 5)->dynamicCall("SetValue(const QVariant&)", punish_res[i][2].toDouble());
+        }
+        // 鍐欏叆妗堜欢绾跨储 鍏�
+        for(int i=0;i<case_clue_res.size();++i){
+            sheet->querySubObject("Cells(int, int)", 56+i, 5)->dynamicCall("SetValue(const QVariant&)", case_clue_res[i][2].toDouble());
+        }
+
+
+    }
+    //wb->dynamicCall("Save()");  // 鑻ユ枃浠跺凡瀛樺湪锛屽垯鐩存帴淇濆瓨
+    //wb->dynamicCall("SaveAs(const QString&)", str);  // 淇濆瓨宸ヤ綔绨匡紝鑻ユ枃浠朵笉瀛樺湪锛屽垯甯﹀悕瀛椾繚瀛�
+    if(ui->lineEdit_excel_save->text().size() > 1){
+        wb->dynamicCall("SaveAs(const QString&)", ui->lineEdit_excel_save->text());  // 淇濆瓨宸ヤ綔绨匡紝鑻ユ枃浠朵笉瀛樺湪锛屽垯甯﹀悕瀛椾繚瀛�
+        qDebug()<<"淇濆瓨鍒版柊鏂囦欢涓�";
+    }else{
+        wb->dynamicCall("Save()"); // 鐩存帴淇濆瓨鍒版ā鏉挎枃浠朵腑
+        qDebug()<<"淇濆瓨鍒拌�佹枃浠朵腑";
+    }
+
+    wbs->dynamicCall("Close()");
+    excel.dynamicCall("Quit(void)");
+    QMessageBox::information(this,"瀵煎嚭鎴愬姛",QString("瀵煎嚭鎴愬姛锛屾枃浠惰矾寰勪负:%1").arg(ui->lineEdit_excel_save->text()));
+
+
+}
+
+void SearchInfo::on_comboBox_currentIndexChanged(const QString &arg1)
+{
+     m_model->setHorizontalHeaderLabels(m_labels[arg1]);
+}
+
+void SearchInfo::on_toolButton_seleceExcel_clicked()
+{
+    if(ui->comboBox_company->currentText() == "鍏徃鍚嶇О"){
+        QMessageBox::information(this,"瀵煎嚭璁剧疆閿欒","璇烽�夋嫨涓�涓叕鍙稿悕鍐嶈缃�");
+        return;
+    }
+    QString path = QFileDialog::getOpenFileName(this,"閫夋嫨琛ㄦ牸妯℃澘:鍗曚綅鍐呴儴瀹¤涓氬姟鎯呭喌琛�","./","excel(*鍗曚綅鍐呴儴瀹¤涓氬姟鎯呭喌琛�.xls)");
+    ui->label_excel_path->setText(path);
+    QStringList paths = path.split(".");
+    QString newPath = paths.at(0)+"_"+ui->comboBox_company->currentText()+"."+paths.at(1);
+    ui->lineEdit_excel_save->setText(newPath);
+}
+
+void SearchInfo::on_comboBox_company_currentIndexChanged(const QString &arg1)
+{
+    if(ui->lineEdit_excel_save->text().size() > 5){
+        QStringList paths = ui->label_excel_path->text().split(".");
+        QString newPath = paths.at(0)+"_"+arg1+"."+paths.at(1);
+        ui->lineEdit_excel_save->setText(newPath);
+    }
+}
+
+void SearchInfo::addComName(QString name)
+{
+    ui->comboBox_company->addItem(name);
+}
+
+void SearchInfo::on_checkBox_clicked()
+{
+    if(ui->checkBox->isChecked()){
+        ui->dateEdit_begin->setEnabled(true);
+        ui->dateEdit_end->setEnabled(true);
+    }else{
+        ui->dateEdit_begin->setEnabled(false);
+        ui->dateEdit_end->setEnabled(false);
+    }
+}

--
Gitblit v1.8.0