#include "CivHydrFuncInter.h"
#include "CivHydrCalc.h"
#include "CivSysLog.h"
#include "CivTableTemp.h"

#include "CivPgConn.h"
#include "CivHydrSimulation.h"

bool DLLEXPORT simulation(char* uri, char* netName, char* flag)
{

    char fileName[256];
    char rptR[256];
    char outBinFile[256];

    strcpy_s(fileName, "test.inp");
    strcpy_s(rptR, "test.rpt");
    strcpy_s(outBinFile, "test.bin");

    CivHydrCalc* calc = new CivHydrCalc(uri, netName);
    CivSysLog::getInstance()->info("开始", "interface", __FUNCTION__);
    bool msg = false;
    if (strcmp(flag,"HDY")==0)
        msg = calc->hydrSimulation( fileName, rptR, outBinFile);
    else if(strcmp(flag, "QUALITY")==0)
        msg = calc->qualitySimulation(fileName, rptR, outBinFile);

    if (msg)
    {
        CivSysLog::getInstance()->info("成功", "interface", __FUNCTION__);
    }       
    else
    {
        CivSysLog::getInstance()->info("失败", "interface", __FUNCTION__);
    }
    delete calc;
    return msg;
}

bool DLLEXPORT getDataByInterval(char* uri, char* date, char* interval)
{
    CivDbConn* dbConn = new CivPgConn(uri);
    if (!dbConn->open())
    {
        delete dbConn;
        return false;
    }
       
    if (!dbConn->updateNodeByInterval(date, interval))
    {
        delete dbConn;
        return false;
    }
       
    if (!dbConn->updateLinkByInterval(date, interval))
    {
        delete dbConn;
        return false;
    }
    delete dbConn;

    return true;
}

bool DLLEXPORT hdyrSimulation(char* uri)
{
    // 创建数据库连接
    CivDbConn* pgConn = new CivPgConn(uri);

    // 模拟计算对象
    CivHydrSimulation* mHyDr = new CivHydrSimulation(pgConn);
   
    //开始模拟
    int flag = mHyDr->hdyrSimulation();

    delete mHyDr;
    delete pgConn;
   
    return flag;
}

bool DLLEXPORT qualitySimulation(char* uri)
{
    // 创建数据库连接
    CivDbConn* pgConn = new CivPgConn(uri);

    // 模拟计算对象
    CivHydrSimulation* mHyDr = new CivHydrSimulation();
    mHyDr->registDb(pgConn);

    //开始模拟
    int flag = mHyDr->qualitySimulation();

    delete pgConn;
    delete mHyDr;

    return flag;
}

bool DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
{

    // 创建数据库连接
    CivDbConn* pgConn = new CivPgConn(uri);

    // 模拟计算对象
    CivHydrSimulation* mHyDr = new CivHydrSimulation();
    mHyDr->registDb(pgConn);

    //开始模拟
    bool flag = mHyDr->trackingSimulation(sN, hours);
    std::string strResult;

    if (flag)
    {       
        mHyDr->getTrackingResult(hours, strResult);     
        strcpy(result, strResult.c_str());
    }
  
    delete pgConn;
    delete mHyDr;

    return  flag;
}

bool DLLEXPORT getRptByCondition(char* uri, char* type, char* condition, char* result)
{
    CivDbConn* dbConn = new CivPgConn(uri);
    if (!dbConn->open())
    {
        delete dbConn;
        return false;
    }

    std::string table;
    std::vector<std::string> fileds;
    if (type == PIPENODE)
    {

        table = "__node_result__";
        CivNodeResultTableTemp::NodeResultTable tableTemp;

        fileds.push_back(tableTemp.Number);
        fileds.push_back(tableTemp.dDemand);
        fileds.push_back(tableTemp.dHead);
        fileds.push_back(tableTemp.dPressure);
        fileds.push_back(tableTemp.dElevation);
        fileds.push_back(tableTemp.dQuality);

    }
    else if (type == PIPELINE)
    {
        table = "__pipe_result__";
        CivPipeResultTableTemp::PipeResultTable pipeTable;
        
        fileds.push_back(pipeTable.szNo);
        fileds.push_back(pipeTable.dFlow);
        fileds.push_back(pipeTable.dVelocity);
        fileds.push_back(pipeTable.dHeadloss);
        fileds.push_back(pipeTable.dUnitHeadloss);
        fileds.push_back(pipeTable.dLocalHeadloss);
        fileds.push_back(pipeTable.dFrictionHeadloss);
        fileds.push_back(pipeTable.dQuality);
    }
    else
    {
        delete dbConn;
        return false;
    }
 
    std::vector<std::map<std::string,std::string>> res;
    if (!dbConn->getValuesFromTableByCondition(table, condition, fileds, res))
    {
        delete dbConn;
        return false;
    }
    delete dbConn;

    // 拼接字符串
    size_t total = res.size();
    std::string strRes;
    strRes.append("[");
    for (int i = 0; i < total; i++)
    {
        std::map<std::string, std::string> tempMap = res[i];
        strRes.append("{");
        for (auto iter = tempMap.begin(); iter != tempMap.end(); iter++)
        {
            strRes.append(iter->first);
            strRes.append(":");
            strRes.append(iter->second);
            strRes.append(",");
        }
        strRes = strRes.substr(0, strRes.length() - 1);
        strRes.append("},");
        
    }
    strRes = strRes.substr(0, strRes.length() - 1);
    strRes.append("]");
    std::cout << strRes << std::endl;

    int leng = strRes.length();
    for (int i = 0; i < leng; i++)
    {
        result[i] = strRes[i];
    }
    return true;
}