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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "WindowMsgF.h"

#include "FrmLinkInfoF.h"
#include "FrmLinkGradeSetF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "CDSRoadF.h"
#include "CDSTrafficGradeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"
TFrmLinkInfo *FrmLinkInfo = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmLinkInfo::TFrmLinkInfo(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

Width = 474;
Height= 525;
int nHeight = Height;
int nWidth  = Width;

    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
Height = nHeight;
Width  = nWidth;

//    Caption = " 구간 상세정보";
    m_sLinkId    = "";
    m_nLinkLevel = 0;

    FCodeRDR = ItsCodeManager->FLists.Find("RDR");	// 도로등급
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkInfo::FormClose(TObject *Sender, TCloseAction &Action)
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
    Action = caFree;
    FrmLinkInfo = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkInfo::RefreshLinkInfo()
{
    ShowLinkInfo(m_sLinkId, m_nLinkLevel);
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkInfo::ShowLinkInfo(String sLinkId, int nLinkLevel)
{
    m_sLinkId    = sLinkId;
    m_nLinkLevel = nLinkLevel;

    EdId->Text     = sLinkId;
    EdLevel->Text  = String(m_nLinkLevel);
    EdLength->Text = FormatFloat("##,##0", 0);

    EdStId->Text = "";
    EdEdId->Text = "";
    EdStName->Text = FrmLang->lblNoName->Caption;//"명칭없음";
    EdEdName->Text = FrmLang->lblNoName->Caption;//"명칭없음";

    EdRoadName->Text = "";
    EdRoadRank->Text = "";

    EdSvcDt->Text  = "";
    EdPrcnSorc->Text = "";
    EdAdjsSorc->Text = "";
    PnlGrade->Caption = FrmLang->lblNull->Caption;//"정보없음";
    PnlGrade->Color = clGray;

    EdRoadRank->Visible = false;

    Column04->Visible = false;
    Column05->Visible = false;
    Column06->Visible = false;
    Column07->Visible = false;
    Column08->Visible = false;
    Column10->Visible = false;
    Column11->Visible = false;

    cxGridColumn3->Visible = false;
    cxGridColumn4->Visible = false;
    cxGridColumn5->Visible = false;
    cxGridColumn7->Visible = false;

    switch(m_nLinkLevel)
    {
    case 1: //레벨1 링크(표준링크)
        {
            TItsNode *pNode;
            TItsLink *pLink;

            pLink = ItsLinkManager->FLists.Find(m_sLinkId);
            if (!pLink) return;

            EdStId->Text = pLink->F_NODE_ID;
            EdEdId->Text = pLink->T_NODE_ID;
            EdLength->Text = FormatFloat("##,##0", StrToFloat(pLink->LINK_LENG));

            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode) EdStName->Text = pNode->NODE_NAME.Trim();
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode) EdEdName->Text = pNode->NODE_NAME.Trim();

            EdRoadName->Text = pLink->ROAD_NAME;

            String sRDR = "[" + pLink->ROAD_RANK + "] ";
            if (FCodeRDR)
            {
                TItsSubCode *pSubCode = FCodeRDR->FSubLists.Find(pLink->ROAD_RANK);
                if (pSubCode) sRDR = sRDR + pSubCode->CMMN_CD_KOR_NM;
            }
            EdRoadRank->Text = sRDR;

            //TItsTrafficLink *pTraf = ItsTrafficLinkManager->FList.Find(m_sLinkId);
            //if (pTraf)
            //{
            //}
            EdRoadRank->Visible = true;

            Column04->Visible = true;
            Column05->Visible = true;
            Column06->Visible = true;
            Column07->Visible = true;
            Column08->Visible = true;
            Column10->Visible = true;
            Column11->Visible = true;

            cxGridColumn3->Visible = true;
            cxGridColumn4->Visible = true;
            cxGridColumn5->Visible = true;
            cxGridColumn7->Visible = true;

            SelTrafficLink(m_sLinkId);
        }
        break;
    case 2: //레벨2 링크(서비스링크)
        {
            TItsNode *pNode;
            TItsIfsc *pLink;

            pLink = ItsIfscManager->FLists.Find(m_sLinkId);
            if (!pLink) return;

            EdStId->Text = pLink->F_NODE_ID;
            EdEdId->Text = pLink->T_NODE_ID;
            EdLength->Text = FormatFloat("##,##0", StrToFloat(pLink->SECT_LNGT));

            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode) EdStName->Text = pNode->NODE_NAME.Trim();
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode) EdEdName->Text = pNode->NODE_NAME.Trim();

            EdRoadName->Text = pLink->IFSC_NM;

            //TItsTrafficServiceLink *pTraf = ItsTrafficServiceLinkManager->FList.Find(m_sLinkId);
            //if (pTraf)
            //{
            //}
            SelTrafficServiceLink(m_sLinkId);
        }
        break;
    case 3: //도로
        {
            TItsNode *pNode;
            TItsRoad *pRoad;

            pRoad = ItsRoadManager->FLists.Find(m_sLinkId);
            if (!pRoad) return;

            EdStId->Text = pRoad->F_NODE_ID;
            EdEdId->Text = pRoad->T_NODE_ID;
            EdLength->Text = FormatFloat("##,##0", StrToFloat(pRoad->SECT_LNGT));

            pNode = ItsNodeManager->FLists.Find(pRoad->F_NODE_ID);
            if (pNode) EdStName->Text = pNode->NODE_NAME.Trim();
            pNode = ItsNodeManager->FLists.Find(pRoad->T_NODE_ID);
            if (pNode) EdEdName->Text = pNode->NODE_NAME.Trim();

            EdRoadName->Text = pRoad->ROAD_NM;

            //TItsTrafficRoad *pTraf = ItsTrafficRoadManager->FList.Find(m_sLinkId);
            //if (pTraf)
            //{
            //}
            SelTrafficRoad(m_sLinkId);
        }
        break;
    }
    PnlSystime->Caption = Now().FormatString(STR_DATETIME);
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkInfo::SelTrafficLink(String ALinkId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);
    try
    {
        TvList1->BeginUpdate();
        TvList->BeginUpdate();
        TcxDataController *pGDC1 = TvList1->DataController;
        TcxDataController *pGDC = TvList->DataController;

        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();

        // 현재소통정보
        try
        {
#if 1
            sQry = "SELECT TO_DATE( PRCN_DT, 'YYYYMMDDHH24MISS' ) PRCN_DT2, PRCN_DT, CMTR_GRAD_CD, \r\n"
                   "       DATA_NUM,                                                               \r\n"
                   "       SPED, TRVL_HH, MISS_YN, PRCN_SPED, SMTH_SPED, ADJS_SPED, PTRN_SPED,     \r\n"
                   "       DECODE(C.CMMN_CD, NULL, A.CMTR_GRAD_CD, C.CMMN_CD_KOR_NM) AS GRADE_NM,  \r\n"
                   "       DECODE(B.CMMN_CD, NULL, A.PRCN_SORC, B.CMMN_CD_KOR_NM) AS PRCN_SORC_NM, \r\n"
                   "       DECODE(D.CMMN_CD, NULL, A.ADJS_SORC, D.CMMN_CD_KOR_NM) AS ADJS_SORC_NM  \r\n"
                   "  FROM TB_LINK_TRAF A,                                                         \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                               \r\n"
                   "         WHERE CMMN_CLSF_CD = 'CST' OR CMMN_CLSF_CD = 'MCS'                    \r\n"
                   "       ) B,                                                                    \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                               \r\n"
                   "         WHERE CMMN_CLSF_CD = 'CST' OR CMMN_CLSF_CD = 'MCS'                    \r\n"
                   "       ) D,                                                                    \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                               \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                            \r\n"
                   "       ) C                                                                     \r\n"
                   " WHERE LINK_ID = :p01                                                          \r\n"
                   "   AND PRCN_DT > TO_CHAR( SYSDATE -  10 / 1440, 'YYYYMMDDHH24MISS' )           \r\n"
                   "   AND A.PRCN_SORC    = B.CMMN_CD(+)                                           \r\n"
                   "   AND A.ADJS_SORC    = D.CMMN_CD(+)                                           \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                           \r\n";
#else
            sQry = "SELECT TO_DATE( PRCN_DT, 'YYYYMMDDHH24MISS' ) PRCN_DT, CMTR_GRAD_CD,          \r\n"
                   "       SPED, PTRN_SPED, TRVL_HH,                                              \r\n"
                   "       C.CMMN_CD_KOR_NM GRADE_NM, B.CMMN_CD_KOR_NM AS PRCN_SORC,              \r\n"
                   "       DECODE(B.CMMN_CD, NULL, A.PRCN_SORC, B.CMMN_CD_KOR_NM) AS PRCN_SORC_NM \r\n"
                   "  FROM TB_LINK_TRAF A,                                                        \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                              \r\n"
                   "         WHERE CMMN_CLSF_CD = 'CST' OR CMMN_CLSF_CD = 'MCS'                   \r\n"
                   "       ) B,                                                                   \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                              \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                           \r\n"
                   "       ) C                                                                    \r\n"
                   " WHERE LINK_ID = :p01                                                         \r\n"
                   "   AND PRCN_DT > TO_CHAR( SYSDATE -  10 / 1440, 'YYYYMMDDHH24MISS' )          \r\n"
                   "   AND A.PRCN_SORC    = B.CMMN_CD(+)                                          \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                          \r\n";
#endif

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            if (!pADO->Eof)
            {
                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                EdSvcDt->Text    = ITSUtil_FormatStr(sDbDate, STR_DATETIME);
                EdPrcnSorc->Text = pADO->FieldByName("PRCN_SORC_NM")->AsString;
                EdAdjsSorc->Text = pADO->FieldByName("ADJS_SORC_NM")->AsString;

                pGDC1->Values[0][cxGridColumn1->Index] = pADO->FieldByName("TRVL_HH")->AsString;
                pGDC1->Values[0][cxGridColumn2->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC1->Values[0][cxGridColumn3->Index] = pADO->FieldByName("PRCN_SPED")->AsString;
                pGDC1->Values[0][cxGridColumn4->Index] = pADO->FieldByName("SMTH_SPED")->AsString;
                pGDC1->Values[0][cxGridColumn5->Index] = pADO->FieldByName("ADJS_SPED")->AsString;
                pGDC1->Values[0][cxGridColumn6->Index] = pADO->FieldByName("PTRN_SPED")->AsString;
                pGDC1->Values[0][cxGridColumn7->Index] = pADO->FieldByName("MISS_YN")->AsString;
                pGDC1->Values[0][cxGridColumn8->Index] = pADO->FieldByName("DATA_NUM")->AsString;

                PnlGrade->Caption = pADO->FieldByName("GRADE_NM")->AsString;
                String sGrade = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                if (sGrade == "LTC0")
                {
                    PnlGrade->Color = clGray;
                }
                else if (sGrade == "LTC1")
                {
                    PnlGrade->Color = clLime;
                }
                else if (sGrade == "LTC2")
                {
                    PnlGrade->Color = clYellow;
                }
                else if (sGrade == "LTC3")
                {
                    PnlGrade->Color = clRed;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception(FrmLang->lblDbErr->Caption);//"[링크정보조회] 알수없는 DB 오류가 발생하였습니다.");
        }

        try
        {
#if 1
            sQry = "SELECT PRCN_DT2, PRCN_DT, CMTR_GRAD_CD,                                                 \r\n"
                   "       SPED, TRVL_HH, MISS_YN, PRCN_SPED, SMTH_SPED, ADJS_SPED, PTRN_SPED, DATA_NUM,    \r\n"
                   "       DECODE(C.CMMN_CD, NULL, A.CMTR_GRAD_CD, C.CMMN_CD_KOR_NM) AS GRADE_NM,           \r\n"
                   "       DECODE(B.CMMN_CD, NULL, A.PRCN_SORC, B.CMMN_CD_KOR_NM) AS PRCN_SORC_NM,          \r\n"
                   "       DECODE(D.CMMN_CD, NULL, A.ADJS_SORC, D.CMMN_CD_KOR_NM) AS ADJS_SORC_NM, RANK     \r\n"
                   "  FROM (SELECT RANK() OVER( PARTITION BY LINK_ID ORDER BY LINK_ID, PRCN_DT DESC ) RANK, \r\n"
                   "               TO_DATE(PRCN_DT, 'YYYYMMDDHH24MISS') AS PRCN_DT2, PRCN_DT,               \r\n"
                   "               SUBSTR(CMTR_GRAD_CD, 4, 1) AS GRADE,                                     \r\n"
                   "               MISS_YN, PRCN_SORC, ADJS_SORC, SPED, TRVL_HH, CMTR_GRAD_CD,              \r\n"
                   "               PRCN_SPED, SMTH_SPED, ADJS_SPED, PTRN_SPED, DATA_NUM                     \r\n"
                   "          FROM TB_LINK_TRAF_HS                                                          \r\n"
                   "         WHERE LINK_ID = :p01                                                           \r\n"
                   "           AND PRCN_DT > TO_CHAR(SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS')                  \r\n"
                   "       ) A,                                                                             \r\n"
                   "       (SELECT * FROM TB_CMMN_CD WHERE CMMN_CLSF_CD = 'CST' OR CMMN_CLSF_CD = 'MCS'     \r\n"
                   "       ) B,                                                                             \r\n"
                   "       (SELECT * FROM TB_CMMN_CD WHERE CMMN_CLSF_CD = 'CST' OR CMMN_CLSF_CD = 'MCS'     \r\n"
                   "       ) D,                                                                             \r\n"
                   "       (SELECT * FROM TB_CMMN_CD WHERE CMMN_CLSF_CD = 'LTC'                             \r\n"
                   "       ) C                                                                              \r\n"
                   " WHERE RANK <= 12                                                                       \r\n"
                   "   AND A.PRCN_SORC    = B.CMMN_CD(+)                                                    \r\n"
                   "   AND A.ADJS_SORC    = D.CMMN_CD(+)                                                    \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                                    \r\n"
                   " ORDER BY RANK                                                                          \r\n";
#else
            sQry = "SELECT RANK, PRCN_DT, C.CMMN_CD_KOR_NM GRADE, MISS_YN, B.CMMN_CD_KOR_NM PRCN_SORC, SPED, \r\n"
                   "       TRVL_HH, PRCN_SPED, SMTH_SPED, ADJS_SPED, PTRN_SPED,                              \r\n"
                   "       DECODE(B.CMMN_CD, NULL, A.PRCN_SORC, B.CMMN_CD_KOR_NM) AS PRCN_SORC_NM            \r\n"
                   "  FROM (SELECT RANK() OVER( PARTITION BY LINK_ID ORDER BY LINK_ID, PRCN_DT DESC ) RANK,  \r\n"
                   "               TO_DATE( PRCN_DT, 'YYYYMMDDHH24MISS' ) PRCN_DT,                           \r\n"
                   "               SUBSTR(CMTR_GRAD_CD, 4, 1) GRADE,                                         \r\n"
                   "               MISS_YN, PRCN_SORC, SPED, TRVL_HH, CMTR_GRAD_CD,                          \r\n"
                   "               PRCN_SPED, SMTH_SPED, ADJS_SPED, PTRN_SPED                                \r\n"
                   "          FROM TB_LINK_TRAF_HS                                                           \r\n"
                   "         WHERE LINK_ID = :p01                                                            \r\n"
                   "           AND PRCN_DT > TO_CHAR(SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS' )                  \r\n"
                   "       ) A,                                                                              \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                                         \r\n"
                   "         WHERE CMMN_CLSF_CD = 'CST'                                                      \r\n"
                   "            OR CMMN_CLSF_CD = 'MCS'                                                      \r\n"
                   "       ) B,                                                                              \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                                         \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                                      \r\n"
                   "       ) C                                                                               \r\n"
                   " WHERE RANK <= 12                                                                        \r\n"
                   "   AND A.PRCN_SORC    = B.CMMN_CD(+)                                                     \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                                     \r\n"
                   " ORDER BY RANK                                                                           \r\n";
#endif

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            int nRank;
            int nRow;
            for( ; !pADO->Eof; pADO->Next())
            {
                nRank = pADO->FieldByName("RANK")->AsInteger;    // 이력순석
                //if (nRank == 1) continue;

                nRow = pGDC->AppendRecord();

                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                pGDC->Values[nRow][Column01->Index] = ITSUtil_FormatStr(sDbDate, STR_DATETIME);
                pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("TRVL_HH")->AsString;
                pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("PRCN_SPED")->AsString;
                pGDC->Values[nRow][Column05->Index] = pADO->FieldByName("SMTH_SPED")->AsString;
                pGDC->Values[nRow][Column06->Index] = pADO->FieldByName("ADJS_SPED")->AsString;
                pGDC->Values[nRow][Column07->Index] = pADO->FieldByName("PTRN_SPED")->AsString;
                pGDC->Values[nRow][Column08->Index] = pADO->FieldByName("MISS_YN")->AsString;
                pGDC->Values[nRow][Column09->Index] = pADO->FieldByName("DATA_NUM")->AsString;
                pGDC->Values[nRow][Column10->Index] = pADO->FieldByName("PRCN_SORC_NM")->AsString;
                pGDC->Values[nRow][Column11->Index] = pADO->FieldByName("ADJS_SORC_NM")->AsString;
                pGDC->Values[nRow][Column12->Index] = pADO->FieldByName("GRADE_NM")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception(FrmLang->lblDbErr->Caption);//"[구간 상세정보] 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList1->EndUpdate();
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkInfo::SelTrafficServiceLink(String ALinkId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);
    try
    {
        TvList1->BeginUpdate();
        TvList->BeginUpdate();
        TcxDataController *pGDC1 = TvList1->DataController;
        TcxDataController *pGDC = TvList->DataController;

        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();

        // 현재소통정보
        try
        {
            sQry = "SELECT TO_DATE(PRCN_DT, 'YYYYMMDDHH24MISS') PRCN_DT2, PRCN_DT, CMTR_GRAD_CD, SPED, \r\n"
                   "       C.CMMN_CD_KOR_NM GRADE, PTRN_SPED, TRVL_HH, DATA_NUM                        \r\n"
                   "  FROM TB_IFSC_TRAF A,                                                             \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                                   \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                                \r\n"
                   "       ) C                                                                         \r\n"
                   " WHERE IFSC_ID = :p01                                                              \r\n"
                   "   AND PRCN_DT > TO_CHAR(SYSDATE -  10 / 1440, 'YYYYMMDDHH24MISS')                 \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                               \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            if (!pADO->Eof)
            {
                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                EdSvcDt->Text = ITSUtil_FormatStr(sDbDate, STR_DATETIME);

                pGDC1->Values[0][cxGridColumn1->Index] = pADO->FieldByName("TRVL_HH")->AsString;
                pGDC1->Values[0][cxGridColumn2->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC1->Values[0][cxGridColumn3->Index] = "-";
                pGDC1->Values[0][cxGridColumn4->Index] = "-";
                pGDC1->Values[0][cxGridColumn5->Index] = "-";
                pGDC1->Values[0][cxGridColumn6->Index] = pADO->FieldByName("PTRN_SPED")->AsString;
                pGDC1->Values[0][cxGridColumn7->Index] = "-";
                pGDC1->Values[0][cxGridColumn8->Index] = pADO->FieldByName("DATA_NUM")->AsString;

                PnlGrade->Caption = pADO->FieldByName("GRADE")->AsString;
                String sGrade = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                if (sGrade == "LTC0")
                {
                    PnlGrade->Color = clGray;
                }
                else if (sGrade == "LTC1")
                {
                    PnlGrade->Color = clLime;
                }
                else if (sGrade == "LTC2")
                {
                    PnlGrade->Color = clYellow;
                }
                else if (sGrade == "LTC3")
                {
                    PnlGrade->Color = clRed;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception(FrmLang->lblDbErr->Caption);//"[구간 상세정보] 알수없는 DB 오류가 발생하였습니다.");
        }

        try
        {
            sQry = "SELECT RANK, PRCN_DT2, PRCN_DT, C.CMMN_CD_KOR_NM GRADE_NM, SPED, TRVL_HH, PTRN_SPED, DATA_NUM  \r\n"
                   "  FROM (SELECT RANK() OVER( PARTITION BY IFSC_ID ORDER BY IFSC_ID, PRCN_DT DESC ) RANK,        \r\n"
                   "               TO_DATE( PRCN_DT, 'YYYYMMDDHH24MISS' ) PRCN_DT2, PRCN_DT,                       \r\n"
                   "               SUBSTR( CMTR_GRAD_CD, 4, 1 ) GRADE,                                             \r\n"
                   "               SPED, TRVL_HH, CMTR_GRAD_CD, PTRN_SPED, DATA_NUM                                \r\n"
                   "          FROM TB_IFSC_TRAF_HS                                                                 \r\n"
                   "         WHERE IFSC_ID = :p01                                                                  \r\n"
                   "           AND PRCN_DT > TO_CHAR(SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS')                         \r\n"
                   "       ) A,                                                                                    \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                                               \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                                            \r\n"
                   "       ) C                                                                                     \r\n"
                   " WHERE RANK <= 12                                                                              \r\n"
                   "  AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                                            \r\n"
                   " ORDER BY RANK                                                                                 \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            int nRank;
            int nRow;
            for( ; !pADO->Eof; pADO->Next())
            {
                nRank = pADO->FieldByName("RANK")->AsInteger;    // 이력순석
                //if (nRank == 1) continue;

                nRow = pGDC->AppendRecord();

                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                pGDC->Values[nRow][Column01->Index] = ITSUtil_FormatStr(sDbDate, STR_DATETIME);
                pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("TRVL_HH")->AsString;
                pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC->Values[nRow][Column04->Index] = "-";
                pGDC->Values[nRow][Column05->Index] = "-";
                pGDC->Values[nRow][Column06->Index] = "-";
                pGDC->Values[nRow][Column07->Index] = "-";
                pGDC->Values[nRow][Column08->Index] = "-";
                pGDC->Values[nRow][Column09->Index] = pADO->FieldByName("DATA_NUM")->AsString;
                pGDC->Values[nRow][Column10->Index] = "-";
                pGDC->Values[nRow][Column11->Index] = "-";
                pGDC->Values[nRow][Column12->Index] = pADO->FieldByName("GRADE_NM")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception(FrmLang->lblDbErr->Caption);//"[구간 상세정보] 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
        TvList1->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkInfo::SelTrafficRoad(String ALinkId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);
    try
    {
        TvList1->BeginUpdate();
        TvList->BeginUpdate();
        TcxDataController *pGDC1 = TvList1->DataController;
        TcxDataController *pGDC = TvList->DataController;

        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();

        // 현재소통정보
        try
        {
            sQry = "SELECT TO_DATE( PRCN_DT, 'YYYYMMDDHH24MISS' ) PRCN_DT2, PRCN_DT, CMTR_GRAD_CD, SPED, \r\n"
                   "       C.CMMN_CD_KOR_NM GRADE, PTRN_SPED, TRVL_HH, DATA_NUM                          \r\n"
                   "  FROM TB_ROAD_TRAF A,                                                               \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                                     \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                                  \r\n"
                   "       ) C                                                                           \r\n"
                   " WHERE ROAD_ID = :p01                                                                \r\n"
                   "   AND PRCN_DT > TO_CHAR(SYSDATE -  10 / 1440, 'YYYYMMDDHH24MISS')                   \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                                 \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            if (!pADO->Eof)
            {
                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                EdSvcDt->Text = ITSUtil_FormatStr(sDbDate, STR_DATETIME);

                pGDC1->Values[0][cxGridColumn1->Index] = pADO->FieldByName("TRVL_HH")->AsString;
                pGDC1->Values[0][cxGridColumn2->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC1->Values[0][cxGridColumn3->Index] = "-";
                pGDC1->Values[0][cxGridColumn4->Index] = "-";
                pGDC1->Values[0][cxGridColumn5->Index] = "-";
                pGDC1->Values[0][cxGridColumn6->Index] = pADO->FieldByName("PTRN_SPED")->AsString;
                pGDC1->Values[0][cxGridColumn7->Index] = "-";
                pGDC1->Values[0][cxGridColumn8->Index] = pADO->FieldByName("DATA_NUM")->AsString;

                PnlGrade->Caption = pADO->FieldByName("GRADE")->AsString;
                String sGrade = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                if (sGrade == "LTC0")
                {
                    PnlGrade->Color = clGray;
                }
                else if (sGrade == "LTC1")
                {
                    PnlGrade->Color = clLime;
                }
                else if (sGrade == "LTC2")
                {
                    PnlGrade->Color = clYellow;
                }
                else if (sGrade == "LTC3")
                {
                    PnlGrade->Color = clRed;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception(FrmLang->lblDbErr->Caption);//"[도로정보조회] 알수없는 DB 오류가 발생하였습니다.");
        }

        try
        {
            sQry = "SELECT RANK, PRCN_DT2, PRCN_DT, C.CMMN_CD_KOR_NM GRADE_NM, SPED, TRVL_HH, PTRN_SPED, DATA_NUM     \r\n"
                   "  FROM (SELECT RANK() OVER( PARTITION BY ROAD_ID ORDER BY ROAD_ID, PRCN_DT DESC ) RANK,           \r\n"
                   "               TO_DATE( PRCN_DT, 'YYYYMMDDHH24MISS' ) PRCN_DT2, PRCN_DT,                          \r\n"
                   "               SUBSTR( CMTR_GRAD_CD, 4, 1 ) GRADE,                                                \r\n"
                   "               SPED, TRVL_HH, CMTR_GRAD_CD, PTRN_SPED, DATA_NUM                                   \r\n"
                   "          FROM TB_ROAD_TRAF_HS                                                                    \r\n"
                   "         WHERE ROAD_ID = :p01                                                                     \r\n"
                   "           AND PRCN_DT > TO_CHAR( SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS' )                          \r\n"
                   "       ) A,                                                                                       \r\n"
                   "       (SELECT * FROM TB_CMMN_CD                                                                  \r\n"
                   "         WHERE CMMN_CLSF_CD = 'LTC'                                                               \r\n"
                   "       ) C                                                                                        \r\n"
                   " WHERE RANK <= 12                                                                                 \r\n"
                   "   AND A.CMTR_GRAD_CD = C.CMMN_CD(+)                                                              \r\n"
                   " ORDER BY RANK                                                                                    \r\n";

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            int nRank;
            int nRow;
            for( ; !pADO->Eof; pADO->Next())
            {
                nRank = pADO->FieldByName("RANK")->AsInteger;    // 이력순석
                //if (nRank == 1) continue;

                nRow = pGDC->AppendRecord();

                String sDbDate = pADO->FieldByName("PRCN_DT")->AsString;
                pGDC->Values[nRow][Column01->Index] = ITSUtil_FormatStr(sDbDate, STR_DATETIME);
                pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("TRVL_HH")->AsString;
                pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("SPED")->AsString;
                pGDC->Values[nRow][Column04->Index] = "-";
                pGDC->Values[nRow][Column05->Index] = "-";
                pGDC->Values[nRow][Column06->Index] = "-";
                pGDC->Values[nRow][Column07->Index] = "-";
                pGDC->Values[nRow][Column08->Index] = "-";
                pGDC->Values[nRow][Column09->Index] = pADO->FieldByName("DATA_NUM")->AsString;
                pGDC->Values[nRow][Column10->Index] = "-";
                pGDC->Values[nRow][Column11->Index] = "-";
                pGDC->Values[nRow][Column12->Index] = pADO->FieldByName("GRADE_NM")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception(FrmLang->lblDbErr->Caption);//"[구간 상세정보] 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
        TvList1->EndUpdate();
    }
}
//---------------------------------------------------------------------------


void __fastcall TFrmLinkInfo::BtnEditGradeClick(TObject *Sender)
{
	if (!FrmLinkGradeSet)
	{
		FrmLinkGradeSet = new TFrmLinkGradeSet(this);
	}
    if (FrmLinkGradeSet)
    {
        FrmLinkGradeSet->SetInfo(m_sLinkId, m_nLinkLevel);
        FrmLinkGradeSet->ShowModal();
        FrmLinkGradeSet = NULL;
    }
}
//---------------------------------------------------------------------------

