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

#pragma hdrstop

#include "CDSLinkF.h"
#include "CDSLayerF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsLink::TItsLink()
{
    IFSC_ID = "";
    ROAD_ID = "";
    Clear();
}
//---------------------------------------------------------------------------
void TItsLink::Clear()
{
}
//---------------------------------------------------------------------------
TItsLink::~TItsLink()
{
}
//---------------------------------------------------------------------------
void TItsLink::InitTraffic()
{
    TRAF->InitTraffic();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsLinkManager *ItsLinkManager = NULL;
//---------------------------------------------------------------------------

/*
* Link Manager
*/
TItsLinkManager::TItsLinkManager()
{
}
//---------------------------------------------------------------------------
TItsLinkManager::~TItsLinkManager()
{
    FLists.RemoveAll();
    FClctSystStts.RemoveAll();
}
//---------------------------------------------------------------------------
void TItsLinkManager::InitTraffic()
{
    try
    {
        FLists.Lock();

        FOR_STL(TItsLink*, pObj, FLists)
        {
            pObj->InitTraffic();
        }
    }
    __finally
    {
        FLists.UnLock();
    }
}
//---------------------------------------------------------------------------
bool TItsLinkManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 1
    // for POHANG
    sQry = "SELECT LINK_ID, F_NODE AS F_NODE_ID, T_NODE AS T_NODE_ID, ROAD_USE, LANES,          \r\n"
           "       ROAD_RANK, TRIM(ROAD_TYPE) AS ROAD_TYPE, ROAD_NO, TRIM(ROAD_NAME) ROAD_NAME, \r\n"
           "       MULTI_LINK, TRIM(LINK_CONNECT) AS LINK_CONNECT, MAX_SPD,                     \r\n"
           "       TRIM(REST_VEH) AS REST_VEH, REST_W, REST_H, LINK_LENG, RMRK,                 \r\n"
           "       DEL_YN, DFLT_SPED, DFLT_SPED_USE_YN,                                         \r\n"
           "       SECT_GRAD_CD, AREA_CD,                                                       \r\n"
           "       DECODE(AREA_CD, 'LAT001', 'Y', 'N') REGN_YN, 1 AS PARA_ID                    \r\n"
           "  FROM TB_LINK                                                                      \r\n";
#else
    sQry = "SELECT LINK_ID, F_NODE_ID, T_NODE_ID, ROAD_USE, LANES,                              \r\n"
           "       ROAD_RANK, TRIM(ROAD_TYPE) AS ROAD_TYPE, ROAD_NO, TRIM(ROAD_NAME) ROAD_NAME, \r\n"
           "       MULTI_LINK, TRIM(LINK_CONNECT) AS LINK_CONNECT, MAX_SPD,                     \r\n"
           "       TRIM(REST_VEH) AS REST_VEH, REST_W, REST_H, LINK_LENG, RMRK,                 \r\n"
           "       DEL_YN, DFLT_SPED, DFLT_SPED_USE_YN,                                         \r\n"
           "       SECT_GRAD_CD, AREA_CD,                                                       \r\n"
           "       DECODE(AREA_CD, 'LAT001', 'Y', 'N') REGN_YN, PARA_ID                         \r\n"
           "  FROM TB_LINK                                                                      \r\n";
#endif
    FLists.Lock();
    try
    {
        FLists.RemoveAll();
        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())
            {
				TItsLink *pLink = new TItsLink();

                pLink->IsVertex         = false;
                pLink->LINK_ID          = pADO->FieldByName("LINK_ID")->AsString;          //'링크ID';;
                pLink->F_NODE_ID        = pADO->FieldByName("F_NODE_ID")->AsString;        //'시작노드ID';;
                pLink->T_NODE_ID        = pADO->FieldByName("T_NODE_ID")->AsString;        //'종료노드ID';;
                pLink->ROAD_USE         = pADO->FieldByName("ROAD_USE")->AsString;         //'도로사용여부';;
                pLink->LANES            = pADO->FieldByName("LANES")->AsString;            //'차로수';;
                pLink->ROAD_RANK        = pADO->FieldByName("ROAD_RANK")->AsString;        //'도로등급';;
                pLink->ROAD_TYPE        = pADO->FieldByName("ROAD_TYPE")->AsString;        //'도로유형';;
                pLink->ROAD_NO          = pADO->FieldByName("ROAD_NO")->AsString;          //'도로번호';;
                pLink->ROAD_NAME        = pADO->FieldByName("ROAD_NAME")->AsString.Trim();        //'도로명';;
                pLink->MULTI_LINK       = pADO->FieldByName("MULTI_LINK")->AsString;       //'중용구간여부';;
                pLink->LINK_CONNECT     = pADO->FieldByName("LINK_CONNECT")->AsString;     //'연결로코드';;
                pLink->MAX_SPD          = pADO->FieldByName("MAX_SPD")->AsString;          //'최고제한속도';;
                pLink->REST_VEH         = pADO->FieldByName("REST_VEH")->AsString;         //'통행제한차량';;
                pLink->REST_W           = pADO->FieldByName("REST_W")->AsString;           //'통과제한하중';;
                pLink->REST_H           = pADO->FieldByName("REST_H")->AsString;           //'통과제한높이';;
                pLink->LINK_LENG        = pADO->FieldByName("LINK_LENG")->AsString;        //'연장';;
                pLink->RMRK             = pADO->FieldByName("RMRK")->AsString.Trim();             //'비고';;
                pLink->DEL_YN           = pADO->FieldByName("DEL_YN")->AsString;           //'삭제 여부';;
                pLink->DFLT_SPED        = pADO->FieldByName("DFLT_SPED")->AsString;        //'디폴트 속도';;
                pLink->DFLT_SPED_USE_YN = pADO->FieldByName("DFLT_SPED_USE_YN")->AsString; //'디폴트 속도 사용 여부';;
                pLink->SECT_GRAD_CD     = pADO->FieldByName("SECT_GRAD_CD")->AsString;     //'구간 등급 코드';;
                pLink->AREA_CD          = pADO->FieldByName("AREA_CD")->AsString;          //'지역 코드';;
                pLink->REGN_YN          = pADO->FieldByName("REGN_YN")->AsString;          //'로컬지역여부';
                pLink->PARA_ID          = pADO->FieldByName("PARA_ID")->AsString;          //'파라미터ID
                //pLink->REGN_YN          = "Y";

				FLists.Push(pLink->LINK_ID, pLink);
                pLink->Completed = true;

                pLink->TRAF = new TCDSTraffic();
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsLinkManager::LoadFromDb", 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("TItsLinkManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    if (g_AppCfg.isLoadFromFile)
    {
        return LoadVrtxFromFile(ADbConn);
    }
    return LoadVrtxFromDb(ADbConn);
}
//---------------------------------------------------------------------------
bool TItsLinkManager::LoadVrtxFromFile(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    AnsiString sAppDir = ExtractFilePath(Application->ExeName);
    AnsiString sFileName = sAppDir + "\\MAPDATA\\layers\\LINK.dat";

    sQry = String(sFileName);

	FILE *fd;
	if ((fd = fopen(sFileName.c_str(), "r")) == NULL)
    {
        return false;
    }

    FLists.Lock();

    TStringList *sl = NULL;
    char         szBuff[8192];

    int nCnt = 0;
    try
    {
        sl = new TStringList;
        sl->Delimiter = ';';
        sl->StrictDelimiter = true;

        while (1)
        {
            nCnt++;
            memset(szBuff, 0x00, sizeof(szBuff));
            if (fgets(szBuff, sizeof(szBuff), fd) == NULL) break;

            sl->DelimitedText = String(szBuff);
            if (sl->Count != 3)
            {
                continue;
            }
            try {
                String LINK_ID = sl->Strings[0];
				TItsLink *pObj = FLists.Find(LINK_ID);
                if (pObj)
                {
                    pObj->IsVertex   = true;
                    pObj->X_CRDN_ARR = sl->Strings[1];
                    //pObj->Y_CRDN_ARR = sl->Strings[2];
                    pObj->Y_CRDN_ARR = StringReplace(sl->Strings[2], "\n", "", TReplaceFlags() << rfReplaceAll);
                }
            }
            catch(Exception &e)
            {
                DBERRORMSG("TItsLinkManager::LoadVertexFromDb", String(e.ClassName()), e.Message, sQry);
                throw Exception(String(e.ClassName()) + e.Message);
            }
        }
    }
    __finally
    {
        if (sl) delete sl;
        if (fd) fclose(fd);
        FLists.UnLock();
    }

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

#if 0
    sQry = "SELECT A.LINK_ID, A.LEVL,           \r\n"
           "       A.X_CRDN_ARR, A.Y_CRDN_ARR   \r\n"
           "  FROM TB_LINK_VRTX_ARR A,          \r\n"
           "       TB_LINK          B           \r\n"
           " WHERE B.DEL_YN  = 'N'              \r\n"
           "   AND B.LINK_ID = A.LINK_ID        \r\n"
           "   AND A.LEVL    = 1                \r\n";
#else
#if 0
    sQry = "SELECT *                 \r\n"
           "  FROM VW_LINK_VRTX_ARR  \r\n";
#else
        sQry = "SELECT A.LINK_ID AS LINK_ID, A.LEVL AS LEVL,        \r\n"
               "       SUBSTR(A.X_CRDN_ARR,    1, 400) AS XMSG01,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR,  401, 400) AS XMSG02,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR,  801, 400) AS XMSG03,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 1201, 400) AS XMSG04,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 1601, 400) AS XMSG05,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 2001, 400) AS XMSG06,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 2401, 400) AS XMSG07,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 2801, 400) AS XMSG08,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 3201, 400) AS XMSG09,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 3601, 400) AS XMSG10,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 4001, 400) AS XMSG11,   \r\n"
               "       SUBSTR(A.X_CRDN_ARR, 4401, 400) AS XMSG12,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR,    1, 400) AS YMSG01,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR,  401, 400) AS YMSG02,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR,  801, 400) AS YMSG03,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 1201, 400) AS YMSG04,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 1601, 400) AS YMSG05,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 2001, 400) AS YMSG06,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 2401, 400) AS YMSG07,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 2801, 400) AS YMSG08,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 3201, 400) AS YMSG09,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 3601, 400) AS YMSG10,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 4001, 400) AS YMSG11,   \r\n"
               "       SUBSTR(A.Y_CRDN_ARR, 4401, 400) AS YMSG12    \r\n"
               "  FROM VW_LINK_VRTX_ARR A                           \r\n";
#endif
#endif

    TItsLink* pObj;

    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 LINK_ID = pADO->FieldByName("LINK_ID")->AsString;
                pObj = FLists.Find(LINK_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;
#if 0
                pObj->X_CRDN_ARR = pADO->FieldByName("X_CRDN_ARR")->AsString;
                pObj->Y_CRDN_ARR = pADO->FieldByName("Y_CRDN_ARR")->AsString;
#else
                pObj->X_CRDN_ARR = "";
                pObj->Y_CRDN_ARR = "";
                String COLM;
                for (int ii = 1; ii < 13; ii++)
                {
                    COLM.printf(L"XMSG%02d", ii);
                    pObj->X_CRDN_ARR += pADO->FieldByName(COLM)->AsString;
                    COLM.printf(L"YMSG%02d", ii);
                    pObj->Y_CRDN_ARR += pADO->FieldByName(COLM)->AsString;
                }
#endif
#endif
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsLinkManager::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("TItsLinkManager::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 TItsLinkManager::LoadRegionVertexFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT * FROM TB_REGION_VRTX WHERE CENTERID = 'L01'";
    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 CENTERID = pADO->FieldByName("CENTERID")->AsString;

                FRegionPointCnt = pADO->FieldByName("POINTCNT")->AsInteger;
                FRegionVertex   = pADO->FieldByName("VERTEX")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TItsLinkManager::LoadRegionVertexFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("TItsLinkManager::LoadRegionVertexFromDb", 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 TItsLinkManager::LoadTraffic(TADOConnection *ADbConn/*=NULL*/)
{
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    InitTraffic();

    sQry = "SELECT A.LINK_ID, PRCN_DT, TFVL, SPED, OCPY_RATE, TRVL_HH, CMTR_GRAD_CD, MISS_YN,   \r\n"
           "        PRCN_SPED, SMTH_SPED, ADJS_SPED, PTRN_SPED, PRCN_SORC, ADJS_SORC, 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_LINK_TRAF) MPRCN_DT                            \r\n"
           "   FROM TB_LINK_TRAF A, TB_LINK B                                                   \r\n"
           "  WHERE A.LINK_ID = B.LINK_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("LINK_ID")->AsString;
                TItsLink *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->MISSING == "Y") pObj->TRAF->CMTR_GRAD_CD = "LTC0";

                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;

				pObj->TRAF->MISS_YN      = pADO->FieldByName("MISS_YN")->AsString;       // '결측 여부';
				pObj->TRAF->PRCN_SPED    = pADO->FieldByName("PRCN_SPED")->AsInteger;    // '가공 속도';
				pObj->TRAF->SMTH_SPED    = pADO->FieldByName("SMTH_SPED")->AsInteger;    // '평활화 속도';
				pObj->TRAF->ADJS_SPED    = pADO->FieldByName("ADJS_SPED")->AsInteger;    // '보정 속도';
				pObj->TRAF->PRCN_SORC    = pADO->FieldByName("PRCN_SORC")->AsString;     // '가공 소스';
				pObj->TRAF->ADJS_SORC    = pADO->FieldByName("ADJS_SORC")->AsString;     // '보정 소스';

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

            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsLinkManager::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("TItsLinkManager::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 bResult;
}
//---------------------------------------------------------------------------
bool TItsLinkManager::LoadClctSystStts(TADOConnection *ADbConn/*=NULL*/)
{
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    FOR_STL(TClctSystStts*, pObj, FClctSystStts)
    {
        pObj->IsCollected = false;
    }

    sQry = "SELECT A.PRCN_DT, A.CLCT_SYST_CD, B.CLCT_SYST_NM, A.CLCT_NUM, A.FLTR_NUM, A.FUSN_NUM        \r\n"
           "  FROM TB_CLCT_SYST_STTS_HS A,                                                              \r\n"
           "       TB_CLCT_SYST_STUP    B                                                               \r\n"
           " WHERE A.PRCN_DT = (SELECT MAX(PRCN_DT)                                                     \r\n"
           "                      FROM TB_CLCT_SYST_STTS_HS                                             \r\n"
           "                     WHERE PRCN_DT >= TO_CHAR(SYSDATE - 1/24, 'YYYYMMDDHH24MISS')           \r\n"
           "                   )                                                                        \r\n"
           "   AND B.CLCT_SYST_CD = A.CLCT_SYST_CD(+)                                                   \r\n"
           "   AND B.USE_YN       = 'Y'                                                                 \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 CLCT_SYST_CD = pADO->FieldByName("CLCT_SYST_CD")->AsString;
                TClctSystStts *pObj = FClctSystStts.Find(CLCT_SYST_CD);
                if (!pObj)
                {
                    pObj = new TClctSystStts();
                    pObj->CLCT_SYST_CD = CLCT_SYST_CD;
                    FClctSystStts.Push(pObj->CLCT_SYST_CD, pObj);
                }

                pObj->CLCT_SYST_NM = pADO->FieldByName("CLCT_SYST_NM")->AsString;
                pObj->PRCN_DT = pADO->FieldByName("PRCN_DT")->AsString;

                pObj->CLCT_NUM = pADO->FieldByName("CLCT_NUM")->AsInteger;
                pObj->FLTR_NUM = pADO->FieldByName("FLTR_NUM")->AsInteger;
                pObj->FUSN_NUM = pADO->FieldByName("FUSN_NUM")->AsInteger;

                pObj->IsCollected = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsLinkManager::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("TItsLinkManager::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 bResult;
}
//---------------------------------------------------------------------------

