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


#pragma hdrstop

#include "CDSVmsIfscF.h"
#include "VMSOprMainLibF.h"

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

#pragma package(smart_init)

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

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

    VMS_IFSC_NM       = "";         ///VARCHAR2(60)	Y			VMS 정보제공구간 명
    DSPL_STRT_NODE_NM = "";         //VARCHAR2(30)	Y			표출 시작 노드 명
    DSPL_END_NODE_NM  = "";         //VARCHAR2(30)	Y			표출 종료 노드 명
    DETR_ID           = 0;          //NUMBER(10)	Y			우회도로 ID
    USE_YN            = "Y";
    AXIS_YN           = "N";

    DEL_YN            = "N";

    MIN[0] = 20; MAX[0] = 180;
    MIN[1] = 10; MAX[1] =  19;
    MIN[2] =  0; MAX[2] =   9;

    InitTraffic();
}
//---------------------------------------------------------------------------
TVmsIfsc::~TVmsIfsc()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TVmsIfsc::InitTraffic()
{
    PRCN_DT      = "";	    //N	VARCHAR2(14)	Y			등록 일시
    SPED         = 0;	    //N	NUMBER(3)	    Y	0		속도
    CMTR_GRAD_CD = "0";	    //N	VARCHAR2(7)	    Y			소통 등급 코드 ==>예는 0,1,2,3으로 들어가네???
    TRVL_HH      = 0;	    //N	NUMBER(6)	    Y	0		통행 시간
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TVmsIfscManager *VmsIfscManager = NULL;
//---------------------------------------------------------------------------

TVmsIfscManager::TVmsIfscManager()
{
}
//---------------------------------------------------------------------------
TVmsIfscManager::~TVmsIfscManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TVmsIfscManager::InitTraffic()
{
    FLists.Lock();
    try
    {
        FOR_STL(TVmsIfsc*, pObj, FLists)
        {
            pObj->InitTraffic();
        }
    }
    __finally
    {
        FLists.UnLock();
    }
}
//---------------------------------------------------------------------------

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

    FLists.Lock();
    try
    {
        FLists.RemoveAll();
    }
    __finally
    {
        FLists.UnLock();
    }

#if 0
    sQry = "SELECT A.*              \r\n"
           "  FROM TB_VMS_IFSC A    \r\n"
           " ORDER BY A.VMS_IFSC_ID \r\n";
#else
    sQry = "SELECT A.*,                                                                                                       \r\n"
           "       NVL(B.MIN1, 20) AS MIN1, NVL(B.MAX1, 180) AS MAX1,                                                         \r\n"
           "       NVL(B.MIN2, 10) AS MIN2, NVL(B.MAX2,  19) AS MAX2,                                                         \r\n"
           "       NVL(B.MIN3,  0) AS MIN3, NVL(B.MAX3,   9) AS MAX3                                                          \r\n"
           "  FROM TB_VMS_IFSC A,                                                                                             \r\n"
           "       (SELECT VMS_IFSC_ID,                                                                                       \r\n"
           "               SUM(MIN1) AS MIN1, SUM(MAX1) AS MAX1,                                                              \r\n"
           "               SUM(MIN2) AS MIN2, SUM(MAX2) AS MAX2,                                                              \r\n"
           "               SUM(MIN3) AS MIN3, SUM(MAX3) AS MAX3                                                               \r\n"
           "         FROM (SELECT VMS_IFSC_ID, LWSTSPED AS MIN1, HGHSSPED AS MAX1, 0 AS MIN2, 0 AS MAX2, 0 AS MIN3, 0 AS MAX3 \r\n"
           "                 FROM TB_VMS_IFSC_CMTR_GRAD                                                                       \r\n"
           "                WHERE CMTR_GRAD_CD = '1'                                                                          \r\n"
           "               UNION ALL                                                                                          \r\n"
           "               SELECT VMS_IFSC_ID, 0 AS MIN1, 0 AS MAX1, LWSTSPED AS MIN2, HGHSSPED AS MAX2, 0 AS MIN3, 0 AS MAX3 \r\n"
           "                 FROM TB_VMS_IFSC_CMTR_GRAD                                                                       \r\n"
           "                WHERE CMTR_GRAD_CD = '2'                                                                          \r\n"
           "               UNION ALL                                                                                          \r\n"
           "               SELECT VMS_IFSC_ID, 0 AS MIN1, 0 AS MAX1, 0 AS MIN2, 0 AS MAX2, LWSTSPED AS MIN3, HGHSSPED AS MAX3 \r\n"
           "                 FROM TB_VMS_IFSC_CMTR_GRAD                                                                       \r\n"
           "                WHERE CMTR_GRAD_CD = '3')                                                                         \r\n"
           "         GROUP BY VMS_IFSC_ID) B                                                                                  \r\n"
           " WHERE A.VMS_IFSC_ID = B.VMS_IFSC_ID(+)                                                                           \r\n";
//           " WHERE A.VMS_IFSC_ID = B.VMS_IFSC_ID                                                                              \r\n";
#endif

    FLists.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TVmsIfsc *pObj = new TVmsIfsc();

                pObj->VMS_IFSC_ID       = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;
                pObj->VMS_IFSC_NM       = pADO->FieldByName("VMS_IFSC_NM")->AsString;
                pObj->DSPL_STRT_NODE_NM = pADO->FieldByName("DSPL_STRT_NODE_NM")->AsString;
                pObj->DSPL_END_NODE_NM  = pADO->FieldByName("DSPL_END_NODE_NM")->AsString;
                pObj->DETR_ID           = pADO->FieldByName("DETR_ID")->AsInteger;
                pObj->USE_YN            = pADO->FieldByName("USE_YN")->AsString;
                pObj->ROAD_NM           = pADO->FieldByName("ROAD_NM")->AsString;
                pObj->SPOT_NM           = pADO->FieldByName("SPOT_NM")->AsString;
                pObj->AXIS_YN           = pADO->FieldByName("AXIS_YN")->AsString;

                pObj->MIN[0] = pADO->FieldByName("MIN1")->AsInteger;
                pObj->MAX[0] = pADO->FieldByName("MAX1")->AsInteger;
                pObj->MIN[1] = pADO->FieldByName("MIN2")->AsInteger;
                pObj->MAX[1] = pADO->FieldByName("MAX2")->AsInteger;
                pObj->MIN[2] = pADO->FieldByName("MIN3")->AsInteger;
                pObj->MAX[2] = pADO->FieldByName("MAX3")->AsInteger;

                pObj->CNGS_SPD = pADO->FieldByName("CNGS_SPD")->AsInteger;

                pObj->DEL_YN = "N";

				FLists.Push(pObj->VMS_IFSC_ID, pObj);
                pObj->Completed  = true;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS정보제공구간정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS정보제공구간정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT A.*, NVL(B.IFSC_NM, '-') AS IFSC_NM \r\n"
           "  FROM TB_VMS_IFSC_RLTN_IFSC A,            \r\n"
           "       TB_IFSC               B             \r\n"
           " WHERE A.IFSC_ID = B.IFSC_ID(+)            \r\n"
           " ORDER BY A.VMS_IFSC_ID, A.ORD             \r\n";

    FLists.Lock();
    try
    {
        TVmsIfsc     *pIfsc;
        TVmsLinkIfsc *pObj;
        int           VMS_IFSC_ID;
        int           ORD;
        AnsiString    IFSC_NM;
        //AnsiString    IFSC_ID;

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

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;
                ORD         = pADO->FieldByName("ORD")->AsInteger;
                //IFSC_ID     = pADO->FieldByName("IFSC_ID")->AsString;
                IFSC_NM     = pADO->FieldByName("IFSC_NM")->AsString;

                pIfsc = FLists.Find(VMS_IFSC_ID);
                if (!pIfsc) continue;

                pObj = pIfsc->FLists.Find(ORD);
                if (!pObj)
                {
				    pObj = new TVmsLinkIfsc();
                    pIfsc->FLists.Push(ORD, pObj);
                }
                pObj->VMS_IFSC_ID       = VMS_IFSC_ID;
                pObj->ORD               = ORD;
                pObj->IFSC_ID           = pADO->FieldByName("IFSC_ID")->AsString;
                //pObj->DSPL_STRT_NODE_NM = pADO->FieldByName("DSPL_STRT_NODE_NM")->AsString;
                //pObj->DSPL_END_NODE_NM  = pADO->FieldByName("DSPL_END_NODE_NM")->AsString;
                pObj->IFSC_NM           = pADO->FieldByName("IFSC_NM")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS정보제공구간링크정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS정보제공구간링크정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    InitTraffic();

    sQry = "SELECT VMS_IFSC_ID, NVL(PRCN_DT, '19000101000000') AS PRCN_DT,      \r\n"
           "       NVL(SPED,      0) AS SPEED, NVL(TRVL_HH,   0) AS TRVL_HH,    \r\n"
           "       DECODE(CMTR_GRAD_CD, '1', '1',                               \r\n"
           "                            '2', '2',                               \r\n"
           "                            '3', '3', '0') AS CMTR_GRAD_CD          \r\n"
           "  FROM TB_VMS_IFSC_TRAF                                              \r\n"
           " WHERE PRCN_DT > TO_CHAR(SYSDATE -  10 / 1440, 'YYYYMMDDHH24MISS')  \r\n";

    FLists.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                int VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;

				TVmsIfsc *pObj = FLists.Find(VMS_IFSC_ID);
                if (!pObj) continue;

                pObj->PRCN_DT       = pADO->FieldByName("PRCN_DT")->AsString;	    //N	VARCHAR2(14)	Y			등록 일시
                pObj->SPED          = pADO->FieldByName("SPEED")->AsInteger;	    //N	NUMBER(3)	    Y	0		속도
                pObj->CMTR_GRAD_CD  = pADO->FieldByName("CMTR_GRAD_CD")->AsString;	//N	VARCHAR2(7)	    Y			소통 등급 코드 ==>예는 0,1,2,3으로 들어가네???
                pObj->TRVL_HH       = pADO->FieldByName("TRVL_HH")->AsInteger;	    //N	NUMBER(6)	    Y	0		통행 시간
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS_IFSC교통정보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS_IFSC교통정보조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TVmsIfscManager::GetNextVmsIfscId(int &AIfscId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(VMS_IFSC_ID), 0) + 1 AS NEWID FROM TB_VMS_IFSC \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->Open();
            AIfscId = pADO->FieldByName("NEWID")->AsInteger;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("VMS제공구간ID조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("VMS제공구간ID조회", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TVmsIfscManager::DeleteVmsIfsc(TVmsIfsc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

//VMS_정보제공구간 소통 등급
sQry = "DELETE FROM TB_VMS_IFSC_CMTR_GRAD WHERE VMS_IFSC_ID = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = sIfscId;
			pADO->ExecSQL();

//VMS LINK 정보제공구간
sQry = "DELETE FROM TB_VMS_IFSC_RLTN_IFSC WHERE VMS_IFSC_ID = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = sIfscId;
			pADO->ExecSQL();

//VMS 정보제공구간 관계
sQry = "DELETE FROM TB_VMS_RLTN_IFSC WHERE VMS_IFSC_ID = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = sIfscId;
			pADO->ExecSQL();

//VMS_소통정보
sQry = "DELETE FROM TB_VMS_IFSC_TRAF WHERE VMS_IFSC_ID = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = sIfscId;
			pADO->ExecSQL();

//VMS_소통정보 이력
#if 0
sQry = "DELETE FROM TB_VMS_IFSC_TRAF_HS WHERE VMS_IFSC_ID = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = sIfscId;
			pADO->ExecSQL();
#endif

//VMS_정보제공구간
#if 1
sQry = "DELETE FROM TB_VMS_IFSC WHERE VMS_IFSC_ID = :p01";
#else
sQry = "UPDATE TB_VMS_IFSC SET USE_YN = 'N' WHERE VMS_IFSC_ID = :p01";
#endif
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = sIfscId;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("VMS정보제공구간삭제", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("VMS정보제공구간삭제", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TVmsIfscManager::InsertVmsIfsc(TVmsIfsc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeVmsIfsc(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TVmsIfscManager::UpdateVmsIfsc(TVmsIfsc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeVmsIfsc(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TVmsIfscManager::MergeVmsIfsc(TVmsIfsc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    int ii;

    sQry = "MERGE INTO TB_VMS_IFSC L                                 \r\n"
           "USING (SELECT :p01 AS VMS_IFSC_ID,                       \r\n"
           "              :p02 AS VMS_IFSC_NM,                       \r\n"
           "              :p03 AS DSPL_STRT_NODE_NM,                 \r\n"
           "              :p04 AS DSPL_END_NODE_NM,                  \r\n"
           "              :p05 AS DETR_ID,                           \r\n"
           "              :p06 AS USE_YN,                            \r\n"
           "              :p07 AS ROAD_NM,                           \r\n"
           "              :p08 AS SPOT_NM,                           \r\n"
           "              :p09 AS AXIS_YN,                           \r\n"
           "              :p10 AS CNGS_SPD                           \r\n"
           "         FROM DUAL) M                                    \r\n"
           "        ON (L.VMS_IFSC_ID = M.VMS_IFSC_ID)               \r\n"
           "WHEN MATCHED                                             \r\n"
           "THEN                                                     \r\n"
           "   UPDATE SET L.VMS_IFSC_NM       = M.VMS_IFSC_NM,       \r\n"
           "              L.DSPL_STRT_NODE_NM = M.DSPL_STRT_NODE_NM, \r\n"
           "              L.DSPL_END_NODE_NM  = M.DSPL_END_NODE_NM,  \r\n"
           "              L.DETR_ID           = M.DETR_ID,           \r\n"
           "              L.USE_YN            = M.USE_YN,            \r\n"
           "              L.ROAD_NM           = M.ROAD_NM,           \r\n"
           "              L.SPOT_NM           = M.SPOT_NM,           \r\n"
           "              L.AXIS_YN           = M.AXIS_YN,           \r\n"
           "              L.CNGS_SPD          = M.CNGS_SPD           \r\n"
           "WHEN NOT MATCHED                                         \r\n"
           "THEN                                                     \r\n"
           "   INSERT (VMS_IFSC_ID,                                  \r\n"
           "           VMS_IFSC_NM,                                  \r\n"
           "           DSPL_STRT_NODE_NM,                            \r\n"
           "           DSPL_END_NODE_NM,                             \r\n"
           "           DETR_ID,                                      \r\n"
           "           USE_YN,                                       \r\n"
           "           ROAD_NM,                                      \r\n"
           "           SPOT_NM,                                      \r\n"
           "           AXIS_YN,                                      \r\n"
           "           CNGS_SPD )                                    \r\n"
           "   VALUES (M.VMS_IFSC_ID,                                \r\n"
           "           M.VMS_IFSC_NM,                                \r\n"
           "           M.DSPL_STRT_NODE_NM,                          \r\n"
           "           M.DSPL_END_NODE_NM,                           \r\n"
           "           M.DETR_ID,                                    \r\n"
           "           M.USE_YN,                                     \r\n"
           "           M.ROAD_NM,                                    \r\n"
           "           M.SPOT_NM,                                    \r\n"
           "           M.AXIS_YN,                                    \r\n"
           "           M.CNGS_SPD )                                  \r\n";
    try
    {
        try
        {
            String sDetrId = "";
            if (AObj->DETR_ID != 0) sDetrId = String(AObj->DETR_ID);

            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->VMS_IFSC_ID;
            pADO->Parameters->ParamByName("p02")->Value = AObj->VMS_IFSC_NM;
            pADO->Parameters->ParamByName("p03")->Value = AObj->DSPL_STRT_NODE_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->DSPL_END_NODE_NM;
            pADO->Parameters->ParamByName("p05")->Value = sDetrId;
            pADO->Parameters->ParamByName("p06")->Value = AObj->USE_YN;
            pADO->Parameters->ParamByName("p07")->Value = AObj->ROAD_NM;
            pADO->Parameters->ParamByName("p08")->Value = AObj->SPOT_NM;
            pADO->Parameters->ParamByName("p09")->Value = AObj->AXIS_YN;
            pADO->Parameters->ParamByName("p10")->Value = AObj->CNGS_SPD;
			pADO->ExecSQL();

//VMS_정보제공구간 소통 등급
    sQry = "MERGE INTO TB_VMS_IFSC_CMTR_GRAD L           \r\n"
           "USING (SELECT :p01 AS VMS_IFSC_ID,           \r\n"
           "              :p02 AS CMTR_GRAD_CD,          \r\n"
           "              :p03 AS LWSTSPED,              \r\n"
           "              :p04 AS HGHSSPED               \r\n"
           "         FROM DUAL) M                        \r\n"
           "        ON (L.VMS_IFSC_ID  = M.VMS_IFSC_ID   \r\n"
           "        AND L.CMTR_GRAD_CD = M.CMTR_GRAD_CD) \r\n"
           "WHEN MATCHED                                 \r\n"
           "THEN                                         \r\n"
           "   UPDATE SET L.LWSTSPED = M.LWSTSPED,       \r\n"
           "              L.HGHSSPED = M.HGHSSPED        \r\n"
           "WHEN NOT MATCHED                             \r\n"
           "THEN                                         \r\n"
           "   INSERT (VMS_IFSC_ID,                      \r\n"
           "           CMTR_GRAD_CD,                     \r\n"
           "           LWSTSPED,                         \r\n"
           "           HGHSSPED )                        \r\n"
           "   VALUES (M.VMS_IFSC_ID,                    \r\n"
           "           M.CMTR_GRAD_CD,                   \r\n"
           "           M.LWSTSPED,                       \r\n"
           "           M.HGHSSPED )                      \r\n";

            String sGradCd;
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            for(ii = 0; ii < 3; ii++)
            {
                sGradCd = String(ii+1);
                pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_IFSC_ID;
                pADO->Parameters->ParamByName("p02")->Value = sGradCd;
                pADO->Parameters->ParamByName("p03")->Value = AObj->MIN[ii];
                pADO->Parameters->ParamByName("p04")->Value = AObj->MAX[ii];
    			pADO->ExecSQL();
            }

//VMS LINK 정보제공구간
sQry = "DELETE FROM TB_VMS_IFSC_RLTN_IFSC WHERE VMS_IFSC_ID = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_IFSC_ID;
			pADO->ExecSQL();

sQry = "INSERT INTO TB_VMS_IFSC_RLTN_IFSC(VMS_IFSC_ID, IFSC_ID, ORD) VALUES(:p01, :p02, :p03)";
            ii = 0;
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            FOR_STL(TVmsLinkIfsc *, pLink, AObj->FLists)
            {
                sGradCd = String(ii+1);
                pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_IFSC_ID;
                pADO->Parameters->ParamByName("p02")->Value = pLink->IFSC_ID;
                pADO->Parameters->ParamByName("p03")->Value = String(pLink->ORD);
    			pADO->ExecSQL();
                ii++;
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("VMS제공구간정보저장", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("VMS제공구간정보저장", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TVmsIfscManager::UpdateIfscSpeedInfo(TVmsIfsc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VMS_IFSC_CMTR_GRAD  \r\n"
           "   SET LWSTSPED  = :p01,      \r\n"
           "       HGHSSPED  = :p02       \r\n"
           " WHERE CMTR_GRAD_CD = :p03    \r\n";
    try
    {
        try
        {
            String sGradCd;
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            for(int ii = 0; ii < 3; ii++)
            {
                sGradCd = String(ii+1);
                pADO->Parameters->ParamByName("p01")->Value = AObj->MIN[ii];
                pADO->Parameters->ParamByName("p02")->Value = AObj->MAX[ii];
                pADO->Parameters->ParamByName("p03")->Value = sGradCd;
    			pADO->ExecSQL();
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("VMS제공구간속도정보저장", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("VMS제공구간속도정보저장", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------
bool TVmsIfscManager::UpdateIfscCngsSpeedInfo(int ACngsSpd, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VMS_IFSC       \r\n"
           "   SET CNGS_SPD  = :p01  \r\n";
    try
    {
        try
        {
            String sGradCd;
            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 = ACngsSpd;
            pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("VMS제공구간 정체폼 정체판정속도정보저장", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("VMS제공구간 정체폼 정체판정속도정보저장", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

