#include "mainwindow.h"
#include "ui_mainwindow.h"
#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 "loadingdialog.h"

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    m_importWidget = new ImportWidget;
    m_todayWidget = new TodayWidget;
    m_historyWidget = new HistoryWidget;
    m_importWidget = ui->iwidget;

//    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//    db.setHostName("127.0.0.1");
//    db.setDatabaseName("nstocks");
//    db.setUserName("admin");
//    db.setPassword("1qazse42W3");
//    db.open();
//    XSqlExcute::instance()->openMysql("127.0.0.1","nstocks","admin","1qazse42W3");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::testStocks()
{
    QString filePath = QFileDialog::getOpenFileName(this, QStringLiteral("选择Excel文件"), "",QStringLiteral("Exel file(*.xls *.xlsx)"));
    XExcel excel;
    excel.openExcelCom();
    QList<QList<QVariant> > excelDatas = excel.readExcel(filePath);
    excel.closeExcelCom();

    //测试stocks
    StocksDecoder* decoder = new StocksDecoder;
    decoder->decode(excelDatas,"2021-11-25");
    QList<_stocksInfo> d = decoder->data;
    if(!d.isEmpty())
    {
        qDebug() << d[0].code << " "
                              << d[0].name << " "
                              << d[0].tradeDay << " "
                              << d[0].open << " "
                              << d[0].close << " "
                              << d[0].differRange << " "
                              << d[0].blemind2 << " "
                              << d[0].blemind3 << " "
                              << d[0].differRangeP10 << " "
                              << d[0].differRangeP20 << " "
                              << d[0].differRangeP60 << " "
                              << d[0].listDate << " "
                              << d[0].tradeDays << " "
                              << d[0].volumn << " "
                              << d[0].amount << " "
                              << d[0].freeFloatMarketValue << " "
                              << d[0].totalMarketValue << " "
                              << d[0].agenciesHold << " "
                              << d[0].avgVolume20;
        //插入到数据库
        StocksSqlManager* ssm = new StocksSqlManager;
        QString sql = ssm->toInsertSql(d[0]);
        qDebug() << "sql : " << sql;
        StockBasisSqlManager* sbsm = new StockBasisSqlManager;
        QString bsql = sbsm->toInsertSql(d[0]);
        qDebug() << "bsql : " << bsql;
    }
    return;
}

void MainWindow::testLimitUpStocks()
{
    QString filePath = QFileDialog::getOpenFileName(this, QStringLiteral("选择Excel文件"), "",QStringLiteral("Exel file(*.xls *.xlsx)"));
    XExcel excel;
    excel.openExcelCom();
    QList<QList<QVariant> > excelDatas = excel.readExcel(filePath);
    excel.closeExcelCom();
    //测试limitup
    LimitUpStockDecoder* decoder = new LimitUpStockDecoder;
    decoder->decode(excelDatas);
    QList<_limitUpStocksInfo> d = decoder->data;
    if(!d.isEmpty())
    {
        qDebug() << d[0].code << " "
                              << d[0].tradeDay << " ";
        //插入到数据库
        LimitUpStockSqlManager* ssm = new LimitUpStockSqlManager;
        QString sql = ssm->toInsertSql(d[0]);
        qDebug() << "sql : " << sql;
    }
    return;
}

void MainWindow::on_pushButton_import_clicked()
{
}

void MainWindow::on_pushButton_today_clicked()
{
}

void MainWindow::on_pushButton_history_clicked()
{
}


