wumu
13 小时以前 99b38ba1c1a960d678775a32de3bbf236d8e78b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "analysisbyrediscache.h"
#include "ui_analysisbyrediscache.h"
#include <QDateTime>
#include <QDebug>
#include <QElapsedTimer>
 
AnalysisByRedisCache::AnalysisByRedisCache(QWidget *parent) :
    QMainWindow(parent),
    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";
    }else{
        qDebug()<<"mysql conn fail";
    }
 
    if(m_dbCache.initRedis("127.0.0.1",6379)){
        qDebug()<<"redis conn ok";
    }else{
        qDebug()<<"redis conn fail";
    }
 
    m_model = new QStandardItemModel(this);
    ui->tableView->setModel(m_model);
 
    QStringList labels;
    labels<<"股票名字"<<"市值"<<"收盘价"<<"涨跌幅"<<"排名"<<"交易时间"<<"排名变化";
    m_model->setHorizontalHeaderLabels(labels);
}
 
AnalysisByRedisCache::~AnalysisByRedisCache()
{
    delete ui;
}
 
void AnalysisByRedisCache::on_pushButton_loadDataSQLRedis_clicked()
{
    QString begin_date = ui->dateEdit->text();
    QString limit_str = ui->comboBox->currentText();
    QString sql = QString("select * from stock_day_info where time_trade > '%1' limit %2;").arg(begin_date).arg(limit_str);
    qDebug()<<"sql:"<<sql;
    QElapsedTimer timer;
    timer.start();
 
    auto result = m_dbCache.query(sql);
 
    qDebug() << "Query executed in" << timer.elapsed() << "ms";
    qDebug() << "Retrieved" << result.size() << "rows";
 
    // 第二次查询应该从缓存获取
    timer.restart();
    result = m_dbCache.query(sql);
    qDebug() << "Cached query executed in" << timer.elapsed() << "ms";
    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;
    timer.start();
    qDebug()<<"m_result size:"<<m_result.size()<<name;
 
    int rank_val = 0;
    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();
            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()));
 
            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();
            if(rank_val != 0){
                double rank_rate = 1 - (amount_rank.toDouble()/rank_val);
                //items.append(new QStandardItem(QString::number(rank_rate)));
                rank_val = amount_rank.toInt();
                QStandardItem * amount_rank_item = new QStandardItem(QString::number(rank_rate));
                if(rank_rate > 0.9){
                    amount_rank_item->setData(QColor("red"),Qt::BackgroundColorRole);
                }
                items.append(amount_rank_item);
 
            }
            if(rank_val == 0 && amount_rank.toInt() != 0){
                rank_val = amount_rank.toInt();
            }
 
            m_model->appendRow(items);
        }
 
    }
    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()
{
    // 单个分析
    QString name = ui->lineEdit_name->text();
    searchAndAnalysisData(name);
 
}
 
void AnalysisByRedisCache::on_pushButton_batchAnalysis_clicked()
{
 
    // 批量分析
    // 重设模型的行数为0,清空一下数据
    m_model->setRowCount(0);
 
    int batch_num = ui->comboBox_batchNum->currentText().toInt(); // 批量分析个股的数量
    // 拿出个股交易额排名的前N名,进行缓存分析
    emit getStockNamesSignal(batch_num);
 
}