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

#pragma hdrstop

#include "CDSFacilityCodeF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TFacilityCodeManager *FacilityCodeManager = NULL;
//---------------------------------------------------------------------------

/*
* TFacilityCodeManager Manager
*/
TFacilityCodeManager::TFacilityCodeManager()
{
}
//---------------------------------------------------------------------------
TFacilityCodeManager::~TFacilityCodeManager()
{
}
//---------------------------------------------------------------------------

bool TFacilityCodeManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    LoadEqpmTypeDb(ADbConn);
    LoadFailOccrDb(ADbConn);
    LoadFailCodeDb(ADbConn);
    return true;
}
//---------------------------------------------------------------------------

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

    sQry = "SELECT FAIL_HNDL_CD,     \r\n"
           "       FAIL_HNDL_CD_NM   \r\n"
           "  FROM TB_FAIL_HNDL_TYPE \r\n";

    try
    {
        FHndlLists.Lock();
        FHndlLists.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())
            {
				TFacilityHndlType *pCode = new TFacilityHndlType();

                pCode->FAIL_HNDL_CD    = pADO->FieldByName("FAIL_HNDL_CD")->AsString;       //장애 처리 코드
                pCode->FAIL_HNDL_CD_NM = pADO->FieldByName("FAIL_HNDL_CD_NM")->AsString;    //장애 처리 코드 명
                pCode->Completed = true;
                pCode->Modified  = false;

				FHndlLists.Push(pCode->FAIL_HNDL_CD, pCode);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TFacilityCodeManager::LoadHndlTypeDb", 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("TFacilityCodeManager::LoadHndlTypeDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FHndlLists.UnLock();
    }

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

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

    sQry = "SELECT EQPM_TYPE_CD,     \r\n"
           "       EQPM_TYPE_CD_NM,  \r\n"
           "       FCLT_TYPE         \r\n"
           "  FROM TB_FAIL_EQPM_TYPE \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->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TFacilityEqpmType *pEqpm = new TFacilityEqpmType();

                pEqpm->EQPM_TYPE_CD    = pADO->FieldByName("EQPM_TYPE_CD")->AsString;       //장비 유형 코드
                pEqpm->EQPM_TYPE_CD_NM = pADO->FieldByName("EQPM_TYPE_CD_NM")->AsString;    //장비 유형 코드 명
                pEqpm->FCLT_TYPE       = pADO->FieldByName("FCLT_TYPE")->AsString;          //시설물 유형
                pEqpm->Completed = true;
                pEqpm->Modified  = false;

				FLists.Push(pEqpm->EQPM_TYPE_CD, pEqpm);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TFacilityCodeManager::LoadEqpmTypeDb", 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("TFacilityCodeManager::LoadEqpmTypeDb", 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 TFacilityCodeManager::LoadFailOccrDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.EQPM_TYPE_CD, B.FAIL_OCRR_CD,            \r\n"
           "       MIN(C.FAIL_OCRR_CD_NM) AS FAIL_OCRR_CD_NM  \r\n"
           "  FROM TB_FAIL_EQPM_TYPE A,                       \r\n"
           "       TB_FAIL_DETL_CD   B,                       \r\n"
           "       TB_FAIL_OCRR_DVSN C                        \r\n"
           " WHERE A.EQPM_TYPE_CD = B.EQPM_TYPE_CD            \r\n"
           "   AND B.FAIL_OCRR_CD = C.FAIL_OCRR_CD            \r\n"
           " GROUP BY A.EQPM_TYPE_CD, B.FAIL_OCRR_CD          \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->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String EQPM_TYPE_CD = pADO->FieldByName("EQPM_TYPE_CD")->AsString;          //장비 유형 코드

                TFacilityEqpmType *pEqpm = FLists.Find(EQPM_TYPE_CD);
                if (!pEqpm) continue;

				TFacilityFailOccr *pOccr = new TFacilityFailOccr();

                pOccr->EQPM_TYPE_CD    = EQPM_TYPE_CD;                                      //장비 유형 코드
                pOccr->FAIL_OCRR_CD    = pADO->FieldByName("FAIL_OCRR_CD")->AsString;       //장애 발생 코드
                pOccr->FAIL_OCRR_CD_NM = pADO->FieldByName("FAIL_OCRR_CD_NM")->AsString;    //장애 발생 코드 명
                pOccr->Completed = true;
                pOccr->Modified  = false;

				pEqpm->FLists.Push(pOccr->FAIL_OCRR_CD, pOccr);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TFacilityCodeManager::LoadFailOccrDb", 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("TFacilityCodeManager::LoadFailOccrDb", 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 TFacilityCodeManager::LoadFailCodeDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.EQPM_TYPE_CD, B.FAIL_OCRR_CD, B.FAIL_CD,    \r\n"
           "       MIN(C.FAIL_CD_NM) AS FAIL_CD_NM               \r\n"
           "  FROM TB_FAIL_EQPM_TYPE A,                          \r\n"
           "       TB_FAIL_DETL_CD   B,                          \r\n"
           "       TB_FAIL_TYPE      C                           \r\n"
           " WHERE A.EQPM_TYPE_CD = B.EQPM_TYPE_CD               \r\n"
           "   AND B.FAIL_CD      = C.FAIL_CD                    \r\n"
           " GROUP BY A.EQPM_TYPE_CD, B.FAIL_OCRR_CD, B.FAIL_CD  \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->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String EQPM_TYPE_CD = pADO->FieldByName("EQPM_TYPE_CD")->AsString;          //장비 유형 코드
                TFacilityEqpmType *pEqpm = FLists.Find(EQPM_TYPE_CD);
                if (!pEqpm) continue;

                String FAIL_OCRR_CD = pADO->FieldByName("FAIL_OCRR_CD")->AsString;          //장비 유형 코드
				TFacilityFailOccr *pOccr = pEqpm->FLists.Find(FAIL_OCRR_CD);
                if (!pOccr) continue;

				TFacilityFailCode *pCode = new TFacilityFailCode();

                pCode->EQPM_TYPE_CD = EQPM_TYPE_CD;                              //장비 유형 코드
                pCode->FAIL_OCRR_CD = FAIL_OCRR_CD;                              //장애 발생 코드
                pCode->FAIL_CD      = pADO->FieldByName("FAIL_CD")->AsString;    //장애 코드 명
                pCode->FAIL_CD_NM   = pADO->FieldByName("FAIL_CD_NM")->AsString; //장애 코드 명
                pCode->Completed = true;
                pCode->Modified  = false;

				pOccr->FLists.Push(pCode->FAIL_CD, pCode);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TFacilityCodeManager::LoadFailCodeDb", 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("TFacilityCodeManager::LoadFailCodeDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

