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

#pragma hdrstop

#include "CDSVmsTrafficF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TVmsTraffic::TVmsTraffic()
{
}
//---------------------------------------------------------------------------
TVmsTraffic::~TVmsTraffic()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TVmsTrafficManager *VmsTrafficManager = NULL;
//---------------------------------------------------------------------------

/*
* Link Traffic Manager
*/
TVmsTrafficManager::TVmsTrafficManager()
{
}
//---------------------------------------------------------------------------
TVmsTrafficManager::~TVmsTrafficManager()
{
}
//---------------------------------------------------------------------------
void TVmsTrafficManager::Init()
{
    FLink = false;
    FIfsc = false;
    FRoad = false;
}
//---------------------------------------------------------------------------
bool TVmsTrafficManager::IsLoading()
{
    return (FLink && FIfsc && FRoad) ? true : false;
}
//---------------------------------------------------------------------------
bool TVmsTrafficManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

//ITSUtil_Trace("TVmsTrafficManager::LoadFromDb - start");

    sQry = "SELECT 2 AS LEVL, A.IFSC_ID AS LINKID,  NVL(B.SPED, 0) AS SPEED, NVL(CMTR_GRAD_CD, 'LTC0') CMTR_GRAD_CD, \r\n"
           "       DECODE(CMTR_GRAD_CD, 'LTC1', '1', 'LTC2', '2', 'LTC3', '3', '0') TRF_GRADE,                       \r\n"
           "       NVL(B.PRCN_DT, '19000101000000')     AS REGDATE,                                                  \r\n"
           "       NVL(B.DATA_NUM, 0) AS DATACNT,                                                                    \r\n"
           "       NVL(B.TRVL_HH, 0) AS TRVTM                                                                        \r\n"
           "  FROM TB_IFSC A,                                                                                        \r\n"
           "       (SELECT *                                                                                         \r\n"
           "          FROM TB_IFSC_TRAF                                                                              \r\n"
           //"         WHERE PRCN_DT > TO_CHAR(SYSDATE -  5 / 1440, 'YYYYMMDDHH24MISS')                                \r\n"
           "       ) B                                                                                               \r\n"
           " WHERE A.IFSC_ID = B.IFSC_ID(+)                                                                          \r\n";
    try
    {
        FLists.Lock();
        FLists.RemoveAll();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
				TVmsTraffic *pTraffic = new TVmsTraffic();

				pTraffic->LEVL         = pADO->FieldByName("LEVL")->AsInteger;
				pTraffic->LINKID       = pADO->FieldByName("LINKID")->AsString;
				pTraffic->SPEED        = pADO->FieldByName("SPEED")->AsInteger;
				pTraffic->CMTR_GRAD_CD = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
				pTraffic->TRF_GRADE    = pADO->FieldByName("TRF_GRADE")->AsString;
				pTraffic->REGDATE      = pADO->FieldByName("REGDATE")->AsString;
				pTraffic->DATACNT      = pADO->FieldByName("DATACNT")->AsInteger;
				pTraffic->TRVTM        = pADO->FieldByName("TRVTM")->AsInteger;

				FLists.Push(pTraffic->LINKID, pTraffic);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("소통정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("소통정보조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

