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

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

#include "TAS0020MF.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 "cxDBData"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridBandedTableView"
#pragma link "cxGridChartView"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridDBBandedTableView"
#pragma link "cxGridDBChartView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma resource "*.dfm"

TTAS0020M *TAS0020M = NULL;

//---------------------------------------------------------------------------
__fastcall TTAS0020M::TTAS0020M(TComponent* Owner)
	: TForm(Owner)
{
    ITSSkin_Load(this);
    LoadLocalSkin();
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = "간선도로 통계";

    LoadConfig();
    m_sLinkLevel = "1";

    MyItsAtrdManager = new TItsAtrdManager();
    MyItsAtrdManager->LoadFromDb();
    MyItsAtrdManager->LoadAtrdLength();
}
//---------------------------------------------------------------------------

/*
 * 환경설정정보 로딩
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::LoadConfig()
{
    m_sFooterRight = "";//GetConfigValue("APPLICATION", "REPORTRIGHT", "");
}
//---------------------------------------------------------------------------
/*
 * 공통으로 처리되지 않는 스킨을 변경한다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::LoadLocalSkin()
{
    /*
    *  스킨 변경
    */
    PgQuery->ParentColor = true;
    TsDay->ParentColor = true;
    TsMon->ParentColor = true;
    TsYear->ParentColor = true;
}
//---------------------------------------------------------------------------

