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


#pragma hdrstop

#include "CDSBlackBoxF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TBlackBox::TBlackBox()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";

    //Clear();
}
//---------------------------------------------------------------------------
void TBlackBox::Clear()
{
}
//---------------------------------------------------------------------------
TBlackBox::~TBlackBox()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TBlackBoxManager *BlackBoxManager = NULL;
//---------------------------------------------------------------------------

/*
* BlackBox Manager
*/
TBlackBoxManager::TBlackBoxManager()
{
}
//---------------------------------------------------------------------------
TBlackBoxManager::~TBlackBoxManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

bool TBlackBoxManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    try
    {
        FLists.Lock();

        FLists.RemoveAll();
    }
    __finally
    {
        FLists.UnLock();
    }

    LoadBlackBoxTrmn(ADbConn);

    return true;
}
//---------------------------------------------------------------------------

bool TBlackBoxManager::LoadBlackBoxTrmn(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

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

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

                pObj->BDS_MNGM_NMBR = pADO->FieldByName("BDS_MNGM_NMBR")->AsInteger;
                pObj->OPNC_NMBR     = pADO->FieldByName("OPNC_NMBR")->AsString;
                pObj->MDM_NMBR      = pADO->FieldByName("MDM_NMBR")->AsString;
                pObj->USIM_NMBR     = pADO->FieldByName("USIM_NMBR")->AsString;
                pObj->VHCL_NMBR     = pADO->FieldByName("VHCL_NMBR")->AsString;
                pObj->OWNR_NM       = pADO->FieldByName("OWNR_NM")->AsString;
                pObj->PRCT_NM       = pADO->FieldByName("PRCT_NM")->AsString;

				FLists.Push(pObj->BDS_MNGM_NMBR, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("블랙박스단말기정보조회", 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("블랙박스단말기정보조회", 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 TBlackBoxManager::GetNextBlackBoxId(String &ANewId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT NVL(MAX(TO_NUMBER(BDS_MNGM_NMBR))+1, 1) AS NEWID \r\n"
           "  FROM TB_BDS_TRMN                                      \r\n";
#else
    sQry = "SELECT NVL(MAX((BDS_MNGM_NMBR))+1, 1) AS NEWID \r\n"
           "  FROM TB_BDS_TRMN                             \r\n";
#endif

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

			pADO->Open();
            ANewId = pADO->FieldByName("NEWID")->AsString;
//            ANewId = "PAR" + pADO->FieldByName("NEWID")->AsString;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("블랙박스신규관리번호조회", 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("블랙박스신규관리번호조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TBlackBoxManager::DeleteBlackBox(TBlackBox *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_BDS_TRMN          \r\n"
           " WHERE BDS_MNGM_NMBR = :p01 \r\n";

    try
    {
        //FLists.Lock();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->BDS_MNGM_NMBR;
			pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("블랙박스정보삭제", 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("블랙박스정보삭제", 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 TBlackBoxManager::UpdateBlackBox(TBlackBox *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;


    sQry = "UPDATE TB_BDS_TRMN           \r\n"
           "   SET OPNC_NMBR   = :p02,   \r\n"
           "       MDM_NMBR    = :p03,   \r\n"
           "       USIM_NMBR   = :p04,   \r\n"
           "       VHCL_NMBR   = :p05,   \r\n"
           "       OWNR_NM     = :p06,   \r\n"
           "       PRCT_NM     = :p07    \r\n"
           " WHERE BDS_MNGM_NMBR = :p01  \r\n";

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

            pADO->Parameters->ParamByName("p01")->Value = AObj->BDS_MNGM_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->OPNC_NMBR;
            pADO->Parameters->ParamByName("p03")->Value = AObj->MDM_NMBR;
            pADO->Parameters->ParamByName("p04")->Value = AObj->USIM_NMBR;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VHCL_NMBR;
            pADO->Parameters->ParamByName("p06")->Value = AObj->OWNR_NM;
            pADO->Parameters->ParamByName("p07")->Value = AObj->PRCT_NM;

			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("블랙박스정보수정", 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("블랙박스정보수정", 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 TBlackBoxManager::InsertBlackBox(TBlackBox *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_BDS_TRMN (               \r\n"
           "         BDS_MNGM_NMBR,                 \r\n"
           "         OPNC_NMBR,                     \r\n"
           "         MDM_NMBR,                      \r\n"
           "         USIM_NMBR,                     \r\n"
           "         VHCL_NMBR,                     \r\n"
           "         OWNR_NM,                       \r\n"
           "         PRCT_NM                        \r\n"
           "                        )               \r\n"
           "  VALUES(:p01, :p02, :p03, :p04, :p05,  \r\n"
           "         :p06, :p07                   ) \r\n";
    try
    {
        //FLists.Lock();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = AObj->BDS_MNGM_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->OPNC_NMBR;
            pADO->Parameters->ParamByName("p03")->Value = AObj->MDM_NMBR;
            pADO->Parameters->ParamByName("p04")->Value = AObj->USIM_NMBR;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VHCL_NMBR;
            pADO->Parameters->ParamByName("p06")->Value = AObj->OWNR_NM;
            pADO->Parameters->ParamByName("p07")->Value = AObj->PRCT_NM;

			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("블랙박스정보입력", 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("블랙박스정보입력", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        //FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------



