| | |
| | | #include "analysisbyrediscache.h" |
| | | #include "ui_analysisbyrediscache.h" |
| | | #include <QDateTime> |
| | | #include <QDebug> |
| | | #include <QElapsedTimer> |
| | | |
| | |
| | | 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"; |
| | |
| | | |
| | | m_model = new QStandardItemModel(this); |
| | | ui->tableView->setModel(m_model); |
| | | |
| | | QStringList labels; |
| | | labels<<"股票名字"<<"市值"<<"收盘价"<<"涨跌幅"<<"排名"<<"交易时间"<<"排名变化"; |
| | | m_model->setHorizontalHeaderLabels(labels); |
| | | } |
| | | |
| | | AnalysisByRedisCache::~AnalysisByRedisCache() |
| | |
| | | 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; |
| | |
| | | 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(); |
| | |
| | | } |
| | | 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() |
| | | { |