/*
 * form이 메모리에서 생성될때 호출되는 생성자 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::FormCreate(TObject *Sender)
{
    for (int ii = 0; ii < MAX_CHART; ii++)
    {
        m_pFrmChart[ii] = new TPlugInChart(this);
        switch(ii)
        {
        case 0:
            m_pFrmChart[ii]->Parent = PnlDayGraph;
            m_pFrmChart[ii]->SetChartInfo("", "시간", "평균속도");
            break;
        case 1:
            m_pFrmChart[ii]->Parent = PnlMonGraph;
            m_pFrmChart[ii]->SetChartInfo("", "일", "평균속도");
            break;
        case 2:
            m_pFrmChart[ii]->Parent = PnlYearGraph;
            m_pFrmChart[ii]->SetChartInfo("", "월", "평균속도");
            break;
        }
        m_pFrmChart[ii]->Show();
    }

    PgList->HideTabs = true;
}
//---------------------------------------------------------------------------

/*
 * form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::FormShow(TObject *Sender)
{
    PgList->ActivePageIndex = 0;
    PgQuery->ActivePageIndex= 0;

    //DMMain->InsertListHeaderMenuItem(CxDay, (TcxGridDBTableView*)TvDay);

    // form 초기화
    FormInit();
}
//---------------------------------------------------------------------------

/*
 * form 초기화
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::FormInit()
{
    DtDay->Date  = IncHour(Now(), -24);
    DtMon->Date  = StartOfTheMonth(IncHour(Now(), -24*30));
    DtYear->Date = IncHour(Now(), -24);

    DtStTime->Time = StrToDateTime("00:00");
    DtEdTime->Time = StrToDateTime("23:59");

    ADOD->Connection = ITSDb_GetConnection();
    ADOM->Connection = ITSDb_GetConnection();
    ADOY->Connection = ITSDb_GetConnection();

    TvDay->DataController->DataSource = DSD;
    TvMon->DataController->DataSource = DSM;
    TvYear->DataController->DataSource = DSY;

    //FillStatComboBox(CbStatCfg, "IST0810M");
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);

        //DeleteStatComboBox(CbStatCfg);

        TvDay->BeginUpdate();
        ADOD->Close();
        TvDay->EndUpdate();

        TvMon->BeginUpdate();
        ADOM->Close();
        TvMon->EndUpdate();

        TvYear->BeginUpdate();
        ADOY->Close();
        TvYear->EndUpdate();

        if (MyItsAtrdManager)
        {
            delete MyItsAtrdManager;
            MyItsAtrdManager = NULL;
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * 화면이 로딩된 후 리스트 목록을 조회하도록 한다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::TmrRefreshTimer(TObject *Sender)
{
	TmrRefresh->Enabled = false;
}
//---------------------------------------------------------------------------

/*
 * 통계구분 탭 변경시 발생하는 이벤트 핸들러
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::PgQueryChange(TObject *Sender)
{
    //Tab Change
    PgList->ActivePageIndex = PgQuery->ActivePageIndex;

    switch(PgQuery->ActivePageIndex)
    {
    case 0:  DtDay->SetFocus();    break;
    case 1:  DtMon->SetFocus();    break;
    case 2:  DtYear->SetFocus();   break;
    default: break;
    }
}
//---------------------------------------------------------------------------

/*
 * Graph Button Click Event Handler
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::BtnGraphClick(TObject *Sender)
{
    // 차트를 다른 창에서 보여줘야 할 경우 Frm1121100_1F.cpp 내 소스 참조
}
//---------------------------------------------------------------------------

/*
 * Print Button Click Event Handler
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::BtnPrintClick(TObject *Sender)
{
    TcxGrid                  *pGrid = NULL;
    TcxGridDBBandedTableView *pView = NULL;
    TLabel                   *LblQry= NULL;
    String                   sTitle;

    switch(PgQuery->ActivePageIndex)
    {
    case 0:  pGrid = CxDay;  pView = TvDay;
             LblQry = LblQryTm;
             break;
    case 1:  pGrid = CxMon;  pView = TvMon;
             LblQry = LblQryDay;
             break;
    case 2:  pGrid = CxYear; pView = TvYear;
             LblQry = LblQryYear;
             break;
    default: return;
    }
    sTitle = LblRptTitle->Caption + "\r\n" + LblQry->Caption;

    if (pView->DataController->RecordCount == 0)
    {
        ShowMessage("인쇄 할 데이터가 없습니다.");
        return;
    }

    //ComponentPrinterLink->OptionsView->Footers      = true;
    //ComponentPrinterLink->OptionsView->GroupFooters = true;
    //ComponentPrinterLink->OptionsSize->AutoWidth    = true;

    //CMM_PrintList(pGrid, sTitle, m_sFooterRight);

    pView->DataController->FocusedRecordIndex = 0;
}
//---------------------------------------------------------------------------

/*
 * Excel Button Click Event Handler
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid                  *pGrid = NULL;
    TcxGridDBBandedTableView *pView = NULL;
    TLabel                   *LblQry= NULL;
    String                   sTitle;

    switch(PgQuery->ActivePageIndex)
    {
    case 0:  pGrid = CxDay;  pView = TvDay;
             LblQry = LblQryTm;
             break;
    case 1:  pGrid = CxMon;  pView = TvMon;
             LblQry = LblQryDay;
             break;
    case 2:  pGrid = CxYear; pView = TvYear;
             LblQry = LblQryYear;
             break;
    default: return;
    }
    sTitle = LblRptTitle->Caption + LblQry->Caption;

    if (pView->DataController->RecordCount == 0)
    {
        ShowMessage("저장 할 데이터가 없습니다.");
        return;
    }

    CMM_ExportToExcelFile(sTitle, pGrid, pView, this);
}
//---------------------------------------------------------------------------

/*
 * Refresh Button Click Event Handler
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::BtnSearchClick(TObject *Sender)
{
    m_sLinkLevel = CbLinkLevel->Text;
	RefreshData();
}
//---------------------------------------------------------------------------

/*
 * Refresh Data Event Function
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::RefreshData()
{
    TcxGridDBBandedTableView *pView = NULL;
    TLabel                   *LblQry= NULL;
    TLabel                   *LblRecords= NULL;
    WORD nYear, nMonth, nDay, nHour, nMin, nSec, nMSec;
    int  nDispCols;
    String sBandText = "";
    String sMsgTitle, sMsgString;

    TDateTimePicker *DtQryTm;
    int nStYear, nEdYear;

    switch(PgQuery->ActivePageIndex)
    {
    case 0:
        pView      = TvDay;
        DtQryTm    = DtDay;
        LblQry     = LblQryTm;
        LblRecords = LblRecordsTm;

        sBandText = "(" + DtDay->Date.FormatString("yyyy년 mm월 dd일") + ")";
        nDispCols = 24;
        if (StartOfTheDay(Now()) <= DtDay->DateTime)
        {
            DecodeTime(Now(), nHour, nMin, nSec, nMSec);
            if (nMin > 5)  nDispCols = nHour;
            else           nDispCols = nHour-1;
        }
        break;
    case 1:
        pView      = TvMon;
        DtQryTm    = DtMon;
        LblQry     = LblQryDay;
        LblRecords = LblRecordsDay;

        sBandText = "(" + DtMon->Date.FormatString("yyyy년 mm월") + ")";
        nDispCols = ITSUtil_GetMonthLastDay(AnsiString(DtMon->Date.FormatString("yyyymm")).c_str());
        if (StartOfTheDay(Now()) <= EndOfTheMonth(DtMon->DateTime))
        {
            DecodeDate(Now(), nYear, nMonth, nDay);
            if (nDay < nDispCols) nDispCols = nDay-1;
        }
        {
            String sStDateTime = DtStTime->Time.FormatString("hhnn");
            String sEdDateTime = DtEdTime->Time.FormatString("hhnn");
            if (sStDateTime > sEdDateTime)
            {
                Application->MessageBox(L"검색 시작시각이 검색 종료시각 보다 큽니다.", FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
                return;
            }
        }
        break;
    case 2:
        pView      = TvYear;
        DtQryTm    = DtYear;
        LblQry     = LblQryYear;
        LblRecords = LblRecordsYear;

        sBandText = "(" + DtYear->Date.FormatString("yyyy년") + ")";
        nDispCols = 12;
        if (Now().FormatString("yyyy") == DtYear->DateTime.FormatString("yyyy"))
        {
            DecodeDate(Now(), nYear, nMonth, nDay);
            nDispCols = nMonth;
        }
        break;
    default: return;
    }

    if (!pView) return;

    if (DtQryTm->DateTime.FormatString("yyyymmdd").ToInt() > Now().FormatString("yyyymmdd").ToInt())
    {
        Application->MessageBox(L"통계검색 시각 정보를 확인하세요.", FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);

        MessageDlg(sMsgString.c_str(), mtInformation, TMsgDlgButtons() << mbOK, NULL);
        DtQryTm->SetFocus();
        nDispCols = 0;  /* 리턴하지 않는 경우 모든 컬럼을 숨긴다. */
        return;
    }


    LblQry->Caption = sBandText;

    CMM_ShowListColumn(pView, nDispCols);

    TWaitCursor crs;
    try
    {
        pView->BeginUpdate();
        switch(PgQuery->ActivePageIndex)
        {
        case 0:  DispDayStatistics();   break;
        case 1:  DispMonthStatistics(); break;
        case 2:  DispYearStatistics();  break;
        default: return;
        }
    }
    __finally
    {
        pView->EndUpdate();
        //pView->ApplyBestFit(NULL, false, false);
        LblRecords->Caption = FormatFloat("##,##0", pView->DataController->RecordCount) + " 건";

        String sEnd = "데이터 " + LblRecords->Caption + " 이 조회 되었습니다.";
        Application->MessageBox(sEnd.c_str(), L"데이터 조회 완료", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
    }

    DrawChart(sBandText);
}
//---------------------------------------------------------------------------

