#include "importwidget.h"
#include "ui_importwidget.h"
#include <QStandardItemModel>
#include "db/xsqlexcute.h"
#include "db/xwholestocks.h"
#include "db/xwholestockindexs.h"
#include "db/xhighstocks.h"
#include "db/xhighstockindexs.h"
#include <QtDebug>
#include <QFileDialog>
#include "excel/xexcel.h"
#include "xexceltomysq.h"
#include "excel/stocksdecoder.h"
#include "db/stockssqlmanager.h"
#include "db/stockbasissqlmanager.h"
#include "excel/limitupstockdecoder.h"
#include "db/limitupstocksqlmanager.h"
#include "stocksmanager.h"
#include "loadingdialog.h"
#include "limitupstocksmanager.h"

ImportWidget::ImportWidget(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::importWidget)
{
    ui->setupUi(this);
//    initData();
    ui->currentDate->setDate(QDate::currentDate());

//    connect(&dthread,SIGNAL(managerFinished()),)
    dthread.start();


    if(!QSqlDatabase::contains("importWidget"))
    {
        db = QSqlDatabase::addDatabase("QMYSQL","importWidget");
        db.setHostName("192.168.0.201");
        db.setDatabaseName("nstocks");
        db.setUserName("admin");
        db.setPassword("1qazse42W3");
        db.open();
    }
    importModel = new QSqlQueryModel;
    ui->tableView_import->setModel(importModel);
}

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

void ImportWidget::initData()
{
//    QStandardItemModel* model = new QStandardItemModel();
//    ui->tableView->setModel(model);
//    for (int row = 0 ; row < 10; row++)
//    {
//        for (int col = 0; col < 6; ++col)
//        {
//            QStandardItem* newItem =  new QStandardItem(QString::number(col * row));
//            model->setItem(row, col, newItem);
//        }
//    }
}

void ImportWidget::on_pushButton_Stocks_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, QStringLiteral("选择全部A股Excel文件"), "",QStringLiteral("Exel file(*.xls *.xlsx)"));
    ui->lineEdit->setText(filePath);
    if(!filePath.isEmpty())
    {
        StocksManager* sm = new StocksManager;
        sm->setDataDate(ui->currentDate->date().toString("yyyy-MM-dd"));
        sm->setDecodeFile(filePath);
        LoadingDialog ld;
        connect(&dthread,SIGNAL(managerFinished()),&ld,SLOT(accept()));
        dthread.setManager(sm);
        dthread.startManager();
        ld.exec();
    }
    queryStocks();

//    XExcel excel;
//    excel.openExcelCom();
//    QList<QList<QVariant> > excelDatas = excel.readExcel(filePath);
//    excel.closeExcelCom();

//    QString cdate = ui->currentDate->date().toString("yyyy-MM-dd");
//    //测试stocks
//    StocksDecoder* decoder = new StocksDecoder;
//    decoder->decode(excelDatas,cdate);
//    QList<StocksDecoder::_stocksInfo> d = decoder->data;
//    if(!d.isEmpty())
//    {
//        //插入到数据库
//        StocksSqlManager* ssm = new StocksSqlManager;
//        XSqlExcute::instance()->transaction();
//        for(int i = 0 ; i < d.count() ; i++)
//        {
//            QString sql = ssm->toInsertSql(d[i]);
//            qDebug() << sql;
//            XSqlExcute::instance()->query(sql);
//        }
//        XSqlExcute::instance()->commit();
//    }
}

void ImportWidget::on_pushButton_LimitUp_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, QStringLiteral("选择涨停板Excel文件"), "",QStringLiteral("Exel file(*.xls *.xlsx)"));
    ui->lineEdit->setText(filePath);
    if(!filePath.isEmpty())
    {
        LimitUpStocksManager* sm = new LimitUpStocksManager;
        sm->setDataDate(ui->currentDate->date().toString("yyyy-MM-dd"));
        sm->setDecodeFile(filePath);
        LoadingDialog ld;
        connect(&dthread,SIGNAL(managerFinished()),&ld,SLOT(accept()));
        dthread.setManager(sm);
        dthread.startManager();
        ld.exec();
    }
    queryLimitUp();
}

void ImportWidget::queryLimitUp()
{
    importModel->setQuery(QString("select * from stocks_limit_up where trade_day ='%1'").arg(ui->currentDate->date().toString("yyyy-MM-dd")),db);
    importModel->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
    importModel->setHeaderData(1, Qt::Horizontal, QObject::tr("code"));
    importModel->setHeaderData(2, Qt::Horizontal, QObject::tr("trade_day"));
}

void ImportWidget::queryStocks()
{
    importModel->setQuery(QString("select * from stocks where trade_day ='%1'").arg(ui->currentDate->date().toString("yyyy-MM-dd")),db);
    importModel->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
    importModel->setHeaderData(1, Qt::Horizontal, QObject::tr("code"));
    importModel->setHeaderData(2, Qt::Horizontal, QObject::tr("trade_day"));
    importModel->setHeaderData(3, Qt::Horizontal, QObject::tr("open"));
    importModel->setHeaderData(4, Qt::Horizontal, QObject::tr("close"));
    importModel->setHeaderData(5, Qt::Horizontal, QObject::tr("differrange"));
    importModel->setHeaderData(6, Qt::Horizontal, QObject::tr("trade_days"));
    importModel->setHeaderData(7, Qt::Horizontal, QObject::tr("volumn"));
    importModel->setHeaderData(8, Qt::Horizontal, QObject::tr("amount"));
    importModel->setHeaderData(9, Qt::Horizontal, QObject::tr("differrange10"));
    importModel->setHeaderData(10, Qt::Horizontal, QObject::tr("differrange20"));
    importModel->setHeaderData(11, Qt::Horizontal, QObject::tr("differrange60"));
    importModel->setHeaderData(12, Qt::Horizontal, QObject::tr("avg_volume20"));
    importModel->setHeaderData(13, Qt::Horizontal, QObject::tr("freefloat_market_value"));
    importModel->setHeaderData(14, Qt::Horizontal, QObject::tr("total_market_value"));
    importModel->setHeaderData(15, Qt::Horizontal, QObject::tr("agencies_hold"));

}

void ImportWidget::on_pushButton_LimitUp_query_clicked()
{
    queryLimitUp();
}

void ImportWidget::on_pushButton_Stocks_query_clicked()
{
    queryStocks();
}
