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

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

#include "SMS00202F.h"
#include "FrmSmsFailF.h"
#include "FrmSmsGroupF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#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 link "cxGridBandedTableView"
#pragma resource "*.dfm"
TSMS00202 *SMS00202 = NULL;
//---------------------------------------------------------------------------
__fastcall TSMS00202::TSMS00202(TComponent* Owner, TForm *ParentForm)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    FFailId = "";
    FGropId = "";
    FGroupChanged = false;
    //cxGroupBox2->PanelStyle->Active = true;

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

void __fastcall TSMS00202::CommClose()
{
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::FormDestroy(TObject *Sender)
{
    try
    {
    }
    catch (...)
    {
    }
}
//---------------------------------------------------------------------------
void __fastcall TSMS00202::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    Application->ProcessMessages();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::FormInit()
{
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    Application->ProcessMessages();
    DispData();
}
//---------------------------------------------------------------------------
void __fastcall TSMS00202::BtnListAddClick(TObject *Sender)
{
    // List Data Add
    TWaitCursor crs;

    TcxGridTableView  *pTvList = TvList2;
    TcxDataController *pGDC    = pTvList->DataController;
    if (pTvList->ViewData->RecordCount <= 0) return;
    int nRow = pGDC->FocusedRecordIndex;
    if (nRow < 0)
    {
        Application->MessageBox(L"추가할 대상 SMS 그룹을 먼저 선택하세요", L"SMS 그룹 목록 추가", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        return;
    }

    FGropId       = VarToStr(pGDC->Values[nRow][SColumn02->Index]);
    String sName  = VarToStr(pGDC->Values[nRow][SColumn03->Index]);
    //String sGrade = VarToStr(pGDC->Values[nRow][SColumn04->Index]);
    String sDesc  = VarToStr(pGDC->Values[nRow][SColumn05->Index]);
    String sRegDt = VarToStr(pGDC->Values[nRow][SColumn06->Index]);

    int nSelRows = TvList1->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(L"추가할 SMS 발송 장애유형을 선택하세요", L"SMS 그룹 목록 추가", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        return;
    }

	String sQry;
    TADOQuery *pADO = NULL;

	sQry = "MERGE INTO TB_SMS_FAIL_GROP_RLTN L  \r\n"
           "USING (SELECT :p01 AS SMS_GROP_ID,  \r\n"
           "              :p02 AS SMS_FAIL_ID   \r\n"
           "         FROM DUAL                  \r\n"
           "      ) M                           \r\n"
           "ON (L.SMS_GROP_ID = M.SMS_GROP_ID   \r\n"
           "AND L.SMS_FAIL_ID = M.SMS_FAIL_ID)  \r\n"
           "WHEN NOT MATCHED THEN               \r\n"
           "    INSERT (L.SMS_GROP_ID,          \r\n"
           "            L.SMS_FAIL_ID)          \r\n"
           "    VALUES (M.SMS_GROP_ID,          \r\n"
           "            M.SMS_FAIL_ID)          \r\n";
    try
    {
        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();
        ITSDb_SQLText(pADO, sQry);

        ITSDb_GetConnection()->BeginTrans();

        for (int ii = 0; ii < nSelRows; ii++)
        {
            int nSelIdx = TvList1->Controller->SelectedRows[ii]->RecordIndex;

            String sFailId = TvList1->DataController->Values[nSelIdx][MColumn02->Index];

            try
            {
                ITSDb_SQLBind(pADO, "p01", FGropId);
                ITSDb_SQLBind(pADO, "p02", sFailId);
                ITSDb_SQLExec(pADO);
            }
            catch(EDatabaseError &E)
            {
                ITSDb_GetConnection()->RollbackTrans();
                ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
                ShowMessage(String(E.ClassName()) + E.Message);
                break;
            }
            catch(Exception &exception)
            {
                ITSDb_GetConnection()->RollbackTrans();
                ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
                ShowMessage(String(exception.ClassName()) + exception.Message);
                break;
            }
            catch(...)
            {
                ITSDb_GetConnection()->RollbackTrans();
                ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
                ShowMessage("데이터 작업 중에 알수없는 DB 오류가 발생하였습니다.");
                break;
            }
        }
        ITSDb_GetConnection()->CommitTrans();
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        SelSmsGroupRltnList(FGropId);
    }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnListDeleteClick(TObject *Sender)
{
    // List Data Delete
    TWaitCursor crs;

    int nSelRows = TvList3->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(L"삭제할 SMS 발송 그룹 정보를 선택하세요", L"SMS 그룹 목록 삭제", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        return;
    }

	String sQry;
    TADOQuery *pADO = NULL;

	sQry = "DELETE TB_SMS_FAIL_GROP_RLTN L  \r\n"
           " WHERE L.SMS_GROP_ID = :p01     \r\n"
           "   AND L.SMS_FAIL_ID = :p02     \r\n";
    try
    {
        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();
        ITSDb_SQLText(pADO, sQry);

        ITSDb_GetConnection()->BeginTrans();

        for (int ii = 0; ii < nSelRows; ii++)
        {
            int nSelIdx = TvList3->Controller->SelectedRows[ii]->RecordIndex;

            String sGropId = TvList3->DataController->Values[nSelIdx][RColumn02->Index];
            String sFailId = TvList3->DataController->Values[nSelIdx][RColumn04->Index];

            try
            {
                ITSDb_SQLBind(pADO, "p01", sGropId);
                ITSDb_SQLBind(pADO, "p02", sFailId);
                ITSDb_SQLExec(pADO);
            }
            catch(EDatabaseError &E)
            {
                ITSDb_GetConnection()->RollbackTrans();
                ShowMessage(String(E.ClassName()) + E.Message);
                break;
            }
            catch(Exception &exception)
            {
                ITSDb_GetConnection()->RollbackTrans();
                ShowMessage(String(exception.ClassName()) + exception.Message);
                break;
            }
            catch(...)
            {
                ITSDb_GetConnection()->RollbackTrans();
                ShowMessage("데이터 작업 중에 알수없는 DB 오류가 발생하였습니다.");
                break;
            }
        }
        ITSDb_GetConnection()->CommitTrans();
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        SelSmsGroupRltnList(FGropId);
    }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnInsert1Click(TObject *Sender)
{
    String sNewId = "";
    try {
        if (!GetFailNewId(sNewId)) return;

        TFrmSmsFail *pDlg = new TFrmSmsFail(this->Parent);
        pDlg->EdId->Text = sNewId;
        pDlg->FDbMode = enJobSave;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            FFailId         = pDlg->EdId->Text.Trim();
#if 0
            String sName    = pDlg->EdName->Text.Trim();
            String sCallTel = pDlg->EdCallTel->Text.Trim();
            String sBackTel = pDlg->EdBackTel->Text.Trim();
            String sDesc    = pDlg->EdDesc->Text.Trim();
#endif
            String sRegDt   = pDlg->EdRegDt->Text.Trim();

            SelSmsFailList();
            CMM_SetGridFocusRow(CxList1, TvList1, FFailId, MColumn02->Index);
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnEdit1Click(TObject *Sender)
{
    TcxDataController *pGDC = TvList1->DataController;
    if (TvList1->ViewData->RecordCount <= 0) return;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow < 0 )
    {
        Application->MessageBox(L"목록에서 장애유형을 선택하세요.", L"SMS 발송 장애유형 등록", MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    FFailId        = VarToStr(pGDC->Values[nRow][MColumn02->Index]);
    String sFTY    = VarToStr(pGDC->Values[nRow][ColFTY->Index]);
    String sFFA    = VarToStr(pGDC->Values[nRow][ColFFA->Index]);
    String sFSS    = VarToStr(pGDC->Values[nRow][ColFSS->Index]);
    String sHH     = VarToStr(pGDC->Values[nRow][ColHH->Index]);
    String sDesc   = VarToStr(pGDC->Values[nRow][MColumn05->Index]);
    String sCYCL   = VarToStr(pGDC->Values[nRow][MColumn07->Index]);
    String sYN     = VarToStr(pGDC->Values[nRow][MColumn09->Index]);
    String sRgstDt = VarToStr(pGDC->Values[nRow][MColumn10->Index]);

    try {
        TFrmSmsFail *pDlg = new TFrmSmsFail(this->Parent);
        pDlg->EdId->Text    = FFailId;
        pDlg->EdFTY->Text   = sFTY;
        pDlg->EdFFA->Text   = sFFA;
        pDlg->EdFSS->Text   = sFSS;
        pDlg->EdHH->Text    = sHH;
        pDlg->SeCYCL->Value = sCYCL.ToIntDef(1);
        pDlg->EdYN->Text    = sYN;
        pDlg->EdDesc->Text  = sDesc;
        pDlg->EdRegDt->Text = sRgstDt;

        pDlg->FDbMode = enJobEdit;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            FFailId  = pDlg->EdId->Text.Trim();

            SelSmsFailList();
            SelSmsGroupRltnList(FGropId);
            CMM_SetGridFocusRow(CxList1, TvList1, FFailId, MColumn02->Index);
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnDelete1Click(TObject *Sender)
{
    TcxDataController *pGDC = TvList1->DataController;
    if (TvList1->ViewData->RecordCount <= 0) return;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow < 0 )
    {
        Application->MessageBox(L"목록에서 장애유형을 선택하세요.", L"SMS 발송 장애유형 삭제", MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    String sId  = VarToStr(pGDC->Values[nRow][MColumn02->Index]);
    String sFTY = VarToStr(pGDC->Values[nRow][MColumn03->Index]);
    String sFFA = VarToStr(pGDC->Values[nRow][MColumn04->Index]);

    String sMsg = "";
    sMsg += "<<< 장애ID: " + sId  + " >>>\r\n";
    sMsg += "<<< 시설물 유형: " + sFTY + " >>>\r\n";
    sMsg += "<<< 장애 유형: " + sFFA + " >>>\r\n";
    sMsg += "<<< 장애유형 정보를 삭제 하시겠습니까? >>>\r\n";
    sMsg += "<<< 장애유형-발송그룹 정보가 있는 경우 함께 삭제 됩니다. >>>\r\n";
    if (Application->MessageBox(sMsg.c_str(), L"SMS 장애유형 삭제", MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    TADOQuery *pADOD = NULL;
	String sQryD1, sQryD2;
    try
    {
        sQryD1 = "DELETE TB_SMS_FAIL_GROP_RLTN \r\n"
                 " WHERE SMS_FAIL_ID = :p01    \r\n";

        sQryD2 = "DELETE TB_SMS_FAIL_STUP      \r\n"
                 " WHERE SMS_FAIL_ID = :p01    \r\n";

        try
        {
            pADOD = new TADOQuery(NULL);
			pADOD->Close();
            pADOD->Connection = ITSDb_GetConnection();

            ITSDb_GetConnection()->BeginTrans();

            ITSDb_SQLText(pADOD, sQryD1);
            ITSDb_SQLBind(pADOD, "p01", sId);
            ITSDb_SQLExec(pADOD);

            ITSDb_SQLText(pADOD, sQryD2);
            ITSDb_SQLBind(pADOD, "p01", sId);
            ITSDb_SQLExec(pADOD);

            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(L"SMS 장애유형 정보를 삭제 하였습니다.",
                                    L"SMS 장애유형 삭제", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            FFailId = "";
            SelSmsFailList();
            SelSmsGroupRltnList(FGropId);
            CMM_SetGridFocusRow(CxList1, TvList1, FFailId, MColumn02->Index);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage("데이터 작업 중에 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnInsertGroupClick(TObject *Sender)
{
    String sNewId = "";
    try {
        if (!GetGroupNewId(sNewId)) return;

        TFrmSmsGroup *pDlg = new TFrmSmsGroup(this->Parent);
        pDlg->EdId->Text = sNewId;
        pDlg->FDbMode = enJobSave;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            FGropId = pDlg->EdId->Text;

            SelSmsGroupList();
            SelSmsGroupRltnList(FGropId);
            CMM_SetGridFocusRow(CxList2, TvList2, FGropId, SColumn02->Index);
            FGroupChanged = true;
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnEditGroupClick(TObject *Sender)
{
    TcxDataController *pGDC = TvList2->DataController;
    if (TvList2->ViewData->RecordCount <= 0) return;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow < 0 )
    {
        Application->MessageBox(L"목록에서 그룹을 선택하세요.", L"SMS 발송 그룹 등록", MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    FGropId       = VarToStr(pGDC->Values[nRow][SColumn02->Index]);
    String sName  = VarToStr(pGDC->Values[nRow][SColumn03->Index]);
    //String sGrade = pGDC->Values[nRow][SColumn04->Index];
    String sDesc  = VarToStr(pGDC->Values[nRow][SColumn05->Index]);
    String sRegDt = VarToStr(pGDC->Values[nRow][SColumn06->Index]);

    try {
        TFrmSmsGroup *pDlg = new TFrmSmsGroup(this->Parent);
        pDlg->FDbMode = enJobEdit;
        pDlg->EdId->Text      = FGropId;
        pDlg->EdName->Text    = sName;
        pDlg->EdDesc->Text    = sDesc;
        pDlg->EdRegDt->Text   = sRegDt;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            FGropId = pDlg->EdId->Text;

            SelSmsGroupList();
            SelSmsGroupRltnList(FGropId);
            CMM_SetGridFocusRow(CxList2, TvList2, FGropId, SColumn02->Index);
            FGroupChanged = true;
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::BtnDeleteGroupClick(TObject *Sender)
{
    TcxDataController *pGDC = TvList2->DataController;
    if (TvList2->ViewData->RecordCount <= 0) return;
	int nRow = pGDC->FocusedRecordIndex;
	if( nRow < 0 )
    {
        Application->MessageBox(L"목록에서 그룹을 선택하세요.", L"SMS 발송 그룹 삭제", MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    String sId    = VarToStr(pGDC->Values[nRow][SColumn02->Index]);
    String sName  = VarToStr(pGDC->Values[nRow][SColumn03->Index]);
    //String sGrade = VarToStr(pGDC->Values[nRow][SColumn04->Index]);
    String sDesc  = VarToStr(pGDC->Values[nRow][SColumn05->Index]);
    String sRegDt = VarToStr(pGDC->Values[nRow][SColumn06->Index]);

    String sMsg = "";
    sMsg += "<<< ID: " + sId + " >>>\r\n";
    sMsg += "<<< 그룹명: " + sName + " >>>\r\n";
    sMsg += "<<< SMS 발송 그룹 정보를 삭제 하시겠습니까? >>>\r\n\r\n";
    sMsg += "<<< 연락처-발송그룹 정보가 있는 경우 함께 삭제 됩니다. >>>\r\n";
    sMsg += "<<< 장애유형-발송그룹 정보가 있는 경우 함께 삭제 됩니다. >>>\r\n";
    if (Application->MessageBox(sMsg.c_str(), L"SMS 발송 그룹 정보 삭제", MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    // delete
    TADOQuery *pADOD = NULL;
	String sQryD1, sQryD2, sQryD3;
    try
    {
        sQryD1 = "DELETE TB_SMS_USER_GROP_RLTN \r\n"
                 " WHERE SMS_GROP_ID = :p01    \r\n";

        sQryD2 = "DELETE TB_SMS_FAIL_GROP_RLTN \r\n"
                 " WHERE SMS_GROP_ID = :p01    \r\n";

        sQryD3 = "DELETE TB_SMS_USER_GROP      \r\n"
                 " WHERE SMS_GROP_ID = :p01    \r\n";

        try
        {
            pADOD = new TADOQuery(NULL);
			pADOD->Close();
            pADOD->Connection = ITSDb_GetConnection();

            ITSDb_GetConnection()->BeginTrans();

            ITSDb_SQLText(pADOD, sQryD1);
            ITSDb_SQLBind(pADOD, "p01", sId);
            ITSDb_SQLExec(pADOD);

            ITSDb_SQLText(pADOD, sQryD2);
            ITSDb_SQLBind(pADOD, "p01", sId);
            ITSDb_SQLExec(pADOD);

            ITSDb_SQLText(pADOD, sQryD3);
            ITSDb_SQLBind(pADOD, "p01", sId);
            ITSDb_SQLExec(pADOD);

            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(L"SMS 발송 그룹 정보를 삭제 하였습니다.",
                                    L"SMS 발송 그룹 정보 삭제", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            FGropId = "";
            SelSmsGroupList();
            SelSmsGroupRltnList(FGropId);
            CMM_SetGridFocusRow(CxList2, TvList2, FGropId, SColumn02->Index);
            FGroupChanged = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ShowMessage("데이터 작업 중에 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
    }
}
//---------------------------------------------------------------------------

bool __fastcall TSMS00202::GetFailNewId(String &ANewId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(SMS_FAIL_ID), 0) + 1 AS NEWID \r\n"
           "  FROM TB_SMS_FAIL_STUP                      \r\n";

    ANewId = "";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Open();

            ANewId = pADO->FieldByName("NEWID")->AsString;

            return true;
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception("알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TSMS00202::GetGroupNewId(String &ANewId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(SMS_GROP_ID), 0) + 1 AS NEWID \r\n"
           "  FROM TB_SMS_USER_GROP                      \r\n";

    ANewId = "";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Open();

            ANewId = pADO->FieldByName("NEWID")->AsString;

            return true;
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            throw Exception("알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::DispData()
{
    FFailId = "";
    FGropId = "";
    SelSmsFailList();
    SelSmsGroupList();
    SelSmsGroupRltnList(FGropId);
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::SelSmsFailList()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery         *pADO    = ADOQry;
    TcxGridTableView  *pTvList = TvList1;
    TcxDataController *pGDC    = pTvList->DataController;


    CMM_ClearGridTableView(pTvList);

    sQry = "SELECT A.SMS_FAIL_ID,      \r\n"
           "       A.SMS_FCLT_TYPE,    \r\n"
           "       A.SMS_FAIL_TYPE,    \r\n"
           "       A.SMS_FAIL_EXPL,    \r\n"
           "       A.SMS_SEND_STYL,    \r\n"
           "       A.SEND_CYCL_STYL,   \r\n"
           "       A.SEND_HH_STYL,     \r\n"
           "       A.SEND_YN,          \r\n"
           "       A.RGST_DT           \r\n"
           "  FROM TB_SMS_FAIL_STUP A  \r\n"
           " ORDER BY A.SMS_FAIL_ID    \r\n";

    try
    {
        pADO->Connection = ITSDb_GetConnection();
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLOpen(pADO);

        int nRow = 0;
        try
        {
            pTvList->BeginUpdate();

            pGDC->RecordCount = pADO->RecordCount;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                String sId     = pADO->FieldByName("SMS_FAIL_ID")->AsString;
                String sFTY    = pADO->FieldByName("SMS_FCLT_TYPE")->AsString;
                String sFFA    = pADO->FieldByName("SMS_FAIL_TYPE")->AsString;
                String sDesc   = pADO->FieldByName("SMS_FAIL_EXPL")->AsString;
                String sFSS    = pADO->FieldByName("SMS_SEND_STYL")->AsString;
                String sCYCL   = pADO->FieldByName("SEND_CYCL_STYL")->AsString;
                String sHH     = pADO->FieldByName("SEND_HH_STYL")->AsString;
                String sYN     = pADO->FieldByName("SEND_YN")->AsString;
                String sRgstDt = pADO->FieldByName("RGST_DT")->AsString;

                pGDC->Values[nRow][ColFTY->Index] = sFTY;
                pGDC->Values[nRow][ColFFA->Index] = sFFA;
                pGDC->Values[nRow][ColFSS->Index] = sFSS;
                pGDC->Values[nRow][ColHH->Index ] = sHH;

                pGDC->Values[nRow][MColumn02->Index] = sId;
                pGDC->Values[nRow][MColumn03->Index] = APP_GetCodeDesc(CbFTY, sFTY);
                pGDC->Values[nRow][MColumn04->Index] = APP_GetCodeDesc(CbFFA, sFFA);
                pGDC->Values[nRow][MColumn05->Index] = sDesc;
                pGDC->Values[nRow][MColumn06->Index] = APP_GetCodeDesc(CbFSS, sFSS);
                pGDC->Values[nRow][MColumn07->Index] = sCYCL;

                if (sFSS == "FSS03")
                {   //지정된 시간 발송
                    TDateTime dt = ITSUtil_StrToDateTime(sHH);;
                    pGDC->Values[nRow][MColumn08->Index] = dt.FormatString("hh:nn");
                }
                pGDC->Values[nRow][MColumn09->Index] = sYN;
                pGDC->Values[nRow][MColumn10->Index] = ITSUtil_StrToTime(sRgstDt);
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            pTvList->EndUpdate();
            //LblRecords->Caption = FormatFloat("##,##0", pGDC->RecordCount) + " 건";
        }
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(...)
    {
        throw Exception("알수없는 오류가 발생하였습니다.");
    }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::SelSmsGroupList()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery         *pADO    = ADOQry;
    TcxGridTableView  *pTvList = TvList2;
    TcxDataController *pGDC    = pTvList->DataController;


    CMM_ClearGridTableView(pTvList);

    sQry = "SELECT A.SMS_GROP_ID,     \r\n"
           "       A.SMS_GROP_NM,     \r\n"
           "       A.SMS_GROP_EXPL,   \r\n"
           "       A.RGST_DT          \r\n"
           "  FROM TB_SMS_USER_GROP A \r\n"
           " ORDER BY A.SMS_GROP_ID   \r\n";

    try
    {
        pADO->Connection = ITSDb_GetConnection();
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLOpen(pADO);

        int nRow = 0;
        try
        {
            pTvList->BeginUpdate();

            pGDC->RecordCount = pADO->RecordCount;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                pGDC->Values[nRow][SColumn02->Index] = pADO->FieldByName("SMS_GROP_ID")->AsString;
                pGDC->Values[nRow][SColumn03->Index] = pADO->FieldByName("SMS_GROP_NM")->AsString;
                pGDC->Values[nRow][SColumn05->Index] = pADO->FieldByName("SMS_GROP_EXPL")->AsString;
                pGDC->Values[nRow][SColumn06->Index] = ITSUtil_StrToTime(pADO->FieldByName("RGST_DT")->AsString);
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            pTvList->EndUpdate();
            //LblRecords->Caption = FormatFloat("##,##0", pGDC->RecordCount) + " 건";
        }
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(...)
    {
        throw Exception("알수없는 오류가 발생하였습니다.");
    }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::SelSmsGroupRltnList(String AGroupId)
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery         *pADO    = ADOQry;
    TcxGridTableView  *pTvList = TvList3;
    TcxDataController *pGDC    = pTvList->DataController;


    CMM_ClearGridTableView(pTvList);

    sQry = "SELECT A.SMS_GROP_ID, A.SMS_FAIL_ID,                         \r\n"
           "       B.SMS_FCLT_TYPE,                                      \r\n"
           "       B.SMS_FAIL_TYPE,                                      \r\n"
           "       B.SMS_FAIL_EXPL,                                      \r\n"
           "       B.SMS_SEND_STYL,                                      \r\n"
           "       B.SEND_CYCL_STYL,                                     \r\n"
           "       B.SEND_HH_STYL,                                       \r\n"
           "       B.SEND_YN,                                            \r\n"
           "       B.RGST_DT AS URGST_DT,                                \r\n"
           "       C.SMS_GROP_NM, C.SMS_GROP_EXPL, C.RGST_DT AS GRGST_DT \r\n"
           "  FROM TB_SMS_FAIL_GROP_RLTN A,                              \r\n"
           "       TB_SMS_FAIL_STUP B,                                   \r\n"
           "       TB_SMS_USER_GROP C                                    \r\n"
           " WHERE A.SMS_GROP_ID = :p01                                  \r\n"
           "   AND A.SMS_GROP_ID = C.SMS_GROP_ID                         \r\n"
           "   AND A.SMS_FAIL_ID = B.SMS_FAIL_ID                         \r\n";

    try
    {
        pADO->Connection = ITSDb_GetConnection();
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", AGroupId);
        ITSDb_SQLOpen(pADO);

        int nRow = 0;
        try
        {
            pTvList->BeginUpdate();

            pGDC->RecordCount = pADO->RecordCount;

            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                String sId     = pADO->FieldByName("SMS_FAIL_ID")->AsString;
                String sFTY    = pADO->FieldByName("SMS_FCLT_TYPE")->AsString;
                String sFFA    = pADO->FieldByName("SMS_FAIL_TYPE")->AsString;
                String sDesc   = pADO->FieldByName("SMS_FAIL_EXPL")->AsString;
                String sFSS    = pADO->FieldByName("SMS_SEND_STYL")->AsString;
                String sCYCL   = pADO->FieldByName("SEND_CYCL_STYL")->AsString;
                String sHH     = pADO->FieldByName("SEND_HH_STYL")->AsString;
                String sYN     = pADO->FieldByName("SEND_YN")->AsString;
                String sRgstDt = pADO->FieldByName("URGST_DT")->AsString;

                pGDC->Values[nRow][RColumn02->Index] = pADO->FieldByName("SMS_GROP_ID")->AsString;
                pGDC->Values[nRow][RColumn03->Index] = pADO->FieldByName("SMS_GROP_NM")->AsString;

                pGDC->Values[nRow][RColumn04->Index] = sId;
                pGDC->Values[nRow][RColumn05->Index] = APP_GetCodeDesc(CbFTY, sFTY);
                pGDC->Values[nRow][RColumn06->Index] = APP_GetCodeDesc(CbFFA, sFFA);
                pGDC->Values[nRow][RColumn07->Index] = sDesc;
                pGDC->Values[nRow][RColumn08->Index] = APP_GetCodeDesc(CbFSS, sFSS);
                pGDC->Values[nRow][RColumn09->Index] = sYN;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            pTvList->EndUpdate();
            //LblRecords->Caption = FormatFloat("##,##0", pGDC->RecordCount) + " 건";
        }
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(...)
    {
        throw Exception("알수없는 오류가 발생하였습니다.");
    }
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::TvList1FocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    DispSmsFail();
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::TvList2FocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    DispSmsGroup();
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::DispSmsFail()
{
    BtnDelete1->Enabled = false;
    BtnEdit1->Enabled = false;

    TcxGridTableView  *pTvList = TvList1;
    TcxDataController *pGDC    = pTvList->DataController;
    if (pTvList->ViewData->RecordCount <= 0) return;
    int nRow = pGDC->FocusedRecordIndex;
    if (nRow < 0) return;

    FFailId         = VarToStr(pGDC->Values[nRow][MColumn02->Index]);
    String sName    = VarToStr(pGDC->Values[nRow][MColumn03->Index]);
    String sCallTel = VarToStr(pGDC->Values[nRow][MColumn04->Index]);
    String sBackTel = VarToStr(pGDC->Values[nRow][MColumn05->Index]);
    String sDesc    = VarToStr(pGDC->Values[nRow][MColumn06->Index]);
    String sRegDt   = VarToStr(pGDC->Values[nRow][MColumn07->Index]);

    BtnDelete1->Enabled = true;
    BtnEdit1->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::DispSmsGroup()
{
    BtnDeleteGroup->Enabled = false;
    BtnEditGroup->Enabled = false;

    TcxGridTableView  *pTvList = TvList2;
    TcxDataController *pGDC    = pTvList->DataController;
    if (pTvList->ViewData->RecordCount <= 0) return;
    int nRow = pGDC->FocusedRecordIndex;
    if (nRow < 0) return;

    FGropId       = VarToStr(pGDC->Values[nRow][SColumn02->Index]);
    String sName  = VarToStr(pGDC->Values[nRow][SColumn03->Index]);
    //String sGrade = VarToStr(pGDC->Values[nRow][SColumn04->Index]);
    String sDesc  = VarToStr(pGDC->Values[nRow][SColumn05->Index]);
    String sRegDt = VarToStr(pGDC->Values[nRow][SColumn06->Index]);

    BtnDeleteGroup->Enabled = true;
    BtnEditGroup->Enabled = true;

    SelSmsGroupRltnList(FGropId);
}
//---------------------------------------------------------------------------

void __fastcall TSMS00202::InitCode()
{
    TItsCode *FCodeFTY;
    TItsCode *FCodeFFA;
    TItsCode *FCodeFSS;
    FCodeFTY = ItsCodeManager->FLists.Find("FTY");
    FCodeFFA = ItsCodeManager->FLists.Find("FFA");
    FCodeFSS = ItsCodeManager->FLists.Find("FSS");

    // 시설물유형
    CbFTY->Properties->Items->Clear();
	if (FCodeFTY)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeFTY->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbFTY->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbFTY->ItemIndex = 0;

    // 장애유형
    CbFFA->Properties->Items->Clear();
	if (FCodeFFA)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeFFA->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbFFA->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbFFA->ItemIndex = 0;

    // 송신방식
    CbFSS->Properties->Items->Clear();
	if (FCodeFSS)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeFSS->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbFSS->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbFSS->ItemIndex = 0;
}
//---------------------------------------------------------------------------