/*
 * 조회한 데이터를 기준으로 차트를 그린다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::DrawChart(String sTitle)
{
    TcxGridDBBandedTableView *pView = NULL;
    String                   sAxisX, sAxisY;
    int    nAxisY1, nAxisY2, nAxisY3, nStColIdx, nEdColIdx, nTotColIdx;

    nAxisY3 = 0;
    nEdColIdx = 0;
    nTotColIdx = -1;
    switch(PgQuery->ActivePageIndex)
    {
    case 0:  pView = TvDay;
             nAxisY1 = vDayColumnNo->Index;
             nAxisY2 = vDayColumnTot->Index;
             nAxisY3 = TvDayColumn1->Index;
             nEdColIdx = cxGridDBBandedColumn54->Index;
             nStColIdx = vDayColumnAvg->Index;//vDayColumn1->Index;
             break;
    case 1:  pView = TvMon;
             nAxisY1 = vMonColumnNo->Index;
             nAxisY2 = vMonColumnTot->Index;
             nAxisY3 = TvMonColumn1->Index;
             nEdColIdx = vMonColumn31->Index;
             nStColIdx = vMonColumnAvg->Index;//vMonColumn1->Index;
             break;
    case 2:  pView = TvYear;
             nAxisY1 = vYearColumnNo->Index;
             nAxisY2 = vYearColumnTot->Index;
             nAxisY3 = TvYearColumn1->Index;
             nEdColIdx = vYearColumn12->Index;
             nStColIdx = vYearColumnAvg->Index;//vYearColumn1->Index;
             break;
    default: return;
    }

    /*
    *  Chart draw
    */
    try
    {
        m_pFrmChart[PgQuery->ActivePageIndex]->Clear();
        if (pView->DataController->RecordCount == 0)
        {
            //ShowMessage("표출 할 데이터가 없습니다.");
            return;
        }

        m_pFrmChart[PgQuery->ActivePageIndex]->BeginDraw("", "", "");
        //m_pFrmChart[PgQuery->ActivePageIndex]->DrawChart4((TcxGridDBTableView *)pView, nAxisY1, nAxisY2, nStColIdx, nTotColIdx);
        m_pFrmChart[PgQuery->ActivePageIndex]->DrawChart5((TcxGridDBTableView *)pView, nAxisY1, nAxisY2, nAxisY3, nStColIdx, nEdColIdx, nTotColIdx);
        m_pFrmChart[PgQuery->ActivePageIndex]->EndDraw();
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * 시간대별 일 통계 데이터 조회/출력
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::DispDayStatistics()
{
    String sQry;
    String sDD;
    String sFrom, sTo;
    String sTmp;
    int    ii;

    sDD = DtDay->Date.FormatString("yyyyMMdd");
    sFrom = sDD + "000000";
    sTo   = sDD + "235959";

sQry =
"SELECT LY.ARTERYID, LY.ARTERYNAME, LY.DIRECTION, LY.DIRECTIONNAME, LY.ARTERYLENGHT, LX.TRFTYPE,     \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(TTM, 0, '-', ROUND(TLL*3.6/TTM, 0))) AS ARTERYSPD,     \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T00, 0, '-', ROUND(L00*3.6/T00, 0))) AS C00,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T01, 0, '-', ROUND(L01*3.6/T01, 0))) AS C01,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T02, 0, '-', ROUND(L02*3.6/T02, 0))) AS C02,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T03, 0, '-', ROUND(L03*3.6/T03, 0))) AS C03,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T04, 0, '-', ROUND(L04*3.6/T04, 0))) AS C04,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T05, 0, '-', ROUND(L05*3.6/T05, 0))) AS C05,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T06, 0, '-', ROUND(L06*3.6/T06, 0))) AS C06,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T07, 0, '-', ROUND(L07*3.6/T07, 0))) AS C07,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T08, 0, '-', ROUND(L08*3.6/T08, 0))) AS C08,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T09, 0, '-', ROUND(L09*3.6/T09, 0))) AS C09,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T10, 0, '-', ROUND(L10*3.6/T10, 0))) AS C10,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T11, 0, '-', ROUND(L11*3.6/T11, 0))) AS C11,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T12, 0, '-', ROUND(L12*3.6/T12, 0))) AS C12,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T13, 0, '-', ROUND(L13*3.6/T13, 0))) AS C13,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T14, 0, '-', ROUND(L14*3.6/T14, 0))) AS C14,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T15, 0, '-', ROUND(L15*3.6/T15, 0))) AS C15,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T16, 0, '-', ROUND(L16*3.6/T16, 0))) AS C16,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T17, 0, '-', ROUND(L17*3.6/T17, 0))) AS C17,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T18, 0, '-', ROUND(L18*3.6/T18, 0))) AS C18,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T19, 0, '-', ROUND(L19*3.6/T19, 0))) AS C19,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T20, 0, '-', ROUND(L20*3.6/T20, 0))) AS C20,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T21, 0, '-', ROUND(L21*3.6/T21, 0))) AS C21,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T22, 0, '-', ROUND(L22*3.6/T22, 0))) AS C22,           \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T23, 0, '-', ROUND(L23*3.6/T23, 0))) AS C23            \r\n"
"  FROM (                                                                                            \r\n"
"SELECT GROUPING(ARTERYID)  AS GROUPING_ARTERYID,                                                    \r\n"
"               GROUPING(DIRECTION) AS GROUPING_DIRECTION,                                           \r\n"
"               GROUPING(TRFTYPE)   AS GROUPING_TRFTYPE,                                             \r\n"
"               CASE WHEN GROUPING(ARTERYID ) = 1 THEN '도로' ELSE ARTERYID  END AS GROUP_ARTERYID,  \r\n"
"               CASE WHEN GROUPING(DIRECTION) = 1 THEN '방향' ELSE DIRECTION END AS GROUP_DIRECTION, \r\n"
"               CASE WHEN GROUPING(TRFTYPE)   = 1 THEN '소계' ELSE TRFTYPE   END AS GROUP_TRFTYPE,   \r\n"
"               ARTERYID, DIRECTION, TRFTYPE,                                                        \r\n"
"               SUM(DECODE(TRVTM, NULL, 0, LINKLEN)) TLL,                                            \r\n"
"               SUM(DECODE(TRVTM, NULL, 0, TRVTM  )) TTM,                                            \r\n"
"               SUM(DECODE(STATHOUR, '00', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T00,              \r\n"
"               SUM(DECODE(STATHOUR, '00', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L00,              \r\n"
"               SUM(DECODE(STATHOUR, '01', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T01,              \r\n"
"               SUM(DECODE(STATHOUR, '01', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L01,              \r\n"
"               SUM(DECODE(STATHOUR, '02', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T02,              \r\n"
"               SUM(DECODE(STATHOUR, '02', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L02,              \r\n"
"               SUM(DECODE(STATHOUR, '03', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T03,              \r\n"
"               SUM(DECODE(STATHOUR, '03', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L03,              \r\n"
"               SUM(DECODE(STATHOUR, '04', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T04,              \r\n"
"               SUM(DECODE(STATHOUR, '04', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L04,              \r\n"
"               SUM(DECODE(STATHOUR, '05', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T05,              \r\n"
"               SUM(DECODE(STATHOUR, '05', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L05,              \r\n"
"               SUM(DECODE(STATHOUR, '06', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T06,              \r\n"
"               SUM(DECODE(STATHOUR, '06', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L06,              \r\n"
"               SUM(DECODE(STATHOUR, '07', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T07,              \r\n"
"               SUM(DECODE(STATHOUR, '07', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L07,              \r\n"
"               SUM(DECODE(STATHOUR, '08', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T08,              \r\n"
"               SUM(DECODE(STATHOUR, '08', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L08,              \r\n"
"               SUM(DECODE(STATHOUR, '09', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T09,              \r\n"
"               SUM(DECODE(STATHOUR, '09', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L09,              \r\n"
"               SUM(DECODE(STATHOUR, '10', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T10,              \r\n"
"               SUM(DECODE(STATHOUR, '10', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L10,              \r\n"
"               SUM(DECODE(STATHOUR, '11', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T11,              \r\n"
"               SUM(DECODE(STATHOUR, '11', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L11,              \r\n"
"               SUM(DECODE(STATHOUR, '12', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T12,              \r\n"
"               SUM(DECODE(STATHOUR, '12', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L12,              \r\n"
"               SUM(DECODE(STATHOUR, '13', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T13,              \r\n"
"               SUM(DECODE(STATHOUR, '13', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L13,              \r\n"
"               SUM(DECODE(STATHOUR, '14', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T14,              \r\n"
"               SUM(DECODE(STATHOUR, '14', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L14,              \r\n"
"               SUM(DECODE(STATHOUR, '15', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T15,              \r\n"
"               SUM(DECODE(STATHOUR, '15', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L15,              \r\n"
"               SUM(DECODE(STATHOUR, '16', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T16,              \r\n"
"               SUM(DECODE(STATHOUR, '16', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L16,              \r\n"
"               SUM(DECODE(STATHOUR, '17', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T17,              \r\n"
"               SUM(DECODE(STATHOUR, '17', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L17,              \r\n"
"               SUM(DECODE(STATHOUR, '18', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T18,              \r\n"
"               SUM(DECODE(STATHOUR, '18', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L18,              \r\n"
"               SUM(DECODE(STATHOUR, '19', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T19,              \r\n"
"               SUM(DECODE(STATHOUR, '19', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L19,              \r\n"
"               SUM(DECODE(STATHOUR, '20', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T20,              \r\n"
"               SUM(DECODE(STATHOUR, '20', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L20,              \r\n"
"               SUM(DECODE(STATHOUR, '21', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T21,              \r\n"
"               SUM(DECODE(STATHOUR, '21', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L21,              \r\n"
"               SUM(DECODE(STATHOUR, '22', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T22,              \r\n"
"               SUM(DECODE(STATHOUR, '22', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L22,              \r\n"
"               SUM(DECODE(STATHOUR, '23', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T23,              \r\n"
"               SUM(DECODE(STATHOUR, '23', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L23               \r\n"
"          FROM (                                                                                    \r\n"
"SELECT Y.ARTERYID, Y.ARTERYNAME, Y.DIRECTION, Y.ARTERYLEN, X.TRFTYPE,                               \r\n"
"       Y.LINKID, Y.LINKLEN, X.STATHOUR, X.SPD, X.TRVTM                                              \r\n"
"  FROM (SELECT /*+ INDEX(A IX_ROAD_HH_STAT_PK) */                                                   \r\n"
"               'FSN' AS TRFTYPE,                                                                    \r\n"
"               B.ATRD_ID AS ARTERYID, A.ROAD_ID AS LINKID, C.SECT_LNGT AS LINKLEN,                  \r\n"
"               SUBSTR(A.STAT_DT, 9, 2) AS STATHOUR, A.SPED AS SPD, A.TRVL_HH AS TRVTM               \r\n"
"          FROM TB_ROAD_HH_STAT A, TB_ATRD_ROAD_RLTN B, TB_ROAD C                                    \r\n"
"         WHERE 1=1                                                                                  \r\n"
"           AND A.STAT_DT BETWEEN :p01 AND :p02                                                      \r\n"
"           AND A.ROAD_ID = B.ROAD_ID                                                                \r\n"
"           AND B.ROAD_ID = C.ROAD_ID                                                                \r\n"
"       ) X,                                                                                         \r\n"
"       VW_ATRD_ROAD_RLTN Y                                                                          \r\n"
" WHERE Y.LINKID = X.LINKID                                                                          \r\n"
")                                                                                                   \r\n"
"          GROUP BY ROLLUP (ARTERYID, DIRECTION, TRFTYPE)                                            \r\n"
"         HAVING GROUPING(TRFTYPE) = 0) LX,                                                          \r\n"
"       VW_ATRD LY                                                                                   \r\n"
" WHERE LY.ARTERYID  = LX.ARTERYID(+)                                                                \r\n"
"   AND LY.DIRECTION = LX.DIRECTION(+)                                                               \r\n"
" ORDER BY LY.ARTERYNAME, LY.DIRECTION, LX.TRFTYPE                                                   \r\n";

    try
    {
        ITSDb_SQLText(ADOD, sQry);

        ITSDb_SQLBind(ADOD, "p01", sFrom);
        ITSDb_SQLBind(ADOD, "p02", sTo);

        ITSDb_SQLOpen(ADOD);

        ShowRecordCount(ADOD->RecordCount);
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch (Exception &exception)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
}
//---------------------------------------------------------------------------

/*
 * 일별 월 통계 데이터 조회/출력
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::DispMonthStatistics()
{
	String sQry = "";
    String sMM;
    String sFrom, sTo;
    String sTmp;
    int    ii;

	sMM = DtMon->Date.FormatString("yyyyMM");
    sFrom = sMM + "01000000";
    sTo   = sMM + "31235959";

    String sStTm = DtStTime->Time.FormatString("hhnn");
    String sEdTm = DtEdTime->Time.FormatString("hhnn");

sQry =
"SELECT LY.ARTERYID, LY.ARTERYNAME, LY.DIRECTION, LY.DIRECTIONNAME, LY.ARTERYLENGHT, LX.TRFTYPE,      \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(TTM, 0, '-', ROUND(TLL*3.6/TTM, 0))) AS ARTERYSPD,      \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T01, 0, '-', ROUND(L01*3.6/T01, 0))) AS C01,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T02, 0, '-', ROUND(L02*3.6/T02, 0))) AS C02,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T03, 0, '-', ROUND(L03*3.6/T03, 0))) AS C03,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T04, 0, '-', ROUND(L04*3.6/T04, 0))) AS C04,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T05, 0, '-', ROUND(L05*3.6/T05, 0))) AS C05,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T06, 0, '-', ROUND(L06*3.6/T06, 0))) AS C06,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T07, 0, '-', ROUND(L07*3.6/T07, 0))) AS C07,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T08, 0, '-', ROUND(L08*3.6/T08, 0))) AS C08,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T09, 0, '-', ROUND(L09*3.6/T09, 0))) AS C09,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T10, 0, '-', ROUND(L10*3.6/T10, 0))) AS C10,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T11, 0, '-', ROUND(L11*3.6/T11, 0))) AS C11,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T12, 0, '-', ROUND(L12*3.6/T12, 0))) AS C12,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T13, 0, '-', ROUND(L13*3.6/T13, 0))) AS C13,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T14, 0, '-', ROUND(L14*3.6/T14, 0))) AS C14,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T15, 0, '-', ROUND(L15*3.6/T15, 0))) AS C15,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T16, 0, '-', ROUND(L16*3.6/T16, 0))) AS C16,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T17, 0, '-', ROUND(L17*3.6/T17, 0))) AS C17,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T18, 0, '-', ROUND(L18*3.6/T18, 0))) AS C18,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T19, 0, '-', ROUND(L19*3.6/T19, 0))) AS C19,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T20, 0, '-', ROUND(L20*3.6/T20, 0))) AS C20,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T21, 0, '-', ROUND(L21*3.6/T21, 0))) AS C21,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T22, 0, '-', ROUND(L22*3.6/T22, 0))) AS C22,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T23, 0, '-', ROUND(L23*3.6/T23, 0))) AS C23,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T24, 0, '-', ROUND(L24*3.6/T24, 0))) AS C24,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T25, 0, '-', ROUND(L25*3.6/T25, 0))) AS C25,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T26, 0, '-', ROUND(L26*3.6/T26, 0))) AS C26,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T27, 0, '-', ROUND(L27*3.6/T27, 0))) AS C27,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T28, 0, '-', ROUND(L28*3.6/T28, 0))) AS C28,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T29, 0, '-', ROUND(L29*3.6/T29, 0))) AS C29,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T30, 0, '-', ROUND(L30*3.6/T30, 0))) AS C30,            \r\n"
"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T31, 0, '-', ROUND(L31*3.6/T31, 0))) AS C31             \r\n"
"  FROM (                                                                                             \r\n"
"SELECT GROUPING(ARTERYID)  AS GROUPING_ARTERYID,                                                     \r\n"
"               GROUPING(DIRECTION) AS GROUPING_DIRECTION,                                            \r\n"
"               GROUPING(TRFTYPE)   AS GROUPING_TRFTYPE,                                              \r\n"
"               CASE WHEN GROUPING(ARTERYID ) = 1 THEN '도로' ELSE ARTERYID  END AS GROUP_ARTERYID,   \r\n"
"               CASE WHEN GROUPING(DIRECTION) = 1 THEN '방향' ELSE DIRECTION END AS GROUP_DIRECTION,  \r\n"
"               CASE WHEN GROUPING(TRFTYPE)   = 1 THEN '소계' ELSE TRFTYPE   END AS GROUP_TRFTYPE,    \r\n"
"               ARTERYID, DIRECTION, TRFTYPE,                                                         \r\n"
"               SUM(DECODE(TRVTM, NULL, 0, LINKLEN)) TLL,                                             \r\n"
"               SUM(DECODE(TRVTM, NULL, 0, TRVTM  )) TTM,                                             \r\n"
"               SUM(DECODE(STATDAY, '01', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T01,                \r\n"
"               SUM(DECODE(STATDAY, '01', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L01,                \r\n"
"               SUM(DECODE(STATDAY, '02', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T02,                \r\n"
"               SUM(DECODE(STATDAY, '02', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L02,                \r\n"
"               SUM(DECODE(STATDAY, '03', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T03,                \r\n"
"               SUM(DECODE(STATDAY, '03', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L03,                \r\n"
"               SUM(DECODE(STATDAY, '04', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T04,                \r\n"
"               SUM(DECODE(STATDAY, '04', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L04,                \r\n"
"               SUM(DECODE(STATDAY, '05', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T05,                \r\n"
"               SUM(DECODE(STATDAY, '05', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L05,                \r\n"
"               SUM(DECODE(STATDAY, '06', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T06,                \r\n"
"               SUM(DECODE(STATDAY, '06', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L06,                \r\n"
"               SUM(DECODE(STATDAY, '07', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T07,                \r\n"
"               SUM(DECODE(STATDAY, '07', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L07,                \r\n"
"               SUM(DECODE(STATDAY, '08', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T08,                \r\n"
"               SUM(DECODE(STATDAY, '08', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L08,                \r\n"
"               SUM(DECODE(STATDAY, '09', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T09,                \r\n"
"               SUM(DECODE(STATDAY, '09', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L09,                \r\n"
"               SUM(DECODE(STATDAY, '10', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T10,                \r\n"
"               SUM(DECODE(STATDAY, '10', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L10,                \r\n"
"               SUM(DECODE(STATDAY, '11', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T11,                \r\n"
"               SUM(DECODE(STATDAY, '11', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L11,                \r\n"
"               SUM(DECODE(STATDAY, '12', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T12,                \r\n"
"               SUM(DECODE(STATDAY, '12', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L12,                \r\n"
"               SUM(DECODE(STATDAY, '13', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T13,                \r\n"
"               SUM(DECODE(STATDAY, '13', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L13,                \r\n"
"               SUM(DECODE(STATDAY, '14', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T14,                \r\n"
"               SUM(DECODE(STATDAY, '14', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L14,                \r\n"
"               SUM(DECODE(STATDAY, '15', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T15,                \r\n"
"               SUM(DECODE(STATDAY, '15', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L15,                \r\n"
"               SUM(DECODE(STATDAY, '16', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T16,                \r\n"
"               SUM(DECODE(STATDAY, '16', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L16,                \r\n"
"               SUM(DECODE(STATDAY, '17', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T17,                \r\n"
"               SUM(DECODE(STATDAY, '17', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L17,                \r\n"
"               SUM(DECODE(STATDAY, '18', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T18,                \r\n"
"               SUM(DECODE(STATDAY, '18', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L18,                \r\n"
"               SUM(DECODE(STATDAY, '19', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T19,                \r\n"
"               SUM(DECODE(STATDAY, '19', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L19,                \r\n"
"               SUM(DECODE(STATDAY, '20', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T20,                \r\n"
"               SUM(DECODE(STATDAY, '20', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L20,                \r\n"
"               SUM(DECODE(STATDAY, '21', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T21,                \r\n"
"               SUM(DECODE(STATDAY, '21', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L21,                \r\n"
"               SUM(DECODE(STATDAY, '22', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T22,                \r\n"
"               SUM(DECODE(STATDAY, '22', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L22,                \r\n"
"               SUM(DECODE(STATDAY, '23', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T23,                \r\n"
"               SUM(DECODE(STATDAY, '23', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L23,                \r\n"
"               SUM(DECODE(STATDAY, '24', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T24,                \r\n"
"               SUM(DECODE(STATDAY, '24', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L24,                \r\n"
"               SUM(DECODE(STATDAY, '25', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T25,                \r\n"
"               SUM(DECODE(STATDAY, '25', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L25,                \r\n"
"               SUM(DECODE(STATDAY, '26', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T26,                \r\n"
"               SUM(DECODE(STATDAY, '26', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L26,                \r\n"
"               SUM(DECODE(STATDAY, '27', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T27,                \r\n"
"               SUM(DECODE(STATDAY, '27', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L27,                \r\n"
"               SUM(DECODE(STATDAY, '28', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T28,                \r\n"
"               SUM(DECODE(STATDAY, '28', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L28,                \r\n"
"               SUM(DECODE(STATDAY, '29', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T29,                \r\n"
"               SUM(DECODE(STATDAY, '29', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L29,                \r\n"
"               SUM(DECODE(STATDAY, '30', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T30,                \r\n"
"               SUM(DECODE(STATDAY, '30', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L30,                \r\n"
"               SUM(DECODE(STATDAY, '31', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T31,                \r\n"
"               SUM(DECODE(STATDAY, '31', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L31                 \r\n"
"          FROM (                                                                                     \r\n"
"SELECT Y.ARTERYID, Y.ARTERYNAME, Y.DIRECTION, Y.ARTERYLEN, X.TRFTYPE,                                \r\n"
"       Y.LINKID, Y.LINKLEN, X.STATDAY, X.SPD, X.TRVTM                                                \r\n"
"  FROM (SELECT /*+ INDEX(A IX_ROAD_15M_STAT_PK) */                                                   \r\n"
"               'FSN' AS TRFTYPE,                                                                     \r\n"
"               B.ATRD_ID AS ARTERYID, A.ROAD_ID AS LINKID, C.SECT_LNGT AS LINKLEN,                   \r\n"
"               SUBSTR(A.STAT_DT, 7, 2) AS STATDAY, A.SPED AS SPD, A.TRVL_HH AS TRVTM                 \r\n"
"          FROM TB_ROAD_15M_STAT A, TB_ATRD_ROAD_RLTN B, TB_ROAD C                                    \r\n"
"         WHERE 1=1                                                                                   \r\n"
"           AND A.STAT_DT               BETWEEN :p01 AND :p02                                         \r\n"
"           AND SUBSTR(A.STAT_DT, 9, 4) BETWEEN :p03 AND :p04                                         \r\n"
"           AND A.ROAD_ID = B.ROAD_ID                                                                 \r\n"
"           AND B.ROAD_ID = C.ROAD_ID                                                                 \r\n"
"       ) X,                                                                                          \r\n"
"       VW_ATRD_ROAD_RLTN Y                                                                           \r\n"
" WHERE Y.LINKID = X.LINKID                                                                           \r\n"
")                                                                                                    \r\n"
"          GROUP BY ROLLUP (ARTERYID, DIRECTION, TRFTYPE)                                             \r\n"
"         HAVING GROUPING(TRFTYPE) = 0) LX,                                                           \r\n"
"       VW_ATRD LY                                                                                    \r\n"
" WHERE LY.ARTERYID  = LX.ARTERYID(+)                                                                 \r\n"
"   AND LY.DIRECTION = LX.DIRECTION(+)                                                                \r\n"
" ORDER BY LY.ARTERYNAME, LY.DIRECTION, LX.TRFTYPE                                                    \r\n";

    try
    {
        ITSDb_SQLText(ADOM, sQry);

        ITSDb_SQLBind(ADOM, "p01", sFrom);
        ITSDb_SQLBind(ADOM, "p02", sTo);
        ITSDb_SQLBind(ADOM, "p03", sStTm);
        ITSDb_SQLBind(ADOM, "p04", sEdTm);

        ITSDb_SQLOpen(ADOM);

        ShowRecordCount(ADOM->RecordCount);
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch (Exception &exception)
    {
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
}
//---------------------------------------------------------------------------

/*
 * 월별 년 통계 데이터 조회/출력
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::DispYearStatistics()
{
#if 0
	String sQry = "";
    String sYYYY;
    String sTmp;
    int    ii;
    
	sYYYY = DtYear->Date.FormatString("yyyy");

    String sQry1 = "";
    sQry1 = "SELECT LY.ARTERYID, LY.ARTERYNAME, LY.DIRECTION, LY.DIRECTIONNAME, LY.ARTERYLENGTH, LX.TRFTYPE, \r\n"
            "       DECODE(LX.ARTERYID, NULL, '-', DECODE(TTM, 0, '-', ROUND(TLL*3.6/TTM, 0))) AS ARTERYSPD, \r\n";

    int nLoops = 13;
    for (ii = 1; ii < nLoops; ii++)
    {
        sTmp = "";
        sTmp.printf(L"       DECODE(LX.ARTERYID, NULL, '-', DECODE(T%02d, 0, '-', ROUND(L%02d*3.6/T%02d, 0))) AS C%02d ", ii, ii, ii, ii);
        if (ii == nLoops-1) sTmp += " \r\n";
        else          sTmp += ",\r\n";
        sQry1 += sTmp;
    }

    String sQry2 = "";
    sQry2 = "  FROM (SELECT GROUPING(ARTERYID)  AS GROUPING_ARTERYID, GROUPING(DIRECTION) AS GROUPING_DIRECTION, \r\n"
            "               GROUPING(TRFTYPE)   AS GROUPING_TRFTYPE,                                             \r\n"
            "               CASE WHEN GROUPING(ARTERYID ) = 1 THEN '도로' ELSE ARTERYID  END AS GROUP_ARTERYID,  \r\n"                                                            
            "               CASE WHEN GROUPING(DIRECTION) = 1 THEN '방향' ELSE DIRECTION END AS GROUP_DIRECTION, \r\n"
            "               CASE WHEN GROUPING(TRFTYPE)   = 1 THEN '소계' ELSE TRFTYPE   END AS GROUP_TRFTYPE,   \r\n"
            "               ARTERYID, DIRECTION, TRFTYPE,                                                        \r\n"
            "               SUM(DECODE(TRVTM, NULL, 0, LINKLEN)) TLL,                                            \r\n"
            "               SUM(DECODE(TRVTM, NULL, 0, TRVTM  )) TTM,                                            \r\n";

    for (ii = 0; ii < nLoops; ii++)
    {
        sTmp = "";
        sTmp.printf(L"               SUM(DECODE(STATMON, '%02d', DECODE(TRVTM, NULL, 0, TRVTM),   0)) AS T%02d, \r\n"
                     "               SUM(DECODE(STATMON, '%02d', DECODE(TRVTM, NULL, 0, LINKLEN), 0)) AS L%02d", ii, ii, ii, ii);
        if (ii == nLoops-1) sTmp += " \r\n";
        else          sTmp += ",\r\n";
        sQry2 += sTmp;
    }

    String sQry3 = 0;
    sQry3 = "          FROM ( \r\n";

    int nBinds  = 0;
    int nColCnt = CbStatCfg->Items->Count;
    for (ii = 0; ii < nColCnt; ii++)
    {
        TObjStat *pObj = (TObjStat *)GetStatObject(CbStatCfg, ii);
        if (NULL == pObj) continue;

        sTmp = "";
        sTmp.printf(L"                SELECT Y.ARTERYID, Y.ARTERYNAME, Y.DIRECTION, Y.ARTERYLEN, X.TRFTYPE,        \r\n"
                     "                       Y.LINKID, Y.LINKLEN, X.STATMON, X.SPD, X.TRVTM                        \r\n"
                     "                 FROM (SELECT /*+ INDEX(A %s) */                                             \r\n"
                     "                              '%s' AS TRFTYPE,                                               \r\n"
                     "                              A.LINKID, A.STATMON, A.AVGSPD AS SPD, A.AVGTRVTM AS TRVTM      \r\n"
                     "                         FROM %s A                                                           \r\n"
                     "                        WHERE A.STATYEAR = :f%d) X,                                          \r\n"
                     "                      (SELECT A.ARTERYID, B.ARTERYNAME, A.DIRECTION, B.ARTERYTYPE,           \r\n"
                     "                              DECODE(A.DIRECTION, '0', B.UPLENGTH, B.DNLENGTH) AS ARTERYLEN, \r\n"
                     "                              C.LENGTH AS LINKLEN, C.LINKID, A.LINKSEQ                       \r\n"
                     "                         FROM TB_MP_ARTERY_LINK A, TB_MS_ARTERY B, TB_MS_LINK C              \r\n"
                     "                        WHERE A.LINKLEVEL = :l%d                                             \r\n"
                     "                          AND A.ARTERYID  = B.ARTERYID                                       \r\n"
                     "                          AND B.IMPYN     = 'Y'                                              \r\n"
                     "                          AND A.LINKID    = C.LINKID) Y                                      \r\n"
                     "                 WHERE Y.LINKID = X.LINKID                                                   \r\n",
                     pObj->m_sIdx[enStatMon].c_str(),
                     pObj->m_sStatDesc.c_str(),
                     pObj->m_sTbl[enStatMon].c_str(),
                     nBinds,
                     nBinds); 

        sQry3 += sTmp;

        if (ii != (nColCnt-1))
        {
            TObjStat *pObj = (TObjStat *)GetStatObject(CbStatCfg, ii+1);
            if (NULL != pObj)
            {
                sQry3 += "                UNION ALL \r\n";
            }
        }
        nBinds++;
    }


    String sQry4 = "";
    sQry4 = "               )                                        \r\n"
            "         GROUP BY ROLLUP (ARTERYID, DIRECTION, TRFTYPE) \r\n"
            "         HAVING GROUPING(TRFTYPE) = 0) LX,              \r\n"
            "       (SELECT ARTERYID, DIRECTION, DIRECTIONNAME,      \r\n"
            "               ARTERYNAME, ARTERYLENGTH, ARTERYTYPE     \r\n"
            "          FROM VW_MS_ARTERY ) LY                        \r\n"
            " WHERE LY.ARTERYID  = LX.ARTERYID(+)                    \r\n"
            "   AND LY.DIRECTION = LX.DIRECTION(+)                   \r\n"
            " ORDER BY LY.ARTERYNAME, LY.DIRECTION, LX.TRFTYPE       \r\n";

    sQry = sQry1 + sQry2 + sQry3 + sQry4;
    
    try
    {
        ITSDb_SQLText(ADOY, sQry);

        for (ii = 0; ii < nBinds; ii++)
        {
            sTmp = "";
            sTmp.printf(L"f%d", ii);
            ITSDb_SQLBind(ADOY, sTmp, sYYYY);

            sTmp = "";
            sTmp.printf(L"l%d", ii);
            ITSDb_SQLBind(ADOY, sTmp, m_sLinkLevel);
        }

        ITSDb_SQLOpen(ADOY);

        ShowRecordCount(ADOY->RecordCount);
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch (Exception &exception)
    {
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
#endif
}
//---------------------------------------------------------------------------

/*
 * 조회결과 데이터 건수 표출
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::ShowRecordCount(int nRecCnt)
{
    //
}
//---------------------------------------------------------------------------

/*
 * Summary Footer style set
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::TvListDataControllerSummaryFooterSummaryItemsSummary(TcxDataSummaryItems *ASender, TcxSummaryEventArguments &Arguments,
          TcxSummaryEventOutArguments &OutArguments)
{
    CMM_SetSummaryItemKind(Arguments, OutArguments, 2, false, false);
}
//---------------------------------------------------------------------------

/*
 * Summary Footer style set
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TTAS0020M::TvListDataControllerSummaryDefaultGroupSummaryItemsSummary(TcxDataSummaryItems *ASender, TcxSummaryEventArguments &Arguments,
          TcxSummaryEventOutArguments &OutArguments)
{
    CMM_SetSummaryItemKind(Arguments, OutArguments, 1, false, false);
}
//---------------------------------------------------------------------------

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TTAS0020M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TTAS0020M::vDayColumnCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    TColor tColor = ACanvas->Brush->Color;

    try
    {
        if (!VarIsNull(AViewInfo->Value))
        {
            String sSpd = AViewInfo->Value;
            if (sSpd == NULL || sSpd.IsEmpty() || sSpd == "0" || sSpd == "-")
            {
                tColor = clFuchsia;
            }
        }
        else
        {
            tColor = clFuchsia;
        }

        ACanvas->Font->Color = clBlack;
        ACanvas->SetBrushColor(tColor);
    }
    catch(...)
    {
    }
    
}
//---------------------------------------------------------------------------

void __fastcall TTAS0020M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    TAS0020M = NULL;
    Action = caFree;
}
//---------------------------------------------------------------------------

