#include "xexceltomysq.h"
#include <QVariant>
#include <QString>
#include <QDebug>

XStocksDataParser::XStocksDataParser(QObject *parent) : QObject(parent)
{
}

QList<_WholeStocksParam> XStocksDataParser::parseWholeStocks(QList<QList<QVariant> > datas)
{
    QList<_WholeStocksParam> wholeStocksParamList;
    if(datas.count() <= 1)
        return wholeStocksParamList;
    QList<QVariant> headDatas = datas.at(0);
    m_wholeStocksKeyIndexs.clear();
    QString tradeDay = parseWholeStockHead(headDatas);
    if(!tradeDay.isEmpty())
    {

        qDebug() << " datas count :" << datas.count();
        for (int row = 1; row < datas.count();row++)
        {
            QList<QVariant> rowData = datas.at(row);
            _WholeStocksParam param;
            for(int column = 0; column < rowData.count() ; column++)
            {
                if(rowData.at(column).toString().isEmpty())
                {
                    qDebug() << "break";
                    break;
                }
                param.tradeDay = tradeDay;
                addValueToWholeStockParam(param,column,rowData.at(column).toString());
            }
            if(param.code.isEmpty() || param.name.isEmpty())
                continue;
            wholeStocksParamList.append(param);
        }
    }
    return wholeStocksParamList;
}

QList<XWholeStockIndexs::_WholeStockIndexsParam> XStocksDataParser::parseWholeStockIndexs(QList<QList<QVariant> > datas)
{
    QList<XWholeStockIndexs::_WholeStockIndexsParam> wholeStockIndexsParamList;
    if(datas.count() <= 1)
        return wholeStockIndexsParamList;
    QList<QVariant> headDatas = datas.at(0);
    m_wholeStocksKeyIndexs.clear();
    QString tradeDay = parseWholeStockIndexHead(headDatas);
    if(!tradeDay.isEmpty())
    {

        qDebug() << " datas count :" << datas.count();
        for (int row = 1; row < datas.count();row++)
        {
            QList<QVariant> rowData = datas.at(row);
            XWholeStockIndexs::_WholeStockIndexsParam param;
            for(int column = 0; column < rowData.count() ; column++)
            {
                if(rowData.at(column).toString().isEmpty())
                {
                    qDebug() << "break";
                    break;
                }
                param.tradeDay = tradeDay;
                addValueToWholeStockIndexParam(param,column,rowData.at(column).toString());
            }
            if(param.code.isEmpty() || param.name.isEmpty())
                continue;
            wholeStockIndexsParamList.append(param);
        }
    }
    return wholeStockIndexsParamList;
}

QList<_HighStocksParam> XStocksDataParser::parseHightStocks(QList<QList<QVariant> > datas)
{
    QList<_HighStocksParam> highStocksParamList;
    if(datas.count() <= 1)
        return highStocksParamList;
    QList<QVariant> headDatas = datas.at(0);
    m_wholeStocksKeyIndexs.clear();
    QString tradeDay = parseHighStockHead(headDatas);
    if(!tradeDay.isEmpty())
    {

        qDebug() << " datas count :" << datas.count();
        for (int row = 1; row < datas.count();row++)
        {
            QList<QVariant> rowData = datas.at(row);
            _HighStocksParam param;
            for(int column = 0; column < rowData.count() ; column++)
            {
                if(rowData.at(column).toString().isEmpty())
                {
                    qDebug() << "break";
                    break;
                }
                param.tradeDay = tradeDay;
                addValueToHighStockParam(param,column,rowData.at(column).toString());
            }
            if(param.code.isEmpty() || param.name.isEmpty())
                continue;
            highStocksParamList.append(param);
        }
    }
    return highStocksParamList;
}

