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

#include "VMSCommLibF.h"
#pragma hdrstop

#include "CDSVmsIfscTrafF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
#define     CALSPED(x)              (int)(x+0.5)    /* 속도 반올림 하기 위해서 */
//---------------------------------------------------------------------------

int TCDSVmsIfscTraf::CreateTraffic()
{
    float fSumSpd, fSumLen;
    int   nSpdCnt;

    fSumSpd = fSumLen = 0;
    nSpdCnt = 0;

    if (!CDSVmsIfscTrafManager) return 0;

    FOR_STL(TObjectId*, pObj, FLists)
    {
        if (pObj->IsValid == false) continue;

        TCDSIfscTraf* pIfsc = CDSVmsIfscTrafManager->FIfscTrafLists.Find(pObj->ID);
        if (pIfsc)
        {
            if (pIfsc->IsValid && pIfsc->SPED > 0 && pIfsc->SECT_LNGT > 0)
            {
                fSumLen += pIfsc->SECT_LNGT;
                fSumSpd += (pIfsc->SPED * pIfsc->SECT_LNGT);   /* 거리 가중치 속도 계산 */
                nSpdCnt++;
            }
        }
    }

    if (fSumSpd > 0 && nSpdCnt > 0)
    {
        traf.SPED = CALSPED((fSumSpd/fSumLen));
    }

    if (traf.SPED > 0)
    {
        traf.CMTR_GRAD_CD = "1";    //속도값 최대 보정해야함

        traf.TRVL_HH = CALSPED( ((SECT_LNGT * 3.6) / (float)traf.SPED) );

        traf.TRVL_HH = CALSPED( ((float)traf.TRVL_HH / (float)60.0) );      // 데이터베이스에 분 단위로 입력해 준다.
        if (traf.TRVL_HH <= 0) traf.TRVL_HH = 1;

        FOR_STL(TVmsIfscGrad*, pGrade, FGrads)
        {
            if (traf.SPED >= pGrade->LWSTSPED && traf.SPED <= pGrade->HGHSSPED)
            {
                traf.CMTR_GRAD_CD = pGrade->CMTR_GRAD_CD;
                break;
            }
        }
    }
    return 1;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCDSVmsIfscTrafManager *CDSVmsIfscTrafManager = NULL;
//---------------------------------------------------------------------------

/*
* Manager
*/
TCDSVmsIfscTrafManager::TCDSVmsIfscTrafManager()
{
}
//---------------------------------------------------------------------------
TCDSVmsIfscTrafManager::~TCDSVmsIfscTrafManager()
{
    //FLists.RemoveAll();
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::InitVmsIfscTraffic(TADOConnection *ADbConn)
{
	int nRes = 0;

    // TB_VMS_IFSC_TRAF 테이블 초기화
    nRes = INIT_TB_VMS_IFSC_TRAF(ADbConn);

    // VMS IFSC 에 속한 IFSC 정보 초기화
    nRes = INIT_TB_IFSC_TRAF(ADbConn);

    // TB_VMS_IFSC_RLTN_IFSC 정보 조회
    nRes = SEL_TB_VMS_IFSC_RLTN_IFSC(ADbConn);
    nRes = SEL_TB_VMS_IFSC_CMTR_GRAD(ADbConn);

    return nRes;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::CreateVmsIfscTraffic(TADOConnection *ADbConn)
{
	int nRes = 0;

    // VMS IFSC 에 속한 IFSC 의 소통정보를 조회한다.
    nRes = SEL_TB_IFSC_TRAF(ADbConn);

    nRes = 0;
    FOR_STL(TCDSVmsIfscTraf*, pObj, FVmsIfscLists)
    {
        if (pObj->IsValid == false) continue;
        nRes += pObj->CreateTraffic();
    }

    nRes = UPD_TB_VMS_IFSC_TRAF(ADbConn);

    return nRes;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::INIT_TB_VMS_IFSC_TRAF(TADOConnection *ADbConn)
{
	int         nSelCnt;
	AnsiString  sQry;
	TADOQuery  *pADO = NULL;

    DWORD dwTick = GetTickCount();
    INSSTART;

    sQry = "INSERT INTO TB_VMS_IFSC_TRAF (                                          \r\n"
           "            VMS_IFSC_ID,                                                \r\n"
           "            PRCN_DT,                                                    \r\n"
           "            CMTR_GRAD_CD                                                \r\n"
           "            )                                                           \r\n"
           "SELECT VMS_IFSC_ID,                                                     \r\n"
           "       TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS PRCN_DT,                 \r\n"
           "       '0' AS CMTR_GRAD_CD                                              \r\n"
           "  FROM TB_VMS_IFSC                                                      \r\n"
           " WHERE VMS_IFSC_ID NOT IN (SELECT VMS_IFSC_ID FROM TB_VMS_IFSC_TRAF)    \r\n";

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

			SQLText(pADO, sQry, false);

            nSelCnt = SQLExec(pADO);
		}
		catch(EDatabaseError &E)
		{
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
		}
	}
	__finally
	{
        SQLFree(pADO);
	}

    MDEBUG("DCOM INIT_TB_VMS_IFSC_TRAF: %d", nSelCnt);
    INSEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::INIT_TB_IFSC_TRAF(TADOConnection *ADbConn)
{
    int  nSelCnt = 0;
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;


    // 메모리 상의 IFSC 소통정보를 초기화 한다.
    FOR_STL(TCDSIfscTraf*, pTmpObj, FIfscTrafLists)
    {
        pTmpObj->IsValid = false;
        pTmpObj->SPED    = 0;
        pTmpObj->TRVL_HH = 0;
    }

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT B.IFSC_ID AS IFSC_ID,                \r\n"
           "       C.LENGTH  AS SECT_LNGT               \r\n"
           "  FROM (SELECT DISTINCT IFSC_ID AS IFSC_ID  \r\n"
           "          FROM TB_VMS_IFSC_RLTN_IFSC) B,    \r\n"
           "       INFO_PRV_SCTN         C              \r\n"
           " WHERE B.IFSC_ID = C.INFO_PRV_SCTNID        \r\n";

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

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString IFSC_ID = pADO->FieldByName("IFSC_ID")->AsString.Trim();

				TCDSIfscTraf *pObj = FIfscTrafLists.Find(IFSC_ID);
                if (!pObj)
                {
                    pObj = new TCDSIfscTraf();
                    if (!pObj)
                    {
                        MERROR("DCOM TCDSIfscTraf memory allocation failed: %s", IFSC_ID);
                        continue;
                    }
                    pObj->IFSC_ID = IFSC_ID;
                    FIfscTrafLists.Push(pObj->IFSC_ID, pObj);
                }

                pObj->IsValid = true;
                pObj->SECT_LNGT = pADO->FieldByName("SECT_LNGT")->AsInteger;
                pObj->SPED      = 0;
                pObj->TRVL_HH   = 0;
                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    MDEBUG("DCOM INIT_TB_IFSC_TRAF: %d", nSelCnt);
    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::SEL_TB_IFSC_TRAF(TADOConnection *ADbConn)
{
    int  nSelCnt = 0;
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    // 메모리 상의 IFSC 소통정보를 초기화 한다.
    FOR_STL(TCDSIfscTraf*, pTmpObj, FIfscTrafLists)
    {
        pTmpObj->IsValid = false;
        pTmpObj->SPED    = 0;
        pTmpObj->TRVL_HH = 0;
    }

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT A.IFSC_ID AS IFSC_ID,                                            \r\n"
           "       C.LENGTH  AS SECT_LNGT,                                          \r\n"
           "       A.SPED    AS SPED,                                               \r\n"
           "       A.TRVL_HH AS TRVL_HH                                             \r\n"
           "  FROM TB_IFSC_TRAF          A,                                         \r\n"
           "       (SELECT DISTINCT IFSC_ID AS IFSC_ID                              \r\n"
           "          FROM TB_VMS_IFSC_RLTN_IFSC) B,                                \r\n"
           "       INFO_PRV_SCTN         C                                          \r\n"
           " WHERE A.IFSC_ID = B.IFSC_ID                                            \r\n"
           "   AND B.IFSC_ID = C.INFO_PRV_SCTNID                                    \r\n"
           "   AND A.PRCN_DT > TO_CHAR(SYSDATE - 10 / 1440, 'YYYYMMDDHH24MISS')     \r\n"
           "   AND A.SPED    > 0                                                    \r\n";

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

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString IFSC_ID = pADO->FieldByName("IFSC_ID")->AsString.Trim();

				TCDSIfscTraf *pObj = FIfscTrafLists.Find(IFSC_ID);
                if (!pObj)
                {
                    pObj = new TCDSIfscTraf();
                    if (!pObj)
                    {
                        MERROR("DCOM TCDSIfscTraf memory allocation failed: %s", IFSC_ID);
                        continue;
                    }
                    pObj->IFSC_ID = IFSC_ID;
                    FIfscTrafLists.Push(pObj->IFSC_ID, pObj);
                }

                pObj->IsValid = true;
                pObj->SECT_LNGT = pADO->FieldByName("SECT_LNGT")->AsInteger;
                pObj->SPED      = pADO->FieldByName("SPED")->AsInteger;
                pObj->TRVL_HH   = pADO->FieldByName("TRVL_HH")->AsInteger;
                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    MDEBUG("DCOM SEL_TB_IFSC_TRAF: %d", nSelCnt);
    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::SEL_TB_VMS_IFSC_RLTN_IFSC(TADOConnection *ADbConn)
{
    int  nSelCnt = 0;
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    // 메모리 상의 IFSC 소통정보를 초기화 한다.
    FOR_STL(TCDSVmsIfscTraf*, pTmpObj, FVmsIfscLists)
    {
        // VMS_IFSC_ID(VMS정보제공구간) 소통정보 초기화
        pTmpObj->traf.SPED      = 0;
        pTmpObj->traf.TRVL_HH   = 0;
        pTmpObj->traf.CMTR_GRAD_CD = "0";

        FOR_STL(TObjectId*, pTmpId, pTmpObj->FLists)
        {
            pTmpId->IsValid = false;
        }
    }

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT A.VMS_IFSC_ID AS VMS_IFSC_ID, A.SECT_LNGT,                            \r\n"
           "       B.IFSC_ID     AS IFSC_ID,     B.ORD                                   \r\n"
           "  FROM (SELECT A.VMS_IFSC_ID, B.SECT_LNGT                                    \r\n"
           "          FROM TB_VMS_IFSC A                                                 \r\n"
           "         INNER JOIN (SELECT A.VMS_IFSC_ID, SUM(C.SECT_LNGT) AS SECT_LNGT     \r\n"
           "                       FROM (SELECT VMS_IFSC_ID                              \r\n"
           "                               FROM TB_VMS_IFSC                              \r\n"
           "                              WHERE USE_YN = 'Y'                             \r\n"
           "                            ) A                                              \r\n"
           "                     INNER JOIN TB_VMS_IFSC_RLTN_IFSC B                      \r\n"
           "                             ON A.VMS_IFSC_ID = B.VMS_IFSC_ID                \r\n"
           "                     INNER JOIN TB_IFSC C                                    \r\n"
           "                             ON C.DEL_YN = 'N'                               \r\n"
           "                            AND B.IFSC_ID = C.IFSC_ID                        \r\n"
           "                       GROUP BY A.VMS_IFSC_ID                                \r\n"
           "                    ) B                                                      \r\n"
           "                 ON A.VMS_IFSC_ID = B.VMS_IFSC_ID                            \r\n"
           "       ) A                                                                   \r\n"
           " INNER JOIN TB_VMS_IFSC_RLTN_IFSC B                                          \r\n"
           "         ON A.VMS_IFSC_ID = B.VMS_IFSC_ID                                    \r\n"
           " INNER JOIN TB_IFSC C                                                        \r\n"
           "         ON C.DEL_YN = 'N'                                                   \r\n"
           "        AND B.IFSC_ID = C.IFSC_ID                                            \r\n";

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

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString VMS_IFSC_ID = pADO->FieldByName("VMS_IFSC_ID")->AsString.Trim();
                AnsiString IFSC_ID     = pADO->FieldByName("IFSC_ID")->AsString;
                int                ORD = pADO->FieldByName("ORD")->AsInteger;

				TCDSVmsIfscTraf *pObj = FVmsIfscLists.Find(VMS_IFSC_ID);
                if (!pObj)
                {
                    pObj = new TCDSVmsIfscTraf();
                    if (!pObj)
                    {
                        MERROR("DCOM TCDSVmsIfscTraf memory allocation failed: %s", VMS_IFSC_ID);
                        continue;
                    }

                    pObj->VMS_IFSC_ID = VMS_IFSC_ID;
                    FVmsIfscLists.Push(pObj->VMS_IFSC_ID, pObj);

                    ////소통등급이 없는경우를 대비하여 기본값을 넣어두자
                    TVmsIfscGrad *pSub = new TVmsIfscGrad();
                    if (pSub)
                    {
                        pSub->CMTR_GRAD_CD = "1";
                        pSub->LWSTSPED = 41;
                        pSub->HGHSSPED = 200;
                        pObj->FGrads.Push(pSub->CMTR_GRAD_CD, pSub);
                    }
                    else
                    {
                        MERROR("Memory allocation failed: %d, %s", __LINE__, __FUNCTION__);
                    }

                    pSub = new TVmsIfscGrad();
                    if (pSub)
                    {
                        pSub->CMTR_GRAD_CD = "2";
                        pSub->LWSTSPED = 21;
                        pSub->HGHSSPED = 40;
                        pObj->FGrads.Push(pSub->CMTR_GRAD_CD, pSub);
                    }
                    else
                    {
                        MERROR("Memory allocation failed: %d, %s", __LINE__, __FUNCTION__);
                    }

                    pSub = new TVmsIfscGrad();
                    if (pSub)
                    {
                        pSub->CMTR_GRAD_CD = "3";
                        pSub->LWSTSPED = 0;
                        pSub->HGHSSPED = 20;
                        pObj->FGrads.Push(pSub->CMTR_GRAD_CD, pSub);
                    }
                    else
                    {
                        MERROR("Memory allocation failed: %d, %s", __LINE__, __FUNCTION__);
                    }
                }
                pObj->IsValid   = true;
                pObj->SECT_LNGT = pADO->FieldByName("SECT_LNGT")->AsInteger;

                TObjectId *pSub = pObj->FLists.Find(ORD);
                if (!pSub)
                {
                    pSub = new TObjectId();
                    if (!pSub)
                    {
                        MERROR("DCOM TCDSVmsIfscTraf memory allocation failed: %s, %d", VMS_IFSC_ID, ORD);
                        continue;
                    }

                    pObj->FLists.Push(ORD, pSub);
                }

                pSub->IsValid = true;
                pSub->ID      = IFSC_ID;

                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    MDEBUG("DCOM SEL_TB_VMS_IFSC_RLTN_IFSC: %d", nSelCnt);
    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::SEL_TB_VMS_IFSC_CMTR_GRAD(TADOConnection *ADbConn)
{
    int  nSelCnt = 0;
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    //DDBSTART; //주석으로 안 막으면 컴파일 에러남
    DWORD dwTick = GetTickCount();
    //SELSTART; //주석으로 안 막으면 컴파일 에러남

    //[BCC32 Error] CDSVmsIfscTrafF.cpp(526): E2178 VIRDEF name conflict for '_fastcall ITSLib::TITSStlMap<AnsiString,TVmsIfscGrad *>::Find(AnsiString)'
    //Full parser context
    //CDSVmsIfscTrafF.cpp(491): parsing: int TCDSVmsIfscTrafManager::SEL_TB_VMS_IFSC_GRAD(TADOConnection *)

    sQry = "SELECT VMS_IFSC_ID AS VMS_IFSC_ID,      \r\n"
           "       CMTR_GRAD_CD, LWSTSPED, HGHSSPED \r\n"
           " FROM TB_VMS_IFSC_CMTR_GRAD             \r\n"
           " ORDER BY VMS_IFSC_ID, CMTR_GRAD_CD     \r\n";

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

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString VMS_IFSC_ID  = pADO->FieldByName("VMS_IFSC_ID")->AsString.Trim();
                AnsiString CMTR_GRAD_CD = pADO->FieldByName("CMTR_GRAD_CD")->AsString.Trim();

				TCDSVmsIfscTraf *pObj = FVmsIfscLists.Find(VMS_IFSC_ID);
                if (!pObj)
                {
                    MERROR("Not Found IFSC_GRAD: %s", VMS_IFSC_ID);
                    continue;
                }

                TVmsIfscGrad *pSub = pObj->FGrads.Find(CMTR_GRAD_CD);
                if (!pSub)
                {
                    pSub = new TVmsIfscGrad();
                    if (!pSub)
                    {
                        MERROR("DCOM TVmsIfscGrad memory allocation failed: %s, %s", VMS_IFSC_ID, CMTR_GRAD_CD);
                        continue;
                    }

                    pSub->CMTR_GRAD_CD = CMTR_GRAD_CD;
                    pObj->FGrads.Push(pSub->CMTR_GRAD_CD, pSub);
                }

                pSub->LWSTSPED = pADO->FieldByName("LWSTSPED")->AsInteger;
                pSub->HGHSSPED = pADO->FieldByName("HGHSSPED")->AsInteger;

                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    MDEBUG("DCOM SEL_TB_VMS_IFSC_CMTR_GRAD: %d", nSelCnt);
    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::UPD_TB_VMS_IFSC_TRAF(TADOConnection *ADbConn)
{
    int  nSelCnt = 0;
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    DWORD dwTick = GetTickCount();
    UPDSTART;

    AnsiString PRCN_DT = Now().FormatString("yyyymmddhhnnss");

    sQry = "MERGE INTO TB_VMS_IFSC_TRAF L                                           \r\n"
           "USING (SELECT :p01 AS VMS_IFSC_ID,                                      \r\n"
           "              :p02 AS PRCN_DT,                                          \r\n"
           "              :p03 AS SPED,                                             \r\n"
           "              :p04 AS CMTR_GRAD_CD,                                     \r\n"
           "              :p05 AS TRVL_HH                                           \r\n"
           "         FROM DUAL ) M                                                  \r\n"
           "   ON (L.VMS_IFSC_ID = M.VMS_IFSC_ID)                                   \r\n"
           " WHEN MATCHED THEN                                                      \r\n"
           " UPDATE SET PRCN_DT      = M.PRCN_DT,                                   \r\n"
           "            SPED         = M.SPED,                                      \r\n"
           "            CMTR_GRAD_CD = M.CMTR_GRAD_CD,                              \r\n"
           "            TRVL_HH      = M.TRVL_HH                                    \r\n"
           " WHEN NOT MATCHED THEN                                                  \r\n"
           " INSERT (  VMS_IFSC_ID,   PRCN_DT,   SPED,   CMTR_GRAD_CD,   TRVL_HH )  \r\n"
           " VALUES (M.VMS_IFSC_ID, M.PRCN_DT, M.SPED, M.CMTR_GRAD_CD, M.TRVL_HH )  \r\n";

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

		try
		{
            SQLText(pADO, sQry, false);

            FOR_STL(TCDSVmsIfscTraf*, pObj, FVmsIfscLists)
            {
                //if (pObj->IsValid == false) continue;

                SQLBind(pADO, "p01", pObj->VMS_IFSC_ID);
                SQLBind(pADO, "p02", PRCN_DT);
                SQLBind(pADO, "p03", pObj->traf.SPED);
                SQLBind(pADO, "p04", pObj->traf.CMTR_GRAD_CD);
                SQLBind(pADO, "p05", pObj->traf.TRVL_HH);

                nSelCnt += SQLExec(pADO);
            }
		}
		catch(EDatabaseError &E)
		{
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
		}
	}
	__finally
	{
        SQLFree(pADO);
	}

    MDEBUG("DCOM UPD_TB_VMS_IFSC_TRAF: %d", nSelCnt);
    UPDEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

#if 0
int TCDSVmsIfscTrafManager::INS_TB_VMS_IFSC_TRAF_HS(TDMDb *ADbConn)
{
	int        nJobCnt = 0;
    long       nRows;
	AnsiString sQry;
    SQLRETURN  retcode;

    DWORD dwTick = GetTickCount();
    INSSTART;

    AnsiString ins_prcn_dt = T_TIME.PrcsSave5Min;   //가공주기 시각으로 입력(현재시각 -5분)
    AnsiString sel_prcn_dt = T_TIME.PrcsCurr5Min;

    sQry = "INSERT INTO TB_VMS_IFSC_TRAF_HS                     \r\n"
           "       (PRCN_DT,                                    \r\n"
           "        VMS_IFSC_ID, SPED, CMTR_GRAD_CD, TRVL_HH )  \r\n"
           "SELECT :p01 AS PRCN_DT,                             \r\n"
           "        VMS_IFSC_ID, SPED, CMTR_GRAD_CD, TRVL_HH    \r\n"
           "  FROM TB_VMS_IFSC_TRAF                             \r\n"
           " WHERE PRCN_DT = :p02                               \r\n"
           "   AND SPED > 0                                     \r\n";

    SQLCHAR INS_PRCN_DT[14+1]; SQLLEN lINS_PRCN_DT = SQL_NTS;
    SQLCHAR SEL_PRCN_DT[14+1]; SQLLEN lSEL_PRCN_DT = SQL_NTS;

    sprintf((char*)INS_PRCN_DT, ins_prcn_dt.c_str());
    sprintf((char*)SEL_PRCN_DT, sel_prcn_dt.c_str());

    ADbConn->BeginTrans();
	try
	{
		try
		{
            if (!SQL_SUCCEEDED(ADbConn->AllocSQLHandle()))
            {
                return -2;
            }

            SQLBINDPARAMETER(ADbConn->GetSQLHandle(), 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 14, 0, &INS_PRCN_DT, 14+1, NULL);
            SQLBINDPARAMETER(ADbConn->GetSQLHandle(), 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 14, 0, &SEL_PRCN_DT, 14+1, NULL);

            retcode = SQLExecDirect(ADbConn->GetSQLHandle(), (SQLCHAR*)sQry.c_str(), SQL_NTS);
            if (ADbConn->CheckError(retcode, "INS_TB_VMS_IFSC_TRAF_HS", ADbConn->GetSQLHandle(), SQL_HANDLE_STMT))
            {
                retcode = SQLRowCount(ADbConn->GetSQLHandle(), (SQLLEN*)&nRows);
                nJobCnt = (int)nRows;
            }
		}
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
		}
	}
	__finally
	{
        ADbConn->EndTrans();
        ADbConn->FreeSQLHandle();
	}
    INSEND(nJobCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nJobCnt;
}
//---------------------------------------------------------------------------
int TCDSVmsIfscTrafManager::INS_TB_VMS_IFSC_TRAF_HS_OLD(TDMDb *ADbConn)
{
	int        nJobCnt = 0;
    int        nInsCnt = 0;
    int        nBatCnt = 0;
    long       nRows;
	AnsiString sQry;
    SQLRETURN  retcode;

    DWORD dwTick = GetTickCount();
    INSSTART;

    AnsiString prcn_dt = T_TIME.PrcsSave5Min;   //가공주기 시각으로 입력(현재시각 -5분)

    sQry = "INSERT INTO TB_VMS_IFSC_TRAF_HS                               \r\n"
           "        (PRCN_DT, VMS_IFSC_ID, SPED, CMTR_GRAD_CD, TRVL_HH )  \r\n"
           " VALUES (:p01,        :p02,    :p03, :p04,         :p05    )  \r\n";

    SQLCHAR    PRCN_DT     [MAX_INS_DATA][14+1]; SQLLEN lPRCN_DT     [MAX_INS_DATA];
    SQLCHAR    VMS_IFSC_ID [MAX_INS_DATA][10+1]; SQLLEN lVMS_IFSC_ID [MAX_INS_DATA];
    SQLINTEGER SPED        [MAX_INS_DATA]      ; SQLLEN lSPED        [MAX_INS_DATA];
    SQLCHAR    CMTR_GRAD_CD[MAX_INS_DATA][ 7+1]; SQLLEN lCMTR_GRAD_CD[MAX_INS_DATA];
    SQLINTEGER TRVL_HH     [MAX_INS_DATA]      ; SQLLEN lTRVL_HH     [MAX_INS_DATA];

    SQLULEN    PROCESSED;
    SQLPOINTER sqlpRowSize;
    int        nRowSize = MAX_INS_DATA;
    sqlpRowSize = &nRowSize;

	try
	{
        if (!SQL_SUCCEEDED(ADbConn->AllocSQLHandle()))
        {
            return -2;
        }

#if 0
        retcode = SQLSetStmtAttr(ADbConn->GetSQLHandle(), SQL_ATTR_PARAMSET_SIZE, sqlpRowSize, 0);
        if (!ADbConn->CheckError(retcode, "TB_VMS_IFSC_TRAF_HS1", ADbConn->GetSQLHandle(), SQL_HANDLE_STMT))
        {
            return -3;
        }
#endif

        retcode = SQLSetStmtAttr(ADbConn->GetSQLHandle(), SQL_ATTR_PARAMS_PROCESSED_PTR, &PROCESSED, 0);
        if (!ADbConn->CheckError(retcode, "TB_VMS_IFSC_TRAF_HS2", ADbConn->GetSQLHandle(), SQL_HANDLE_STMT))
        {
            return -4;
        }

        SQLBINDPARAMETER(ADbConn->GetSQLHandle(),  1, 1, SQL_C_CHAR,  SQL_VARCHAR, 14, 0, PRCN_DT,      14+1, lPRCN_DT);
        SQLBINDPARAMETER(ADbConn->GetSQLHandle(),  2, 1, SQL_C_CHAR,  SQL_VARCHAR, 10, 0, VMS_IFSC_ID,  10+1, lVMS_IFSC_ID);
        SQLBINDPARAMETER(ADbConn->GetSQLHandle(),  3, 1, SQL_C_LONG,  SQL_INTEGER,  4, 0, SPED,            0, lSPED);
        SQLBINDPARAMETER(ADbConn->GetSQLHandle(),  4, 1, SQL_C_CHAR,  SQL_VARCHAR,  7, 0, CMTR_GRAD_CD,  7+1, lCMTR_GRAD_CD);
        SQLBINDPARAMETER(ADbConn->GetSQLHandle(),  5, 1, SQL_C_LONG,  SQL_INTEGER,  4, 0, TRVL_HH,         0, lTRVL_HH);

        retcode = SQLPrepare(ADbConn->GetSQLHandle(),(SQLCHAR*)sQry.c_str(), SQL_NTS);
        if (!ADbConn->CheckError(retcode, "TB_VMS_IFSC_TRAF_HS3", ADbConn->GetSQLHandle(), SQL_HANDLE_STMT))
        {
            return -5;
        }

        ADbConn->BeginTrans();
		try
		{
            FOR_STL(TCDSVmsIfsc*, pObj, FLists)
            {
                if (pObj->IsValid == false) continue;
                if (pObj->traf.SPED <= 0) continue;

                sprintf((char*)PRCN_DT     [nInsCnt], prcn_dt.c_str());                 lPRCN_DT     [nInsCnt] = SQL_NTS;
                sprintf((char*)VMS_IFSC_ID [nInsCnt], pObj->VMS_IFSC_ID.c_str());       lVMS_IFSC_ID [nInsCnt] = SQL_NTS;
                SPED        [nInsCnt] = pObj->traf.SPED;                                lSPED        [nInsCnt] = SQL_NTS;
                sprintf((char*)CMTR_GRAD_CD[nInsCnt], pObj->traf.CMTR_GRAD_CD.c_str()); lCMTR_GRAD_CD[nInsCnt] = SQL_NTS;
                TRVL_HH     [nInsCnt] = pObj->traf.TRVL_HH;                             lTRVL_HH     [nInsCnt] = SQL_NTS;

                nInsCnt++;

                if (nInsCnt >= MAX_INS_DATA)
                {
                    SQLSETSTMTATTR_PARAMSET(ADbConn->GetSQLHandle(), SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)nInsCnt, 0);
                    retcode = SQLExecute(ADbConn->GetSQLHandle());
                    if (ADbConn->CheckError(retcode, "TB_VMS_IFSC_TRAF_HS4", ADbConn->GetSQLHandle(), SQL_HANDLE_STMT))
                    {
                        //retcode = SQLRowCount(hstmt, (SQLLEN*)&nRows);
                        nJobCnt += nInsCnt;
                        nBatCnt++;
                    }
                    else
                    {
                        if (retcode != SQL_NO_DATA)
                        {
                            MERROR("TB_VMS_IFSC_TRAF_HS4(%d)(%d)(%d)(%d): %s", retcode, nBatCnt, nInsCnt, nJobCnt, AnsiString(ADbConn->ErrorString).c_str());
                            return -8;
                        }
                    }
                    nInsCnt = 0;
                }
            }
            if (nInsCnt > 0)
            {
                SQLSETSTMTATTR_PARAMSET(ADbConn->GetSQLHandle(), SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)nInsCnt, 0);
                retcode = SQLExecute(ADbConn->GetSQLHandle());
                if (ADbConn->CheckError(retcode, "TB_VMS_IFSC_TRAF_HS5", ADbConn->GetSQLHandle(), SQL_HANDLE_STMT))
                {
                    //retcode = SQLRowCount(hstmt, (SQLLEN*)&nRows);
                    nJobCnt += nInsCnt;
                    nBatCnt++;
                }
                else
                {
                    if (retcode != SQL_NO_DATA)
                    {
                        MERROR("TB_VMS_IFSC_TRAF_HS5(%d)(%d)(%d)(%d): %s", retcode, nBatCnt, nInsCnt, nJobCnt, AnsiString(ADbConn->ErrorString).c_str());
                        return -9;
                    }
                }
            }
		}
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
		}
	}
	__finally
	{
        ADbConn->EndTrans();
        ADbConn->FreeSQLHandle();
	}
    INSEND(nJobCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nJobCnt;
}
//---------------------------------------------------------------------------
#endif

