From 99b38ba1c1a960d678775a32de3bbf236d8e78b7 Mon Sep 17 00:00:00 2001
From: wumu <mayi@mayi.com>
Date: 星期四, 21 八月 2025 23:53:04 +0800
Subject: [PATCH] 250821 缓存分析优化显示

---
 analysisbyrediscache.cpp |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/analysisbyrediscache.cpp b/analysisbyrediscache.cpp
index 24bd35b..f9fff34 100644
--- a/analysisbyrediscache.cpp
+++ b/analysisbyrediscache.cpp
@@ -1,5 +1,6 @@
 锘�#include "analysisbyrediscache.h"
 #include "ui_analysisbyrediscache.h"
+#include <QDateTime>
 #include <QDebug>
 #include <QElapsedTimer>
 
@@ -8,6 +9,8 @@
     ui(new Ui::AnalysisByRedisCache)
 {
     ui->setupUi(this);
+
+    ui->dateEdit->setDate(QDate::currentDate().addMonths(-1));
 
     if(m_dbCache.initMySQL("127.0.0.1",3306,"root","root","stock_plan")){
         qDebug()<<"mysql conn ok";
@@ -23,6 +26,10 @@
 
     m_model = new QStandardItemModel(this);
     ui->tableView->setModel(m_model);
+
+    QStringList labels;
+    labels<<"鑲$エ鍚嶅瓧"<<"甯傚��"<<"鏀剁洏浠�"<<"娑ㄨ穼骞�"<<"鎺掑悕"<<"浜ゆ槗鏃堕棿"<<"鎺掑悕鍙樺寲";
+    m_model->setHorizontalHeaderLabels(labels);
 }
 
 AnalysisByRedisCache::~AnalysisByRedisCache()
@@ -51,6 +58,39 @@
     m_result = result;
 }
 
+// 婊戝姩绐楀彛璁$畻鎺掑悕鍙樺寲
+void AnalysisByRedisCache::calcWindowRank(int start)
+{
+
+    // 婊戝姩绐楀彛鎺掑悕锛�6涓2+鐨勫墠100
+    QVector<int> ranks;
+    for(int i=start;i<m_model->rowCount();++i){
+        ranks.append(m_model->item(i,4)->text().toInt());
+        if(i >= 6+start){
+            // 鍏堢粺璁℃帓鍚嶈鏁�
+            int cntBig=0, cntSmall=0;
+            for(int j=0;j<ranks.size();++j){
+                if(ranks.at(j) > 100){
+                    cntBig++;
+                }else{
+                    cntSmall++;
+                }
+            }
+            // 缁熻瀵规瘮
+            if(cntSmall == 2 && ranks.back() <= 100){
+                // 灏嗗綋鍓�6涓厓绱犲畾涓虹鍚堢殑绐楀彛锛岃儗鏅壊璁剧疆涓虹传鑹�
+                for(int k=0;k<6;++k){
+                    m_model->item(i-k,4)->setData(QColor("red"),Qt::BackgroundColorRole);
+                }
+            }
+
+            // 娓呴櫎绐楀彛绗竴涓厓绱�
+            ranks.pop_front();
+        }
+
+    }
+
+}
 void AnalysisByRedisCache::searchAndAnalysisData(QString name)
 {
     QElapsedTimer timer;
@@ -61,14 +101,27 @@
     int cnt = 0;
     for(auto it:m_result){
         if(it["name"].toString() == name){
-            qDebug()<<it["name"].toString()<<it["close"].toString()<<it["amount_rank"].toString()<<it["time_trade"].toString();
+//            qDebug()<<it["name"].toString()<<it["close"].toString()<<it["amount_rank"].toString()<<it["time_trade"].toString();
             cnt++;
             QList<QStandardItem*> items;
             items.append(new QStandardItem(it["name"].toString()));
             double market_capital = it["market_capital"].toDouble() / 100000000;
             items.append(new QStandardItem(QString::number(market_capital)));
             items.append(new QStandardItem(it["close"].toString()));
-            items.append(new QStandardItem(it["percent"].toString()));
+
+            QStandardItem *percentItem = new QStandardItem(it["percent"].toString());
+            if(percentItem->text().toDouble() > 0){
+                percentItem->setData(QColor("red"),Qt::DecorationRole); // 娣诲姞瑁呴グ棰滆壊涓虹孩鑹�
+                percentItem->setData(QColor("red"),Qt::TextColorRole); // 娣诲姞鏂囨湰棰滆壊涓虹孩鑹�
+                items.at(0)->setData(QColor("red"),Qt::TextColorRole); // 璁剧疆鑲$エ鍚嶅瓧涓虹孩鑹�
+
+            }else if(percentItem->text().toDouble() < 0){
+                percentItem->setData(QColor("green"),Qt::DecorationRole); // 娣诲姞瑁呴グ棰滆壊涓虹孩鑹�
+                percentItem->setData(QColor("green"),Qt::TextColorRole); // 璁剧疆鑳屾櫙棰滆壊涓虹豢鑹�
+                items.at(0)->setData(QColor("green"),Qt::TextColorRole); // 璁剧疆鑲$エ鍚嶅瓧涓虹豢鑹�
+            }
+            items.append(percentItem);
+            //items.append(new QStandardItem(it["percent"].toString()));
             items.append(new QStandardItem(it["amount_rank"].toString()));
             items.append(new QStandardItem(it["time_trade"].toString()));
             QString amount_rank = it["amount_rank"].toString();
@@ -93,6 +146,8 @@
     }
     qDebug()<<"cnt="<<cnt;
     qDebug() << "Query executed in" << timer.elapsed() << "ms";
+    qDebug()<<"婊戝姩绐楀彛鎺掑悕-cnt:"<<m_model->rowCount()<<cnt<<"start:"<<m_model->rowCount() - cnt + 1;
+    calcWindowRank(m_model->rowCount() - cnt + 1);
 }
 void AnalysisByRedisCache::on_pushButton_analysisByRedis_clicked()
 {

--
Gitblit v1.8.0