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

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

#include "TAS00701F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCheckBox"
#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 "cxGridBandedTableView"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TTAS00701 *TAS00701 = NULL;

//---------------------------------------------------------------------------
__fastcall TTAS00701::TTAS00701(TComponent* Owner, HWND hHandle,
                                String AStDay, String AEdDay, String AStHour, String AEdHour, String AWeek, String ADir, String AAtrdNm, int ATable, int AQryType)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FParent = hHandle;

    FStDay = AStDay;
    FEdDay = AEdDay;
    FStHour = AStHour;
    FEdHour = AEdHour;
    FWeek = AWeek;
    FDir = ADir;
    FAtrdNm = AAtrdNm;
    FTable = ATable;
    FQryType = AQryType;

    FHourSt = FStHour.ToIntDef(0);
    FHourEd = FEdHour.ToIntDef(0);

    FStatManager = new TStat0070Manager();
    FChartManager = new TStat0070ChartManager();
}
//---------------------------------------------------------------------------
__fastcall TTAS00701::~TTAS00701(void)
{
}
//--------------------------------------------------------------------------

void __fastcall TTAS00701::FormInit()
{
    ADOQry->Connection = ITSDb_GetConnection();
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<노선별속도분석 정보>";

    LblSearch->Caption = FrmLang->lblQryCond->Caption + FStDay + " ~ " + FEdDay;
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::FormShow(TObject *Sender)
{
    FormInit();
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::CommClose()
{
    try
    {
        delete FStatManager;
        FStatManager = NULL;
        delete FChartManager;
        FChartManager = NULL;
    }
    catch(Exception &e)
    {
    }
    CMM_SaveForm(g_sFormsDir, this);
    //TAS00701 = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false; 

#if 0
    TcxGridBand *pBand = TvList->Bands->Items[1];
    pBand->Visible = false;
    int nCols = pBand->ColumnCount;
    int ii;
    for (ii = 0; ii < FHourSt; ii++)
    {
        pBand->Columns[ii]->Visible = false;
    }

    for (ii = FHourEd+1; ii < nCols; ii++)
    {
        pBand->Columns[ii]->Visible = false;
    }
    pBand->Visible = true;
#endif

    Application->ProcessMessages();

    m_pGDC = TvList->DataController;
    CMM_ClearGridTableView(TvList);
    TvList->BeginUpdate(lsimImmediate);

    try
    {
        if (FQryType == 0)  //시간
        {
            m_pFrmChart->CbOption->Properties->OnChange = NULL;
            m_pFrmChart->CbOption->Properties->Items->Clear();
            m_pFrmChart->CbOption->Properties->Items->Add("시간");
            m_pFrmChart->CbOption->Properties->Items->Add("일자");
            m_pFrmChart->CbOption->ItemIndex = 0;
            m_pFrmChart->PnlOption->Visible = true;

            Col01->Caption = "일자";
            SelHistory1();
            DspHistory1();

            m_pFrmChart->CbOption->Properties->OnChange = CbOptionPropertiesChange;
            DrawChart(0);
        }
        else
        if (FTable == 1)    //요일
        {
            m_pFrmChart->PnlOption->Visible = false;

            Col01->Caption = "요일";
            SelHistory2();
            DspHistory2();
            DrawChart(2);
        }
    }
    __finally
    {
        TvList->EndUpdate();

        LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + FrmLang->lblEA->Caption;//" 건";

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

void __fastcall TTAS00701::ChkExpandClick(TObject *Sender)
{
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

String __fastcall TTAS00701::GetDayWeekCode(String ADay)
{
    TDateTime dt;

    System::Word Year  = (System::Word)ADay.SubString( 1, 4).ToIntDef(2018);
    System::Word Month = (System::Word)ADay.SubString( 5, 2).ToIntDef(1);
    System::Word Day   = (System::Word)ADay.SubString( 7, 2).ToIntDef(1);
    dt = EncodeDate(Year, Month, Day);

    System::Word week = DayOfWeek(dt);
    //const days: array[1..7] of string = ('일','월','화','수','목','금','토');
    String sWeek[8] = { "", "DTW7", "DTW1", "DTW2", "DTW3", "DTW4", "DTW5", "DTW6" };

    return sWeek[week];
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::SelHistory1()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

    String SQLAtrd = "";
    String SQLDrct = "";
    String SQLWeek = "";

    if (FAtrdNm != "")
    {
        SQLAtrd = "                              AND ATRD_NM IN (" + FAtrdNm + ")  \r\n";
    }
    if (FDir != "")
    {
        SQLDrct = "                              AND DRCT_CD IN (" + FDir + ")  \r\n";
    }
    if (FWeek != "")
    {
        SQLWeek = "           AND A.DAY_TYPE_CD IN (" + FWeek + ") \r\n";
    }

    sQry = "SELECT X.STAT_DAY AS STAT_TP,                               \r\n"
           "       Y.ATRD_NM, Y.DRCT_CD_NM, Y.DRCT_NM,                  \r\n"
           "       X.*                                                  \r\n"
           "  FROM (SELECT /*+ INDEX(A PK_TAS_IFSC_SPED_HH) */          \r\n"
           "               A.STAT_DAY, B.ATRD_ID,                       \r\n"
           "               ROUND(AVG(H00), 0) AS H00,                   \r\n"
           "               ROUND(AVG(H01), 0) AS H01,                   \r\n"
           "               ROUND(AVG(H02), 0) AS H02,                   \r\n"
           "               ROUND(AVG(H03), 0) AS H03,                   \r\n"
           "               ROUND(AVG(H04), 0) AS H04,                   \r\n"
           "               ROUND(AVG(H05), 0) AS H05,                   \r\n"
           "               ROUND(AVG(H06), 0) AS H06,                   \r\n"
           "               ROUND(AVG(H07), 0) AS H07,                   \r\n"
           "               ROUND(AVG(H08), 0) AS H08,                   \r\n"
           "               ROUND(AVG(H09), 0) AS H09,                   \r\n"
           "               ROUND(AVG(H10), 0) AS H10,                   \r\n"
           "               ROUND(AVG(H11), 0) AS H11,                   \r\n"
           "               ROUND(AVG(H12), 0) AS H12,                   \r\n"
           "               ROUND(AVG(H13), 0) AS H13,                   \r\n"
           "               ROUND(AVG(H14), 0) AS H14,                   \r\n"
           "               ROUND(AVG(H15), 0) AS H15,                   \r\n"
           "               ROUND(AVG(H16), 0) AS H16,                   \r\n"
           "               ROUND(AVG(H17), 0) AS H17,                   \r\n"
           "               ROUND(AVG(H18), 0) AS H18,                   \r\n"
           "               ROUND(AVG(H19), 0) AS H19,                   \r\n"
           "               ROUND(AVG(H20), 0) AS H20,                   \r\n"
           "               ROUND(AVG(H21), 0) AS H21,                   \r\n"
           "               ROUND(AVG(H22), 0) AS H22,                   \r\n"
           "               ROUND(AVG(H23), 0) AS H23,                   \r\n"
           "               ROUND(AVG(SPD), 0) AS SPD                    \r\n"
           "          FROM TB_TAS_IFSC_SPED_HH A,                       \r\n"
           "               MV_ATRD_IFSC        B                        \r\n"
           "         WHERE A.STAT_DAY BETWEEN :p01 AND :p02             \r\n"
           "           AND A.IFSC_ID = B.IFSC_ID                        \r\n"
           + SQLAtrd + SQLDrct + SQLWeek +
           "         GROUP BY A.STAT_DAY, B.ATRD_ID, B.DRCT_CD          \r\n"
           "       ) X,                                                 \r\n"
           "       MV_ATRD Y                                            \r\n"
           " WHERE 1=1                                                  \r\n"
           "   AND Y.ATRD_ID = X.ATRD_ID                                \r\n"
           + SQLAtrd + SQLDrct +
           " ORDER BY X.STAT_DAY, Y.ATRD_NM                             \r\n";


    int nRow = 0;
    int nIdxSped, ii;
    String colName;

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDay);
        ITSDb_SQLBind(pADO, "p02", FEdDay);

        ITSDb_SQLOpen(pADO);

        for( ; !pADO->Eof; pADO->Next(), nRow++)
        {
            String STAT_TP = pADO->FieldByName("STAT_TP")->AsString;
            TStat0070 *pStat = FStatManager->FLists.Find(STAT_TP);
            if (!pStat)
            {
                pStat = new TStat0070();
                pStat->STAT_TP = STAT_TP;
                pStat->STAT_TP_DESC = ITSUtil_FormatStr(pStat->STAT_TP, STR_DATE);
                FStatManager->FLists.Push(pStat->STAT_TP, pStat);
            }
            if (!pStat) continue;   //ERROR

            String ATRD_ID = pADO->FieldByName("ATRD_ID")->AsString;
            TTraf0070 *pTraf = pStat->FLists.Find(ATRD_ID);
            if (!pTraf)
            {
                pTraf = new TTraf0070();
                pTraf->ATRD_ID = ATRD_ID;
                pTraf->ATRD_NM    = pADO->FieldByName("ATRD_NM")->AsString;
                pTraf->DRCT_CD_NM = pADO->FieldByName("DRCT_CD_NM")->AsString;
                pTraf->DRCT_NM    = pADO->FieldByName("DRCT_NM")->AsString;
                pStat->FLists.Push(pTraf->ATRD_ID, pTraf);
            }
            if (!pTraf) continue;   //ERROR

            TChart0070 *pChart = FChartManager->FLists.Find(ATRD_ID);
            if (!pChart)
            {
                pChart = new TChart0070();
                pChart->ATRD_ID = ATRD_ID;
                pChart->ATRD_NM    = pADO->FieldByName("ATRD_NM")->AsString;
                pChart->DRCT_CD_NM = pADO->FieldByName("DRCT_CD_NM")->AsString;
                pChart->DRCT_NM    = pADO->FieldByName("DRCT_NM")->AsString;
                FChartManager->FLists.Push(pChart->ATRD_ID, pChart);
            }
            if (!pChart) continue;   //ERROR

            for (ii = FHourSt; ii <= FHourEd; ii++)
            {
                colName.printf(L"h%02d", ii);
                pTraf->SPED[ii] = pADO->FieldByName(colName)->AsInteger;
                if (pTraf->SPED[ii] > 0)
                {
                    pTraf->SUM_SPED += pTraf->SPED[ii];
                    pTraf->DATA_CNT++;
                }
            }
        }

        pADO->Close();
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::SelHistory2()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

    String SQLAtrd = "";
    String SQLDrct = "";
    String SQLWeek = "";

    if (FAtrdNm != "")
    {
        SQLAtrd = "                              AND ATRD_NM IN (" + FAtrdNm + ")  \r\n";
    }
    if (FDir != "")
    {
        SQLDrct = "                              AND DRCT_CD IN (" + FDir + ")  \r\n";
    }
    if (FWeek != "")
    {
        SQLWeek = "           AND A.DAY_TYPE_CD IN (" + FWeek + ") \r\n";
    }

    sQry = "SELECT X.DAY_TYPE_CD AS STAT_TP,                            \r\n"
           "       Y.ATRD_NM, Y.DRCT_CD_NM, Y.DRCT_NM,                  \r\n"
           "       X.*                                                  \r\n"
           "  FROM (SELECT /*+ INDEX(A PK_TAS_IFSC_SPED_HH) */          \r\n"
           "               A.DAY_TYPE_CD, B.ATRD_ID,                    \r\n"
           "               ROUND(AVG(H00), 0) AS H00,                   \r\n"
           "               ROUND(AVG(H01), 0) AS H01,                   \r\n"
           "               ROUND(AVG(H02), 0) AS H02,                   \r\n"
           "               ROUND(AVG(H03), 0) AS H03,                   \r\n"
           "               ROUND(AVG(H04), 0) AS H04,                   \r\n"
           "               ROUND(AVG(H05), 0) AS H05,                   \r\n"
           "               ROUND(AVG(H06), 0) AS H06,                   \r\n"
           "               ROUND(AVG(H07), 0) AS H07,                   \r\n"
           "               ROUND(AVG(H08), 0) AS H08,                   \r\n"
           "               ROUND(AVG(H09), 0) AS H09,                   \r\n"
           "               ROUND(AVG(H10), 0) AS H10,                   \r\n"
           "               ROUND(AVG(H11), 0) AS H11,                   \r\n"
           "               ROUND(AVG(H12), 0) AS H12,                   \r\n"
           "               ROUND(AVG(H13), 0) AS H13,                   \r\n"
           "               ROUND(AVG(H14), 0) AS H14,                   \r\n"
           "               ROUND(AVG(H15), 0) AS H15,                   \r\n"
           "               ROUND(AVG(H16), 0) AS H16,                   \r\n"
           "               ROUND(AVG(H17), 0) AS H17,                   \r\n"
           "               ROUND(AVG(H18), 0) AS H18,                   \r\n"
           "               ROUND(AVG(H19), 0) AS H19,                   \r\n"
           "               ROUND(AVG(H20), 0) AS H20,                   \r\n"
           "               ROUND(AVG(H21), 0) AS H21,                   \r\n"
           "               ROUND(AVG(H22), 0) AS H22,                   \r\n"
           "               ROUND(AVG(H23), 0) AS H23,                   \r\n"
           "               ROUND(AVG(SPD), 0) AS SPD                    \r\n"
           "          FROM TB_TAS_IFSC_SPED_HH A,                       \r\n"
           "               MV_ATRD_IFSC        B                        \r\n"
           "         WHERE A.STAT_DAY BETWEEN :p01 AND :p02             \r\n"
           "           AND A.IFSC_ID = B.IFSC_ID                        \r\n"
           + SQLAtrd + SQLDrct + SQLWeek +
           "         GROUP BY A.DAY_TYPE_CD, B.ATRD_ID, B.DRCT_CD       \r\n"
           "       ) X,                                                 \r\n"
           "       MV_ATRD Y                                            \r\n"
           " WHERE 1=1                                                  \r\n"
           "   AND Y.ATRD_ID = X.ATRD_ID                                \r\n"
           + SQLAtrd + SQLDrct +
           " ORDER BY X.DAY_TYPE_CD, Y.ATRD_NM                          \r\n";


    int nRow = 0;
    int nIdxSped, ii;
    String colName;

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDay);
        ITSDb_SQLBind(pADO, "p02", FEdDay);

        ITSDb_SQLOpen(pADO);

        for( ; !pADO->Eof; pADO->Next(), nRow++)
        {
            String STAT_TP = pADO->FieldByName("STAT_TP")->AsString;
            TStat0070 *pStat = FStatManager->FLists.Find(STAT_TP);
            if (!pStat)
            {
                pStat = new TStat0070();
                pStat->STAT_TP = STAT_TP;
                if      (pStat->STAT_TP == "DTW1") pStat->STAT_TP_DESC = "월";
                else if (pStat->STAT_TP == "DTW2") pStat->STAT_TP_DESC = "화";
                else if (pStat->STAT_TP == "DTW3") pStat->STAT_TP_DESC = "수";
                else if (pStat->STAT_TP == "DTW4") pStat->STAT_TP_DESC = "목";
                else if (pStat->STAT_TP == "DTW5") pStat->STAT_TP_DESC = "금";
                else if (pStat->STAT_TP == "DTW6") pStat->STAT_TP_DESC = "토";
                else if (pStat->STAT_TP == "DTW7") pStat->STAT_TP_DESC = "일";
                FStatManager->FLists.Push(pStat->STAT_TP, pStat);
            }
            if (!pStat) continue;   //ERROR

            String ATRD_ID = pADO->FieldByName("ATRD_ID")->AsString;
            TTraf0070 *pTraf = pStat->FLists.Find(ATRD_ID);
            if (!pTraf)
            {
                pTraf = new TTraf0070();
                pTraf->ATRD_ID = ATRD_ID;
                pTraf->ATRD_NM    = pADO->FieldByName("ATRD_NM")->AsString;
                pTraf->DRCT_CD_NM = pADO->FieldByName("DRCT_CD_NM")->AsString;
                pTraf->DRCT_NM    = pADO->FieldByName("DRCT_NM")->AsString;
                pStat->FLists.Push(pTraf->ATRD_ID, pTraf);
            }
            if (!pTraf) continue;   //ERROR

            TChart0070 *pChart = FChartManager->FLists.Find(ATRD_ID);
            if (!pChart)
            {
                pChart = new TChart0070();
                pChart->ATRD_ID = ATRD_ID;
                pChart->ATRD_NM    = pADO->FieldByName("ATRD_NM")->AsString;
                pChart->DRCT_CD_NM = pADO->FieldByName("DRCT_CD_NM")->AsString;
                pChart->DRCT_NM    = pADO->FieldByName("DRCT_NM")->AsString;
                FChartManager->FLists.Push(pChart->ATRD_ID, pChart);
            }
            if (!pChart) continue;   //ERROR

            for (ii = FHourSt; ii <= FHourEd; ii++)
            {
                colName.printf(L"h%02d", ii);
                pTraf->SPED[ii] = pADO->FieldByName(colName)->AsInteger;
                if (pTraf->SPED[ii] > 0)
                {
                    pTraf->SUM_SPED += pTraf->SPED[ii];
                    pTraf->DATA_CNT++;
                }
            }
        }

        pADO->Close();
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::DspHistory1()
{
    m_pGDC = TvList->DataController;

    try
    {
        int nRow = 0;
        int nIdxSped = 0;
        int ii;

        float fTotSumSped = 0;
        int   nTotSumCnt  = 0;

        //일자별/요일별 순환
        FOR_STL(TStat0070*, pStat, FStatManager->FLists)
        {
            float fSumSped = 0;
            int   nSumCnt  = 0;

            //간선도로별 순환
            FOR_STL(TTraf0070*, pTraf, pStat->FLists)
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Col01->Index] = pStat->STAT_TP_DESC;
                m_pGDC->Values[nRow][Col02->Index] = pTraf->ATRD_NM;
                m_pGDC->Values[nRow][Col03->Index] = pTraf->DRCT_CD_NM;
                if (pTraf->DATA_CNT > 0)
                {
                    pTraf->AVG_SPED = (int)(pTraf->SUM_SPED/pTraf->DATA_CNT+0.5);
                    m_pGDC->Values[nRow][Col04->Index] = pTraf->AVG_SPED;

                    fSumSped += pTraf->AVG_SPED;
                    nSumCnt++;
                }
                else
                {
                    m_pGDC->Values[nRow][Col04->Index] = 0;
                }

                nIdxSped = ColSped->Index;
                for (ii = FHourSt; ii <= FHourEd; ii++)
                {
                    m_pGDC->Values[nRow][nIdxSped+ii] = pTraf->SPED[ii];
                    if (pTraf->SPED[ii] > 0)
                    {
                        pStat->SUM_SPED[ii] += pTraf->SPED[ii];
                        pStat->DATA_CNT[ii]++;

                        TChart0070 *pChart = FChartManager->FLists.Find(pTraf->ATRD_ID);
                        if (pChart)
                        {
                            pChart->SUM_SPED[ii] += pTraf->SPED[ii];
                            pChart->DATA_CNT[ii]++;
                        }
                    }
                }
            }//간선도로별 표출완료

            //일자별/요일별 평균계산 및 표출
            for (ii = FHourSt; ii <= FHourEd; ii++)
            {
                if (pStat->DATA_CNT[ii] > 0)
                {
                    pStat->AVG_SPED[ii] = (int)(pStat->SUM_SPED[ii]/pStat->DATA_CNT[ii]+0.5);

                    FStatManager->SUM_SPED[ii] = pStat->AVG_SPED[ii];
                    FStatManager->DATA_CNT[ii]++;
                }
            }

            nRow = m_pGDC->AppendRecord();

            m_pGDC->Values[nRow][Col01->Index] = pStat->STAT_TP_DESC;
            m_pGDC->Values[nRow][Col02->Index] = "평균통행속도";
            m_pGDC->Values[nRow][Col03->Index] = "-";
            if (nSumCnt > 0)
            {
                pStat->TOT_AVG_SPED = (int)(fSumSped/nSumCnt+0.5);
                m_pGDC->Values[nRow][Col04->Index] = pStat->TOT_AVG_SPED;

                fTotSumSped += pStat->TOT_AVG_SPED;
                nTotSumCnt++;
            }
            else
            {
                m_pGDC->Values[nRow][Col04->Index] = 0;
            }

            nIdxSped = ColSped->Index;
            for (ii = FHourSt; ii <= FHourEd; ii++)
            {
                m_pGDC->Values[nRow][nIdxSped+ii] = pStat->AVG_SPED[ii];
            }
        }

        //전체평균표출
        for (ii = FHourSt; ii <= FHourEd; ii++)
        {
            if (FStatManager->DATA_CNT[ii] > 0)
            {
                FStatManager->AVG_SPED[ii] = (int)(FStatManager->SUM_SPED[ii]/FStatManager->DATA_CNT[ii]+0.5);
            }
        }

        nRow = m_pGDC->AppendRecord();

        m_pGDC->Values[nRow][Col01->Index] = "전체";
        m_pGDC->Values[nRow][Col02->Index] = "평균통행속도";
        m_pGDC->Values[nRow][Col03->Index] = "-";
        if (nTotSumCnt > 0)
        {
            FStatManager->TOT_AVG_SPED = (int)(fTotSumSped/nTotSumCnt+0.5);
            m_pGDC->Values[nRow][Col04->Index] = FStatManager->TOT_AVG_SPED;
        }
        else
        {
            m_pGDC->Values[nRow][Col04->Index] = 0;
        }

        nIdxSped = ColSped->Index;
        for (ii = FHourSt; ii <= FHourEd; ii++)
        {
            m_pGDC->Values[nRow][nIdxSped+ii] = FStatManager->AVG_SPED[ii];
        }
        FOR_STL(TChart0070*, pChart, FChartManager->FLists)
        {
            for (ii = FHourSt; ii <= FHourEd; ii++)
            {
                if (pChart->DATA_CNT[ii] > 0)
                {
                    pChart->AVG_SPED[ii] = (int)(pChart->SUM_SPED[ii]/pChart->DATA_CNT[ii]+0.5);
                }
            }
        }
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &e)
    {
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::DspHistory2()
{
    DspHistory1();
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::DrawChart(int ADrawType)
{
    m_pFrmChart->Clear();

    switch(ADrawType)
    {
    case 0: m_pFrmChart->SetChartInfo("간선도로별 분석(시간)", "시간", "속도"); break;
    case 1: m_pFrmChart->SetChartInfo("간선도로별 분석(일자)", "일자", "속도"); break;
    case 2: m_pFrmChart->SetChartInfo("간선도로별 분석(요일)", "요일", "속도"); break;
    default: return;
    }

    int             ii, jj, kk;
    SeriesType      SrsType;
    TColor          clSrsColor;
    TChartSeries    *pSeries;

    TCursor cOldCursor= Screen->Cursor;
    Screen->Cursor = crHourGlass;

    SrsType = (SeriesType)m_pFrmChart->CbChartKind->ItemIndex;
    m_pFrmChart->BeginDraw("", "", "");
    m_pFrmChart->LstLegend->Items->BeginUpdate();

    ii = jj = kk = 0;
    try
    {
        switch(ADrawType)
        {
        case 0: //시간별
            FOR_STL(TChart0070*, pChart, FChartManager->FLists)
            {
                clSrsColor = GetDefaultColor(kk++);
                if (clSrsColor == clWhite) clSrsColor = GetDefaultColor(++kk);
                if (clSrsColor == clYellow) clSrsColor = GetDefaultColor(++kk);

                pSeries = m_pFrmChart->AddSeriesItem(pChart->ATRD_NM+":"+pChart->DRCT_CD_NM, SrsType, clSrsColor, true);
                if (!pSeries) continue;

                for (ii = FHourSt; ii <= FHourEd; ii++)
                {
                    String sAxisX;
                    sAxisX.printf(L"%02d시", ii);
                    if (pChart->AVG_SPED[ii] > 0)
                    {
                        pSeries->AddY(pChart->AVG_SPED[ii], sAxisX);
                    }
                }
            }
            break;
        case 1: //일자별
        case 2: //요일별
            FOR_STL(TChart0070*, pChart, FChartManager->FLists)
            {
                clSrsColor = GetDefaultColor(kk++);
                if (clSrsColor == clWhite) clSrsColor = GetDefaultColor(++kk);
                if (clSrsColor == clYellow) clSrsColor = GetDefaultColor(++kk);

                pSeries = m_pFrmChart->AddSeriesItem(pChart->ATRD_NM+":"+pChart->DRCT_CD_NM, SrsType, clSrsColor, true);
                if (!pSeries) continue;

                FOR_STL(TStat0070*, pStat, FStatManager->FLists)
                {
                    TTraf0070 *pTraf = pStat->FLists.Find(pChart->ATRD_ID);
                    if (!pTraf) continue;

                    String sAxisX = pStat->STAT_TP_DESC;
                    if (pTraf->AVG_SPED > 0)
                    {
                        pSeries->AddY(pTraf->AVG_SPED, sAxisX);
                    }
                }
            }
            break;
        }

        m_pFrmChart->DbChart->LeftAxis->Minimum = 0;
        m_pFrmChart->DbChart->LeftAxis->Maximum = m_pFrmChart->GetActiveMaxY() + 10;
        if (m_pFrmChart->DbChart->LeftAxis->Minimum >= m_pFrmChart->DbChart->LeftAxis->Maximum)
        {
            m_pFrmChart->DbChart->LeftAxis->Maximum = m_pFrmChart->DbChart->LeftAxis->Minimum + 1;
        }
    }
    __finally
    {
        m_pFrmChart->DbChart->Refresh();
        m_pFrmChart->LstLegend->Items->EndUpdate();
        m_pFrmChart->EndDraw();
        Screen->Cursor = cOldCursor;
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::OnMessage(TMessage &Msg)
{
    switch (Msg.Msg)
    {
    case WM_PARAM_DATABASE:
        if (WP_DB_SELECT_OK == Msg.WParam)
        {
            //ShowMessage("Select Ok");
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"노선별속도분석";

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

void __fastcall TTAS00701::FormClose(TObject *Sender, TCloseAction &Action)
{
    POST_MSG(FParent, WM_SUBFORM_CLOSE, 0, 0);
    CommClose();
    //TAS00701 = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::FormDestroy(TObject *Sender)
{
    CommClose();
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::FormCreate(TObject *Sender)
{
    m_pFrmChart = new TPlugInChart(this);
    m_pFrmChart->Parent = PnlChart;
    m_pFrmChart->SetChartInfo("", "시간", "속도");
    m_pFrmChart->Show();
}
//---------------------------------------------------------------------------

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

    try
    {
        if (!VarIsNull(AViewInfo->Value))
        {
            int nSpd = AViewInfo->Value;
            if (nSpd == 0)
            {
                tFont = tColor;
                tColor = tFont;
            }
#if 0
            else
            {
                if (nSpd <= 10)
                {
                    tFont = clWhite;
                    tColor = (TColor)0x333333;
                }
                else
                if (nSpd <= 20)
                {
                    tFont = tFont;
                    tColor = (TColor)0x575757;
                }
                else
                if (nSpd <= 30)
                {
                    tFont = tFont;
                    tColor = (TColor)0x7B7B7B;
                }
                else
                if (nSpd <= 40)
                {
                    tFont = tFont;
                    tColor = (TColor)0x9F9F9F;
                }
                else
                if (nSpd <= 50)
                {
                    tFont = clBlue;
                    tColor = (TColor)0xC3C3C3;
                }
                else
                if (nSpd <= 60)
                {
                    tFont = clBlue;
                    tColor = (TColor)0xE7E7E7;
                }
                else
                {
                    tFont = clBlue;
                    tColor = (TColor)0xF3F3F3;
                }
            }
#endif
        }
        else
        {
            tColor = clSilver;
            tFont = tColor;
        }

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

void __fastcall TTAS00701::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Col01->Index];
        if (sType.Pos("전체"))
        {
            ACanvas->Brush->Color = clLime;
        }
        else
        {
            String sDesc = AViewInfo->GridRecord->DisplayTexts[Col02->Index];
            if (sDesc.Pos("평균"))
            {
                ACanvas->Brush->Color = clOlive;
            }
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::Col01CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Col02->Index];
        if (sType.Pos("전체"))
        {
            ACanvas->Brush->Color = clLime;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TTAS00701::CbOptionPropertiesChange(TObject *Sender)
{
    TcxComboBox *pCombo = (TcxComboBox*)Sender;
    DrawChart(pCombo->ItemIndex);
    m_pFrmChart->SetFocus();
}
//---------------------------------------------------------------------------

