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

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

#include "SMS00201F.h"
#include "FrmSmsUserF.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"
TSMS00201 *SMS00201 = NULL;
//---------------------------------------------------------------------------
__fastcall TSMS00201::TSMS00201(TComponent* Owner, TForm *ParentForm)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    FUserId = "";
    FGropId = "";
    FGroupChanged = false;
    //cxGroupBox2->PanelStyle->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TSMS00201::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    //SMS00201 = NULL;
    Action = caFree;
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TSMS00201::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    Application->ProcessMessages();
    DispData();
}
//---------------------------------------------------------------------------
void __fastcall TSMS00201::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_USER_GROP_RLTN L  \r\n"
           "USING (SELECT :p01 AS SMS_GROP_ID,  \r\n"
           "              :p02 AS SMS_USER_ID   \r\n"
           "         FROM DUAL                  \r\n"
           "      ) M                           \r\n"
           "ON (L.SMS_GROP_ID = M.SMS_GROP_ID   \r\n"
           "AND L.SMS_USER_ID = M.SMS_USER_ID)  \r\n"
           "WHEN NOT MATCHED THEN               \r\n"
           "    INSERT (L.SMS_GROP_ID,          \r\n"
           "            L.SMS_USER_ID)          \r\n"
           "    VALUES (M.SMS_GROP_ID,          \r\n"
           "            M.SMS_USER_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 sUserId = TvList1->DataController->Values[nSelIdx][MColumn02->Index];

            try
            {
                ITSDb_SQLBind(pADO, "p01", FGropId);
                ITSDb_SQLBind(pADO, "p02", sUserId);
                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 TSMS00201::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_USER_GROP_RLTN L  \r\n"
           " WHERE L.SMS_GROP_ID = :p01     \r\n"
           "   AND L.SMS_USER_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 sUserId = TvList3->DataController->Values[nSelIdx][RColumn04->Index];

            try
            {
                ITSDb_SQLBind(pADO, "p01", sGropId);
                ITSDb_SQLBind(pADO, "p02", sUserId);
                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 TSMS00201::BtnInsert1Click(TObject *Sender)
{
    String sNewId = "";
    try {
        if (!GetUserNewId(sNewId)) return;

        TFrmSmsUser *pDlg = new TFrmSmsUser(this->Parent);
        pDlg->EdId->Text = sNewId;
        pDlg->EdBackTel->Text = "044-300-5545";

        pDlg->FDbMode = enJobSave;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            FUserId         = pDlg->EdId->Text.Trim();
            String sName    = pDlg->EdName->Text.Trim();
            String sCallTel = pDlg->EdCallTel->Text.Trim();
            String sBackTel = pDlg->EdBackTel->Text.Trim();
            String sDesc    = pDlg->EdDesc->Text.Trim();
            String sRegDt   = pDlg->EdRegDt->Text.Trim();

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

void __fastcall TSMS00201::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;
    }

    FUserId         = 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]);

    try {
        TFrmSmsUser *pDlg = new TFrmSmsUser(this->Parent);
        pDlg->EdId->Text      = FUserId;
        pDlg->EdName->Text    = sName;
        pDlg->EdCallTel->Text = sCallTel;
        pDlg->EdBackTel->Text = sBackTel;
        pDlg->EdDesc->Text    = sDesc;
        pDlg->EdRegDt->Text   = sRegDt;

        if (pDlg->EdBackTel->Text == "")
        {
            pDlg->EdBackTel->Text = "044-300-5545";
        }

        pDlg->FDbMode = enJobEdit;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            FUserId  = pDlg->EdId->Text.Trim();
            sName    = pDlg->EdName->Text.Trim();
            sCallTel = pDlg->EdCallTel->Text.Trim();
            sBackTel = pDlg->EdBackTel->Text.Trim();
            sDesc    = pDlg->EdDesc->Text.Trim();
            sRegDt   = pDlg->EdRegDt->Text.Trim();

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

void __fastcall TSMS00201::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 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]);

    String sMsg = "";
    sMsg += "<<< ID: " + sId + " >>>\r\n";
    sMsg += "<<< 사용자: " + sName + " >>>\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_USER_GROP_RLTN \r\n"
                 " WHERE SMS_USER_ID = :p01    \r\n";

        sQryD2 = "DELETE TB_SMS_USER           \r\n"
                 " WHERE SMS_USER_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);

            FUserId = "";
            SelSmsUserList();
            SelSmsGroupRltnList(FGropId);
            CMM_SetGridFocusRow(CxList1, TvList1, FUserId, 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 TSMS00201::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 TSMS00201::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 TSMS00201::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 TSMS00201::GetUserNewId(String &ANewId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(SMS_USER_ID), 0) + 1 AS NEWID \r\n"
           "  FROM TB_SMS_USER                           \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 TSMS00201::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 TSMS00201::DispData()
{
    FUserId = "";
    FGropId = "";
    SelSmsUserList();
    SelSmsGroupList();
    SelSmsGroupRltnList(FGropId);
}
//---------------------------------------------------------------------------

void __fastcall TSMS00201::SelSmsUserList()
{
    TSqlCursor sqlCrs;

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


    CMM_ClearGridTableView(pTvList);

    sQry = "SELECT A.SMS_USER_ID,    \r\n"
           "       A.SMS_USER_NM,    \r\n"
           "       A.SMS_USER_TEL,   \r\n"
           "       A.SMS_USER_EXPL,  \r\n"
           "       A.SMS_RPLY_TEL,   \r\n"
           "       A.RGST_DT         \r\n"
           "  FROM TB_SMS_USER A     \r\n"
           " ORDER BY A.SMS_USER_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][MColumn02->Index] = pADO->FieldByName("SMS_USER_ID")->AsString;
                pGDC->Values[nRow][MColumn03->Index] = pADO->FieldByName("SMS_USER_NM")->AsString;
                pGDC->Values[nRow][MColumn04->Index] = pADO->FieldByName("SMS_USER_TEL")->AsString;
                pGDC->Values[nRow][MColumn05->Index] = pADO->FieldByName("SMS_RPLY_TEL")->AsString;
                pGDC->Values[nRow][MColumn06->Index] = pADO->FieldByName("SMS_USER_EXPL")->AsString;
                pGDC->Values[nRow][MColumn07->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 TSMS00201::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 TSMS00201::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_USER_ID,                         \r\n"
           "       B.SMS_USER_NM, B.SMS_USER_TEL, B.SMS_USER_EXPL,       \r\n"
           "       B.SMS_RPLY_TEL, 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_USER_GROP_RLTN A,                              \r\n"
           "       TB_SMS_USER 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_USER_ID = B.SMS_USER_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++)
            {
                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] = pADO->FieldByName("SMS_USER_ID")->AsString;
                pGDC->Values[nRow][RColumn05->Index] = pADO->FieldByName("SMS_USER_NM")->AsString;
                pGDC->Values[nRow][RColumn06->Index] = pADO->FieldByName("SMS_USER_TEL")->AsString;
                pGDC->Values[nRow][RColumn07->Index] = pADO->FieldByName("SMS_RPLY_TEL")->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 TSMS00201::TvList1FocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    DispSmsUser();
}
//---------------------------------------------------------------------------

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

void __fastcall TSMS00201::DispSmsUser()
{
    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;

    FUserId         = 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 TSMS00201::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);
}
//---------------------------------------------------------------------------

