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

#pragma hdrstop

#include "CDSCrossF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)


//---------------------------------------------------------------------------
void TCrossCam::Copy(TCrossCam *AObj)
{
    if (AObj)
    {
        AObj->CROSS_ID  = CROSS_ID;
        AObj->CAM_ID    = CAM_ID;
        AObj->CAM_NM    = CAM_NM;

        AObj->DIRECTION = DIRECTION;
        AObj->X_CRDN    = X_CRDN;
        AObj->Y_CRDN    = Y_CRDN;
        AObj->ANGLE     = ANGLE;

        AObj->IP        = IP;
        AObj->ID        = ID;
        AObj->PWD       = PWD;
        AObj->RTSP_URL  = RTSP_URL;
        AObj->RTSP_PORT = RTSP_PORT;
        AObj->HTTP_PORT = HTTP_PORT;

        AObj->TYPE_CD   = TYPE_CD;
        AObj->DEL_YN    = DEL_YN;

        AObj->CTLR_ID  = CTLR_ID;

        AObj->STRM_ADDR = RTSP_URL;
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCross::TCross()
{
}
//---------------------------------------------------------------------------

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

//---------------------------------------------------------------------------
TCrossManager *CrossManager = NULL;
//---------------------------------------------------------------------------

TCrossManager::TCrossManager()
{
}
//---------------------------------------------------------------------------
TCrossManager::~TCrossManager()
{
    FLists.RemoveAll();
    FCamLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TCrossManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*           \r\n"
           "  FROM TB_CRS 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())
            {
				TCross *pObj = new TCross();

                pObj->CROSS_ID       = pADO->FieldByName("CROSS_ID")->AsString;
                pObj->CTLR_ID        = pObj->CROSS_ID;
                pObj->CROSS_NM       = pADO->FieldByName("NAME")->AsString.Trim();
                pObj->CROSS_TYPE     = pADO->FieldByName("CROSS_TYPE")->AsString.Trim();
                pObj->X_CRDN         = pADO->FieldByName("X")->AsFloat;
                pObj->Y_CRDN         = pADO->FieldByName("Y")->AsFloat;
                pObj->ZOOM_LEVEL     = pADO->FieldByName("ZOOM_LEVEL")->AsInteger;
                pObj->LOCAL_GROUP_ID = pADO->FieldByName("LOCAL_GROUP_ID")->AsString.Trim();

                pObj->TYPE_CD        = "CRS";
                pObj->DEL_YN         = pADO->FieldByName("DEL_YN")->AsString.Trim();

				FLists.Push(pObj->CROSS_ID, pObj);
                pObj->Completed  = true;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TCrossManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TCrossManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return LoadCrossCam(ADbConn);
}
//---------------------------------------------------------------------------

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

    FLists.Lock();
    try
    {
        FOR_STL(TCross*, pObj, FLists)
        {
            pObj->FLists.RemoveAll();
        }
    }
    __finally
    {
        FLists.UnLock();
    }

    sQry = "SELECT A.CROSS_ID, B.CAM_ID, B.NAME, B.DEL_YN, C.DIRECTION, C.X, C.Y, C.ANGLE,  \r\n"
           "       B.IP, B.ID, B.PW, B.RTSP_URL, B.RTSP_PORT, B.HTTP_PORT, B.RIGHT_USE      \r\n"
           "  FROM TB_CRS A, TB_CRS_CMRA B, TB_CRS_CMRA_DIR C                               \r\n"
           " WHERE A.CROSS_ID = B.CROSS_ID                                                  \r\n"
           "   AND B.CAM_ID = C.CAM_ID                                                      \r\n"
           " ORDER BY A.CROSS_ID, B.CAM_ID                                                  \r\n";

    try
    {
        FLists.Lock();
        FCamLists.Lock();
        FCamLists.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())
            {
                String CROSS_ID = pADO->FieldByName("CROSS_ID")->AsString;
				TCross *pCross = FLists.Find(String(CROSS_ID));
                if (!pCross) continue;

                TCrossCam *pObj = new TCrossCam();
                if (pObj)
                {
                    pObj->CROSS_ID = CROSS_ID;
                    pObj->CAM_ID   = pADO->FieldByName("CAM_ID")->AsString;
                    pObj->CTLR_ID  = pObj->CAM_ID;
                    pObj->CAM_NM   = pADO->FieldByName("NAME")->AsString;

                    pObj->DIRECTION = pADO->FieldByName("DIRECTION")->AsString.Trim();
                    pObj->X_CRDN    = pADO->FieldByName("X")->AsFloat;
                    pObj->Y_CRDN    = pADO->FieldByName("Y")->AsFloat;
                    pObj->ANGLE     = pADO->FieldByName("ANGLE")->AsInteger;

                    pObj->IP        = pADO->FieldByName("IP")->AsString.Trim();
                    pObj->ID        = pADO->FieldByName("ID")->AsString.Trim();
                    pObj->PWD       = pADO->FieldByName("PW")->AsString.Trim();
                    pObj->RTSP_URL  = pADO->FieldByName("RTSP_URL")->AsString.Trim();
                    pObj->RTSP_PORT = pADO->FieldByName("RTSP_PORT")->AsInteger;
                    pObj->HTTP_PORT = pADO->FieldByName("HTTP_PORT")->AsInteger;

                    if (pObj->RTSP_URL == "<NULL>" || pObj->RTSP_URL == "NULL")
                    {
                        pObj->RTSP_URL = "";
                    }

                    pObj->STRM_ADDR = pObj->RTSP_URL;

                    pObj->TYPE_CD   = "CRSCAM";
                    pObj->DEL_YN    = pADO->FieldByName("DEL_YN")->AsString.Trim();

                    pCross->FLists.Push(pObj->CAM_ID, pObj);

                    TCrossCam *pObj2 = new TCrossCam();
                    pObj->Copy(pObj2);
                    FCamLists.Push(pObj2->CAM_ID, pObj2);
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TCrossManager::LoadCrossCam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TCrossManager::LoadCrossCam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
        FCamLists.UnLock();
    }

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

bool TCrossManager::LoadStatusFromDb(TADOConnection *ADbConn/*=NULL*/)
{
#if 0
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *                                                        \r\n"
           "  FROM TB_WCAM_CTLR_STTS A,                                     \r\n"
           "       TB_WCAM_CTLR      B                                      \r\n"
           " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR                      \r\n"
           "   AND A.UPDT_DT >= TO_CHAR(SYSDATE-2/1440, 'YYYYMMDDHH24MISS') \r\n";

    FLists.Lock();
    try
    {
        FOR_STL(TCross*, pTmp, FLists)
        {
            pTmp->STATE.Comm = wcam_error;
        }
        TCross *pObj = NULL;
        try
        {
            int nORD;

            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 WCAM_CTLR_NMBR = pADO->FieldByName("WCAM_CTLR_NMBR")->AsString;
                pObj = FLists.Find(WCAM_CTLR_NMBR);
                if (!pObj) continue;

                String UPDT_DT      = pADO->FieldByName("UPDT_DT")->AsString;
                String CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                if (CMNC_STTS_CD != "CMS0")
                {
                    //Åë½ÅÀå¾Ö
                    continue;
                }
                pObj->STATE.Comm = (CMNC_STTS_CD == "CMS0") ? wcam_normal : wcam_error;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::LoadStatusFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        FLists.UnLock();

        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
#endif

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

bool TCrossManager::DeleteCross(TCross *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_CRS           \r\n"
           "   SET DEL_YN = 'Y'     \r\n"
           " WHERE CROSS_ID = :p01  \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->Parameters->ParamByName("p01")->Value = AObj->CROSS_ID;
			pADO->ExecSQL();

        sQry = "DELETE TB_FCLT_INFR      \r\n"
               " WHERE FCLT_ID = :p01    \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->CROSS_ID;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::DeleteCross", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::DeleteCross", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------

bool TCrossManager::InsertCross(TCross *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeCross(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TCrossManager::UpdateCross(TCross *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeCross(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TCrossManager::MergeCross(TCross *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_CRS L                                 \r\n"
           "USING (SELECT :p01 AS CROSS_ID,                     \r\n"
           "              :p02 AS NAME,                         \r\n"
           "              :p03 AS CROSS_TYPE,                   \r\n"
           "              :p04 AS X,                            \r\n"
           "              :p05 AS Y,                            \r\n"
           "              :p06 AS ZOOM_LEVEL,                   \r\n"
           "              :p07 AS LOCAL_GROUP_ID,               \r\n"
           "              :p08 AS DEL_YN                        \r\n"
           "         FROM DUAL) M                               \r\n"
           "        ON (L.CROSS_ID = M.CROSS_ID)                \r\n"
           "WHEN MATCHED THEN                                   \r\n"
           "   UPDATE SET L.NAME            = M.NAME,           \r\n"
           "              L.CROSS_TYPE      = M.CROSS_TYPE,     \r\n"
           "              L.X               = M.X,              \r\n"
           "              L.Y               = M.Y,              \r\n"
           "              L.ZOOM_LEVEL      = M.ZOOM_LEVEL,     \r\n"
           "              L.LOCAL_GROUP_ID  = M.LOCAL_GROUP_ID, \r\n"
           "              L.DEL_YN          = M.DEL_YN          \r\n"
           "WHEN NOT MATCHED THEN                               \r\n"
           "   INSERT (CROSS_ID,                                \r\n"
           "           NAME,                                    \r\n"
           "           CROSS_TYPE,                              \r\n"
           "           X,                                       \r\n"
           "           Y,                                       \r\n"
           "           ZOOM_LEVEL,                              \r\n"
           "           LOCAL_GROUP_ID,                          \r\n"
           "           DEL_YN )                                 \r\n"
           "   VALUES (M.CROSS_ID,                              \r\n"
           "           M.NAME,                                  \r\n"
           "           M.CROSS_TYPE,                            \r\n"
           "           M.X,                                     \r\n"
           "           M.Y,                                     \r\n"
           "           M.ZOOM_LEVEL,                            \r\n"
           "           M.LOCAL_GROUP_ID,                        \r\n"
           "           M.DEL_YN )                               \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->Parameters->ParamByName("p01")->Value = AObj->CROSS_ID;
            pADO->Parameters->ParamByName("p02")->Value = AObj->CROSS_NM;
            pADO->Parameters->ParamByName("p03")->Value = AObj->CROSS_TYPE;
            pADO->Parameters->ParamByName("p04")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p05")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p06")->Value = AObj->ZOOM_LEVEL;
            pADO->Parameters->ParamByName("p07")->Value = AObj->LOCAL_GROUP_ID;
            pADO->Parameters->ParamByName("p08")->Value = AObj->DEL_YN;

			pADO->ExecSQL();

            return MergeFcltInfo(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::MergeWCam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::MergeWCam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TCrossManager::MergeFcltInfo(TCross *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_FCLT_INFR L                                         \r\n"
           "USING (SELECT :p01 AS FCLT_ID,                                    \r\n"
           "              :p02 AS FCLT_TYPE,                                  \r\n"
           "              :p03 AS FCLT_LCTN                                   \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.FCLT_ID = M.FCLT_ID AND L.FCLT_TYPE = M.FCLT_TYPE)  \r\n"
           "WHEN MATCHED                                                      \r\n"
           "THEN                                                              \r\n"
           "   UPDATE SET L.FCLT_LCTN = M.FCLT_LCTN,                          \r\n"
           "              L.DEL_YN    = 'N'                                   \r\n"
           "WHEN NOT MATCHED                                                  \r\n"
           "THEN                                                              \r\n"
           "   INSERT (  FCLT_ID,   FCLT_TYPE,   FCLT_LCTN)                   \r\n"
           "   VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN)                   \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->Parameters->ParamByName("p01")->Value = AObj->CROSS_ID;
			pADO->Parameters->ParamByName("p02")->Value = "CRS";
			pADO->Parameters->ParamByName("p03")->Value = AObj->CROSS_NM;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::MergeFcltInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::MergeFcltInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TCrossManager::GetNextCrossId(int &ANmbr, int &AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(CROSS_ID), 0) + 1 AS NEWID FROM TB_CRS \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();
            ANmbr = pADO->FieldByName("NEWID")->AsInteger;
            AId = ANmbr;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::GetNextCrossId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::GetNextCrossId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TCrossManager::SaveCrossCam(TCrossCam *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeCrossCam(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TCrossManager::DeleteCrossCam(TCrossCam *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_CRS_CMRA    \r\n"
           "   SET DEL_YN = 'Y'   \r\n"
           " WHERE CAM_ID = :p01  \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->Parameters->ParamByName("p01")->Value = AObj->CAM_ID;
			pADO->ExecSQL();

        sQry = "DELETE TB_FCLT_INFR      \r\n"
               " WHERE FCLT_ID = :p01    \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->CAM_ID;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::DeleteCrossCam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::DeleteCrossCam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------

bool TCrossManager::MergeCrossCam(TCrossCam* AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();

    sQry = "MERGE INTO TB_CRS_CMRA L                        \r\n"
           "USING (SELECT :p01 AS CAM_ID,                   \r\n"
           "              :p02 AS PARENT_ID,                \r\n"
           "              :p03 AS NAME,                     \r\n"
           "              :p04 AS IP,                       \r\n"
           "              :p05 AS ID,                       \r\n"
           "              :p06 AS PW,                       \r\n"
           "              :p07 AS RTSP_URL,                 \r\n"
           "              :p08 AS RTSP_PORT,                \r\n"
           "              :p09 AS HTTP_PORT,                \r\n"
           "              :p10 AS CROSS_ID,                 \r\n"
           "              :p11 AS RIGHT_USE,                \r\n"
           "              :p12 AS DEL_YN                    \r\n"
           "         FROM DUAL) M                           \r\n"
           "        ON (L.CAM_ID = M.CAM_ID)                \r\n"
           "WHEN MATCHED THEN                               \r\n"
           "   UPDATE SET L.PARENT_ID = M.PARENT_ID,        \r\n"
           "              L.NAME      = M.NAME,             \r\n"
           "              L.IP        = M.IP,               \r\n"
           "              L.ID        = M.ID,               \r\n"
           "              L.PW        = M.PW,               \r\n"
           "              L.RTSP_URL  = M.RTSP_URL,         \r\n"
           "              L.RTSP_PORT = M.RTSP_PORT,        \r\n"
           "              L.HTTP_PORT = M.HTTP_PORT,        \r\n"
           "              L.CROSS_ID  = M.CROSS_ID,         \r\n"
           "              L.RIGHT_USE = M.RIGHT_USE,        \r\n"
           "              L.DEL_YN    = M.DEL_YN            \r\n"
           "WHEN NOT MATCHED THEN                           \r\n"
           "   INSERT (CAM_ID,                              \r\n"
           "           PARENT_ID,                           \r\n"
           "           NAME,                                \r\n"
           "           IP,                                  \r\n"
           "           ID,                                  \r\n"
           "           PW,                                  \r\n"
           "           RTSP_URL,                            \r\n"
           "           RTSP_PORT,                           \r\n"
           "           HTTP_PORT,                           \r\n"
           "           CROSS_ID,                            \r\n"
           "           RIGHT_USE,                           \r\n"
           "           DEL_YN )                             \r\n"
           "   VALUES (M.CAM_ID,                            \r\n"
           "           M.PARENT_ID,                         \r\n"
           "           M.NAME,                              \r\n"
           "           M.IP,                                \r\n"
           "           M.ID,                                \r\n"
           "           M.PW,                                \r\n"
           "           M.RTSP_URL,                          \r\n"
           "           M.RTSP_PORT,                         \r\n"
           "           M.HTTP_PORT,                         \r\n"
           "           M.CROSS_ID,                          \r\n"
           "           M.RIGHT_USE,                         \r\n"
           "           M.DEL_YN )                           \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = AObj->CAM_ID;
            pADO->Parameters->ParamByName("p02")->Value = "";
            pADO->Parameters->ParamByName("p03")->Value = AObj->CAM_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->ID;
            pADO->Parameters->ParamByName("p06")->Value = AObj->PWD;
            pADO->Parameters->ParamByName("p07")->Value = AObj->RTSP_URL;
            pADO->Parameters->ParamByName("p08")->Value = AObj->RTSP_PORT;
            pADO->Parameters->ParamByName("p09")->Value = AObj->HTTP_PORT;
            pADO->Parameters->ParamByName("p10")->Value = AObj->CROSS_ID;
            pADO->Parameters->ParamByName("p11")->Value = AObj->RIGHT_USE;
            pADO->Parameters->ParamByName("p12")->Value = AObj->DEL_YN;

   			pADO->ExecSQL();

    sQry = "MERGE INTO TB_CRS_CMRA_DIR L                    \r\n"
           "USING (SELECT                                   \r\n"
           "              :p01 AS CAM_ID,                   \r\n"
           "              :p02 AS DIRECTION,                \r\n"
           "              :p03 AS NAME,                     \r\n"
           "              :p04 AS X,                        \r\n"
           "              :p05 AS Y,                        \r\n"
           "              :p06 AS ANGLE                     \r\n"
           "         FROM DUAL) M                           \r\n"
           "        ON (L.CAM_ID = M.CAM_ID)                \r\n"
           "WHEN MATCHED THEN                               \r\n"
           "   UPDATE SET                                   \r\n"
           "              L.DIRECTION = M.DIRECTION,        \r\n"
           "              L.NAME      = M.NAME,             \r\n"
           "              L.X         = M.X,                \r\n"
           "              L.Y         = M.Y,                \r\n"
           "              L.ANGLE     = M.ANGLE             \r\n"
           "WHEN NOT MATCHED THEN                           \r\n"
           "   INSERT (                                     \r\n"
           "           CAM_ID,                              \r\n"
           "           DIRECTION,                           \r\n"
           "           NAME,                                \r\n"
           "           X,                                   \r\n"
           "           Y,                                   \r\n"
           "           ANGLE )                              \r\n"
           "   VALUES (                                     \r\n"
           "           M.CAM_ID,                            \r\n"
           "           M.DIRECTION,                         \r\n"
           "           M.NAME,                              \r\n"
           "           M.X,                                 \r\n"
           "           M.Y,                                 \r\n"
           "           M.ANGLE )                            \r\n";


			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = AObj->CAM_ID;
            pADO->Parameters->ParamByName("p02")->Value = AObj->DIRECTION;
            pADO->Parameters->ParamByName("p03")->Value = AObj->CAM_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p05")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p06")->Value = AObj->ANGLE;

   			pADO->ExecSQL();

            return MergeFcltInfo2(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::MergeCrossCam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::MergeCrossCam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------
bool TCrossManager::MergeFcltInfo2(TCrossCam *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_FCLT_INFR L                                         \r\n"
           "USING (SELECT :p01 AS FCLT_ID,                                    \r\n"
           "              :p02 AS FCLT_TYPE,                                  \r\n"
           "              :p03 AS FCLT_LCTN                                   \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.FCLT_ID = M.FCLT_ID AND L.FCLT_TYPE = M.FCLT_TYPE)  \r\n"
           "WHEN MATCHED                                                      \r\n"
           "THEN                                                              \r\n"
           "   UPDATE SET L.FCLT_LCTN = M.FCLT_LCTN,                          \r\n"
           "              L.DEL_YN    = 'N'                                   \r\n"
           "WHEN NOT MATCHED                                                  \r\n"
           "THEN                                                              \r\n"
           "   INSERT (  FCLT_ID,   FCLT_TYPE,   FCLT_LCTN)                   \r\n"
           "   VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN)                   \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->Parameters->ParamByName("p01")->Value = AObj->CAM_ID;
			pADO->Parameters->ParamByName("p02")->Value = "CRSCAM";
			pADO->Parameters->ParamByName("p03")->Value = AObj->CAM_NM;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TCrossManager::MergeFcltInfo2", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TCrossManager::MergeFcltInfo2", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

