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

#pragma hdrstop

#include "CDSAviF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsAviDtct::TItsAviDtct()
{
    DTCT_NMBR       = "";
    DTCT_NM         = "";
    CTLR_MNGM_NMBR  = "";
    DTCT_TYPE_CD    = "";
    ISTL_LANE       = "";
    USE_YN          = "";
    DEL_YN          = "";

    MAX_TFVL        = 0;
    MIN_TFVL        = 0;
    MAX_OCPY_RATE   = 0;
    MIN_OCPY_RATE   = 0;
    MIN_SPED        = 0;
    MAX_SPED        = 0;

    MEM_DEL         = "";
}
//---------------------------------------------------------------------------
TItsAviDtct::~TItsAviDtct()
{
}
//---------------------------------------------------------------------------

TItsAvi::TItsAvi()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsAvi::Clear()
{
    AVI_CTLR_MNGM_NMBR  = "";
    AVI_ID              = "";
    LINK_ID             = "";
    AVI_LCTN_NM         = "";
    AVI_VALD_YN         = "";
    AVI_CTLR_IP         = "";
    AVI_CTLR_CMNC_PORT  = "";
    AVI_MAIN_CTLR_YN    = "";
    AVI_MNGM_LANE_NUM   = "";
    AVI_CMRA_NUM        = "";
    AVI_DTCT_NUM        = "";
    AVI_DTCT_NM         = "";
    AVI_CLCT_CYCL       = "";
    AVI_LAMP_AUTO_YN    = "";
    AVI_LAMP_ON_HMS     = "";
    AVI_LAMP_OFF_HMS    = "";
    AVI_LAMP_ON_YN      = "";
    X_CRDN              = "";
    Y_CRDN              = "";
    RMRK                = "";
    DEL_YN              = "";
    MEM_DEL             = "";
}
//---------------------------------------------------------------------------
TItsAvi::~TItsAvi()
{
}
//---------------------------------------------------------------------------

TItsAviSect::TItsAviSect()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsAviSect::Clear()
{
    AVI_CTLR_MNGM_NMBR = "";
    ORD             = 0;

    AVI_SECT_NMBR   = "";
    STRT_SPOT_NM    = "";
    END_SPOT_NM     = "";
    AVI_SECT_NM     = "";
    AVI_SECT_DSTC   = "";
    SHRT_PASS_HH    = "";
    LNST_PASS_HH    = "";
    SPOT_NUM        = "";
    STND_DRFT_VAL   = "";
    MIN_SPED        = "";
    MAX_SPED        = "";
    SMTH_FCTR       = "";
    DEL_YN          = "";
    MEM_DEL         = "";
}
//---------------------------------------------------------------------------
TItsAviSect::~TItsAviSect()
{
}
//---------------------------------------------------------------------------

TItsAviLink::TItsAviLink()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsAviLink::Clear()
{
    AVI_CTLR_MNGM_NMBR = "";
    AVI_SECT_NMBR   = "";
    LINK_ID         = "";
    ORD             = 0;
    MEM_DEL         = "";
}
//---------------------------------------------------------------------------
TItsAviLink::~TItsAviLink()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsAviManager *ItsAviManager = NULL;
//---------------------------------------------------------------------------

