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


#pragma hdrstop

#include "CDSIfsc_VMSF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TIfsc::TIfsc()
{
    InitTraffic();
}
//---------------------------------------------------------------------------
TIfsc::~TIfsc()
{
}
//---------------------------------------------------------------------------
void TIfsc::InitTraffic()
{
    PRCN_DT      = "";	        //N	VARCHAR2(14)	Y			가공 일시
    TFVL         = 0;	            //N	NUMBER(6)	    Y	0		교통량
    SPED         = 0;	            //N	NUMBER(3)	    Y	0		속도
    PTRN_SPED    = 0;	        //N	NUMBER(3)	    Y	0		패턴 속도
    OCPY_RATE    = 0;	        //N	NUMBER(5,2)	    Y	0		점유 율
    TRVL_HH      = 0;	        //N	NUMBER(6)	    Y	0		통행 시간
    CMTR_GRAD_CD = "0";	    //N	VARCHAR2(7)	    Y			소통 등급 코드
    DATA_NUM     = 0;	        //N	NUMBER(3)	    Y	0		데이터 개수
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TIfscManager *IfscManager = NULL;
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TIfscManager::TIfscManager()
{
}
//---------------------------------------------------------------------------
TIfscManager::~TIfscManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TIfscManager::InitTraffic()
{
    FLists.Lock();
    try
    {
        FOR_STL(TIfsc*, pObj, FLists)
        {
            pObj->InitTraffic();
        }
    }
    __finally
    {
        FLists.UnLock();
    }
}
//---------------------------------------------------------------------------
bool TIfscManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*        \r\n"
           "  FROM TB_IFSC A  \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())
            {
                String DEL_YN = pADO->FieldByName("DEL_YN")->AsString;
                if (DEL_YN == "Y") continue;

				TIfsc *pObj = new TIfsc();

                pObj->IFSC_ID           = pADO->FieldByName("IFSC_ID")->AsString;	        //N	NUMBER(10)	    N			정보제공구간 ID
                pObj->IFSC_NM           = pADO->FieldByName("IFSC_NM")->AsString;	        //N	VARCHAR2(100)	Y			정보제공구간 명
                pObj->DRCT_CD           = pADO->FieldByName("DRCT_CD")->AsString;	        //N	VARCHAR2(7)	    Y			방향 코드
                pObj->STRT_NM           = pADO->FieldByName("STRT_NM")->AsString;	        //N	VARCHAR2(100)	Y			시점 명
                pObj->END_NM            = pADO->FieldByName("END_NM")->AsString;	        //N	VARCHAR2(100)	Y			종점 명
                pObj->SECT_LNGT         = pADO->FieldByName("SECT_LNGT")->AsInteger;	    //N	NUMBER(5)	    Y	0		구간 길이
                pObj->EXTR_CNCT_SECT_YN = pADO->FieldByName("EXTR_CNCT_SECT_YN")->AsString;	//N	CHAR(1)	        Y	'N'		외부 연계 구간 여부
                pObj->DEL_YN            = pADO->FieldByName("DEL_YN")->AsString;	        //N	CHAR(1)	        Y	'N'		삭제 여부
                pObj->RMRK              = pADO->FieldByName("RMRK")->AsString;	            //N	VARCHAR2(600)	Y			비고
                pObj->F_NODE_ID         = pADO->FieldByName("F_NODE_ID")->AsString;	        //N	NUMBER(10)	    Y			시작 노드 ID
                pObj->T_NODE_ID         = pADO->FieldByName("T_NODE_ID")->AsString;	        //N	NUMBER(10)	    Y			종료 노드 ID
                pObj->SECT_GRAD_CD      = pADO->FieldByName("SECT_GRAD_CD")->AsString;	    //N	VARCHAR2(7)	    Y			구간 등급 코드
                pObj->AREA_CD           = pADO->FieldByName("AREA_CD")->AsString;	        //N	VARCHAR2(7)	    Y			지역 코드

                pObj->vertex = false;
				FLists.Push(pObj->IFSC_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TIfscManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TIfscManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

#if 0
    sQry = "SELECT B.IFSC_ID,                  \r\n"
           "       B.X_CRDN_ARR, B.Y_CRDN_ARR  \r\n"
           "  FROM TB_IFSC          A,         \r\n"
           "       TB_IFSC_VRTX_ARR B          \r\n"
           " WHERE A.DEL_YN  = 'N'             \r\n"
           "   AND A.IFSC_ID = B.IFSC_ID       \r\n"
           "   AND B.LEVL    = 2               \r\n";
#else
    sQry = "SELECT *                 \r\n"
           "  FROM VW_IFSC_VRTX_ARR  \r\n";
#endif

    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())
            {
                String IFSC_ID = pADO->FieldByName("IFSC_ID")->AsString;

				TIfsc *pObj = FLists.Find(IFSC_ID);
                if (pObj)
                {
                    pObj->vertex     = true;
                    pObj->X_CRDN_ARR = pADO->FieldByName("X_CRDN_ARR")->AsString;
                    pObj->Y_CRDN_ARR = pADO->FieldByName("Y_CRDN_ARR")->AsString;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TIfscManager::LoadVertexFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TIfscManager::LoadVertexFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

#if 0
    sQry = "SELECT INFO_PRV_SCTNID  AS IFSC_ID,     \r\n"
           "       SEQUENCE         AS ORD,         \r\n"
           "       TRUNC(COORDX, 8) AS X_CRDN,      \r\n"
           "       TRUNC(COORDY, 8) AS Y_CRDN       \r\n"
           "  FROM LINK_GIS                         \r\n"
           " ORDER BY INFO_PRV_SCTNID, SEQUENCE     \r\n";
#else
    sQry = "SELECT  S.INFO_PRV_SCTNID   AS IFSC_ID                          \r\n"
           "       ,S.SEQUENCE          AS ORD                              \r\n"
           "       ,L.LINK_SHP_SEQUENCE AS SEQ                              \r\n"
           "       ,TRUNC(L.GPS_X, 8)   AS X_CRDN                           \r\n"
           "       ,TRUNC(L.GPS_Y, 8)   AS Y_CRDN                           \r\n"
           "  FROM  LINK_SHP L                                              \r\n"
           "       ,INFO_PRV_SCTN I                                         \r\n"
           "       ,INFO_PRV_SCT_STDCFG S                                   \r\n"
           " WHERE 1 = 1                                                    \r\n"
           "   AND L.LINKID = S.LINKID                                      \r\n"
           "   AND S.INFO_PRV_SCTNID = I.INFO_PRV_SCTNID                    \r\n"
           " ORDER BY S.INFO_PRV_SCTNID, S.SEQUENCE, L.LINK_SHP_SEQUENCE    \r\n";
#endif

    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();

            TIfsc *pObj;
            String o_IFSC_ID  = "";
            String X_CRDN_ARR = "";
            String Y_CRDN_ARR = "";

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

                if (IFSC_ID != o_IFSC_ID)
                {
                    // 아이디가 변경되었으면 이전 데이터를 설정
                    pObj = FLists.Find(o_IFSC_ID);
                    if (pObj)
                    {
                        pObj->vertex     = true;
                        pObj->X_CRDN_ARR = X_CRDN_ARR;
                        pObj->Y_CRDN_ARR = Y_CRDN_ARR;
                    }

                    // 데이터 초기화
                    o_IFSC_ID = IFSC_ID;

                    // 현재 정보는 최초 정보이므로 그냥 할당
                    X_CRDN_ARR = pADO->FieldByName("X_CRDN")->AsString;
                    Y_CRDN_ARR = pADO->FieldByName("Y_CRDN")->AsString;
                }
                else
                {
                    // 두번째 데이터 이므로 분리자 추가
                    X_CRDN_ARR += "," + pADO->FieldByName("X_CRDN")->AsString;
                    Y_CRDN_ARR += "," + pADO->FieldByName("Y_CRDN")->AsString;
                }
            }
            //마지막 데이터 처리
            pObj = FLists.Find(o_IFSC_ID);
            if (pObj)
            {
                pObj->vertex     = true;
                pObj->X_CRDN_ARR = X_CRDN_ARR;
                pObj->Y_CRDN_ARR = Y_CRDN_ARR;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TIfscManager::LoadVertexFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TIfscManager::LoadVertexFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

    InitTraffic();

    sQry = "SELECT IFSC_ID AS LINKID, NVL(PRCN_DT, '19000101000000') AS PRCN_DT,    \r\n"
           "       NVL(TFVL,      0) AS TFVL,      NVL(SPED,      0) AS SPED,       \r\n"
           "       NVL(PTRN_SPED, 0) AS PTRN_SPED, NVL(OCPY_RATE, 0) AS OCPY_RATE,  \r\n"
           "       NVL(TRVL_HH,   0) AS TRVL_HH,                                    \r\n"
           "       DECODE(CMTR_GRAD_CD, 'LTC1', '1',                                \r\n"
           "                            'LTC2', '2',                                \r\n"
           "                            'LTC3', '3', '0') AS CMTR_GRAD_CD,          \r\n"
           "       NVL(DATA_NUM, 0) AS DATA_NUM                                     \r\n"
           "  FROM TB_IFSC_TRAF                                                     \r\n"
           " WHERE PRCN_DT > TO_CHAR(SYSDATE -  5 / 1440, 'YYYYMMDDHH24MISS')       \r\n";

#if 0
    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";
#endif

    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())
            {
                String LINKID = pADO->FieldByName("LINKID")->AsString;

				TIfsc *pObj = FLists.Find(LINKID);
                if (!pObj) continue;

                pObj->PRCN_DT       = pADO->FieldByName("PRCN_DT")->AsString;	        //N	VARCHAR2(14)	Y			가공 일시
                pObj->TFVL          = pADO->FieldByName("TFVL")->AsInteger;	            //N	NUMBER(6)	    Y	0		교통량
                pObj->SPED          = pADO->FieldByName("SPED")->AsInteger;	            //N	NUMBER(3)	    Y	0		속도
                pObj->PTRN_SPED     = pADO->FieldByName("PTRN_SPED")->AsInteger;	    //N	NUMBER(3)	    Y	0		패턴 속도
                pObj->OCPY_RATE     = pADO->FieldByName("OCPY_RATE")->AsFloat;	        //N	NUMBER(5,2)	    Y	0		점유 율
                pObj->TRVL_HH       = pADO->FieldByName("TRVL_HH")->AsInteger;	        //N	NUMBER(6)	    Y	0		통행 시간
                pObj->CMTR_GRAD_CD  = pADO->FieldByName("CMTR_GRAD_CD")->AsString;	    //N	VARCHAR2(7)	    Y			소통 등급 코드
                pObj->DATA_NUM      = pADO->FieldByName("DATA_NUM")->AsInteger;	        //N	NUMBER(3)	    Y	0		데이터 개수
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("IFSC교통정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("IFSC교통정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

