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

#pragma hdrstop

#include "CDSUtisF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsUtis::TItsUtis()
{
	FGridIndex  = -1;	    // 그리드 인덱스
    Clear();
}
//---------------------------------------------------------------------------
void TItsUtis::Clear()
{
#if 0

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

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


//---------------------------------------------------------------------------
TItsUtisManager *ItsUtisManager = NULL;
//---------------------------------------------------------------------------

/*
* Utis Manager
*/
TItsUtisManager::TItsUtisManager()
{
}
//---------------------------------------------------------------------------
TItsUtisManager::~TItsUtisManager()
{
}
//---------------------------------------------------------------------------
void TItsUtisManager::SetCenterId(String ACenterId)
{
    CenterId = ACenterId;
}
//---------------------------------------------------------------------------

bool TItsUtisManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    return LoadUtisMaster(ADbConn);
}
//---------------------------------------------------------------------------
bool TItsUtisManager::LoadUtisMaster(TADOConnection *ADbConn/*=NULL*/)
{
    for (int ii = 0; ii < MAX_UTIS_RUN; ii++)
    {
        UtisRun.Status[ii] = "0";
        UtisRun.TotCnt[ii] = 0;
        UtisRun.RunCnt[ii] = 0;
        UtisRun.LastTime[ii] = "";
        RseRun[ii] = 0;
    }

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT COUNT(1) AS CCTVCNT \r\n"
           "  FROM TB_CCTV_CTLR        \r\n"
           " WHERE DEL_YN = 'N'        \r\n";

    FTotCctvCnt = 0;
    try
    {
        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())
            {
                 FTotCctvCnt = pADO->FieldByName("CCTVCNT")->AsInteger;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsUtisManager::LoadUtisMaster", 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("TItsUtisManager::LoadUtisMaster", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------
bool TItsUtisManager::LoadUtisStatusFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    try
    {
        ItsUtisManager->FLists.Lock();
        for (int ii = 0; ii < MAX_UTIS_RUN; ii++)
        {
            UtisRun.Status[ii] = "0";
            UtisRun.TotCnt[ii] = 0;
            UtisRun.RunCnt[ii] = 0;
            UtisRun.LastTime[ii] = "";
            RseRun[ii] = 0;
        }

        LoadUtisStatusUtisRun(ADbConn);
        LoadUtisStatusRseRun(ADbConn);
    }
    __finally
    {
        ItsUtisManager->FLists.UnLock();
    }
    return true;
}
//---------------------------------------------------------------------------
bool TItsUtisManager::LoadUtisStatusUtisRun(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

//
return true;

    sQry = "SELECT OPCODE, TO_CHAR(COLLDT, 'YYYY-MM-DD HH24:MI:SS') AS COLLDT, RECORDCNT,                    \r\n"
           "       DECODE(OPCODE, '20', DECODE(SIGN(COLLDT - (SYSDATE - 9/60/24)), -1, 'X', 'O'),            \r\n"
           "                            DECODE(SIGN(COLLDT - (SYSDATE - 9/60/24)), -1, 'X', 'O')) AS COMMCHK \r\n"
           "  FROM RUTIS.CENTERCOMM                                                                          \r\n"
           " WHERE COMMTYPE = '1'                                                                            \r\n"
           "   AND OPCODE IN ('38', '39', '20')                                                              \r\n";

#if 0
SELECT COUNT(1)
  FROM RUTIS.CCTVIMG_BLOB A,
       (SELECT CCTVID,
               MAX(COLLDT) AS COLLDT
          FROM RUTIS.CCTVIMG_BLOB
         WHERE COLLDT >= (SYSDATE - 9/60/24)
         GROUP BY CCTVID) B,
       TB_CCTV_CTLR C
 WHERE A.CCTVID = B.CCTVID
   AND A.COLLDT = B.COLLDT
   AND A.CCTVID = TRIM(C.CCTV_CTLR_ID)
   AND C.DEL_YN = 'N'
#endif

    String sTrfTime = "-?-";
    String sDirTime = "-?-";
    String sImgTime = "-?-";
    int    nTrfCnt = 0;
    int    nDirCnt = 0;
    int    nImgCnt = 0;
    bool bTrf = false;
    bool bDir = false;
    bool bImg = false;

    try
    {
        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 sOpCode = pADO->FieldByName("OPCODE")->AsString;
                if (sOpCode == "38")    // 5분 교통정보 수신(센터)
                {
                    String sCommChk = pADO->FieldByName("COMMCHK")->AsString;
                    if (sCommChk == "O")
                    {
                        bTrf = true;
                    }
                    sTrfTime = pADO->FieldByName("COLLDT")->AsString;
                    nTrfCnt  = pADO->FieldByName("RECORDCNT")->AsInteger;
                }
                else
                if (sOpCode == "39")    // 5분 방향별 교통정보 수신(센터)
                {
                    String sCommChk = pADO->FieldByName("COMMCHK")->AsString;
                    if (sCommChk == "O")
                    {
                        bDir = true;
                    }
                    sDirTime = pADO->FieldByName("COLLDT")->AsString;
                    nDirCnt  = pADO->FieldByName("RECORDCNT")->AsInteger;
                }
                else
                if (sOpCode == "20")   // 정지영상 생성
                {
                    String sCommChk = pADO->FieldByName("COMMCHK")->AsString;
                    if (sCommChk == "O")
                    {
                        bImg = true;
                    }
                    sImgTime = pADO->FieldByName("COLLDT")->AsString;
                    nImgCnt  = pADO->FieldByName("RECORDCNT")->AsInteger;
                }
            }

            if (bTrf)
            {
                UtisRun.Status[1] = "1";
                UtisRun.TotCnt[1] = 0;
                UtisRun.RunCnt[1] = nTrfCnt;
                UtisRun.LastTime[1] = sTrfTime;
            }
            else
            {
                UtisRun.Status[1] = "0";
                UtisRun.TotCnt[1] = 0;
                UtisRun.RunCnt[1] = 0;
                UtisRun.LastTime[1] = sTrfTime;
            }

            if (bDir)
            {
                UtisRun.Status[2] = "1";
                UtisRun.TotCnt[2] = 0;
                UtisRun.RunCnt[2] = nDirCnt;
                UtisRun.LastTime[2] = sTrfTime;
            }
            else
            {
                UtisRun.Status[2] = "0";
                UtisRun.TotCnt[2] = 0;
                UtisRun.RunCnt[2] = 0;
                UtisRun.LastTime[2] = sDirTime;
            }

            if (bImg)
            {
                UtisRun.Status[0] = "1";
                UtisRun.TotCnt[0] = nImgCnt;//m_nCctvCnt;

                if (nImgCnt > FTotCctvCnt)
                    nImgCnt = FTotCctvCnt;

                UtisRun.RunCnt[0] = nImgCnt;
                UtisRun.LastTime[0] = sImgTime;
                //if (m_nCctvCnt != nImgCnt)
                //{
                //    UtisRun.Status[0] = 2;
                //}
            }
            else
            {
                UtisRun.Status[0] = "0";
                UtisRun.TotCnt[0] = 0;//nImgCnt;//m_nCctvCnt;
                UtisRun.RunCnt[0] = 0;//nImgCnt;
                UtisRun.LastTime[0] = sImgTime;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsUtisManager::LoadUtisStatusUtisRun", 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("TItsUtisManager::LoadUtisStatusUtisRun", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

return true;
    sQry = "SELECT 0 AS COLTYPE, COUNT(1) AS UPCNT    \r\n"
           "  FROM RUTIS.RSEMST                       \r\n"
           "UNION ALL                                 \r\n"
           "SELECT 1 AS COLTYPE, COUNT(1) AS UPCNT    \r\n"
           "  FROM RUTIS.RSEOPSTATE A, RUTIS.RSEMST B \r\n"
           " WHERE B.RSEID = A.RSEID                  \r\n"
           "   AND A.COLLDT > SYSDATE - 30/1440       \r\n";

    try
    {
        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())
            {
                int nColType = pADO->FieldByName("COLTYPE")->AsInteger;
                if (nColType >= 0 && nColType < MAX_UTIS_RUN)
                {
                    RseRun[nColType] = pADO->FieldByName("UPCNT")->AsInteger;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsUtisManager::LoadUtisStatusRseRun", 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("TItsUtisManager::LoadUtisStatusRseRun", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        ItsUtisManager->FLists.UnLock();
    }

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


