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


#pragma hdrstop

#include "CDSProcessF.h"
#include "CDSCtlrF.h"
#include "VMSCommLibF.h"

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

#pragma package(smart_init)

TCDSProcess::TCDSProcess()
{
}
//---------------------------------------------------------------------------
TCDSProcess::~TCDSProcess()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCDSProcessManager *CDSProcessManager = NULL;
//---------------------------------------------------------------------------

/*
* Process Manager
*/
TCDSProcessManager::TCDSProcessManager()
{
    FUnitStts5Min = -1;
}
//---------------------------------------------------------------------------
TCDSProcessManager::~TCDSProcessManager()
{
}
//---------------------------------------------------------------------------
int TCDSProcessManager::LoadUnitInfo(TADOConnection *ADbConn)
{
    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"
           " WHERE A.DEL_YN = 'N'  \r\n";

    try
    {
        FLists.Lock();
        FLists.RemoveAll();
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
				TCDSProcess *pObj = new TCDSProcess();

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

				FLists.Push(pObj->SYST_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        FLists.UnLock();
        SQLFree(pADO);
    }

    return FLists.Size();
}
//---------------------------------------------------------------------------
int TCDSProcessManager::SaveUnitStts(TADOConnection *ADbConn)
{
	int        nSelCnt = 0;
	AnsiString sQry;
	TADOQuery *pADO = NULL;

    DDBSTART;

    // 1분주기 상태정보 업데이트, 5분마다 이력정보를 입력한다.
    bool bSaveHist = false;
    int nCurrMin = StrToInt(Now().FormatString("nn"));
    if ((nCurrMin % 5) == 0) //매 5분마다 무조건 입력
    {
        if (FUnitStts5Min != nCurrMin)
        {
            bSaveHist = true;
            FUnitStts5Min = nCurrMin;
        }
    }

    String UPDT_DT = Now().FormatString("yyyymmddhhnnss");

    sQry = "UPDATE TB_UNIT_SYST_STTS    \r\n"
           "   SET UPDT_DT      = :p03, \r\n"
           "       SYST_STTS_CD = :p02  \r\n"
           " WHERE SYST_ID = :p01       \r\n";

    AnsiString SYST_ID      = g_AppCfg.sSystemId;
    AnsiString SYST_STTS_CD = "SPS1";

	try
	{
		try
		{
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, false);

            if (g_SysInfo->RunSts == state_error)
            {
                SYST_STTS_CD = "SPS3";
            }

            SQLBind(pADO, "p01", SYST_ID);
            SQLBind(pADO, "p02", SYST_STTS_CD);
            SQLBind(pADO, "p03", UPDT_DT);
            nSelCnt = SQLExec(pADO);

            if (bSaveHist)
            {
    sQry = "INSERT INTO TB_UNIT_SYST_STTS_HS(CRTN_DT, SYST_ID, SYST_STTS_CD) \r\n"
           "   VALUES(:p03, :p01, :p02)      \r\n";

    			SQLText(pADO, sQry, false);
                SQLBind(pADO, "p01", SYST_ID);
                SQLBind(pADO, "p02", SYST_STTS_CD);
                SQLBind(pADO, "p03", UPDT_DT);
                SQLExec(pADO);
            }
		}
		catch(EDatabaseError &E)
		{
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
		}
	}
	__finally
	{
        SQLFree(pADO);
	}

    MDEBUG("DCOM PROCESS_STTS, SYST_ID: %s, STTS_CD: %s", SYST_ID.c_str(), SYST_STTS_CD.c_str());

    INT_PG_STATE_RES PgState;

    memset(&PgState, 0x00, sizeof(PgState));

    PgState.Count = 1;
    PgState.Unit[0].Type    = INT_ID_VMS_SERVER;
    PgState.Unit[0].DB      = g_SysInfo->DbmSts;
    PgState.Unit[0].Comm    = g_SysInfo->ComSts;
    PgState.Unit[0].Action  = g_SysInfo->RunSts;

    CComm_CmmProcessStateNotify(&PgState);

    IDBSTOP(nSelCnt);

    return nSelCnt;
}
//---------------------------------------------------------------------------
#if 0
int TCDSProcessManager::SaveUnitSttsHs(TADOConnection *ADbConn)
{
	int         nSelCnt;
	AnsiString  sQry;
	TADOQuery  *pADO = NULL;

    DDBSTART;

    sQry = "INSERT INTO TB_UNIT_SYST_STTS_HS(CRTN_DT, SYST_ID, SYST_STTS_CD) \r\n"
           "   VALUES(TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS'), :p01, :p02)      \r\n";

    AnsiString SYST_ID      = g_AppCfg.sSystemId;
    AnsiString SYST_STTS_CD = "SPS1";
	try
	{
		try
		{
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, false);

            if (g_SysInfo->RunSts == state_error)
            {
                SYST_STTS_CD = "SPS3";
            }

            SQLBind(pADO, "p01", SYST_ID);
            SQLBind(pADO, "p02", SYST_STTS_CD);
            nSelCnt = SQLExec(pADO);
		}
		catch(EDatabaseError &E)
		{
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
		}
	}
	__finally
	{
        SQLFree(pADO);
	}

    MDEBUG("DCOM PROCESS_STTS, SYST_ID: %s, STTS_CD: %s", SYST_ID.c_str(), SYST_STTS_CD.c_str());
    IDBSTOP(nSelCnt);
    return nSelCnt;
}
//---------------------------------------------------------------------------
#endif

