wumu
2025-02-26 637610bcf851d70657c2dcf251d6daed2429a9a5
historydata.cpp
@@ -25,6 +25,8 @@
    // ui相关
    ui->dateEdit->setDate(QDate::currentDate());
    ui->dateEdit_2->setDate(QDate::currentDate());
    m_model = new QStandardItemModel(1,9,this);
    m_customModel = new CustomSortProxyModel; // 可自定义排序的模型
@@ -66,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()
@@ -110,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)
@@ -123,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;
    }
    
}
@@ -215,9 +239,9 @@
        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();
        }
    }
@@ -298,3 +322,96 @@
    //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; // 刷新时间
}