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

#pragma hdrstop

#include "CDSEventF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsEvent::TItsEvent()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";

    //Clear();
}
//---------------------------------------------------------------------------
void TItsEvent::Clear()
{
}
//---------------------------------------------------------------------------
TItsEvent::~TItsEvent()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsEventManager *ItsEventManager = NULL;
//---------------------------------------------------------------------------

/*
* Event Manager
*/
TItsEventManager::TItsEventManager()
{
}
//---------------------------------------------------------------------------
TItsEventManager::~TItsEventManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

bool TItsEventManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    try
    {
        FLists.Lock();
        FEventId = 0;
        FLists.RemoveAll();
    }
    __finally
    {
        FLists.UnLock();
    }

    LoadBlackBoxEvent(ADbConn);

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

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

    sQry = "SELECT A.*,                                                      \r\n"
           "       B.OCRR_DT,                                                \r\n"
           "       B.TRMN_ID,                                                \r\n"
           "       B.EVT_TYPE,                                               \r\n"
           "       DECODE(B.EVT_TYPE, '1', '1.주기보고',                     \r\n"
           "                          '2', '2.사용자전송',                   \r\n"
           "                          '3', '3.긴급전송',                     \r\n"
           "                          '---') AS EVT_DESC,                    \r\n"
           "       B.X_CRDN,                                                 \r\n"
           "       B.Y_CRDN,                                                 \r\n"
           "       B.GPS_DRCT,                                               \r\n"
           "       B.SPED,                                                   \r\n"
           "       B.VHRN_MODE,                                              \r\n"
           "       DECODE(B.VHRN_MODE, '1', '1.주행모드',                    \r\n"
           "                           '2', '2.주차모드',                    \r\n"
           "                           '---') AS VHRN_DESC,                  \r\n"
           "       B.TRMN_VER,                                               \r\n"
           "       B.IMGN_PATH, B.LINK_ID                                    \r\n"
           "  FROM TB_BDS_TRMN     A,                                        \r\n"
           "       TB_BDS_INCD_CLCT B                                        \r\n"
           " WHERE A.BDS_MNGM_NMBR = B.BDS_MNGM_NMBR                         \r\n"
           "   AND B.OCRR_DT >= TO_CHAR(SYSDATE-10/1440, 'YYYYMMDDHH24MISS') \r\n"
           " ORDER BY B.OCRR_DT DESC                                         \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())
            {
				TItsEvent *pObj = new TItsEvent();

                pObj->BDS_MNGM_NMBR = pADO->FieldByName("BDS_MNGM_NMBR")->AsString;
                pObj->OPNC_NMBR     = pADO->FieldByName("OPNC_NMBR")->AsString;
                pObj->MDM_NMBR      = pADO->FieldByName("MDM_NMBR")->AsString;
                pObj->USIM_NMBR     = pADO->FieldByName("USIM_NMBR")->AsString;
                pObj->VHCL_NMBR     = pADO->FieldByName("VHCL_NMBR")->AsString;
                pObj->OWNR_NM       = pADO->FieldByName("OWNR_NM")->AsString;
                pObj->PRCT_NM       = pADO->FieldByName("PRCT_NM")->AsString;

                pObj->OCRR_DT       = pADO->FieldByName("OCRR_DT")->AsString;
                //String BDS_MNGM_NMBR;   //	N	VARCHAR2(20)	N			블랙박스 관리 번호
                pObj->TRMN_ID       = pADO->FieldByName("TRMN_ID")->AsString;
                pObj->EVT_TYPE      = pADO->FieldByName("EVT_DESC")->AsString;
                pObj->X_CRDN        = pADO->FieldByName("X_CRDN")->AsString;
                pObj->Y_CRDN        = pADO->FieldByName("Y_CRDN")->AsString;
                pObj->GPS_DRCT      = pADO->FieldByName("GPS_DRCT")->AsString;
                pObj->SPED          = pADO->FieldByName("SPED")->AsString;
                pObj->VHRN_MODE     = pADO->FieldByName("VHRN_DESC")->AsString;
                pObj->TRMN_VER      = pADO->FieldByName("TRMN_VER")->AsString;
                pObj->IMGN_PATH     = pADO->FieldByName("IMGN_PATH")->AsString;
                pObj->LINK_ID       = pADO->FieldByName("LINK_ID")->AsString;
                if (pObj->LINK_ID.Trim() == "0") pObj->LINK_ID = "";

                pObj->ID.printf(L"%05d", ++FEventId);
				FLists.Push(pObj->ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsEventManager::LoadBlackBoxEvent", 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("TItsEventManager::LoadBlackBoxEvent", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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