/*
* Avi Manager
*/
TItsAviManager::TItsAviManager()
{
}
//---------------------------------------------------------------------------
TItsAviManager::~TItsAviManager()
{
    FLists.RemoveAll();
    FSectLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsAviManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT AVI_CTLR_MNGM_NMBR,   \r\n"
           "       AVI_ID,               \r\n"
           "       LINK_ID,              \r\n"
           "       AVI_LCTN_NM,          \r\n"
           "       AVI_VALD_YN,          \r\n"
           "       AVI_CTLR_IP,          \r\n"
           "       AVI_CTLR_CMNC_PORT,   \r\n"
           "       AVI_MAIN_CTLR_YN,     \r\n"
           "       AVI_MNGM_LANE_NUM,    \r\n"
           "       AVI_CMRA_NUM,         \r\n"
           "       AVI_DTCT_NUM,         \r\n"
           "       AVI_DTCT_NM,          \r\n"
           "       AVI_CLCT_CYCL,        \r\n"
           "       AVI_LAMP_AUTO_YN,     \r\n"
           "       AVI_LAMP_ON_HMS,      \r\n"
           "       AVI_LAMP_OFF_HMS,     \r\n"
           "       AVI_LAMP_ON_YN,       \r\n"
           "       X_CRDN,               \r\n"
           "       Y_CRDN,               \r\n"
           "       RMRK,                 \r\n"
           "       DEL_YN                \r\n"
           "  FROM TB_AVI_CTLR           \r\n";
           //" WHERE DEL_YN = 'N'          \r\n";
           //" ORDER BY AVI_CTLR_MNGM_NMBR \r\n";
    try
    {
        FLists.Lock();
        FLists.RemoveAll();
        FSectLists.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())
            {
				TItsAvi *pObj = new TItsAvi();

                pObj->AVI_CTLR_MNGM_NMBR= pADO->FieldByName("AVI_CTLR_MNGM_NMBR")->AsString;
                pObj->AVI_ID            = pADO->FieldByName("AVI_ID")->AsString;
                pObj->LINK_ID           = pADO->FieldByName("LINK_ID")->AsString;
                pObj->AVI_LCTN_NM       = pADO->FieldByName("AVI_LCTN_NM")->AsString;
                pObj->AVI_VALD_YN       = pADO->FieldByName("AVI_VALD_YN")->AsString;
                pObj->AVI_CTLR_IP       = pADO->FieldByName("AVI_CTLR_IP")->AsString;
                pObj->AVI_CTLR_CMNC_PORT= pADO->FieldByName("AVI_CTLR_CMNC_PORT")->AsString;
                pObj->AVI_MAIN_CTLR_YN  = pADO->FieldByName("AVI_MAIN_CTLR_YN")->AsString;
                pObj->AVI_MNGM_LANE_NUM = pADO->FieldByName("AVI_MNGM_LANE_NUM")->AsString;
                pObj->AVI_CMRA_NUM      = pADO->FieldByName("AVI_CMRA_NUM")->AsString;
                pObj->AVI_DTCT_NUM      = pADO->FieldByName("AVI_DTCT_NUM")->AsString;
                pObj->AVI_DTCT_NM       = pADO->FieldByName("AVI_DTCT_NM")->AsString;
                pObj->AVI_CLCT_CYCL     = pADO->FieldByName("AVI_CLCT_CYCL")->AsString;
                pObj->AVI_LAMP_AUTO_YN  = pADO->FieldByName("AVI_LAMP_AUTO_YN")->AsString;
                pObj->AVI_LAMP_ON_HMS   = pADO->FieldByName("AVI_LAMP_ON_HMS")->AsString;
                pObj->AVI_LAMP_OFF_HMS  = pADO->FieldByName("AVI_LAMP_OFF_HMS")->AsString;
                pObj->AVI_LAMP_ON_YN    = pADO->FieldByName("AVI_LAMP_ON_YN")->AsString;
                pObj->X_CRDN            = pADO->FieldByName("X_CRDN")->AsString;
                pObj->Y_CRDN            = pADO->FieldByName("Y_CRDN")->AsString;
                pObj->RMRK              = pADO->FieldByName("RMRK")->AsString;
                pObj->DEL_YN            = pADO->FieldByName("DEL_YN")->AsString;

				FLists.Push(pObj->AVI_CTLR_MNGM_NMBR, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::LoadFromDb", 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("TItsAviManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT *                 \r\n"
           "  FROM TB_AVI_DTCT_MSTR  \r\n";
           //" WHERE DEL_YN = 'N'      \r\n"
           //" ORDER BY AVI_CTLR_MNGM_NMBR, AVI_DTCT_NMBR      \r\n";
    try
    {
        TItsAvi *pAvi;
        FLists.Lock();
        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 CTLR_MNGM_NMBR = pADO->FieldByName("AVI_CTLR_MNGM_NMBR")->AsString;
                pAvi = FLists.Find(CTLR_MNGM_NMBR);
                if (!pAvi) continue;

				TItsAviDtct *pObj = new TItsAviDtct();

                pObj->CTLR_MNGM_NMBR = CTLR_MNGM_NMBR;
                pObj->DTCT_NMBR      = pADO->FieldByName("AVI_DTCT_NMBR")->AsString;
                pObj->DTCT_NM        = pADO->FieldByName("AVI_DTCT_NM")->AsString;
                pObj->DTCT_TYPE_CD   = pADO->FieldByName("DTCT_TYPE_CD")->AsString;
                pObj->ISTL_LANE      = pADO->FieldByName("ISTL_LANE")->AsString;
                pObj->USE_YN         = pADO->FieldByName("USE_YN")->AsString;
                pObj->DEL_YN         = pADO->FieldByName("DEL_YN")->AsString;

                pObj->MAX_TFVL       = pADO->FieldByName("MAX_TFVL")->AsInteger;
                pObj->MIN_TFVL       = pADO->FieldByName("MIN_TFVL")->AsInteger;
                pObj->MAX_OCPY_RATE  = pADO->FieldByName("MAX_OCPY_RATE")->AsFloat;
                pObj->MIN_OCPY_RATE  = pADO->FieldByName("MIN_OCPY_RATE")->AsFloat;
                pObj->MIN_SPED       = pADO->FieldByName("MIN_SPED")->AsInteger;
                pObj->MAX_SPED       = pADO->FieldByName("MAX_SPED")->AsInteger;

				pAvi->FDtctLists.Push(pObj->DTCT_NMBR, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::LoadFromDtctDb", 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("TItsAviManager::LoadFromDtctDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT A.AVI_CTLR_MNGM_NMBR,             \r\n"
           "       A.AVI_SECT_NMBR,                  \r\n"
           "       A.ORD,                            \r\n"
           "       B.AVI_SECT_NMBR,                  \r\n"
           "       B.STRT_SPOT_NM,                   \r\n"
           "       B.END_SPOT_NM,                    \r\n"
           "       B.AVI_SECT_NM,                    \r\n"
           "       B.AVI_SECT_DSTC,                  \r\n"
           "       B.SHRT_PASS_HH,                   \r\n"
           "       B.LNST_PASS_HH,                   \r\n"
           "       B.SPOT_NUM,                       \r\n"
           "       B.STND_DRFT_VAL,                  \r\n"
           "       B.MIN_SPED,                       \r\n"
           "       B.MAX_SPED,                       \r\n"
           "       B.SMTH_FCTR,                      \r\n"
           "       B.DEL_YN                          \r\n"
           "  FROM TB_AVI_SECT_CNFG A,               \r\n"
           "       TB_AVI_SECT_MSTR B                \r\n"
           " WHERE A.AVI_SECT_NMBR = B.AVI_SECT_NMBR \r\n"
           " ORDER BY A.AVI_CTLR_MNGM_NMBR, A.ORD    \r\n";
    try
    {
        TItsAvi *pAvi;
        FLists.Lock();
        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 AVI_CTLR_MNGM_NMBR = pADO->FieldByName("AVI_CTLR_MNGM_NMBR")->AsString;

				pAvi = FLists.Find(AVI_CTLR_MNGM_NMBR);
				if (!pAvi) continue;

				TItsAviSect *pObj = new TItsAviSect();

                pObj->AVI_CTLR_MNGM_NMBR = AVI_CTLR_MNGM_NMBR;
                pObj->ORD             = pADO->FieldByName("ORD")->AsInteger;

                pObj->AVI_SECT_NMBR   = pADO->FieldByName("AVI_SECT_NMBR")->AsString;
                pObj->STRT_SPOT_NM    = pADO->FieldByName("STRT_SPOT_NM")->AsString;
                pObj->END_SPOT_NM     = pADO->FieldByName("END_SPOT_NM")->AsString;
                pObj->AVI_SECT_NM     = pADO->FieldByName("AVI_SECT_NM")->AsString;
                pObj->AVI_SECT_DSTC   = pADO->FieldByName("AVI_SECT_DSTC")->AsString;
                pObj->SHRT_PASS_HH    = pADO->FieldByName("SHRT_PASS_HH")->AsString;
                pObj->LNST_PASS_HH    = pADO->FieldByName("LNST_PASS_HH")->AsString;
                pObj->SPOT_NUM        = pADO->FieldByName("SPOT_NUM")->AsString;
                pObj->STND_DRFT_VAL   = pADO->FieldByName("STND_DRFT_VAL")->AsString;
                pObj->MIN_SPED        = pADO->FieldByName("MIN_SPED")->AsString;
                pObj->MAX_SPED        = pADO->FieldByName("MAX_SPED")->AsString;
                pObj->SMTH_FCTR       = pADO->FieldByName("SMTH_FCTR")->AsString;
                pObj->DEL_YN          = pADO->FieldByName("DEL_YN")->AsString;

				pAvi->FSubLists.Push(pObj->AVI_SECT_NMBR, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";

                TItsAviSect *pTmpObj = FSectLists.Find(pObj->AVI_SECT_NMBR);
                if (!pTmpObj)
                {
                    TItsAviSect *pTmpObj = new TItsAviSect();

                    pTmpObj->AVI_CTLR_MNGM_NMBR = pObj->AVI_CTLR_MNGM_NMBR;
                    pTmpObj->ORD             = pObj->ORD;

                    pTmpObj->AVI_SECT_NMBR   = pObj->AVI_SECT_NMBR;
                    pTmpObj->STRT_SPOT_NM    = pObj->STRT_SPOT_NM;
                    pTmpObj->END_SPOT_NM     = pObj->END_SPOT_NM;
                    pTmpObj->AVI_SECT_NM     = pObj->AVI_SECT_NM;
                    pTmpObj->AVI_SECT_DSTC   = pObj->AVI_SECT_DSTC;
                    pTmpObj->SHRT_PASS_HH    = pObj->SHRT_PASS_HH;
                    pTmpObj->LNST_PASS_HH    = pObj->LNST_PASS_HH;
                    pTmpObj->SPOT_NUM        = pObj->SPOT_NUM;
                    pTmpObj->STND_DRFT_VAL   = pObj->STND_DRFT_VAL;
                    pTmpObj->MIN_SPED        = pObj->MIN_SPED;
                    pTmpObj->MAX_SPED        = pObj->MAX_SPED;
                    pTmpObj->SMTH_FCTR       = pObj->SMTH_FCTR;
                    pTmpObj->DEL_YN          = pObj->DEL_YN;

                    pTmpObj->Completed = true;
                    pTmpObj->MEM_DEL   = "N";

                    FSectLists.Push(pTmpObj->AVI_SECT_NMBR, pTmpObj);
                }

            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::LoadFromSectDb", 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("TItsAviManager::LoadFromSectDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT A.AVI_CTLR_MNGM_NMBR,                           \r\n"
           "       A.AVI_SECT_NMBR,                                \r\n"
           "       B.LINK_ID,                                      \r\n"
           "       B.ORD                                           \r\n"
           "  FROM TB_AVI_SECT_CNFG A,                             \r\n"
           "       TB_AVI_SECT_LINK B                              \r\n"
           " WHERE A.AVI_SECT_NMBR = B.AVI_SECT_NMBR               \r\n"
           " ORDER BY A.AVI_CTLR_MNGM_NMBR, A.AVI_SECT_NMBR, B.ORD \r\n";
    try
    {
        TItsAvi     *pAvi;
        TItsAviSect *pAviSect;
        FLists.Lock();
        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 AVI_CTLR_MNGM_NMBR = pADO->FieldByName("AVI_CTLR_MNGM_NMBR")->AsString;
				pAvi = FLists.Find(AVI_CTLR_MNGM_NMBR);
				if (!pAvi) continue;

                String AVI_SECT_NMBR = pADO->FieldByName("AVI_SECT_NMBR")->AsString;
				pAviSect = pAvi->FSubLists.Find(AVI_SECT_NMBR);
				if (!pAviSect) continue;

				TItsAviLink *pObj = new TItsAviLink();

                pObj->AVI_CTLR_MNGM_NMBR = AVI_CTLR_MNGM_NMBR;
                pObj->AVI_SECT_NMBR      = AVI_SECT_NMBR;
                pObj->LINK_ID            = pADO->FieldByName("LINK_ID")->AsString;
                pObj->ORD                = pADO->FieldByName("ORD")->AsInteger;

				pAviSect->FLinkLists.Push(pObj->ORD, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::LoadFromSectLinkDb", 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("TItsAviManager::LoadFromSectLinkDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TItsAviManager::GetNextAviId(String &ANewId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_AVI_CTLR)                          \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(AVI_CTLR_MNGM_NMBR)) + 1) \r\n"
           "                                     FROM TB_AVI_CTLR)                                 \r\n"
           "                             ELSE '1' END) NEWID                                       \r\n"
           " FROM DUAL                                                                             \r\n";

    ANewId = "";
    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();
            ANewId = pADO->FieldByName("NEWID")->AsString;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::GetNextAviId", 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("TItsAviManager::GetNextAviId", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsAviManager::GetNextAviSectId(String &ANewId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_AVI_SECT_MSTR)                \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(AVI_SECT_NMBR)) + 1) \r\n"
           "                                     FROM TB_AVI_SECT_MSTR)                       \r\n"
           "                             ELSE '1' END) NEWID                                  \r\n"
           " FROM DUAL                                                                        \r\n";

    ANewId = "";
    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();
            ANewId = pADO->FieldByName("NEWID")->AsString;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::GetNextAviSectId", 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("TItsAviManager::GetNextAviSectId", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsAviManager::DeleteAvi(TItsAvi *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_AVI_CTLR   \r\n"
           "   SET DEL_YN = 'Y'  \r\n"
           " WHERE ID = :p01     \r\n";
    try
    {
        FLists.Lock();
        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->AVI_CTLR_MNGM_NMBR;
			pADO->ExecSQL();

        sQry = "DELETE TB_FCLT_INFR      \r\n"
               " WHERE FCLT_ID = :p01    \r\n";
               //"   AND FCLT_TYPE = 'AVI' \r\n";

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::DeleteAvi", 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("TItsAviManager::DeleteAvi", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TItsAviManager::UpdateAvi(TItsAvi *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_AVI_CTLR                 \r\n"
           "   SET AVI_ID              = :p02, \r\n"
           "       LINK_ID             = :p03, \r\n"
           "       AVI_LCTN_NM         = :p04, \r\n"
           "       AVI_VALD_YN         = :p05, \r\n"
           "       AVI_CTLR_IP         = :p06, \r\n"
           "       AVI_CTLR_CMNC_PORT  = :p07, \r\n"
           "       AVI_MAIN_CTLR_YN    = :p08, \r\n"
           "       AVI_MNGM_LANE_NUM   = :p09, \r\n"
           "       AVI_CMRA_NUM        = :p10, \r\n"
           "       AVI_DTCT_NUM        = :p11, \r\n"
           "       AVI_DTCT_NM         = :p12, \r\n"
           "       AVI_CLCT_CYCL       = :p13, \r\n"
           "       AVI_LAMP_AUTO_YN    = :p14, \r\n"
           "       AVI_LAMP_ON_HMS     = :p15, \r\n"
           "       AVI_LAMP_OFF_HMS    = :p16, \r\n"
           "       AVI_LAMP_ON_YN      = :p17, \r\n"
           "       X_CRDN              = :p18, \r\n"
           "       Y_CRDN              = :p19, \r\n"
           "       RMRK                = :p20, \r\n"
           "       DEL_YN              = :p21  \r\n"
           " WHERE AVI_CTLR_MNGM_NMBR  = :p01  \r\n";
    try
    {
        FLists.Lock();
        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->AVI_CTLR_MNGM_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = AObj->AVI_ID;
			pADO->Parameters->ParamByName("p03")->Value = AObj->LINK_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->AVI_LCTN_NM;
			pADO->Parameters->ParamByName("p05")->Value = AObj->AVI_VALD_YN;
			pADO->Parameters->ParamByName("p06")->Value = AObj->AVI_CTLR_IP;
			pADO->Parameters->ParamByName("p07")->Value = AObj->AVI_CTLR_CMNC_PORT;
			pADO->Parameters->ParamByName("p08")->Value = AObj->AVI_MAIN_CTLR_YN;
			pADO->Parameters->ParamByName("p09")->Value = AObj->AVI_MNGM_LANE_NUM;
			pADO->Parameters->ParamByName("p10")->Value = AObj->AVI_CMRA_NUM;
			pADO->Parameters->ParamByName("p11")->Value = AObj->AVI_DTCT_NUM;
			pADO->Parameters->ParamByName("p12")->Value = AObj->AVI_DTCT_NM;
			pADO->Parameters->ParamByName("p13")->Value = AObj->AVI_CLCT_CYCL;
			pADO->Parameters->ParamByName("p14")->Value = AObj->AVI_LAMP_AUTO_YN;
			pADO->Parameters->ParamByName("p15")->Value = AObj->AVI_LAMP_ON_HMS;
			pADO->Parameters->ParamByName("p16")->Value = AObj->AVI_LAMP_OFF_HMS;
			pADO->Parameters->ParamByName("p17")->Value = AObj->AVI_LAMP_ON_YN;
			pADO->Parameters->ParamByName("p18")->Value = AObj->X_CRDN;
			pADO->Parameters->ParamByName("p19")->Value = AObj->Y_CRDN;
			pADO->Parameters->ParamByName("p20")->Value = AObj->RMRK;
			pADO->Parameters->ParamByName("p21")->Value = AObj->DEL_YN;

			pADO->ExecSQL();

        sQry = "UPDATE TB_FCLT_INFR       \r\n"
               "   SET FCLT_LCTN  = :p02  \r\n"
               " WHERE FCLT_ID    = :p01  \r\n";
               //"   AND FCLT_TYPE = 'AVI' \r\n";

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::UpdateAvi", 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("TItsAviManager::UpdateAvi", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TItsAviManager::InsertAvi(TItsAvi *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_AVI_CTLR(AVI_CTLR_MNGM_NMBR, AVI_ID, LINK_ID, AVI_LCTN_NM, AVI_VALD_YN, AVI_CTLR_IP,  \r\n"
           "                        AVI_CTLR_CMNC_PORT, AVI_MAIN_CTLR_YN, AVI_MNGM_LANE_NUM, AVI_CMRA_NUM,       \r\n"
           "                        AVI_DTCT_NUM, AVI_DTCT_NM, AVI_CLCT_CYCL, AVI_LAMP_AUTO_YN, AVI_LAMP_ON_HMS, \r\n"
           "                        AVI_LAMP_OFF_HMS, AVI_LAMP_ON_YN, X_CRDN, Y_CRDN, RMRK, DEL_YN )             \r\n"
           "                 VALUES(:p01,         :p02,   :p03,         :p04,        :p05,     :p06,             \r\n"
           "                        :p07,         :p08,   :p09,         :p10,                                    \r\n"
           "                        :p11,         :p12,   :p13,         :p14,        :p15,                       \r\n"
           "                        :p16,         :p17,   :p18,         :p19,        :p20,     :p21 )            \r\n";
    try
    {
        FLists.Lock();
        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->AVI_CTLR_MNGM_NMBR;
			pADO->Parameters->ParamByName("p01")->Value = AObj->AVI_CTLR_MNGM_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = AObj->AVI_ID;
			pADO->Parameters->ParamByName("p03")->Value = AObj->LINK_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->AVI_LCTN_NM;
			pADO->Parameters->ParamByName("p05")->Value = AObj->AVI_VALD_YN;
			pADO->Parameters->ParamByName("p06")->Value = AObj->AVI_CTLR_IP;
			pADO->Parameters->ParamByName("p07")->Value = AObj->AVI_CTLR_CMNC_PORT;
			pADO->Parameters->ParamByName("p08")->Value = AObj->AVI_MAIN_CTLR_YN;
			pADO->Parameters->ParamByName("p09")->Value = AObj->AVI_MNGM_LANE_NUM;
			pADO->Parameters->ParamByName("p10")->Value = AObj->AVI_CMRA_NUM;
			pADO->Parameters->ParamByName("p11")->Value = AObj->AVI_DTCT_NUM;
			pADO->Parameters->ParamByName("p12")->Value = AObj->AVI_DTCT_NM;
			pADO->Parameters->ParamByName("p13")->Value = AObj->AVI_CLCT_CYCL;
			pADO->Parameters->ParamByName("p14")->Value = AObj->AVI_LAMP_AUTO_YN;
			pADO->Parameters->ParamByName("p15")->Value = AObj->AVI_LAMP_ON_HMS;
			pADO->Parameters->ParamByName("p16")->Value = AObj->AVI_LAMP_OFF_HMS;
			pADO->Parameters->ParamByName("p17")->Value = AObj->AVI_LAMP_ON_YN;
			pADO->Parameters->ParamByName("p18")->Value = AObj->X_CRDN;
			pADO->Parameters->ParamByName("p19")->Value = AObj->Y_CRDN;
			pADO->Parameters->ParamByName("p20")->Value = AObj->RMRK;
			pADO->Parameters->ParamByName("p21")->Value = AObj->DEL_YN;
			pADO->ExecSQL();

        sQry = "INSERT INTO TB_FCLT_INFR(FCLT_ID, FCLT_TYPE, FCLT_LCTN, RSCE_YN, DEL_YN, USE_YEAR, AMT) \r\n"
               "                  VALUES(:p01,    'AVI',     :p03,      'N',     'N',    '0',      '0') \r\n";

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::InsertAvi", 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("TItsAviManager::InsertAvi", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TItsAviManager::DeleteAviSectSubTable(TItsAviSect *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 = "DELETE TB_AVI_SECT_LINK     \r\n"
               " WHERE AVI_SECT_NMBR = :p01 \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->AVI_SECT_NMBR;
			pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::DeleteAviSectSubTable", 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("TItsAviManager::DeleteAviSectSubTable", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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


bool TItsAviManager::DeleteAviSect(TItsAviSect *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_AVI_SECT_MSTR      \r\n"
           "   SET DEL_YN = 'Y'          \r\n"
           " WHERE AVI_SECT_NMBR = :p01  \r\n";
    try
    {
        DeleteAviSectSubTable(AObj, ADbConn);
        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->AVI_SECT_NMBR;
			pADO->ExecSQL();


        sQry = "DELETE TB_AVI_SECT_CNFG          \r\n"
               " WHERE AVI_SECT_NMBR = :p01      \r\n"
               "   AND AVI_CTLR_MNGM_NMBR = :p02 \r\n";

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::DeleteAviSect", 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("TItsAviManager::DeleteAviSect", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TItsAviManager::SaveAviSect(TItsAviSect *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_AVI_SECT_MSTR L                            \r\n"
           "USING (SELECT :p01 AS AVI_SECT_NMBR,                     \r\n"
           "              :p02 AS STRT_SPOT_NM,                      \r\n"
           "              :p03 AS END_SPOT_NM,                       \r\n"
           "              :p04 AS AVI_SECT_NM,                       \r\n"
           "              :p05 AS AVI_SECT_DSTC,                     \r\n"
           "              :p06 AS SHRT_PASS_HH,                      \r\n"
           "              :p07 AS LNST_PASS_HH,                      \r\n"
           "              :p08 AS SPOT_NUM,                          \r\n"
           "              :p09 AS STND_DRFT_VAL,                     \r\n"
           "              :p10 AS MIN_SPED,                          \r\n"
           "              :p11 AS MAX_SPED,                          \r\n"
           "              :p12 AS SMTH_FCTR,                         \r\n"
           "              :p13 AS DEL_YN                             \r\n"
           "         FROM DUAL) M                                    \r\n"
           "   ON (L.AVI_SECT_NMBR = M.AVI_SECT_NMBR)                \r\n"
           "WHEN MATCHED THEN                                        \r\n"
           "UPDATE SET STRT_SPOT_NM  = M.STRT_SPOT_NM,               \r\n"
           "           END_SPOT_NM   = M.END_SPOT_NM,                \r\n"
           "           AVI_SECT_NM   = M.AVI_SECT_NM,                \r\n"
           "           AVI_SECT_DSTC = M.AVI_SECT_DSTC,              \r\n"
           "           SHRT_PASS_HH  = M.SHRT_PASS_HH,               \r\n"
           "           LNST_PASS_HH  = M.LNST_PASS_HH,               \r\n"
           "           SPOT_NUM      = M.SPOT_NUM,                   \r\n"
           "           STND_DRFT_VAL = M.STND_DRFT_VAL,              \r\n"
           "           MIN_SPED      = M.MIN_SPED,                   \r\n"
           "           MAX_SPED      = M.MAX_SPED,                   \r\n"
           "           SMTH_FCTR     = M.SMTH_FCTR,                  \r\n"
           "           DEL_YN        = M.DEL_YN                      \r\n"
           "WHEN NOT MATCHED THEN                                    \r\n"
           "INSERT (AVI_SECT_NMBR, STRT_SPOT_NM, END_SPOT_NM,        \r\n"
           "        AVI_SECT_NM, AVI_SECT_DSTC, SHRT_PASS_HH,        \r\n"
           "        LNST_PASS_HH, SPOT_NUM, STND_DRFT_VAL,           \r\n"
           "        MIN_SPED, MAX_SPED, SMTH_FCTR, DEL_YN)           \r\n"
           "VALUES (M.AVI_SECT_NMBR, M.STRT_SPOT_NM, M.END_SPOT_NM,  \r\n"
           "        M.AVI_SECT_NM, M.AVI_SECT_DSTC, M.SHRT_PASS_HH,  \r\n"
           "        M.LNST_PASS_HH, M.SPOT_NUM, M.STND_DRFT_VAL,     \r\n"
           "        M.MIN_SPED, M.MAX_SPED, M.SMTH_FCTR, M.DEL_YN)   \r\n";

    try
    {
        //관련정보를 모두 삭제
        DeleteAviSectSubTable(AObj, ADbConn);
        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->AVI_SECT_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = AObj->STRT_SPOT_NM;
			pADO->Parameters->ParamByName("p03")->Value = AObj->END_SPOT_NM;
			pADO->Parameters->ParamByName("p04")->Value = AObj->AVI_SECT_NM;
			pADO->Parameters->ParamByName("p05")->Value = AObj->AVI_SECT_DSTC;
			pADO->Parameters->ParamByName("p06")->Value = AObj->SHRT_PASS_HH;
			pADO->Parameters->ParamByName("p07")->Value = AObj->LNST_PASS_HH;
			pADO->Parameters->ParamByName("p08")->Value = AObj->SPOT_NUM;
			pADO->Parameters->ParamByName("p09")->Value = AObj->STND_DRFT_VAL;
			pADO->Parameters->ParamByName("p10")->Value = AObj->MIN_SPED;
			pADO->Parameters->ParamByName("p11")->Value = AObj->MAX_SPED;
			pADO->Parameters->ParamByName("p12")->Value = AObj->SMTH_FCTR;
			pADO->Parameters->ParamByName("p13")->Value = AObj->DEL_YN;
			pADO->ExecSQL();

    sQry = "MERGE INTO TB_AVI_SECT_CNFG L                         \r\n"
           "USING (SELECT :p01 AS AVI_CTLR_MNGM_NMBR,             \r\n"
           "              :p02 AS AVI_SECT_NMBR,                  \r\n"
           "              :p03 AS ORD                             \r\n"
           "         FROM DUAL) M                                 \r\n"
           "   ON (L.AVI_CTLR_MNGM_NMBR = M.AVI_CTLR_MNGM_NMBR    \r\n"
           "       AND L.AVI_SECT_NMBR = M.AVI_SECT_NMBR)         \r\n"
           "WHEN MATCHED THEN                                     \r\n"
           "UPDATE SET ORD  = M.ORD                               \r\n"
           "WHEN NOT MATCHED THEN                                 \r\n"
           "INSERT (  AVI_CTLR_MNGM_NMBR,   AVI_SECT_NMBR,   ORD) \r\n"
           "VALUES (M.AVI_CTLR_MNGM_NMBR, M.AVI_SECT_NMBR, M.ORD) \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->AVI_CTLR_MNGM_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = AObj->AVI_SECT_NMBR;
			pADO->Parameters->ParamByName("p03")->Value = AObj->ORD;
			pADO->ExecSQL();

    //링크구성정보 입력
    sQry = "INSERT INTO TB_AVI_SECT_LINK (AVI_SECT_NMBR, LINK_ID, ORD) \r\n"
           "                      VALUES (:p01, :p02, :p03           ) \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            FOR_STL(TItsAviLink *, pObj, AObj->FLinkLists)
            {
                pADO->Parameters->ParamByName("p01")->Value = AObj->AVI_SECT_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pObj->LINK_ID;
                pADO->Parameters->ParamByName("p03")->Value = pObj->ORD;
                pADO->ExecSQL();
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAviManager::SaveAviSect", 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("TItsAviManager::SaveAviSect", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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


