//---------------------------------------------------------------------------


#pragma hdrstop

#include "CDSRoadF.h"
#include "CDSLayerF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TItsRoad::TItsRoad()
{
    Clear();
}
//---------------------------------------------------------------------------
void TItsRoad::Clear()
{
}
//---------------------------------------------------------------------------
TItsRoad::~TItsRoad()
{
}
//---------------------------------------------------------------------------
void TItsRoad::InitTraffic()
{
    TRAF->InitTraffic();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsRoadManager *ItsRoadManager = NULL;
//---------------------------------------------------------------------------

/*
* Road Manager
*/
TItsRoadManager::TItsRoadManager()
{
}
//---------------------------------------------------------------------------
TItsRoadManager::~TItsRoadManager()
{
}
//---------------------------------------------------------------------------
void TItsRoadManager::InitTraffic()
{
    try
    {
        FLists.Lock();

        FOR_STL(TItsRoad*, pObj, FLists)
        {
            pObj->InitTraffic();
        }
    }
    __finally
    {
        FLists.UnLock();
    }
}
//---------------------------------------------------------------------------
bool TItsRoadManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    FLists.RemoveAll();

    return (LoadRoad(ADbConn) && LoadIfscList(ADbConn));
}
//---------------------------------------------------------------------------
bool TItsRoadManager::LoadRoad(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT ROAD_ID      ,                              \r\n"
           "       ROAD_NM      ,                              \r\n"
           "       DRCT_CD      ,                              \r\n"
           "       STRT_NM      ,                              \r\n"
           "       END_NM       ,                              \r\n"
           "       DEL_YN       ,                              \r\n"
           "       SECT_LNGT    ,                              \r\n"
           "       F_NODE_ID    ,                              \r\n"
           "       T_NODE_ID    ,                              \r\n"
           "       SECT_GRAD_CD ,                              \r\n"
           "       AREA_CD      ,                              \r\n"
           "       DECODE(AREA_CD, 'LAT001', 'Y', 'N') REGN_YN \r\n"
           "  FROM TB_ROAD                                     \r\n"
           " WHERE DEL_YN = 'N'                                \r\n";

    FLists.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
				TItsRoad *pRoad = new TItsRoad();

                pRoad->IsVertex     = false;
                pRoad->ROAD_ID      = pADO->FieldByName("ROAD_ID")->AsString;
                pRoad->ROAD_NM      = pADO->FieldByName("ROAD_NM")->AsString.Trim();
                pRoad->DRCT_CD      = pADO->FieldByName("DRCT_CD")->AsString;
                pRoad->STRT_NM      = pADO->FieldByName("STRT_NM")->AsString.Trim();
                pRoad->END_NM       = pADO->FieldByName("END_NM")->AsString.Trim();
                pRoad->DEL_YN       = pADO->FieldByName("DEL_YN")->AsString;
                pRoad->SECT_LNGT    = pADO->FieldByName("SECT_LNGT")->AsString;
                pRoad->F_NODE_ID    = pADO->FieldByName("F_NODE_ID")->AsString;
                pRoad->T_NODE_ID    = pADO->FieldByName("T_NODE_ID")->AsString;
                pRoad->SECT_GRAD_CD = pADO->FieldByName("SECT_GRAD_CD")->AsString;
                pRoad->AREA_CD      = pADO->FieldByName("AREA_CD")->AsString;
                pRoad->REGN_YN      = pADO->FieldByName("REGN_YN")->AsString;
                //pRoad->REGN_YN          = "Y";

				FLists.Push(pRoad->ROAD_ID, pRoad);
                pRoad->Completed = true;

                pRoad->TRAF = new TCDSTraffic();
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsRoadManager::LoadRoad", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsRoadManager::LoadRoad", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TItsRoadManager::LoadIfscList(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT ROAD_ID, IFSC_ID, ORD \r\n"
           "  FROM TB_ROAD_RLTN_IFSC     \r\n";

    TItsRoad *pRoad = NULL;
    TItsIfsc *pIfsc;
    TItsLink *pLink;

    FLists.Lock();
    try
    {
        try
        {
            int nORD;

            pADO = new TADOQuery(NULL);
            pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
            pADO->SQL->Clear();
            pADO->SQL->Text = sQry;

            pADO->Prepared = true;
            pADO->Open();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sROAD_ID = pADO->FieldByName("ROAD_ID")->AsString;
                pRoad = FLists.Find(sROAD_ID);
                if (!pRoad) continue;

                String sIFSC_ID = pADO->FieldByName("IFSC_ID")->AsString;
				nORD            = pADO->FieldByName("ORD")->AsInteger;
                pRoad->FIfscs.insert(std::make_pair<int, String>(nORD, sIFSC_ID));

                pIfsc = ItsIfscManager->FLists.Find(sIFSC_ID);
                if (pIfsc)
                {
                    pIfsc->ROAD_ID = sROAD_ID;

                    std::map<int, String>::iterator itPos2;
                    for(itPos2 = pIfsc->FLinks.begin(); itPos2 != pIfsc->FLinks.end(); ++itPos2)
                    {
                        String sLinkId = itPos2->second;
                        pLink = ItsLinkManager->FLists.Find(sLinkId);
                        if (pLink) pLink->ROAD_ID = sROAD_ID;
                    }
                }

#if 0
				String sORD     = pADO->FieldByName("ORD")->AsString;
                TSubRoad *pSubRoad = new TSubRoad();
                pSubRoad->FIFSC_ID = sIFSC_ID;
                pRoad->FIfscs.Push(sORD, pSubRoad);
#endif
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsRoadManager::LoadIfscList", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsRoadManager::LoadIfscList", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return LoadVrtxFromDb(ADbConn);
}
//---------------------------------------------------------------------------
bool TItsRoadManager::LoadVrtxFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT A.ROAD_ID, A.LEVL,           \r\n"
           "       A.X_CRDN_ARR, A.Y_CRDN_ARR   \r\n"
           "  FROM TB_ROAD_VRTX_ARR A,          \r\n"
           "       TB_ROAD          B           \r\n"
           " WHERE B.DEL_YN  = 'N'              \r\n"
           "   AND B.ROAD_ID = A.ROAD_ID        \r\n"
           "   AND A.LEVL    = 5                \r\n";
#else
    sQry = "SELECT *                 \r\n"
           "  FROM VW_ROAD_VRTX_ARR  \r\n";
#endif

    TItsRoad* pObj;
//    int       LEVL;

    FLists.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String ROAD_ID = pADO->FieldByName("ROAD_ID")->AsString;
                pObj = FLists.Find(ROAD_ID);
                if (!pObj) continue;

#if 0
                LEVL = pADO->FieldByName("LEVL")->AsInteger;
                TItsVrtx* pVrtx = new TItsVrtx();
                pVrtx->X_CRDN_ARR = pADO->FieldByName("X_CRDN_ARR")->AsString;
                pVrtx->Y_CRDN_ARR = pADO->FieldByName("Y_CRDN_ARR")->AsString;

				pObj->Vrtxs.Push(LEVL, pVrtx);
#else
                pObj->IsVertex   = true;
                pObj->X_CRDN_ARR = pADO->FieldByName("X_CRDN_ARR")->AsString;
                pObj->Y_CRDN_ARR = pADO->FieldByName("Y_CRDN_ARR")->AsString;
#endif
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsRoadManager::LoadVrtxFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsRoadManager::LoadVrtxFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TItsRoadManager::LoadTraffic(TADOConnection *ADbConn/*=NULL*/)
{
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    InitTraffic();

    sQry = "SELECT A.ROAD_ID, PRCN_DT, TFVL, SPED, PTRN_SPED, OCPY_RATE, TRVL_HH, CMTR_GRAD_CD, DATA_NUM, \r\n"
           "       (CASE WHEN PRCN_DT <= TO_CHAR(SYSDATE - 10/1440, 'YYYYMMDDHH24MISS')                   \r\n"
           "             THEN 'Y' ELSE 'N' END) AS MISSING,                                               \r\n"
           "       (CASE WHEN PRCN_DT >= TO_CHAR(SYSDATE - 15/1440, 'YYYYMMDDHH24MISS')                   \r\n"
           "             THEN 'Y' ELSE 'N' END) AS LASTANS,                                               \r\n"
           "       (SELECT MAX(PRCN_DT) FROM TB_ROAD_TRAF) MPRCN_DT                                       \r\n"
           "  FROM TB_ROAD_TRAF A, TB_ROAD B                                                              \r\n"
           " WHERE A.ROAD_ID = B.ROAD_ID                                                                  \r\n";

    FLists.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String ID = pADO->FieldByName("ROAD_ID")->AsString;
                TItsRoad *pObj = FLists.Find(ID);
                if (!pObj) continue;

                pObj->TRAF->PRCN_DT      = pADO->FieldByName("PRCN_DT")->AsString;
                pObj->TRAF->TFVL         = pADO->FieldByName("TFVL")->AsInteger;
                pObj->TRAF->SPED         = pADO->FieldByName("SPED")->AsInteger;
                pObj->TRAF->OCPY_RATE    = pADO->FieldByName("OCPY_RATE")->AsFloat;
                pObj->TRAF->TRVL_HH      = pADO->FieldByName("TRVL_HH")->AsInteger;
                pObj->TRAF->CMTR_GRAD_CD = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
				pObj->TRAF->MISSING      = pADO->FieldByName("MISSING")->AsString;       // 결측여부
				pObj->TRAF->LASTANS      = pADO->FieldByName("LASTANS")->AsString;       // 최근 15분 이내 가공
				pObj->TRAF->MPRCN_DT     = pADO->FieldByName("MPRCN_DT")->AsString;      // 가공시각(최신)
                pObj->TRAF->DATA_NUM     = pADO->FieldByName("DATA_NUM")->AsInteger;
                pObj->TRAF->PTRN_SPED    = pADO->FieldByName("PTRN_SPED")->AsInteger;

                if (pObj->TRAF->CMTR_GRAD_CD == "LTC0") pObj->TRAF->MISSING = "Y";

                if      (pObj->TRAF->CMTR_GRAD_CD == "LTC1") pObj->TRAF->TRAF_GRAD = 1;
                else if (pObj->TRAF->CMTR_GRAD_CD == "LTC2") pObj->TRAF->TRAF_GRAD = 2;
                else if (pObj->TRAF->CMTR_GRAD_CD == "LTC3") pObj->TRAF->TRAF_GRAD = 3;
                else                                         pObj->TRAF->TRAF_GRAD = 0;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsRoadManager::LoadTraffic", String(E.ClassName()), E.Message, sQry);
            return false;
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsRoadManager::LoadTraffic", String(exception.ClassName()), exception.Message, sQry);
            return false;
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------

