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

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

#include "IST00501F.h"

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

TIST00501 *IST00501 = NULL;

//---------------------------------------------------------------------------
__fastcall TIST00501::TIST00501(TComponent* Owner, HWND hHandle, String sFacType, String sFrom, String sTo, String sIdList, String sTypeNm)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FParent     = hHandle;
    FFacType    = sFacType;
    FStDateTime = sFrom;
    FEdDateTime = sTo;
    FIdList     = sIdList;
    FTypeNm     = sTypeNm;

    //Caption = "시설물 통신단절통계";
}
//---------------------------------------------------------------------------
__fastcall TIST00501::~TIST00501(void)
{
}
//--------------------------------------------------------------------------

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

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<시설물 통신단절통계 정보>";

    FRAMEChart1->FTitle = Caption;//"시설물 통신단절통계";
    FRAMEChart1->SetChartLegend(lblText1->Caption, lblText2->Caption);//"시설물", "장애수");
    FRAMEChart1->ChartValueDisplay(FRAMEChart1->ChkDisplayVal->Checked);
    FRAMEChart1->TvChart->ToolBox->CustomizeButton = true;
    FRAMEChart1->TvChart->Legend->Position = cppDefault;
    FRAMEChart1->TvChart->DiagramStackedBar->Enabled = true;
    FRAMEChart1->TvChart->DiagramStackedColumn->Enabled = true;
    //FRAMEChart1->TvChart->DiagramStackedColumn->Active = true;
    FRAMEChart1->TvChart->DiagramColumn->Active = true;

    LblSearch->Caption = FrmLang->lblQryCond->Caption + FFacType + " (" + FStDateTime.SubString(1, 12) + " ~ " + FEdDateTime.SubString(1, 12) + ")";
}
//---------------------------------------------------------------------------

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

void __fastcall TIST00501::CommClose()
{
    CMM_SaveForm(g_sFormsDir, this);
    //IST00501 = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TIST00501::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    Application->ProcessMessages();
    SelHistory();
}
//---------------------------------------------------------------------------

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

void __fastcall TIST00501::SelHistory()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

