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

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

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;
}

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

    CivTrackingAnalysis analySis;
    if (!analySis.createGraphFrom(pgConn))
        return false;

    std::string res;
    std::string strSn = sn;
    if (!analySis.upstreamTracking(strSn, res))
    {
        delete pgConn;
        return false;
    }
       
    strcpy(result, res.c_str());

    delete pgConn;
    return true;
}

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

    CivTrackingAnalysis analySis;
    if (!analySis.createGraphFrom(pgConn))
        return false;

    std::string res;
    std::string strSn = sn;
    if (!analySis.downstreamTracking(strSn, res))
    {
        delete pgConn;
        return false;
    }

    strcpy(result, res.c_str());

    delete pgConn;
    return true;
}

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

    CivTrackingAnalysis analySis;
    if (!analySis.createGraphFrom(pgConn))
        return false;

    std::string res;
    std::string strSn = sn;
    if (!analySis.waterSupplyScopeAnalysis(strSn, res))
    {
        delete pgConn;
        return false;
    }

    strcpy(result, res.c_str());
  
    delete pgConn;
    return true;
}