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

#pragma hdrstop

#include "CDSOrganF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsOrgan::TItsOrgan()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsOrgan::Clear()
{
    CNCT_INTN_NMBR      = "";
    CNCT_INTN_NM        = "";
    CNCT_INTN_TYPE_CD   = "";
    CNCT_INTN_TEL       = "";
    CNCT_INTN_ADDR      = "";
    RMRK                = "";
    DEL_YN              = "N";
}
//---------------------------------------------------------------------------
TItsOrgan::~TItsOrgan()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsOrganManager *ItsOrganManager = NULL;
//---------------------------------------------------------------------------

/*
* Organ Manager
*/
TItsOrganManager::TItsOrganManager()
{
}
//---------------------------------------------------------------------------
TItsOrganManager::~TItsOrganManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsOrganManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *               \r\n"
           "  FROM TB_CNCT_INTN    \r\n";

    FLists.Lock();
    try
    {
        FLists.RemoveAll();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TItsOrgan *pObj = new TItsOrgan();


                //pObj->CNCT_INTN_NMBR      = pADO->FieldByName("CNCT_INTN_NMBR")->AsString;          //NUMBER(10)	N			연계 기관 관리 번호
                pObj->CNCT_INTN_NMBR      = pADO->FieldByName("CNCT_INTN_MNGM_NMBR")->AsString;          //NUMBER(10)	N			연계 기관 관리 번호
                pObj->CNCT_INTN_NM        = pADO->FieldByName("CNCT_INTN_NM")->AsString.Trim();     //VARCHAR2(50)	Y			연계 기관 명
                pObj->CNCT_INTN_TYPE_CD   = pADO->FieldByName("CNCT_INTN_TYPE_CD")->AsString;       //VARCHAR2(7)	Y			연계 기관 유형 코드
                pObj->CNCT_INTN_TEL       = pADO->FieldByName("CNCT_INTN_TEL")->AsString;           //VARCHAR2(128)	Y			연계 기관 전화번호
                pObj->CNCT_INTN_ADDR      = pADO->FieldByName("CNCT_INTN_ADDR")->AsString.Trim();   //VARCHAR2(200)	Y			연계 기관 주소
                pObj->RMRK                = pADO->FieldByName("RMRK")->AsString;                    //VARCHAR2(600)	Y			비고
                pObj->DEL_YN              = pADO->FieldByName("DEL_YN")->AsString;                  //CHAR(1)	Y	'N'		삭제 여부

				FLists.Push(pObj->CNCT_INTN_NMBR, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsOrganManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsOrganManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TItsOrganManager::GetNextOrganId(String &ANewOrgId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT NVL(MAX(CNCT_INTN_NMBR) + 1, 1) AS NEWID \r\n"
           "  FROM TB_CNCT_INTN_INFR                        \r\n";
#else
    sQry = "SELECT NVL(MAX(CNCT_INTN_MNGM_NMBR) + 1, 1) AS NEWID \r\n"
           "  FROM TB_CNCT_INTN                                  \r\n";
#endif
    ANewOrgId = "1";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            ANewOrgId = pADO->FieldByName("NEWID")->AsString;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsOrganManager::GetNextOrganId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsOrganManager::GetNextOrganId", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

