#include "trendwidet.h"
#include "ui_trendwidet.h"
#include "trendmanager.h"
#include "loadingdialog.h"
#include <QStandardItemModel>
#include <QSqlQuery>
#include <QStandardItem>
#include <QDebug>
#include "trendtablemodel.h"

trendWidet::trendWidet(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::trendWidet)
{
    ui->setupUi(this);
    if(!QSqlDatabase::contains("trendWidet"))
    {
        db = QSqlDatabase::addDatabase("QMYSQL","trendWidet");
        db.setHostName("192.168.0.201");
        db.setDatabaseName("nstocks");
        db.setUserName("admin");
        db.setPassword("1qazse42W3");
        db.open();
    }

    trend10Model = new QSqlQueryModel;
    ui->tableView_trend10->setModel(trend10Model);
    trend20Model = new QSqlQueryModel;
    ui->tableView_trend20->setModel(trend20Model);
    stocks10Model = new QSqlQueryModel;
    ui->tableView_stocks10->setModel(stocks10Model);
    stocks20Model = new QSqlQueryModel;
    ui->tableView_stocks20->setModel(stocks20Model);

    ui->currentDate->setDate(QDate::currentDate());

    dthread.start();

//    trend20_Model = new QStandardItemModel(ui->tableView_trend20);
//    trend20_Model->setColumnCount(3);
//    trend20_Model->setHeaderData(0,Qt::Horizontal,QStringLiteral("名称"));
//    trend20_Model->setHeaderData(1,Qt::Horizontal,QDate::currentDate().toString("yyyy-MM-dd"));
//    trend20_Model->setHeaderData(2,Qt::Horizontal,QDate::currentDate().addDays(-1).toString("yyyy-MM-dd"));
//    ui->tableView_trend20->setModel(trend20_Model);

    m_trend20_Model = new TrendTableModel(ui->tableView_trend20);
    m_trend20_Model->setColumnCount(3);
    m_trend20_Model->setHeaderData(0,Qt::Horizontal,QStringLiteral("名称"));
    m_trend20_Model->setHeaderData(1,Qt::Horizontal,QDate::currentDate().toString("yyyy-MM-dd"));
    m_trend20_Model->setHeaderData(2,Qt::Horizontal,QDate::currentDate().addDays(-1).toString("yyyy-MM-dd"));
    ui->tableView_trend20->setModel(m_trend20_Model);

}

trendWidet::~trendWidet()
{
    dthread.terminate();
    delete ui;
}

void trendWidet::on_pushButton_trend_clicked()
{
    TrendManager* tm = new TrendManager;
    tm->setDataDate(ui->currentDate->date().toString("yyyy-MM-dd"));
    tm->addParam("date",ui->currentDate->date().toString("yyyy-MM-dd"));
    tm->addParam("limitCount","590");
    tm->addParam("type","10");
    LoadingDialog ld;
    connect(&dthread,SIGNAL(managerFinished()),&ld,SLOT(accept()));
//    //10日
//    //获取stocks中所有数据
//    tm->setExcuteType(DataManagerBase::getData);
//    dthread.setManager(tm);
//    dthread.startManager();
//    ld.exec();
//    //简单筛选排序后数据插入数据库
//    tm->setExcuteType(DataManagerBase::decode);
//    dthread.setManager(tm);
//    dthread.startManager();
//    ld.exec();

    //20日
    tm->addParam("type","20");
    //获取stocks中所有数据
    tm->setExcuteType(DataManagerBase::getData);
    dthread.setManager(tm);
    dthread.startManager();
    ld.exec();
    //简单筛选排序后数据插入数据库
    tm->setExcuteType(DataManagerBase::decode);
    dthread.setManager(tm);
    dthread.startManager();
    ld.exec();
    queryTrends(ui->currentDate->date().toString("yyyy-MM-dd"),"20");
    queryTrends(ui->currentDate->date().addDays(-1).toString("yyyy-MM-dd"),"20");
    QStringList dates;
    dates.append(ui->currentDate->date().toString("yyyy-MM-dd"));
    dates.append(ui->currentDate->date().addDays(-1).toString("yyyy-MM-dd"));
    updateViews(dates);
}

void trendWidet::on_pushButton_query_clicked()
{
//    queryStocks("10");
//    queryStocks("20");
//    queryTrends("10");
//    queryTrends("20");
//    trend20_Model->setHeaderData(1,Qt::Horizontal,ui->currentDate->date().toString("yyyy-MM-dd"));
//    trend20_Model->setHeaderData(2,Qt::Horizontal,ui->currentDate->date().addDays(-1).toString("yyyy-MM-dd"));
    m_trend20_Model->setHeaderData(1,Qt::Horizontal,ui->currentDate->date().toString("yyyy-MM-dd"));
    m_trend20_Model->setHeaderData(2,Qt::Horizontal,ui->currentDate->date().addDays(-1).toString("yyyy-MM-dd"));

    queryTrends(ui->currentDate->date().toString("yyyy-MM-dd"),"20");
    queryTrends(ui->currentDate->date().addDays(-1).toString("yyyy-MM-dd"),"20");

    QStringList dates;
    dates.append(ui->currentDate->date().toString("yyyy-MM-dd"));
    dates.append(ui->currentDate->date().addDays(-1).toString("yyyy-MM-dd"));
    updateViews(dates);
}

void trendWidet::queryStocks(QString type)
{

}

void trendWidet::queryTrends(QString date,QString type)
{
    QSqlQuery q(QSqlDatabase::database("trendWidet"));
    QString sql = QString("select * from trends WHERE trade_day = '%1' and type = '%2' ORDER BY sort ASC;").arg(date).arg(type);
    qDebug() << sql;
    q.exec(sql);
    while (q.next())
    {
        trends t;
        t.blemind2 = q.value("blemind2").toString();
        t.tradeDay = q.value("trade_day").toString();
        t.stocksCount = q.value("stocks_count").toString();
        t.trendsValue = q.value("trend_value").toString();
        t.trendsValueChanged = q.value("trend_value_change").toString();
        t.sort = q.value("sort").toString();
        t.sortChanged = q.value("sort_change").toString();
        t.type = q.value("type").toString();
        if(trends20.contains(t.blemind2))
        {
            trends20[t.blemind2].append(t);
        }
        else
        {
            QList<trends> tl;
            tl.append(t);
            trends20.insert(t.blemind2,tl);
        }
    }
}

void trendWidet::updateViews(QStringList dates)
{
//    int row = trend20_Model->rowCount();
    int row = m_trend20_Model->rowCount();
    QHashIterator<QString,QList<trends> > ti(trends20);
    while (ti.hasNext()) {
        ti.next();
        QList<trends> l = ti.value();
//        trend20_Model->setItem(row,0,new QStandardItem(ti.key()));
        m_trend20_Model->addData(row,0,ti.key());
        for (int di = 0; di < dates.size(); ++di)
        {
            for (int i = 0 ; i < l.count() ; i++)
            {
                if(l[i].tradeDay == dates[di])
                {
//                    trend20_Model->setItem(row,di+1,new QStandardItem(l[i].sort));
                    m_trend20_Model->addData(row,di+1,l[i].sort);
                    break;
                }
            }
        }
        row++;
    }

}
