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

#pragma hdrstop

#include "CDSVmsIfscF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TVmsIfsc::TVmsIfsc()
{
    VMS_IFSC_NM       = "";         ///VARCHAR2(60)	Y			VMS 정보제공구간 명
    DSPL_STRT_NODE_NM = "";         //VARCHAR2(30)	Y			표출 시작 노드 명
    DSPL_END_NODE_NM  = "";         //VARCHAR2(30)	Y			표출 종료 노드 명
    DETR_ID           = 0;          //NUMBER(10)	Y			우회도로 ID
    USE_YN            = "Y";
    AXIS_YN           = "N";

    DEL_YN            = "N";

    MIN[0] = 20; MAX[0] = 180;
    MIN[1] = 10; MAX[1] =  19;
    MIN[2] =  0; MAX[2] =   9;

    InitTraffic();
}
//---------------------------------------------------------------------------
TVmsIfsc::~TVmsIfsc()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TVmsIfsc::InitTraffic()
{
    PRCN_DT      = "19700101000000";
    SPED         = 0;
    CMTR_GRAD_CD = "0";
    TRVL_HH      = 0;

}
//---------------------------------------------------------------------------
void TVmsIfsc::CalcSectLngt()
{
    SECT_LNGT = 0;
    FOR_STL(TVmsLinkIfsc*, pIfsc, FLists) {
        SECT_LNGT += pIfsc->SECT_LNGT;
    }
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TVmsIfscManager *VmsIfscManager = NULL;
//---------------------------------------------------------------------------

TVmsIfscManager::TVmsIfscManager()
{
}
//---------------------------------------------------------------------------
TVmsIfscManager::~TVmsIfscManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TVmsIfscManager::InitTraffic()
{
    FLists.Lock();
    try
    {
        FOR_STL(TVmsIfsc*, pObj, FLists)
        {
            pObj->InitTraffic();
        }
    }
    __finally
    {
        FLists.UnLock();
    }

    FIfscLists.Lock();
    try
    {
        FOR_STL(TIfscTraf*, pIfsc, FIfscLists) {
            pIfsc->PRCN_DT      = "19700101000000";
            pIfsc->SPED         = 0;
            pIfsc->CMTR_GRAD_CD = "LTC0";
            pIfsc->TRVL_HH      = 0;
        }
    }
    __finally
    {
        FIfscLists.UnLock();
    }
}
//---------------------------------------------------------------------------

bool TVmsIfscManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    FLists.Lock();
    try
    {
        FLists.RemoveAll();
    }
    __finally
    {
        FLists.UnLock();
    }

    sQry = "SELECT A.*,                                                                                                       \r\n"
           "       NVL(B.MIN1, 20) AS MIN1, NVL(B.MAX1, 180) AS MAX1,                                                         \r\n"
           "       NVL(B.MIN2, 10) AS MIN2, NVL(B.MAX2,  19) AS MAX2,                                                         \r\n"
           "       NVL(B.MIN3,  0) AS MIN3, NVL(B.MAX3,   9) AS MAX3                                                          \r\n"
           "  FROM TB_VMS_IFSC A,                                                                                             \r\n"
           "       (SELECT VMS_IFSC_ID,                                                                                       \r\n"
           "               SUM(MIN1) AS MIN1, SUM(MAX1) AS MAX1,                                                              \r\n"
           "               SUM(MIN2) AS MIN2, SUM(MAX2) AS MAX2,                                                              \r\n"
           "               SUM(MIN3) AS MIN3, SUM(MAX3) AS MAX3                                                               \r\n"
           "         FROM (SELECT VMS_IFSC_ID, LWSTSPED AS MIN1, HGHSSPED AS MAX1, 0 AS MIN2, 0 AS MAX2, 0 AS MIN3, 0 AS MAX3 \r\n"
           "                 FROM TB_VMS_IFSC_CMTR_GRAD                                                                       \r\n"
           "                WHERE CMTR_GRAD_CD = '1'                                                                          \r\n"
           "               UNION ALL                                                                                          \r\n"
           "               SELECT VMS_IFSC_ID, 0 AS MIN1, 0 AS MAX1, LWSTSPED AS MIN2, HGHSSPED AS MAX2, 0 AS MIN3, 0 AS MAX3 \r\n"
           "                 FROM TB_VMS_IFSC_CMTR_GRAD                                                                       \r\n"
           "                WHERE CMTR_GRAD_CD = '2'                                                                          \r\n"
           "               UNION ALL                                                                                          \r\n"
           "               SELECT VMS_IFSC_ID, 0 AS MIN1, 0 AS MAX1, 0 AS MIN2, 0 AS MAX2, LWSTSPED AS MIN3, HGHSSPED AS MAX3 \r\n"
           "                 FROM TB_VMS_IFSC_CMTR_GRAD                                                                       \r\n"
           "                WHERE CMTR_GRAD_CD = '3')                                                                         \r\n"
           "         GROUP BY VMS_IFSC_ID) B                                                                                  \r\n"
           " WHERE A.USE_YN  = 'Y'                                                                                            \r\n"
           "   AND A.EDTN_CD = 'EDI0'                                                                                         \r\n"
           "   AND A.VMS_IFSC_ID = B.VMS_IFSC_ID                                                                              \r\n"
           " ORDER BY A.VMS_IFSC_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->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TVmsIfsc *pObj = new TVmsIfsc();

                pObj->VMS_IFSC_ID       = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;
                pObj->VMS_IFSC_NM       = pADO->FieldByName("VMS_IFSC_NM")->AsString;
                pObj->DSPL_STRT_NODE_NM = pADO->FieldByName("DSPL_STRT_NODE_NM")->AsString;
                pObj->DSPL_END_NODE_NM  = pADO->FieldByName("DSPL_END_NODE_NM")->AsString;
                pObj->DETR_ID           = pADO->FieldByName("DETR_ID")->AsInteger;
                pObj->USE_YN            = pADO->FieldByName("USE_YN")->AsString;
                pObj->ROAD_NM           = pADO->FieldByName("ROAD_NM")->AsString;
                pObj->SPOT_NM           = pADO->FieldByName("SPOT_NM")->AsString;
                pObj->AXIS_YN           = pADO->FieldByName("AXIS_YN")->AsString;

                pObj->MIN[0] = pADO->FieldByName("MIN1")->AsInteger;
                pObj->MAX[0] = pADO->FieldByName("MAX1")->AsInteger;
                pObj->MIN[1] = pADO->FieldByName("MIN2")->AsInteger;
                pObj->MAX[1] = pADO->FieldByName("MAX2")->AsInteger;
                pObj->MIN[2] = pADO->FieldByName("MIN3")->AsInteger;
                pObj->MAX[2] = pADO->FieldByName("MAX3")->AsInteger;

                pObj->CNGS_SPD = pADO->FieldByName("CNGS_SPD")->AsInteger;

                pObj->DEL_YN = "N";

				FLists.Push(pObj->VMS_IFSC_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS정보제공구간정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS정보제공구간정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return LoadSubFromDb(ADbConn);
}
//---------------------------------------------------------------------------

bool TVmsIfscManager::LoadSubFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*, NVL(B.IFSC_NM, '-') AS IFSC_NM,                     \r\n"
           "       B.STRT_NM, B.END_NM, NVL(B.SECT_LNGT, 0) AS SECT_LNGT    \r\n"
           "  FROM TB_VMS_LINK_IFSC A,                                      \r\n"
           "       TB_IFSC          B                                       \r\n"
//           " WHERE A.IFSC_ID = B.IFSC_ID(+)                                 \r\n"
           " WHERE A.IFSC_ID = B.IFSC_ID                                    \r\n"
           " ORDER BY A.VMS_IFSC_ID, A.ORD                                  \r\n";

    FLists.Lock();
    try
    {
        TVmsIfsc     *pIfsc;
        TVmsLinkIfsc *pObj;
        int           VMS_IFSC_ID;
        int           ORD;

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

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;
                //ORD         = pADO->FieldByName("ORD")->AsInteger;

                pIfsc = FLists.Find(VMS_IFSC_ID);
                if (!pIfsc) continue;

                ORD  = pIfsc->FLists.Size()+1;
                pObj = pIfsc->FLists.Find(ORD);
                if (!pObj)
                {
				    pObj = new TVmsLinkIfsc();
                    pIfsc->FLists.Push(ORD, pObj);
                }
                pObj->VMS_IFSC_ID = VMS_IFSC_ID;
                pObj->ORD         = ORD;
                pObj->IFSC_ID     = pADO->FieldByName("IFSC_ID")->AsString;
                pObj->IFSC_NM     = pADO->FieldByName("IFSC_NM")->AsString;
                pObj->STRT_NM     = pADO->FieldByName("STRT_NM")->AsString;
                pObj->END_NM      = pADO->FieldByName("END_NM")->AsString;
                pObj->SECT_LNGT   = pADO->FieldByName("SECT_LNGT")->AsInteger;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS정보제공구간링크정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS정보제공구간링크정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        FOR_STL(TVmsIfsc*, pVmsIfsc, FLists) {
            pVmsIfsc->CalcSectLngt();
        }

        FLists.UnLock();
    }

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

bool TVmsIfscManager::LoadTraffic(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    InitTraffic();

    sQry = "SELECT A.VMS_IFSC_ID,                                                        \r\n"
           "       NVL(B.RGST_DT,       '19700101000000') 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_CMTRINFR                                               \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";

    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->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                int VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;

				TVmsIfsc *pObj = FLists.Find(VMS_IFSC_ID);
                if (!pObj) continue;

                pObj->PRCN_DT       = pADO->FieldByName("PRCN_DT")->AsString;	    //N	VARCHAR2(14)	Y			등록 일시
                pObj->SPED          = pADO->FieldByName("SPED")->AsInteger;	    //N	NUMBER(3)	    Y	0		속도
                pObj->CMTR_GRAD_CD  = pADO->FieldByName("CMTR_GRAD_CD")->AsString;	//N	VARCHAR2(7)	    Y			소통 등급 코드 ==>예는 0,1,2,3으로 들어가네???
                pObj->TRVL_HH       = pADO->FieldByName("TRVL_HH")->AsInteger;	    //N	NUMBER(6)	    Y	0		통행 시간
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS_IFSC교통정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS_IFSC교통정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

    sQry = "SELECT A.VMS_IFSC_ID, A.IFSC_ID,                                            \r\n"
           "       NVL(B.PRCN_DT,      '19700101000000') AS PRCN_DT,                    \r\n"
           "       NVL(B.CMTR_GRAD_CD, 'LTC0') 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_LINK_IFSC A,                                                  \r\n"
           "       (SELECT *                                                            \r\n"
           "          FROM TB_IFSC_TRAF                                                 \r\n"
           "         WHERE PRCN_DT >= TO_CHAR(SYSDATE-10/1440, 'YYYYMMDDHH24MISS') ) B  \r\n"
           "WHERE A.IFSC_ID = B.IFSC_ID(+)                                              \r\n";

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

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String IFSC_ID = pADO->FieldByName("IFSC_ID")->AsString;

				TIfscTraf *pObj = FIfscLists.Find(IFSC_ID);
                if (!pObj) {
                    pObj = new TIfscTraf();
                    FIfscLists.Push(IFSC_ID, pObj);
                }

                pObj->PRCN_DT       = pADO->FieldByName("PRCN_DT")->AsString;	    //N	VARCHAR2(14)	Y			등록 일시
                pObj->SPED          = pADO->FieldByName("SPED")->AsInteger;	    //N	NUMBER(3)	    Y	0		속도
                pObj->CMTR_GRAD_CD  = pADO->FieldByName("CMTR_GRAD_CD")->AsString;	//N	VARCHAR2(7)	    Y			소통 등급 코드 ==>예는 0,1,2,3으로 들어가네???
                pObj->TRVL_HH       = pADO->FieldByName("TRVL_HH")->AsInteger;	    //N	NUMBER(6)	    Y	0		통행 시간
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS_IFSC교통정보조회2", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS_IFSC교통정보조회2", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FIfscLists.UnLock();
    }

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

