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

#pragma hdrstop

#include "CDSIfscF.h"
#include "CDSCtlrF.h"
#include "CDSManagerF.h"
#include "VMSCommLibF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TCDSIfsc::TCDSIfsc()
{
    CNGST_CNT = 0;

    Init();
}
//---------------------------------------------------------------------------

TCDSIfsc::~TCDSIfsc()
{
}
//---------------------------------------------------------------------------
void TCDSIfsc::Init()
{
    PRCN_DT      = "";
    CMTR_GRAD_CD = "0";
    SPED         = 0;
    TRVL_HH      = 0;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TCDSIfscManager *CDSIfscManager = NULL;
//---------------------------------------------------------------------------

TCDSIfscManager::TCDSIfscManager()
{
}
//---------------------------------------------------------------------------
TCDSIfscManager::~TCDSIfscManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
int TCDSIfscManager::LoadVmsIfscInfo(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    int        nSelCnt = 0;

    try
    {
        CtlrItr it;
        for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
        {
            TCDSCtlr *pObj = (TCDSCtlr*)it->second;

            //기존 제공구간정보를 모두 삭제
            //pObj->FIfscLists.RemoveAll();

            //기존삭제하던것을 삭제하지 않고 플래그를 설정해서 처리하자.
            FOR_STL(TVmsIfsc*, pIfsc, pObj->FIfscLists)
            {
                pIfsc->IsUsed = false;
            }
        }
    }
    catch(Exception &e)
    {
    }

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT A.VMS_CTLR_NMBR, B.VMS_IFSC_ID,     \r\n"
           "       B.DSPL_PRRT, C.AXIS_YN, C.CNGS_SPD, \r\n"
           "       B.CNGS_CNFM_YN, B.CNGS_CNFM_SPED    \r\n"
           "  FROM TB_VMS_CTLR      A,                 \r\n"
           "       TB_VMS_RLTN_IFSC B,                 \r\n"
           "       TB_VMS_IFSC      C                  \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR   \r\n"
           "   AND B.VMS_IFSC_ID   = C.VMS_IFSC_ID     \r\n"
           "   AND C.USE_YN = 'Y'                      \r\n"
           "   AND A.DEL_YN = 'N'                      \r\n"
           " ORDER BY A.VMS_CTLR_NMBR, B.DSPL_PRRT     \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString VMS_CTLR_NMBR = pADO->FieldByName("VMS_CTLR_NMBR")->AsString.Trim();

				TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(VMS_CTLR_NMBR);
                if (!pObj) continue;

#if 0
                TVmsIfsc *pIfsc = new TVmsIfsc();
                pIfsc->VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsString;
                pIfsc->AXIS_YN     = pADO->FieldByName("AXIS_YN")->AsString;
                pIfsc->AXIS_YN     = pADO->FieldByName("AXIS_YN")->AsString;
                pIfsc->CNGS_SPD    = pADO->FieldByName("CNGS_SPD")->AsInteger;

                pIfsc->CNGS_CNFM_YN   = pADO->FieldByName("CNGS_CNFM_YN")->AsString == "Y" ? true : false;
                pIfsc->CNGS_CNFM_SPED = pADO->FieldByName("CNGS_CNFM_SPED")->AsInteger;

                pObj->FIfscLists.Push(pIfsc->DSPL_PRRT, pIfsc);
#else
                AnsiString VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsString;
                TVmsIfsc *pIfsc = pObj->FIfscLists.Find(VMS_IFSC_ID);
                if (!pIfsc)
                {
                    pIfsc = new TVmsIfsc();
                    if (!pIfsc) continue;
                    pIfsc->VMS_IFSC_ID = VMS_IFSC_ID;
                    pObj->FIfscLists.Push(pIfsc->VMS_IFSC_ID, pIfsc);
                }

                pIfsc->IsUsed = true;

                bool CNGS_CNFM_YN   = pADO->FieldByName("CNGS_CNFM_YN")->AsString == "Y" ? true : false;
                int  CNGS_CNFM_SPED = pADO->FieldByName("CNGS_SPD")->AsInteger;

                if (CDSManager->FIsStaticCycle)
                {
                    //정체판정 정보가 변경되었으면 정체판정횟수를 초기화한다.
                    if (pIfsc->CNGS_CNFM_YN != CNGS_CNFM_YN || pIfsc->CNGS_CNFM_SPED != CNGS_CNFM_SPED)
                    {
                        pIfsc->CNGST_CNT = 0;
                    }
                }

                pIfsc->DSPL_PRRT   = pADO->FieldByName("DSPL_PRRT")->AsInteger;
                pIfsc->AXIS_YN     = pADO->FieldByName("AXIS_YN")->AsString;
                pIfsc->AXIS_YN     = pADO->FieldByName("AXIS_YN")->AsString;
                pIfsc->CNGS_SPD    = pADO->FieldByName("CNGS_SPD")->AsInteger;

                pIfsc->CNGS_CNFM_YN   = CNGS_CNFM_YN;
                pIfsc->CNGS_CNFM_SPED = CNGS_CNFM_SPED;
#endif
                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------
int TCDSIfscManager::LoadVmsIfscTrafficInfo(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    int        nSelCnt = 0;

    DWORD dwTick = GetTickCount();
    SELSTART;

#if 0
    sQry = "SELECT A.VMS_IFSC_ID, A.VMS_IFSC_NM, A.DSPL_STRT_NODE_NM, A.DSPL_END_NODE_NM, \r\n"
           "       A.ROAD_NM, A.SPOT_NM,                                                  \r\n"
           "       A.DETR_ID, A.AXIS_YN, A.CNGS_SPD,                                      \r\n"
           "       NVL(B.PRCN_DT,       '') AS PRCN_DT,                                   \r\n"
           "       NVL(B.CMTR_GRAD_CD, '0') AS CMTR_GRAD_CD,                              \r\n"
           "       NVL(B.SPED,           0) AS SPED,                                      \r\n"
           "       NVL(B.TRVL_HH,        0) AS TRVL_HH                                    \r\n"
           "  FROM TB_VMS_IFSC A,                                                         \r\n"
           "       (SELECT *                                                              \r\n"
           "          FROM TB_VMS_IFSC_TRAF                                               \r\n"
           "         WHERE PRCN_DT >= TO_CHAR(SYSDATE-10/1440, 'YYYYMMDDHH24MISS') ) B    \r\n"
           " WHERE A.VMS_IFSC_ID = B.VMS_IFSC_ID(+)                                       \r\n";
#else
    //TODO:  기존 테이블 호환을 위해 PRCN_DT 컬럼을 RGST_DT 로 변경
    sQry = "SELECT A.VMS_IFSC_ID, A.VMS_IFSC_NM, A.DSPL_STRT_NODE_NM, A.DSPL_END_NODE_NM, \r\n"
           "       A.ROAD_NM, A.SPOT_NM,                                                  \r\n"
           "       A.DETR_ID, A.AXIS_YN, A.CNGS_SPD,                                      \r\n"
           "       NVL(B.RGST_DT,       '') AS PRCN_DT,                                   \r\n"
           "       NVL(B.CMTR_GRAD_CD, '0') AS CMTR_GRAD_CD,                              \r\n"
           "       NVL(B.SPED,           0) AS SPED,                                      \r\n"
           "       NVL(B.TRVL_HH,        0) AS TRVL_HH                                    \r\n"
           "  FROM TB_VMS_IFSC A,                                                         \r\n"
           "       (SELECT *                                                              \r\n"
           "          FROM TB_VMS_IFSC_TRAF                                               \r\n"
           "         WHERE RGST_DT >= TO_CHAR(SYSDATE-10/1440, 'YYYYMMDDHH24MISS') ) B    \r\n"
           " WHERE A.VMS_IFSC_ID = B.VMS_IFSC_ID(+)                                       \r\n";
#endif

    FOR_STL(TCDSIfsc*, pObj, CDSIfscManager->FLists)
    {
        pObj->CMTR_GRAD_CD = "0";
        pObj->TRVL_HH      = 0;
        pObj->SPED         = 0;
    }

    try
    {
        TCDSIfsc *pIfsc;
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsString.Trim();
				pIfsc = FLists.Find(VMS_IFSC_ID);
                if (!pIfsc)
                {
                    pIfsc = new TCDSIfsc();
                    pIfsc->VMS_IFSC_ID = VMS_IFSC_ID;
                    FLists.Push(pIfsc->VMS_IFSC_ID, pIfsc);
                }

                pIfsc->VMS_IFSC_NM       = pADO->FieldByName("VMS_IFSC_NM")->AsString;
                pIfsc->DSPL_STRT_NODE_NM = pADO->FieldByName("DSPL_STRT_NODE_NM")->AsString;
                pIfsc->DSPL_END_NODE_NM  = pADO->FieldByName("DSPL_END_NODE_NM")->AsString;
                pIfsc->DETR_ID           = pADO->FieldByName("DETR_ID")->AsString;
                pIfsc->ROAD_NM           = pADO->FieldByName("ROAD_NM")->AsString;
                pIfsc->SPOT_NM           = pADO->FieldByName("SPOT_NM")->AsString;
                pIfsc->AXIS_YN           = pADO->FieldByName("AXIS_YN")->AsString;

                pIfsc->PRCN_DT      = pADO->FieldByName("PRCN_DT")->AsString;
                pIfsc->CMTR_GRAD_CD = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                pIfsc->SPED         = pADO->FieldByName("SPED")->AsInteger;
                pIfsc->TRVL_HH      = pADO->FieldByName("TRVL_HH")->AsInteger;
                if (pIfsc->CMTR_GRAD_CD != "0" && pIfsc->TRVL_HH <= 0) {
                    pIfsc->TRVL_HH = 1;
                }

                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    if (0)
    {
        FOR_STL(TCDSIfsc*, pTmp, FLists)
        {
            try
            {
                MDEBUG("Ifsc: %s, Grad: %s", pTmp->VMS_IFSC_ID.c_str(), pTmp->CMTR_GRAD_CD.c_str());
            }
            catch(Exception &e)
            {
            }
        }
    }

    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

