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

#pragma hdrstop

#include "CDSFacilityFailF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsFacilityFail::TItsFacilityFail()
{
	FGridIndex = -1;
	FEditMode  = DB_INIT;
    ActionCnt  = 0;
}
//---------------------------------------------------------------------------
void TItsFacilityFail::Clear()
{
    FAIL_HS_SEQ = "";   //"장애이력일련번호";
    OCRR_DT     = "";   //"발생일시";
    RCPR_NM     = "";   //"접수자명";
    ADTN_NM     = "";   //"추가자명";
    ADTN_DT     = "";   //"추가일시";
    CHNG_NM     = "";   //"변경자명";
    CHNG_DT     = "";   //"변경일시";
    FAIL_TYPE   = "";   //"장애 유형";
    FAIL_CONT   = "";   //"장애 내용";
    FCLT_ID     = "";   //"시설물 ID";
}
//---------------------------------------------------------------------------
TItsFacilityFail::~TItsFacilityFail()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TItsFacilityFailManager *ItsFacilityFailManager = NULL;
//---------------------------------------------------------------------------

/*
* FacilityFail Manager
*/
TItsFacilityFailManager::TItsFacilityFailManager()
{
}
//---------------------------------------------------------------------------
TItsFacilityFailManager::~TItsFacilityFailManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsFacilityFailManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    FLists.RemoveAll();
    LoadFromDbFail(ADbConn);
    LoadFromDbFailAction(ADbConn);
    return true;
}
//---------------------------------------------------------------------------
bool TItsFacilityFailManager::LoadFromDbFail(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *               \r\n"
           "  FROM TB_FCLT_FAIL_HS \r\n";
//           " ORDER BY FAIL_HS_SEQ  \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())
            {
				TItsFacilityFail *pObj = new TItsFacilityFail();

                pObj->FAIL_HS_SEQ = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                pObj->OCRR_DT     = pADO->FieldByName("OCRR_DT")->AsString;
                pObj->RCPR_NM     = pADO->FieldByName("RCPR_NM")->AsString;
                pObj->ADTN_NM     = pADO->FieldByName("ADTN_NM")->AsString;
                pObj->ADTN_DT     = pADO->FieldByName("ADTN_DT")->AsString;
                pObj->CHNG_NM     = pADO->FieldByName("CHNG_NM")->AsString;
                pObj->CHNG_DT     = pADO->FieldByName("CHNG_DT")->AsString;
                pObj->FAIL_TYPE   = pADO->FieldByName("FAIL_TYPE")->AsString;
                pObj->FAIL_CONT   = pADO->FieldByName("FAIL_CONT")->AsString;
                pObj->FCLT_ID     = pADO->FieldByName("FCLT_ID")->AsString;
                pObj->DEL_YN      = "N";
                pObj->ActionCnt   = 0;
				FLists.Push(pObj->FAIL_HS_SEQ, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailManager::LoadFromDbFail", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailManager::LoadFromDbFail", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TItsFacilityFailManager::LoadFromDbFailAction(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *                       \r\n"
           "  FROM TB_FCLT_FAIL_RPAR_HS    \r\n";
//           " ORDER BY FAIL_HS_SEQ, CRTN_DT \r\n";
    try
    {
        ItsFacilityFailManager->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 FAIL_HS_SEQ  = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                TItsFacilityFail *pFail = ItsFacilityFailManager->FLists.Find(FAIL_HS_SEQ);
                if (!pFail) continue;

				TItsFacilityFailAction *pObj = new TItsFacilityFailAction();

                pObj->CRTN_DT      = pADO->FieldByName("CRTN_DT")->AsString;
                pObj->FAIL_HS_SEQ  = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                pObj->WRKR_NM      = pADO->FieldByName("WRKR_NM")->AsString;
                pObj->WORK_CONT    = pADO->FieldByName("WORK_CONT")->AsString;
                pObj->WORK_STRT_DT = pADO->FieldByName("WORK_STRT_DT")->AsString;
                pObj->WORK_END_DT  = pADO->FieldByName("WORK_END_DT")->AsString;
                pObj->WORK_FINS_YN = pADO->FieldByName("WORK_FINS_YN")->AsString;
                pObj->ADTN_NM      = pADO->FieldByName("ADTN_NM")->AsString;
                pObj->CHNG_NM      = pADO->FieldByName("CHNG_NM")->AsString;
                pObj->CHNG_DT      = pADO->FieldByName("CHNG_DT")->AsString;
                pObj->DEL_YN       = "N";

				pFail->FLists.Push(pObj->CRTN_DT, pObj);
                pFail->ActionCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailManager::LoadFromDbFailAction", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailManager::LoadFromDbFailAction", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        ItsFacilityFailManager->FLists.UnLock();
    }

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

TItsFacilityFailAction::TItsFacilityFailAction()
{
	FGridIndex = -1;
	FEditMode  = DB_INIT;
}
//---------------------------------------------------------------------------
void TItsFacilityFailAction::Clear()
{
    CRTN_DT      = "";  //'생성 일시
    FAIL_HS_SEQ  = "";  //'장애 이력 일련번호
    WRKR_NM      = "";  //'작업자 명
    WORK_CONT    = "";  //'작업 내용
    WORK_STRT_DT = "";  //'작업 시작 일시
    WORK_END_DT  = "";  //'작업 종료 일시
    WORK_FINS_YN = "";  //'작업 완료 여부
    ADTN_NM      = "";  //'추가자 명
    CHNG_NM      = "";  //'변경자 명
    CHNG_DT      = "";  //'변경 일시*/
}
//---------------------------------------------------------------------------
TItsFacilityFailAction::~TItsFacilityFailAction()
{
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TItsFacilityFailActionManager *ItsFacilityFailActionManager = NULL;
//---------------------------------------------------------------------------

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

    sQry = "SELECT *                       \r\n"
           "  FROM TB_FCLT_FAIL_RPAR_HS    \r\n";
//           " ORDER BY FAIL_HS_SEQ, CRTN_DT \r\n";
    try
    {
        ItsFacilityFailManager->FLists.Lock();
        FLists.Lock();
        FDataCnt = 0;
        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())
            {
                String FAIL_HS_SEQ  = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                TItsFacilityFail *pFail = ItsFacilityFailManager->FLists.Find(FAIL_HS_SEQ);
                if (!pFail) continue;

				TItsFacilityFailAction *pObj = new TItsFacilityFailAction();

                pObj->CRTN_DT      = pADO->FieldByName("CRTN_DT")->AsString;
                pObj->FAIL_HS_SEQ  = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                pObj->WRKR_NM      = pADO->FieldByName("WRKR_NM")->AsString;
                pObj->WORK_CONT    = pADO->FieldByName("WORK_CONT")->AsString;
                pObj->WORK_STRT_DT = pADO->FieldByName("WORK_STRT_DT")->AsString;
                pObj->WORK_END_DT  = pADO->FieldByName("WORK_END_DT")->AsString;
                pObj->WORK_FINS_YN = pADO->FieldByName("WORK_FINS_YN")->AsString;
                pObj->ADTN_NM      = pADO->FieldByName("ADTN_NM")->AsString;
                pObj->CHNG_NM      = pADO->FieldByName("CHNG_NM")->AsString;
                pObj->CHNG_DT      = pADO->FieldByName("CHNG_DT")->AsString;
                pObj->DEL_YN       = "N";
                pFail->ActionCnt++;

				FLists.Push(String(FDataCnt++), pObj);

				pFail->FLists.Push(pObj->CRTN_DT, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailActionManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailActionManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        ItsFacilityFailManager->FLists.UnLock();
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TItsFacilityFailActionManager::LoadFromDbFailAction(String AKeySeq, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *                       \r\n"
           "  FROM TB_FCLT_FAIL_RPAR_HS    \r\n"
           " WHERE FAIL_HS_SEQ = :p01      \r\n";
//           " ORDER BY FAIL_HS_SEQ, CRTN_DT \r\n";
    try
    {
        ItsFacilityFailManager->FLists.Lock();
        FLists.Lock();
        FDataCnt = 0;
        FLists.RemoveAll();
        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 = AKeySeq;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String FAIL_HS_SEQ  = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                TItsFacilityFail *pFail = ItsFacilityFailManager->FLists.Find(FAIL_HS_SEQ);
                if (!pFail) continue;

				TItsFacilityFailAction *pObj = new TItsFacilityFailAction();

                pObj->CRTN_DT      = pADO->FieldByName("CRTN_DT")->AsString;
                pObj->FAIL_HS_SEQ  = pADO->FieldByName("FAIL_HS_SEQ")->AsString;
                pObj->WRKR_NM      = pADO->FieldByName("WRKR_NM")->AsString;
                pObj->WORK_CONT    = pADO->FieldByName("WORK_CONT")->AsString;
                pObj->WORK_STRT_DT = pADO->FieldByName("WORK_STRT_DT")->AsString;
                pObj->WORK_END_DT  = pADO->FieldByName("WORK_END_DT")->AsString;
                pObj->WORK_FINS_YN = pADO->FieldByName("WORK_FINS_YN")->AsString;
                pObj->ADTN_NM      = pADO->FieldByName("ADTN_NM")->AsString;
                pObj->CHNG_NM      = pADO->FieldByName("CHNG_NM")->AsString;
                pObj->CHNG_DT      = pADO->FieldByName("CHNG_DT")->AsString;
                pObj->DEL_YN       = "N";
                pFail->ActionCnt++;

				FLists.Push(String(FDataCnt++), pObj);

				pFail->FLists.Push(pObj->CRTN_DT, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailActionManager::LoadFromDbFailAction", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsFacilityFailActionManager::LoadFromDbFailAction", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        ItsFacilityFailManager->FLists.UnLock();
        FLists.UnLock();
    }

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