void MainWindow::on_pushButton_7_clicked()
{
    LoadingDialog ld;
    ld.exec();
    return;
    testStocks();
    return;
    //    QList<QString> fileNames;
    QString filePath = QFileDialog::getOpenFileName(this, QStringLiteral("选择Excel文件"), "",QStringLiteral("Exel file(*.xls *.xlsx)"));
    //    fileNames.append(filePath);
    //    filePath = QFileDialog::getOpenFileName(this, QStringLiteral("选择Excel文件"), "",QStringLiteral("Exel file(*.xls *.xlsx)"));
    //    fileNames.append(filePath);
    XExcel excel;
    excel.openExcelCom();
    //    excel.readExcels(fileNames);
    QList<QList<QVariant> > excelDatas = excel.readExcel(filePath);
    excel.closeExcelCom();

    XStocksDataParser stocksParser;
    if(filePath.contains(QStringLiteral("动量原始股")))
    {
        //全A
        QList<_WholeStocksParam> wholeStocks = stocksParser.parseWholeStocks(excelDatas);
        if(wholeStocks.count() > 0)
        {
            qDebug() << " wholeStocks count :" << wholeStocks.count();
            XWholeStocks stock;
            XSqlExcute::instance()->transaction("mainWindow");
            for(int i = 0 ; i < wholeStocks.count() ; i++)
            {
                _WholeStocksParam wp = wholeStocks.at(i);
                QString sql = stock.toInsertSql(wp);
                qDebug() << sql;
                XSqlExcute::instance()->query(sql,"mainWindow");
            }
            XSqlExcute::instance()->commit("mainWindow");

            //计算统计值
            QString totalSql = QString("INSERT INTO wholeaindex (tradeDay,COMPONENTNUM,amount,freeFloatMv,mvByCSRC) "
                                       "SELECT tradeDay,COUNT(`code`),SUM(amount ),SUM(freeFloatMv),SUM(mvByCSRC)"
                                       "FROM wholestocks WHERE tradeDay = '%1';").arg(wholeStocks.at(0).tradeDay);
            XSqlExcute::instance()->transaction("mainWindow");
            XSqlExcute::instance()->query(totalSql,"mainWindow");
            XSqlExcute::instance()->commit("mainWindow");

        }
    }
    else if(filePath.contains(QStringLiteral("东财二级行业指数每日成交情况")))
    {
        //全部指数
        QList<XWholeStockIndexs::_WholeStockIndexsParam> wholeStockIndexs = stocksParser.parseWholeStockIndexs(excelDatas);
        if(wholeStockIndexs.count() > 0)
        {
            qDebug() << " wholeStocks count :" << wholeStockIndexs.count();
            XWholeStockIndexs stockIndex;
            XSqlExcute::instance()->transaction("mainWindow");
            for(int i = 0 ; i < wholeStockIndexs.count() ; i++)
            {
                XWholeStockIndexs::_WholeStockIndexsParam wp = wholeStockIndexs.at(i);
                QString sql = stockIndex.toInsertSql(wp);
                qDebug() << sql;
                XSqlExcute::instance()->query(sql,"mainWindow");
            }
            XSqlExcute::instance()->commit("mainWindow");
        }
    }
    else if(filePath.contains(QStringLiteral("创300天新高")))
    {
        //创新高个股
        QList<_HighStocksParam> wholeStockIndexs = stocksParser.parseHightStocks(excelDatas);
        if(wholeStockIndexs.count() > 0)
        {
            qDebug() << " wholeStocks count :" << wholeStockIndexs.count();
            XHighStocks stockIndex;
            XSqlExcute::instance()->transaction("mainWindow");
            for(int i = 0 ; i < wholeStockIndexs.count() ; i++)
            {
                _HighStocksParam wp = wholeStockIndexs.at(i);
                QString sql = stockIndex.toInsertSql(wp);
                qDebug() << sql;
                XSqlExcute::instance()->query(sql,"mainWindow");
            }
            XSqlExcute::instance()->commit("mainWindow");
        }
    }
    else if(filePath.contains(QStringLiteral("东财二级创250天新高")))
    {
        //创新高指数
        QList<_HighStockIndexsParam> wholeStockIndexs = stocksParser.parseHightStockIndexs(excelDatas);
        if(wholeStockIndexs.count() > 0)
        {
            qDebug() << " wholeStocks count :" << wholeStockIndexs.count();
            XHighStockIndexs stockIndex;
            XSqlExcute::instance()->transaction("mainWindow");
            for(int i = 0 ; i < wholeStockIndexs.count() ; i++)
            {
                _HighStockIndexsParam wp = wholeStockIndexs.at(i);
                QString sql = stockIndex.toInsertSql(wp);
                qDebug() << sql;
                XSqlExcute::instance()->query(sql,"mainWindow");
            }
            XSqlExcute::instance()->commit("mainWindow");
        }
    }

}

