wumu
2025-02-26 637610bcf851d70657c2dcf251d6daed2429a9a5
historydata.cpp
@@ -19,6 +19,28 @@
{
    ui->setupUi(this);
    // 注册元类型
    qRegisterMetaType<QVector<QVector<QString>>>("QVector<QVector<QString>>");
    qRegisterMetaType<QVector<QVector<QString>>>("QVector<QVector<QString>> &");
    // ui相关
    ui->dateEdit->setDate(QDate::currentDate());
    ui->dateEdit_2->setDate(QDate::currentDate());
    m_model = new QStandardItemModel(1,9,this);
    m_customModel = new CustomSortProxyModel; // 可自定义排序的模型
    m_customModel->setSourceModel(m_model); // 设置源模型
    //ui->tableView->setModel(m_model);
    ui->tableView->setModel(m_customModel);
    // 表格列排序
    ui->tableView->setSortingEnabled(true);
    QStringList labels;
    labels<<"股票名字"<<"股票代号"<<"市值(亿)"<<"涨跌幅"<<"收盘价"<<"成交额(亿)"<<"成交量"<<"换手率"<<"PE_TTM";
    m_model->setHorizontalHeaderLabels(labels);
    initMySQL(); // 初始化MySQL
    // 处理数据爬取的操作
@@ -46,7 +68,19 @@
    // 去请求首页
    m_manager.get(m_request);
    // 股票池相关
    m_poolModel = new QStandardItemModel(1,9,this);
    QStringList poolLabel;
    poolLabel<<"id"<<"股票名字"<<"股票代号"<<"监控记录时间"<<"监控买入价格"<<"当前价格"<<"盈亏百分比"<<"监控卖出价格"<<"监控卖出时间";
    m_poolModel->setHorizontalHeaderLabels(poolLabel);
    ui->tableView_2->setModel(m_poolModel);
    // 股票池定时刷新
    m_poolTimer = new QTimer(this);
    connect(m_poolTimer,SIGNAL(timeout()),this,SLOT(poolTimerSlot()));
    m_poolTimer->start(10*1000);
}
HistoryData::~HistoryData()
@@ -90,9 +124,10 @@
{
    // 把5000支股票挨个获取到,然后进行数据保存,存到表格中
    // 更新的时候,默认支持10年的数据查询
    if(m_days <= 0) return;
    QString time_tar=QString::number(QDateTime::currentMSecsSinceEpoch());
    QString dayCnt=QString::number(3000);
    QString dayCnt=QString::number(m_days);
    QString type = "day"; // day week month
    for(auto code:m_codeNames.keys()){
        QString url = QString("https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=%1&begin=%2&period=%3&type=before&count=-%4&indicator=kline,pe,market_capital,ma").arg(code)
@@ -103,6 +138,15 @@
        m_manager.get(m_request);
        //break; // 测试用
    }
    // 更新时间
    QString curDate = QDate::currentDate().toString("yyyy-MM-dd");
    QString sql = QString("insert into days_info (log_time) values ('%1')").arg(curDate);
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"update log time ok:"<<curDate;
    }else{
        qDebug()<<"update log time fail:"<<curDate;
    }
    
}
@@ -195,10 +239,179 @@
        QSqlQuery que(db);
        if(que.exec(sql)){
            qDebug()<<"intsert ok";
            qDebug()<<"insert ok";
        }else{
            qDebug()<<"intsert fail"<<que.lastError().text();
            qDebug()<<"insert fail"<<que.lastError().text();
        }
    }
}
void HistoryData::on_pushButton_search_clicked()
{
    // 提示查询等待
    ui->statusbar->showMessage("查询中...请耐心等待结果...");
    QString searchTime = ui->dateEdit->text();
    qDebug()<<"查询的时间:"<<searchTime;
    QString sql = QString("select name,code,market_capital,percent,close,amount,volume,turnover_rate,pe_ttm from stock_day_info where time_trade = '%1'").arg(searchTime);
    qDebug()<<"sql:"<<sql;
    m_modelDatas.clear(); // 清空数组中的数据
    qint64 tt = QDateTime::currentMSecsSinceEpoch();
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"select ok";
        m_model->setRowCount(0); // 重置模型行数
        int rows = 0;
        while (que.next()) {
            QString name = que.value(0).toString();
            QString code = que.value(1).toString();
            QString market_capital = QString::number(que.value(2).toDouble()/100000000);
            QString percent = que.value(3).toString();
            QString close = que.value(4).toString();
            QString amount = QString::number(que.value(5).toDouble()/100000000);
            QString volume = que.value(6).toString();
            QString turnover_rate = que.value(7).toString();
            QString pe_ttm = que.value(8).toString();
            m_modelDatas.append({name,code,market_capital,percent,close,amount,volume,turnover_rate,pe_ttm});
            QList<QStandardItem*> items;
            items.append(new QStandardItem(name));
            items.append(new QStandardItem(code));
            items.append(new QStandardItem(market_capital));
            //items.append(new QStandardItem(percent));
            QStandardItem *percentItem =  new QStandardItem(percent);
            if(percent.toDouble() > 0){
                percentItem->setData(QColor("red"),Qt::DecorationRole); // 添加一个装饰的颜色为红色
                percentItem->setData(QColor("red"),Qt::TextColorRole);  // 将字体颜色设置为红色
                items.at(0)->setData(QColor("red"),Qt::TextColorRole);  // 将股票名字设置为红色
             }
            else if(percent.toDouble() < 0){
                percentItem->setData(QColor("green"),Qt::BackgroundColorRole);
                items.at(0)->setData(QColor("green"),Qt::TextColorRole);
            }
            items.append(percentItem);
            items.append(new QStandardItem(close));
            items.append(new QStandardItem(amount));
            items.append(new QStandardItem(volume));
            items.append(new QStandardItem(turnover_rate));
            items.append(new QStandardItem(pe_ttm));
            m_model->appendRow(items);
            rows++;
        }
        qDebug()<<"查询到行数:"<<rows;
    }else{
        qDebug()<<"select fail"<<que.lastError().text();
    }
    // 最后提示完成
    qint64 need = QDateTime::currentMSecsSinceEpoch()-tt;
    ui->statusbar->showMessage(QString("耗时:%1 毫秒,查询完成.").arg(QString::number(need)),10*1000);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //emit sendHistoryModel(m_modelDatas); // 给信息展示发模型
    ui->textBrowser->append(QString("查询日期:%1 耗时: %2 毫秒 查到条数:%3").arg(searchTime).arg(QString::number(need)).arg(QString::number(m_model->rowCount())));
}
void HistoryData::on_pushButton_2_clicked()
{
    //emit sendHistoryModel(m_modelDatas); // 给信息展示发模型
    emit sendHistoryModel(m_model);
}
void HistoryData::on_pushButton_addStock_clicked()
{
    QString name = ui->lineEdit->text();
    QString code = ui->lineEdit_2->text();
    QString price = ui->lineEdit_3->text();
    QString in_date = ui->dateEdit_2->text();
    qDebug()<<name<<code<<price<<in_date;
    QString sql = QString("insert into stock_pool (name,code,in_price,in_time) values('%1','%2',%3,'%4')").arg(name).arg(code).arg(price).arg(in_date);
    qDebug()<<"sql:"<<sql;
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"insert ok";
        poolTimerSlot(); // 立马刷新股票池的模型,刷新视图
    }else{
        qDebug()<<"insert fail";
    }
}
void HistoryData::poolTimerSlot()
{
    QString sql = "select * from stock_pool where state=1";
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"select ok:"<<QDateTime::currentDateTime();
        m_poolModel->setRowCount(0);
        while (que.next()) {
            QList<QStandardItem*> items;
            for(int i=0;i<9;++i){
                QString item = que.value(i).toString();
                QStandardItem *it = new QStandardItem(item);
                if(i==6){
                    if(item.toDouble() > 0){
                        it->setData(QColor("red"),Qt::DecorationRole); // 添加一个装饰的颜色为红色
                        it->setData(QColor("red"),Qt::TextColorRole);  // 将字体颜色设置为红色
                        items.at(1)->setData(QColor("red"),Qt::TextColorRole);  // 将股票名字设置为红色
                    }else if(item.toDouble() < 0){
                        it->setData(QColor("green"),Qt::BackgroundColorRole);
                        items.at(1)->setData(QColor("green"),Qt::TextColorRole);
                    }
                }
                items.append(it);
            }
            m_poolModel->appendRow(items); // 整行添加 效率更高
        }
    }else{
        qDebug()<<"select fail";
    }
    m_poolTimer->setInterval(60*1000);
}
void HistoryData::on_checkBox_clicked()
{
    QString dt = "2025-02-10";
    QString sql = "select log_time from days_info order by log_time desc";
    QSqlQuery que(db);
    if(que.exec(sql)){
        if (que.next()) {
            dt = que.value(0).toString();
        }else{
            qDebug()<<"查询结果集为空";
            return;
        }
    }else{
        qDebug()<<"查询失败";
        return ;
    }
    if(ui->checkBox->isChecked()){
        ui->pushButton_update->setEnabled(true);
    }else{
        ui->pushButton_update->setEnabled(false);
    }
    QDate d1 = QDate::fromString(dt,"yyyy-MM-dd");
    QDate d2 = QDate::currentDate();
    int days = 0; // 经过了多少个周内的天数
    for(QDate begin = d1.addDays(1);begin <= d2;begin = begin.addDays(1)){
        if(begin.dayOfWeek() != Qt::Saturday && begin.dayOfWeek() != Qt::Sunday){
            days++;
        }
    }
    qDebug()<<"days:"<<days;
    m_days = days; // 刷新时间
}