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

#pragma hdrstop

#include "CDSSafeF.h"
#include "CDSCtlrF.h"
#include "VmsUtilsF.h"
#include "VMSCommLibF.h"

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TCDSSafe::TCDSSafe()
{
}
//---------------------------------------------------------------------------

TCDSSafe::~TCDSSafe()
{
}
//---------------------------------------------------------------------------
void TCDSSafe::Init()
{
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TCDSSafeManager *CDSSafeManager = NULL;
//---------------------------------------------------------------------------

TCDSSafeManager::TCDSSafeManager()
{
}
//---------------------------------------------------------------------------
TCDSSafeManager::~TCDSSafeManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
int TCDSSafeManager::LoadSafeOccrInfo(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    int        nSelCnt = 0;

    DDBSTART;

    sQry = "SELECT X.VMS_CTLR_NMBR,                                                                     \r\n"
           "       Y.SAFE_ALM_ID, Y.MSG_TP, Y.RGST_DT, Y.ALM_STRT_DT, Y.ALM_END_DT,                     \r\n"
           "       Y.MSG_LN_1, Y.MSG_LN_2, Y.MSG_LN_3                                                   \r\n"
           "  FROM (SELECT A.VMS_CTLR_NMBR                                                              \r\n"
           "          FROM TB_VMS_CTLR A                                                                \r\n"
           "         WHERE A.DEL_YN = 'N'                                                               \r\n"
           "       ) X,                                                                                 \r\n"
           "       (SELECT B.SAFE_ALM_ID, B.MSG_TP, B.RGST_DT, B.ALM_STRT_DT, B.ALM_END_DT,             \r\n"
           "               B.MSG_LN_1, B.MSG_LN_2, B.MSG_LN_3                                           \r\n"
           "          FROM TB_VMS_SAFE_ALM B                                                            \r\n"
           "         WHERE TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN B.ALM_STRT_DT AND B.ALM_END_DT  \r\n"
           "        ) Y                                                                                 \r\n"
           " ORDER BY X.VMS_CTLR_NMBR, Y.SAFE_ALM_ID, Y.MSG_TP                                          \r\n";

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

		try
		{
            SQLText(pADO, sQry, false);
			SQLOpen(pADO);

			for( ; !pADO->Eof; pADO->Next())
			{
                AnsiString CTLR_NMBR = pADO->FieldByName("VMS_CTLR_NMBR")->AsAnsiString;

                TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(CTLR_NMBR);
                if (!pObj) continue;

                if (!pObj->FProvide) continue;

                if (pObj->SAFE->Count >= VMS_MAX_SAFE)
                {
                    MERROR("VMS Safe Over: %s, %d", CTLR_NMBR.c_str(), pObj->SAFE->Count);
                    continue;
                }

                VMS_SAFE_DETL* pEvt = &pObj->SAFE->Unit[pObj->SAFE->Count];

                pEvt->SAFE_ALM_ID = pADO->FieldByName("SAFE_ALM_ID")->AsString.Trim();  //재난안전 알람 ID(YYYYMMDDNNNNN)
                pEvt->MSG_TP      = pADO->FieldByName("MSG_TP")->AsInteger;             //표출메시지종류(2:2단메시지,3:3단메시지)
                pEvt->RGST_DT     = pADO->FieldByName("RGST_DT")->AsString.Trim();      //등록 일시(YYYYMMDDHH24MISS)
                pEvt->ALM_STRT_DT = pADO->FieldByName("ALM_STRT_DT")->AsString.Trim();  //방송시작시각(YYYYMMDDHH24MISS)
                pEvt->ALM_END_DT  = pADO->FieldByName("ALM_END_DT")->AsString.Trim();   //방송종료시각(YYYYMMDDHH24MISS)
                pEvt->MSG_LN_1    = pADO->FieldByName("MSG_LN_1")->AsString;            //재난안전문자 1열
                pEvt->MSG_LN_2    = pADO->FieldByName("MSG_LN_2")->AsString;            //재난안전문자 2열
                pEvt->MSG_LN_3    = pADO->FieldByName("MSG_LN_3")->AsString;            //재난안전문자 3열

                pObj->SAFE->Count++;

                nSelCnt++;
			}

            if (nSelCnt > 0)
            {
                sQry = "UPDATE TB_VMS_SAFE_ALM                                                          \r\n"
                       "   SET ALM_CNFM = 'Y'                                                           \r\n"
                       " WHERE ALM_CNFM = 'N'                                                           \r\n"
                       "   AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN ALM_STRT_DT AND ALM_END_DT  \r\n";
                pADO->SQL->Clear();
                pADO->SQL->Text = sQry;
                SQLExec(pADO);
            }
		}
		catch(EDatabaseError &E)
		{
            DBERROR(String(E.ClassName()), E.Message, sQry);
		}
		catch(Exception &e)
		{
            DBERROR(String(e.ClassName()), e.Message, sQry);
		}
	}
	__finally
	{
		SQLFree(pADO);
	}

    IDBSTOP(nSelCnt);
    return nSelCnt;
}
//---------------------------------------------------------------------------

