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

#pragma hdrstop

#include "CDSProcessF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsProcess::TItsProcess()
{
    FCommStatus = "CMS2";     // 장비 통신상태값
    FDoorStatus = "CMS2";     // 장비 Door상태값

	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	FMEM_DEL   = "N";

    Clear();
}
//---------------------------------------------------------------------------
void TItsProcess::Clear()
{
#if 0
    SYST_ID   = "";
    SYST_TYPE = "";
    SYST_NMBR = "";
    SYST_NM   = "";
    SYST_IP_1 = "";
    SYST_IP_2 = "";
    PRGM_PORT = "";
    HOST_NM   = "";
    OS_NM     = "";
    DEL_YN    = "N";
    STTS_DSPL_YN = "N";

    CommStatus = "CMS2";   // 장비 통신상태값
    DoorStatus = "CMS2";   // 장비 Door상태값

	GridIndex  = -1;	    // 그리드 인덱스

#endif
}
//---------------------------------------------------------------------------
TItsProcess::~TItsProcess()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsProcessManager *ItsProcessManager = NULL;
//---------------------------------------------------------------------------

/*
* Process Manager
*/
TItsProcessManager::TItsProcessManager()
{
}
//---------------------------------------------------------------------------
TItsProcessManager::~TItsProcessManager()
{
}
//---------------------------------------------------------------------------
bool TItsProcessManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.SYST_ID  ,    \r\n"
           "       A.SYST_TYPE,    \r\n"
           "       A.SYST_NMBR,    \r\n"
           "       A.SYST_NM  ,    \r\n"
           "       A.SYST_IP_1,    \r\n"
           "       A.SYST_IP_2,    \r\n"
           "       A.PRGM_PORT,    \r\n"
           "       A.HOST_NM  ,    \r\n"
           "       A.OS_NM    ,    \r\n"
           "       A.STTS_DSPL_YN, \r\n"
           "       A.DEL_YN        \r\n"
           "  FROM TB_UNIT_SYST A  \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())
            {
				TItsProcess *pObj = new TItsProcess();

                pObj->SYST_ID    = pADO->FieldByName("SYST_ID")->AsString;
                pObj->SYST_TYPE  = pADO->FieldByName("SYST_TYPE")->AsString;
                pObj->SYST_NMBR  = pADO->FieldByName("SYST_NMBR")->AsString;
                pObj->SYST_NM    = pADO->FieldByName("SYST_NM")->AsString;
                pObj->SYST_IP_1  = pADO->FieldByName("SYST_IP_1")->AsString;
                pObj->SYST_IP_2  = pADO->FieldByName("SYST_IP_2")->AsString;
                pObj->PRGM_PORT  = pADO->FieldByName("PRGM_PORT")->AsString;
                pObj->HOST_NM    = pADO->FieldByName("HOST_NM")->AsString;
                pObj->OS_NM      = pADO->FieldByName("OS_NM")->AsString;
                pObj->DEL_YN     = pADO->FieldByName("DEL_YN")->AsString;
                pObj->STTS_DSPL_YN = pADO->FieldByName("STTS_DSPL_YN")->AsString;

                pObj->GridIndex  = -1;
                pObj->CommStatus = "0";

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

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

    sQry = "SELECT SYST_ID,                                                \r\n"
           "       DECODE(SYST_STTS_CD, 'SPS1', '1',                       \r\n"
           "                            'SPS2', '1',                       \r\n"
           "                            'SPS4', '2',                       \r\n"
           "                                    '0') AS COMMSTATUS         \r\n"
           "  FROM TB_UNIT_SYST_STTS                                       \r\n"
           " WHERE UPDT_DT >= TO_CHAR(SYSDATE-10/1440, 'YYYYMMDDHH24MISS') \r\n";

    ItsProcessManager->FLists.Lock();
    try
    {
        TItsProcess *pProcess = NULL;
        try
        {
            FOR_STL(TItsProcess*, pObj, ItsProcessManager->FLists)
            {
                pObj->FCommStatus = "0";
            }

            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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sId = pADO->FieldByName("SYST_ID")->AsString;
                pProcess = ItsProcessManager->FLists.Find(sId);
                if (!pProcess) continue;

                pProcess->FCommStatus = pADO->FieldByName("COMMSTATUS")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsProcessManager::LoadProcessStatusFromDb", String(E.ClassName()), E.Message, sQry);
            return false;
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsProcessManager::LoadProcessStatusFromDb", String(exception.ClassName()), exception.Message, sQry);
            return false;
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        ItsProcessManager->FLists.UnLock();
    }

    return bResult;
}
//---------------------------------------------------------------------------


