#include "stocksmanager.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"

StocksManager::StocksManager()
{

}

void StocksManager::doExcute()
{
    decodeToMysql();
}

void StocksManager::decodeToMysql()
{
    qDebug() << "StocksManager decodeToMysql";
    XExcel excel;
    excel.openExcelCom();
    QList<QList<QVariant> > excelDatas = excel.readExcel(getDecodeFile());
    excel.closeExcelCom();

    QString cdate = getDataDate();
    //测试stocks
    StocksDecoder* decoder = new StocksDecoder;
    decoder->decode(excelDatas,cdate);
    QList<_stocksInfo> d = decoder->data;
    if(!d.isEmpty())
    {
        //插入到数据库
        StocksSqlManager* ssm = new StocksSqlManager;
        StockBasisSqlManager* sbsm = new StockBasisSqlManager;
//        XSqlExcute::instance()->transaction();
        XSqlExcute* sqlIns = new XSqlExcute;
        sqlIns->openMysql("192.168.0.201","nstocks","admin","1qazse42W3","StockBasisSqlManager");
//        sqlIns->transaction("StockBasisSqlManager");
        int count = d.count();
        for(int i = 0 ; i < count ; i++)
        {
            QString sql = ssm->toInsertSql(d[i]);
            qDebug() << sql;
//            XSqlExcute::instance()->query(sql);
            sqlIns->query(sql,"StockBasisSqlManager");
            sql = sbsm->toInsertSql(d[i]);
            qDebug() << sql;
            sqlIns->query(sql,"StockBasisSqlManager");
        }
//        XSqlExcute::instance()->commit();
//        sqlIns->commit("StockBasisSqlManager");
        sqlIns->deleteLater();
    }
}

void StocksManager::getDataFromMysql()
{

}