QList<_HighStockIndexsParam> XStocksDataParser::parseHightStockIndexs(QList<QList<QVariant> > datas)
{
    QList<_HighStockIndexsParam> highStockIndexParamList;
    if(datas.count() <= 1)
        return highStockIndexParamList;
    QList<QVariant> headDatas = datas.at(0);
    m_wholeStocksKeyIndexs.clear();
    QString tradeDay = parseHighStockIndexHead(headDatas);
    if(!tradeDay.isEmpty())
    {

        qDebug() << " datas count :" << datas.count();
        for (int row = 1; row < datas.count();row++)
        {
            QList<QVariant> rowData = datas.at(row);
            _HighStockIndexsParam param;
            for(int column = 0; column < rowData.count() ; column++)
            {
                if(rowData.at(column).toString().isEmpty())
                {
                    qDebug() << "break";
                    break;
                }
                param.tradeDay = tradeDay;
                addValueToHighStockIndexParam(param,column,rowData.at(column).toString());
            }
            if(param.code.isEmpty() || param.name.isEmpty())
                continue;
            highStockIndexParamList.append(param);
        }
    }
    return highStockIndexParamList;
}

QString XStocksDataParser::parseWholeStockHead(QList<QVariant> headDatas)
{
    QString tradeDay = "";
    for(int i = 0; i < headDatas.count(); i++)
    {
        QVariant value = headDatas[i];
        QString headValue = value.toString();
        if(headValue.contains(QStringLiteral("证券代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"code");
        }
        else if(headValue.contains(QStringLiteral("证券名称")))
        {
            m_wholeStocksKeyIndexs.insert(i,"name");
        }
        else if(headValue.contains(QStringLiteral("开盘价")))
        {
//            //开盘价[交易日期]最新(2021-05-20)[复权方式]前复权
            int dayStart = headValue.indexOf("(");
            int dayEnd = headValue.indexOf(")");
            tradeDay = headValue.mid(dayStart+1,dayEnd - dayStart-1);
            m_wholeStocksKeyIndexs.insert(i,"open");
        }
        else if(headValue.contains(QStringLiteral("收盘价")))
        {
            m_wholeStocksKeyIndexs.insert(i,"close");
        }
        else if(headValue.contains(QStringLiteral("涨跌幅")) && !headValue.contains(QStringLiteral("前20个交易日")) && !headValue.contains(QStringLiteral("前60个交易日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange");
        }
        else if(headValue.contains(QStringLiteral("所属东财行业指数")) && !headValue.contains(QStringLiteral("所属东财行业指数代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"BLEMIND2");
        }
        else if(headValue.contains(QStringLiteral("区间涨跌幅")) && headValue.contains(QStringLiteral("前20个交易日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRangeP20");
        }
        else if(headValue.contains(QStringLiteral("区间涨跌幅")) && headValue.contains(QStringLiteral("前60个交易日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRangeP60");
        }
        else if(headValue.contains(QStringLiteral("是否为ST股票")))
        {
            m_wholeStocksKeyIndexs.insert(i,"isSTStock");
        }
        else if(headValue.contains(QStringLiteral("是否为*ST股票")))
        {
            m_wholeStocksKeyIndexs.insert(i,"isSTXStock");
        }
        else if(headValue.contains(QStringLiteral("首发上市日期")))
        {
            m_wholeStocksKeyIndexs.insert(i,"listDate");
        }
        else if(headValue.contains(QStringLiteral("区间可交易日数")))
        {
            m_wholeStocksKeyIndexs.insert(i,"tradeDaysP");
        }
        else if(headValue.contains(QStringLiteral("成交量")) && !headValue.contains(QStringLiteral("区间日均成交量")))
        {
            m_wholeStocksKeyIndexs.insert(i,"volumn");
        }
        else if(headValue.contains(QStringLiteral("成交额")))
        {
            m_wholeStocksKeyIndexs.insert(i,"amount");
        }
        else if(headValue.contains(QStringLiteral("所属东财行业指数代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"BLEMINDCODE2");
        }
        else if(headValue.contains(QStringLiteral("自由流通市值")))
        {
            m_wholeStocksKeyIndexs.insert(i,"freeFloatMv");
        }
        else if(headValue.contains(QStringLiteral("总市值")))
        {
            m_wholeStocksKeyIndexs.insert(i,"mvByCSRC");
        }
        else if(headValue.contains(QStringLiteral("区间日均成交量")))
        {
            m_wholeStocksKeyIndexs.insert(i,"avgVolumeP20");
        }
        qDebug() << "parseWholeStockHead : " <<m_wholeStocksKeyIndexs[i];
    }
    return tradeDay;
}

QString XStocksDataParser::parseWholeStockIndexHead(QList<QVariant> headDatas)
{
    QString tradeDay = "";
    for(int i = 0; i < headDatas.count(); i++)
    {
        QVariant value = headDatas[i];
        QString headValue = value.toString();
        if(headValue.contains(QStringLiteral("证券代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"code");
        }
        else if(headValue.contains(QStringLiteral("证券名称")))
        {
            m_wholeStocksKeyIndexs.insert(i,"name");
        }
        else if(headValue.contains(QStringLiteral("成份个数")))
        {
            m_wholeStocksKeyIndexs.insert(i,"componentNum");
        }
        else if(headValue.contains(QStringLiteral("开盘价")))
        {
//            //开盘价[交易日期]最新(2021-05-20)[复权方式]前复权
            int dayStart = headValue.indexOf("(");
            int dayEnd = headValue.indexOf(")");
            tradeDay = headValue.mid(dayStart+1,dayEnd - dayStart-1);
            m_wholeStocksKeyIndexs.insert(i,"open");
        }
        else if(headValue.contains(QStringLiteral("收盘价")))
        {
            m_wholeStocksKeyIndexs.insert(i,"close");
        }
        else if(headValue.contains(QStringLiteral("涨跌幅")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange");
        }
        else if(headValue.contains(QStringLiteral("成交量")))
        {
            m_wholeStocksKeyIndexs.insert(i,"volumn");
        }
        else if(headValue.contains(QStringLiteral("成交额")))
        {
            m_wholeStocksKeyIndexs.insert(i,"amount");
        }
        else if(headValue.contains(QStringLiteral("自由流通市值")))
        {
            m_wholeStocksKeyIndexs.insert(i,"freeFloatMv");
        }
        else if(headValue.contains(QStringLiteral("总市值")))
        {
            m_wholeStocksKeyIndexs.insert(i,"mvByCSRC");
        }
        qDebug() << "parseWholeStockIndexHead : " <<m_wholeStocksKeyIndexs[i];
    }
    return tradeDay;
}

QString XStocksDataParser::parseHighStockHead(QList<QVariant> headDatas)
{
    QString tradeDay = "";
    for(int i = 0; i < headDatas.count(); i++)
    {
        QVariant value = headDatas[i];
        QString headValue = value.toString();
        if(headValue.contains(QStringLiteral("证券代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"code");
        }
        else if(headValue.contains(QStringLiteral("证券名称")))
        {
            m_wholeStocksKeyIndexs.insert(i,"name");
        }
        else if(headValue.contains(QStringLiteral("开盘价")))
        {
//            //开盘价[交易日期]最新(2021-05-20)[复权方式]前复权
            int dayStart = headValue.indexOf("(");
            int dayEnd = headValue.indexOf(")");
            tradeDay = headValue.mid(dayStart+1,dayEnd - dayStart-1);
            m_wholeStocksKeyIndexs.insert(i,"open");
        }
        else if(headValue.contains(QStringLiteral("收盘价")))
        {
            m_wholeStocksKeyIndexs.insert(i,"close");
        }
        else if(headValue.contains(QStringLiteral("最高价")) && !headValue.contains(QStringLiteral("区间最高价日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"high");
        }
        else if(headValue.contains(QStringLiteral("涨跌幅")) && !headValue.contains(QStringLiteral("前20个交易日")) && !headValue.contains(QStringLiteral("前60个交易日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange");
        }
        else if(headValue.contains(QStringLiteral("成交量")) && !headValue.contains(QStringLiteral("区间日均成交量")))
        {
            m_wholeStocksKeyIndexs.insert(i,"volumn");
        }
        else if(headValue.contains(QStringLiteral("近期创阶段新高")))
        {
            m_wholeStocksKeyIndexs.insert(i,"isHighRangeP");
        }
        else if(headValue.contains(QStringLiteral("所属东财行业指数")) && !headValue.contains(QStringLiteral("所属东财行业指数代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"BLEMIND2");
        }
        else if(headValue.contains(QStringLiteral("区间最高价日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"HpDateP");
        }
        qDebug() << "parseWholeStockHead : " <<m_wholeStocksKeyIndexs[i];
    }
    return tradeDay;
}

QString XStocksDataParser::parseHighStockIndexHead(QList<QVariant> headDatas)
{
    QString tradeDay = "";
    for(int i = 0; i < headDatas.count(); i++)
    {
        QVariant value = headDatas[i];
        QString headValue = value.toString();
        if(headValue.contains(QStringLiteral("证券代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"code");
        }
        else if(headValue.contains(QStringLiteral("证券名称")))
        {
            m_wholeStocksKeyIndexs.insert(i,"name");
        }
        else if(headValue.contains(QStringLiteral("开盘价")))
        {
//            //开盘价[交易日期]最新(2021-05-20)[复权方式]前复权
            int dayStart = headValue.indexOf("(");
            int dayEnd = headValue.indexOf(")");
            tradeDay = headValue.mid(dayStart+1,dayEnd - dayStart-1);
            m_wholeStocksKeyIndexs.insert(i,"open");
        }
        else if(headValue.contains(QStringLiteral("收盘价")))
        {
            m_wholeStocksKeyIndexs.insert(i,"close");
        }
        else if(headValue.contains(QStringLiteral("涨跌幅")) && !headValue.contains(QStringLiteral("前20个交易日")) && !headValue.contains(QStringLiteral("前60个交易日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange");
        }
        else if(headValue.contains(QStringLiteral("近期创阶段新高")))
        {
            m_wholeStocksKeyIndexs.insert(i,"isHighRangeP");
        }
        else if(headValue.contains(QStringLiteral("区间最高价日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"HpDateP");
        }
        else if(headValue.contains(QStringLiteral("涨停家数")))
        {
            m_wholeStocksKeyIndexs.insert(i,"LIMITUPNUM");
        }
        else if(headValue.contains(QStringLiteral("总市值")))
        {
            m_wholeStocksKeyIndexs.insert(i,"mvByCSRC");
        }
        else if(headValue.contains(QStringLiteral("成份个数")))
        {
            m_wholeStocksKeyIndexs.insert(i,"componentNum");
        }
        qDebug() << "parseWholeStockHead : " <<m_wholeStocksKeyIndexs[i];
    }
    return tradeDay;
}

void XStocksDataParser::addValueToWholeStockParam(_WholeStocksParam &param ,int index,QString value)
{
    if(m_wholeStocksKeyIndexs[index] == "code")
    {
        param.code = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "name")
    {
        param.name = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "open")
    {
//        param.open = value.toDouble();
        param.open = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "close")
    {
//        param.close = value.toDouble();
        param.close = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange")
    {
//        param.differRange = value.toDouble();
        param.differRange = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "BLEMIND2")
    {
        param.BLEMIND2 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRangeP20")
    {
//        param.differRangeP20 = value.toDouble();
        param.differRangeP20 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRangeP60")
    {
//        param.differRangeP60 = value.toDouble();
        param.differRangeP60 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "isSTStock")
    {
//        param.isSTStock = value.toInt();
        param.isSTStock = value == QStringLiteral("是") ? 1 : 0;
    }
    else if(m_wholeStocksKeyIndexs[index] == "isSTXStock")
    {
        param.isSTXStock = value == QStringLiteral("是") ? 1 : 0;
    }
    else if(m_wholeStocksKeyIndexs[index] == "listDate")
    {
        param.listDate = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "tradeDaysP")
    {
//        param.tradeDaysP = value.toInt();
        param.tradeDaysP = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "volumn")
    {
//        param.volumn = value.toDouble();
        param.volumn = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "amount")
    {
//        param.amount = value.toDouble();
        param.amount = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "BLEMINDCODE2")
    {
        param.BLEMINDCODE2 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "freeFloatMv")
    {
//        param.freeFloatMv = value.toDouble();
        param.freeFloatMv = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "mvByCSRC")
    {
//        param.mvByCSRC = value.toDouble();
        param.mvByCSRC = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "avgVolumeP20")
    {
//        param.avgVolumeP20 = value.toDouble();
        param.avgVolumeP20 = value;
    }
}

void XStocksDataParser::addValueToWholeStockIndexParam(XWholeStockIndexs::_WholeStockIndexsParam &param ,int index,QString value)
{
    if(m_wholeStocksKeyIndexs[index] == "code")
    {
        param.code = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "name")
    {
        param.name = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "componentNum")
    {
//        param.componentNum = value.toInt();
        param.componentNum = value;
//        qDebug() << "componentNum : " << value << "  toInt : " << param.componentNum;
    }
    else if(m_wholeStocksKeyIndexs[index] == "open")
    {
//        param.open = value.toDouble();
        param.open = value;
//        qDebug() << "open : " << param.open;
    }
    else if(m_wholeStocksKeyIndexs[index] == "close")
    {
//        param.close = value.toDouble();
        param.close = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange")
    {
//        param.differRange = value.toDouble();
        param.differRange = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "volumn")
    {
//        param.volumn = value.toDouble();
        param.volumn = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "amount")
    {
//        param.amount = value.toDouble();
        param.amount = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "freeFloatMv")
    {
//        param.freeFloatMv = value.toDouble();
        param.freeFloatMv = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "mvByCSRC")
    {
//        param.mvByCSRC = value.toDouble();
        param.mvByCSRC = value;
    }
}

void XStocksDataParser::addValueToHighStockParam(_HighStocksParam &param ,int index,QString value)
{
    if(m_wholeStocksKeyIndexs[index] == "code")
    {
        param.code = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "name")
    {
        param.name = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "open")
    {
//        param.open = value.toDouble();
        param.open = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "close")
    {
//        param.close = value.toDouble();
        param.close = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "high")
    {
//        param.high = value.toDouble();
        param.high = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange")
    {
//        param.differRange = value.toDouble();
        param.differRange = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "volumn")
    {
//        param.volumn = value.toDouble();
        param.volumn = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "isHighRangeP")
    {
//        param.isHighRangeP = value.toInt();
        param.isHighRangeP = value == QStringLiteral("是") ? 1 : 0;
    }
    else if(m_wholeStocksKeyIndexs[index] == "BLEMIND2")
    {
        param.BLEMIND2 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "HpDateP")
    {
        param.HpDateP = value;
    }
}

void XStocksDataParser::addValueToHighStockIndexParam(_HighStockIndexsParam &param ,int index,QString value)
{
    if(m_wholeStocksKeyIndexs[index] == "code")
    {
        param.code = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "name")
    {
        param.name = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "open")
    {
//        param.open = value.toDouble();
        param.open = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "close")
    {
//        param.close = value.toDouble();
        param.close = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange")
    {
//        param.differRange = value.toDouble();
        param.differRange = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "isHighRangeP")
    {
//        param.isHighRangeP = value.toInt();
        param.isHighRangeP = value == QStringLiteral("是") ? 1 : 0;
    }
    else if(m_wholeStocksKeyIndexs[index] == "HpDateP")
    {
//        param.HpDateP = value;
        param.HpDateP = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "LIMITUPNUM")
    {
        param.LIMITUPNUM = value.toInt();
    }
    else if(m_wholeStocksKeyIndexs[index] == "mvByCSRC")
    {
        param.mvByCSRC = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "componentNum")
    {
        param.COMPONENTNUM = value.toInt();
    }
}
