#include "stocksdecoder.h"
#include <QDebug>

StocksDecoder::StocksDecoder()
{
}

void StocksDecoder::decode(QList<QList<QVariant> > edatas,QString date)
{
    if(edatas.count() <= 1)
        return;
    QList<QVariant> headDatas = edatas.at(0);
    data.clear();
    QString tradeDay = parseStockHead(headDatas);
    if(tradeDay.isEmpty())
        tradeDay = date;
    if(!tradeDay.isEmpty())
    {
        qDebug() << " datas count :" << edatas.count();
        for (int row = 1; row < edatas.count();row++)
        {
            QList<QVariant> rowData = edatas.at(row);
            _stocksInfo param;
            for(int column = 0; column < rowData.count() ; column++)
            {
                if(rowData.at(column).toString().isEmpty())
                {
                    qDebug() << "break";
                    break;
                }
                param.tradeDay = tradeDay;
                toValueStockInfo(param,column,rowData.at(column).toString());
            }
            if(param.code.isEmpty() || param.name.isEmpty())
                continue;
            data.append(param);
        }
    }
}

QString StocksDecoder::parseStockHead(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,"open");
//            if(headValue.contains(QStringLiteral("最新")))
//            {

//            }
//            else
//            {
//                //开盘价[交易日期]最新(2021-05-20)[复权方式]前复权
//                int dayStart = headValue.indexOf("(");
//                int dayEnd = headValue.indexOf(")");
//                tradeDay = headValue.mid(dayStart+1,dayEnd - dayStart-1);
//            }
        }
        else if(headValue.contains(QStringLiteral("收盘价")))
        {
            m_wholeStocksKeyIndexs.insert(i,"close");
        }
        else if(headValue.contains(QStringLiteral("涨跌幅")) &&
                !headValue.contains(QStringLiteral("前10个交易日")) &&
                !headValue.contains(QStringLiteral("前20个交易日")) &&
                !headValue.contains(QStringLiteral("前60个交易日")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange");
        }
        else if(headValue.contains(QStringLiteral("所属东财行业指数")) &&
                headValue.contains(QStringLiteral("2级")) &&
                !headValue.contains(QStringLiteral("所属东财行业指数代码")))
        {
            m_wholeStocksKeyIndexs.insert(i,"blemind2");
        }
        else if(headValue.contains(QStringLiteral("所属东财行业")) &&
                headValue.contains(QStringLiteral("3级")))
        {
            m_wholeStocksKeyIndexs.insert(i,"blemind3");
        }
        else if(headValue.contains(QStringLiteral("区间涨跌幅")) &&
                headValue.contains(QStringLiteral("前10")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange10");
        }
        else if(headValue.contains(QStringLiteral("区间涨跌幅")) &&
                headValue.contains(QStringLiteral("前20")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange20");
        }
        else if(headValue.contains(QStringLiteral("区间涨跌幅")) &&
                headValue.contains(QStringLiteral("前60")))
        {
            m_wholeStocksKeyIndexs.insert(i,"differRange60");
        }
        else if(headValue.contains(QStringLiteral("首发上市日期")))
        {
            m_wholeStocksKeyIndexs.insert(i,"listDate");
        }
        else if(headValue.contains(QStringLiteral("区间可交易日数")))
        {
            m_wholeStocksKeyIndexs.insert(i,"tradeDays");
        }
        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,"freeFloatMarketValue");
        }
        else if(headValue.contains(QStringLiteral("总市值")))
        {
            m_wholeStocksKeyIndexs.insert(i,"totalMarketValue");
        }
        else if(headValue.contains(QStringLiteral("区间日均成交量")))
        {
            m_wholeStocksKeyIndexs.insert(i,"avgVolume20");
        }
        else if(headValue.contains(QStringLiteral("机构持股比例合计")))
        {
            m_wholeStocksKeyIndexs.insert(i,"agenciesHold");
        }
        qDebug() << "parseWholeStockHead : " <<m_wholeStocksKeyIndexs[i];
    }
    return tradeDay;
}

void StocksDecoder::toValueStockInfo(_stocksInfo &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] == "blemind3")
    {
        param.blemind3 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange10")
    {
//        param.differRangeP20 = value.toDouble();
        param.differRangeP10 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange20")
    {
//        param.differRangeP20 = value.toDouble();
        param.differRangeP20 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "differRange60")
    {
//        param.differRangeP60 = value.toDouble();
        param.differRangeP60 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "listDate")
    {
        param.listDate = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "tradeDays")
    {
//        param.tradeDaysP = value.toInt();
        param.tradeDays = 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] == "freeFloatMarketValue")
    {
//        param.freeFloatMv = value.toDouble();
        param.freeFloatMarketValue = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "totalMarketValue")
    {
//        param.mvByCSRC = value.toDouble();
        param.totalMarketValue = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "avgVolume20")
    {
//        param.avgVolumeP20 = value.toDouble();
        param.avgVolume20 = value;
    }
    else if(m_wholeStocksKeyIndexs[index] == "agenciesHold")
    {
//        param.avgVolumeP20 = value.toDouble();
        param.agenciesHold = value;
    }
}