//'20140713000000' AND '20140713235959'
//    int nLevel = StrToInt(FIdList);
    int nLevel = 0;
    if      (FTypeNm == "CCTV")     nLevel = 1;
    else if (FTypeNm == "VMS")      nLevel = 2;
    else if (FTypeNm == "VDS")      nLevel = 3;
    else if (FTypeNm == "AVI")      nLevel = 4;
    else if (FTypeNm == "DSRC")     nLevel = 5;
    else if (FTypeNm == FrmLang->lblWebCam->Caption) nLevel = 6;
    else if (FTypeNm == FrmLang->lblCrsCam->Caption) nLevel = 7;
    else if (FTypeNm == "주차장")   nLevel = 8;
    else                            nLevel = 0; //전 체

    switch(nLevel)
    {
    case 1: //CCTV
    sQry = "SELECT A.CCTV_CTLR_NMBR AS FCLT_ID, A.CCTV_CTLR_ID AS CTLR_ID,          \r\n"
           "       A.ISTL_LCTN_NM AS FCLT_NM,                                       \r\n"
           "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2   \r\n"
           "  FROM TB_CCTV_CTLR A,                                                  \r\n"
           "       (SELECT CCTV_CTLR_NMBR,                                          \r\n"
           "               SUM(DECODE(CONN_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,     \r\n"
           "               SUM(DECODE(CONN_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2      \r\n"
           "          FROM TB_CCTV_STTS_HS                                          \r\n"
           "         WHERE CRTN_DT BETWEEN :p01 AND :p02                            \r\n"
           "           AND CONN_STTS_CD IN ('CMS1', 'CMS2')                         \r\n"
           "         GROUP BY CCTV_CTLR_NMBR) B                                     \r\n"
           " WHERE A.CCTV_CTLR_NMBR = B.CCTV_CTLR_NMBR(+)                           \r\n";
           //" ORDER BY TO_NUMBER(A.CCTV_CTLR_NMBR)                                   \r\n";
        break;
    case 2: //VMS
    sQry = "SELECT A.VMS_CTLR_NMBR AS FCLT_ID, A.VMS_CTLR_ID AS CTLR_ID,            \r\n"
           "       A.VMS_NM AS FCLT_NM,                                             \r\n"
           "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2   \r\n"
           "  FROM TB_VMS_CTLR A,                                                   \r\n"
           "       (SELECT VMS_CTLR_NMBR,                                           \r\n"
           "               SUM(DECODE(CMNC_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,     \r\n"
           "               SUM(DECODE(CMNC_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2      \r\n"
           "          FROM TB_VMS_CTLR_STTS_HS                                      \r\n"
           "         WHERE CRTN_DT BETWEEN :p01 AND :p02                            \r\n"
           "           AND CMNC_STTS_CD IN ('CMS1', 'CMS2')                         \r\n"
           "         GROUP BY VMS_CTLR_NMBR) B                                      \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR(+)                             \r\n";
        break;
    case 3: //VDS
      sQry = "SELECT A.VDS_CTLR_NMBR AS FCLT_ID, A.VDS_CTLR_ID AS CTLR_ID,          \r\n"
             "       A.VDS_NM AS FCLT_NM,                                           \r\n"
             "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2 \r\n"
             "  FROM TB_VDS_CTLR A,                                                 \r\n"
             "       (SELECT VDS_CTLR_NMBR,                                         \r\n"
             "               SUM(DECODE(CMNC_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,   \r\n"
             "               SUM(DECODE(CMNC_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2    \r\n"
             "          FROM TB_VDS_CTLR_STTS_HS                                    \r\n"
             "         WHERE CRTN_DT BETWEEN :p01 AND :p02                          \r\n"
             "           AND CMNC_STTS_CD IN ('CMS1', 'CMS2')                       \r\n"
             "         GROUP BY VDS_CTLR_NMBR) B                                    \r\n"
             " WHERE A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR(+)                           \r\n";
         break;
    case 4: //AVI
      sQry = "SELECT A.AVI_CTLR_MNGM_NMBR AS FCLT_ID, A.AVI_ID AS CTLR_ID,          \r\n"
             "       A.AVI_LCTN_NM AS FCLT_NM,                                      \r\n"
             "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2 \r\n"
             "  FROM TB_AVI_CTLR A,                                                 \r\n"
             "       (SELECT AVI_CTLR_MNGM_NMBR,                                    \r\n"
             "               SUM(DECODE(CONN_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,   \r\n"
             "               SUM(DECODE(CONN_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2    \r\n"
             "          FROM TB_AVI_CTLR_STTS_HS                                    \r\n"
             "         WHERE CRTN_DT BETWEEN :p01 AND :p02                          \r\n"
             "           AND CONN_STTS_CD IN ('CMS1', 'CMS2')                       \r\n"
             "         GROUP BY AVI_CTLR_MNGM_NMBR) B                               \r\n"
             " WHERE A.AVI_CTLR_MNGM_NMBR = B.AVI_CTLR_MNGM_NMBR(+)                 \r\n";
         break;
    case 5: //DSRC
      sQry = "SELECT A.ID AS FCLT_ID, A.RSE_ID AS CTLR_ID,                          \r\n"
             "       A.ISTL_LCTN_NM AS FCLT_NM,                                     \r\n"
             "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2 \r\n"
             "  FROM TB_RSE_MSTR A,                                                 \r\n"
             "       (SELECT ID,                                                    \r\n"
             "               SUM(DECODE(CMNC_STTS, 'CMS1', 1, 0)) AS CNT_CMS1,      \r\n"
             "               SUM(DECODE(CMNC_STTS, 'CMS2', 1, 0)) AS CNT_CMS2       \r\n"
             "          FROM TB_RSE_STTS_HS                                         \r\n"
             "         WHERE CLCT_DT BETWEEN :p01 AND :p02                          \r\n"
             "           AND CMNC_STTS IN ('1', '2')                                \r\n"
             "         GROUP BY ID) B                                               \r\n"
             " WHERE A.ID = B.ID(+)                                                 \r\n";
         break;
    case 6: //웹카메라
    sQry = "SELECT A.WCAM_CTLR_NMBR AS FCLT_ID, A.WCAM_CTLR_ID AS CTLR_ID,          \r\n"
           "       A.WCAM_NM AS FCLT_NM,                                            \r\n"
           "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2   \r\n"
           "  FROM TB_WCAM_CTLR A,                                                  \r\n"
           "       (SELECT WCAM_CTLR_NMBR,                                          \r\n"
           "               SUM(DECODE(CMNC_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,     \r\n"
           "               SUM(DECODE(CMNC_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2      \r\n"
           "          FROM TB_WCAM_CTLR_STTS_HS                                     \r\n"
           "         WHERE CRTN_DT BETWEEN :p01 AND :p02                            \r\n"
           "           AND CMNC_STTS_CD IN ('CMS1', 'CMS2')                         \r\n"
           "         GROUP BY WCAM_CTLR_NMBR) B                                     \r\n"
           " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR(+)                           \r\n";
        break;
    case 7: //교차로감시 카메라
      sQry = "SELECT A.CCAM_CTLR_NMBR AS FCLT_ID, A.CCAM_CTLR_ID AS CTLR_ID,        \r\n"
             "       A.CCAM_NM AS FCLT_NM,                                          \r\n"
             "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2 \r\n"
             "  FROM TB_CCAM_CTLR A,                                                \r\n"
             "       (SELECT CCAM_CTLR_NMBR,                                        \r\n"
             "               SUM(DECODE(CMNC_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,   \r\n"
             "               SUM(DECODE(CMNC_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2    \r\n"
             "          FROM TB_CCAM_CTLR_STTS_HS                                   \r\n"
             "         WHERE CRTN_DT BETWEEN :p01 AND :p02                          \r\n"
             "           AND CMNC_STTS_CD IN ('CMS1', 'CMS2')                       \r\n"
             "         GROUP BY CCAM_CTLR_NMBR) B                                   \r\n"
             " WHERE A.CCAM_CTLR_NMBR = B.CCAM_CTLR_NMBR(+)                         \r\n";
         break;
    case 8: //주차장
      sQry = "SELECT A.PRLT_CTLR_NMBR AS FCLT_ID, A.PRLT_CTLR_ID AS CTLR_ID,        \r\n"
             "       A.PRLT_NM AS FCLT_NM,                                          \r\n"
             "       NVL(B.CNT_CMS1, 0) AS CNT_CMS1, NVL(B.CNT_CMS2, 0) AS CNT_CMS2 \r\n"
             "  FROM TB_PRLT_CTLR A,                                                \r\n"
             "       (SELECT PRLT_CTLR_NMBR,                                        \r\n"
             "               SUM(DECODE(CMNC_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1,   \r\n"
             "               SUM(DECODE(CMNC_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2    \r\n"
             "          FROM TB_PRLT_CTLR_STTS_HS                                   \r\n"
             "         WHERE CRTN_DT BETWEEN :p01 AND :p02                          \r\n"
             "           AND CMNC_STTS_CD IN ('CMS1', 'CMS2')                       \r\n"
             "         GROUP BY PRLT_CTLR_NMBR) B                                   \r\n"
             " WHERE A.REAL_DATA_YN  = 'Y'                                          \r\n"
             "   AND A.PRLT_CTLR_NMBR = B.PRLT_CTLR_NMBR(+)                         \r\n";
         break;
    default: return;
    }

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        //ITSDb_SQLBind(pADO, "p03", FStDateTime);
        //ITSDb_SQLBind(pADO, "p04", FEdDateTime);
        ITSDb_SQLOpen(pADO);

        DspHistory();
    }
    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 TIST00501::DspHistory()
{
    TADOQuery *pADO = ADOQry;
    TcxGridChartView *TvChart = FRAMEChart1->TvChart;

    try
    {
    	TcxGridChartSeries *pChart1 = NULL;
    	TcxGridChartSeries *pChart2 = NULL;
    	TcxGridChartSeries *pChart3 = NULL;

        TvChart->ClearSeries();
        FRAMEChart1->ChartValueDisplay(FRAMEChart1->ChkDisplayVal->Checked);

        CMM_ClearGridTableView(TvList);

        int nRow = 0;
        int nDataCnt = 0;
        try {
            TvList->BeginUpdate();
            TvChart->BeginUpdate();

            nDataCnt = pADO->RecordCount;
            TvChart->DataController->RecordCount = nDataCnt;
            m_pGDC->RecordCount = nDataCnt;

			pChart1 = TvChart->CreateSeries();
			pChart1->DataBinding->ValueType = "Integer";
			pChart1->DisplayText = lblText3->Caption;//"합계";

			pChart2 = TvChart->CreateSeries();
			pChart2->DataBinding->ValueType = "Integer";
			pChart2->DisplayText = lblText2->Caption;//"장애수";

			pChart3 = TvChart->CreateSeries();
			pChart3->DataBinding->ValueType = "Integer";
			pChart3->DisplayText = FrmLang->lblUnknown->Caption;//"알수없음";

			int nCMS1;
			int nCMS2;
			int nCMS_Total;
            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("FCLT_ID")->AsString;
                m_pGDC->Values[nRow][Column06->Index] = pADO->FieldByName("CTLR_ID")->AsString;

                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("FCLT_NM")->AsString;
			    nCMS1                                 = pADO->FieldByName("CNT_CMS1")->AsInteger;
			    nCMS2                                 = pADO->FieldByName("CNT_CMS2")->AsInteger;
			    nCMS_Total                            = nCMS1 + nCMS2;
                m_pGDC->Values[nRow][Column03->Index] = nCMS1;
                m_pGDC->Values[nRow][Column04->Index] = nCMS2;
                m_pGDC->Values[nRow][Column05->Index] = nCMS_Total;

				pChart1->Values[nRow] = nCMS_Total;
				pChart2->Values[nRow] = nCMS1;
				pChart3->Values[nRow] = nCMS2;

				TvChart->Categories->Values[nRow] = m_pGDC->Values[nRow][Column02->Index];
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

    		TvChart->EndUpdate();
            TvList->EndUpdate();
            //CxList->SetFocus();
            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);
        }
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, pADO->SQL->Text);
        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, pADO->SQL->Text);
        throw Exception(String(e.ClassName()) + e.Message);
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TIST00501::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"시설물통신단절통계";

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

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

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

