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

#pragma hdrstop

#include "CDSPrltCtlrF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TPrltCtlrManager *PrltCtlrManager = NULL;
//---------------------------------------------------------------------------

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

#ifdef USE_PARK2
    sQry = "SELECT *             \r\n"
           "  FROM TB_PRLT_CTLR  \r\n"
           " WHERE DEL_YN = 'N'  \r\n";
#else
    sQry = "SELECT *                \r\n"
           "  FROM TB_PRK_PLCE      \r\n"
           " WHERE pis_type != 'N'  \r\n"
           "   AND del_yn    = 'N'  \r\n";
#endif

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

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

                pObj->PRLT_CTLR_NMBR     = pADO->FieldByName("PRLT_CTLR_NMBR")->AsString;
                pObj->PRLT_CTLR_ID       = pADO->FieldByName("PRLT_CTLR_ID")->AsString;
                pObj->PRLT_NM            = pADO->FieldByName("PRLT_NM")->AsString;
                pObj->PRLT_CTLR_IP       = pADO->FieldByName("PRLT_CTLR_IP")->AsString;
                pObj->PRLT_CTLR_PORT     = pADO->FieldByName("PRLT_CTLR_PORT")->AsInteger;
                pObj->PRLT_CTLR_LOCAL_NO = pADO->FieldByName("PRLT_CTLR_LOCAL_NO")->AsInteger;
                //pObj->PRLT_TYPE_CD       = pADO->FieldByName("PRLT_TYPE_CD")->AsString;
                pObj->ISTL_LCTN_NM      = pADO->FieldByName("ISTL_LCTN_NM")->AsString;
                pObj->X_CRDN            = pADO->FieldByName("X_CRDN")->AsFloat;
                pObj->Y_CRDN            = pADO->FieldByName("Y_CRDN")->AsFloat;
                pObj->DEL_YN            = pADO->FieldByName("DEL_YN")->AsString;

                pObj->PRLT_OPER_DVSN_CD = pADO->FieldByName("PRLT_OPER_DVSN_CD")->AsString;
                pObj->PRLT_TYPE_DVSN_CD = pADO->FieldByName("PRLT_TYPE_DVSN_CD")->AsString;
                pObj->PRLT_TEL          = pADO->FieldByName("PRLT_TEL")->AsString;
                pObj->ISTL_LCTN_ADDR    = pADO->FieldByName("ISTL_LCTN_ADDR")->AsString;
                pObj->WHOL_NPLS         = pADO->FieldByName("WHOL_NPLS")->AsInteger;
                pObj->GNRL_NPLS         = pADO->FieldByName("GNRL_NPLS")->AsInteger;
                pObj->LGVH_NPLS         = pADO->FieldByName("LGVH_NPLS")->AsInteger;
                pObj->HVVH_NPLS         = pADO->FieldByName("HVVH_NPLS")->AsInteger;
                pObj->EMVH_NPLS         = pADO->FieldByName("EMVH_NPLS")->AsInteger;
                pObj->HNDC_NPLS         = pADO->FieldByName("HNDC_NPLS")->AsInteger;
                pObj->WMON_NPLS         = pADO->FieldByName("WMON_NPLS")->AsInteger;
                pObj->ELVH_NPLS         = pADO->FieldByName("ELVH_NPLS")->AsInteger;
                pObj->ETC_NPLS          = pADO->FieldByName("ETC_NPLS")->AsInteger;
                pObj->CNGT_NPLS         = pADO->FieldByName("CNGT_NPLS")->AsInteger;
                pObj->PRLT_CHARGE_DESC  = pADO->FieldByName("PRLT_CHARGE_DESC")->AsString;
                pObj->PRLT_OPER_DESC    = pADO->FieldByName("PRLT_OPER_DESC")->AsString;
                pObj->PRLT_ETC_DESC     = pADO->FieldByName("PRLT_ETC_DESC")->AsString;
                pObj->REAL_DATA_YN      = pADO->FieldByName("REAL_DATA_YN")->AsString;
                pObj->CTLR_CLCT_CYCL    = pADO->FieldByName("CTLR_CLCT_CYCL")->AsInteger;
                pObj->CTLR_STTS_CYCL    = pADO->FieldByName("CTLR_STTS_CYCL")->AsInteger;
                pObj->CHNG_DT           = pADO->FieldByName("CHNG_DT")->AsString;

                pObj->MEM_DEL           = "N";

				FLists.Push(pObj->PRLT_CTLR_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("TPrltCtlrManager::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("TPrltCtlrManager::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 TPrltCtlrManager::LoadStatusFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

#ifdef USE_PARK2
    sQry = "SELECT *                                                                \r\n"
           "  FROM TB_PRLT_CTLR_STTS A, TB_PRLT_CTLR B                              \r\n"
           " WHERE B.REAL_DATA_YN   = 'Y'                                           \r\n"
           "   AND A.PRLT_CTLR_NMBR = B.PRLT_CTLR_NMBR                              \r\n"
           "   AND A.UPDT_DT       >= TO_CHAR(SYSDATE-3/1440, 'YYYYMMDDHH24MISS')   \r\n";
#else
    sQry = "SELECT *                                                                \r\n"
           "  FROM TB_PRK_PLCE_STTS A, TB_PRK_PLCE B                                \r\n"
           " WHERE B.pis_type != 'N'                                                \r\n"
           "   AND B.del_yn    = 'N'                                                \r\n"
           "   AND A.prk_plce_nmbr = B.prk_plce_nmbr                                \r\n"
           "   AND A.UPDT_DT       >= TO_CHAR(SYSDATE-10/1440, 'YYYYMMDDHH24MISS')   \r\n";
#endif
    FLists.Lock();
    try
    {
        TPrltCtlr *pObj = NULL;

        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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sPrltNmbr = pADO->FieldByName("PRLT_CTLR_NMBR")->AsString;
                pObj = FLists.Find(sPrltNmbr);
                if (!pObj) continue;

                String UPDT_DT      = pADO->FieldByName("UPDT_DT")->AsString;
                String CONN_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                if (CONN_STTS_CD != "CMS0")
                {
                    pObj->STATE.Comm = park_comm_error;
                    //Åë½ÅÀå¾Ö
                    continue;
                }
                pObj->STATE.Comm = park_comm_normal;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TPrltCtlrManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TPrltCtlrManager::LoadStatusFromDb", 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 TPrltCtlrManager::GetNextPrltId(int &ANmbr, int &AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#ifdef USE_PARK2
    sQry = "SELECT NVL(MAX(PRLT_CTLR_NMBR), 10000) + 1 AS NEWID FROM TB_PRLT_CTLR \r\n";
#else
    sQry = "SELECT NVL(MAX(prk_plce_nmbr), 10000) + 1 AS NEWID FROM TB_PRK_PLCE \r\n";
#endif

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

            pADO->Prepared = true;
			pADO->Open();
            ANmbr = pADO->FieldByName("NEWID")->AsInteger;
            AId = ANmbr / 10000;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TPrltCtlrManager::GetNextPrltId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TPrltCtlrManager::GetNextPrltId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TPrltCtlrManager::DeletePrlt(TPrltCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#ifdef USE_PARK2
    sQry = "UPDATE TB_PRLT_CTLR           \r\n"
           "   SET DEL_YN = 'Y'           \r\n"
           " WHERE PRLT_CTLR_NMBR = :p01  \r\n";
#else
    sQry = "UPDATE TB_PRK_PLCE            \r\n"
           "   SET DEL_YN = 'Y'           \r\n"
           " WHERE prk_plce_nmbr = :p01   \r\n";
#endif

    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->PRLT_CTLR_NMBR;
			pADO->ExecSQL();

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

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrltCtlrManager::DeletePrlt", 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("TPrltCtlrManager::DeletePrlt", 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 TPrltCtlrManager::UpdatePrlt(TPrltCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergePrlt(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TPrltCtlrManager::MergePrlt(TPrltCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_PRLT_CTLR L                                   \r\n"
           "USING (SELECT :p01 AS PRLT_CTLR_NMBR,                       \r\n"
           "              :p02 AS PRLT_CTLR_ID,                         \r\n"
           "              :p03 AS PRLT_NM,                              \r\n"
           "              :p04 AS PRLT_CTLR_IP,                         \r\n"
           "              :p05 AS PRLT_CTLR_PORT,                       \r\n"
           "              :p06 AS PRLT_CTLR_LOCAL_NO,                   \r\n"
           "              :p07 AS PRLT_OPER_DVSN_CD,                    \r\n"
           "              :p08 AS PRLT_TYPE_DVSN_CD,                    \r\n"
           "              :p09 AS PRLT_TEL,                              \r\n"
           "              :p10 AS ISTL_LCTN_NM,                         \r\n"
           "              :p11 AS X_CRDN,                               \r\n"
           "              :p12 AS Y_CRDN,                               \r\n"
           "              :p13 AS DEL_YN,                               \r\n"
           "              :p14 AS ISTL_LCTN_ADDR,                       \r\n"
           "              :p15 AS WHOL_NPLS,                            \r\n"
           "              :p16 AS GNRL_NPLS,                            \r\n"
           "              :p17 AS LGVH_NPLS,                            \r\n"
           "              :p18 AS HVVH_NPLS,                            \r\n"
           "              :p19 AS EMVH_NPLS,                            \r\n"
           "              :p20 AS HNDC_NPLS,                            \r\n"
           "              :p21 AS WMON_NPLS,                            \r\n"
           "              :p22 AS ELVH_NPLS,                            \r\n"
           "              :p23 AS ETC_NPLS,                             \r\n"
           "              :p24 AS PRLT_CHARGE_DESC,                     \r\n"
           "              :p25 AS PRLT_OPER_DESC,                       \r\n"
           "              :p26 AS PRLT_ETC_DESC,                        \r\n"
           "              :p27 AS CTLR_CLCT_CYCL,                       \r\n"
           "              :p28 AS CTLR_STTS_CYCL,                       \r\n"
           "              :p29 AS REAL_DATA_YN,                         \r\n"
           "              :p30 AS CHNG_DT,                              \r\n"
           "              :p31 AS CNGT_NPLS                             \r\n"
           "         FROM DUAL) M                                       \r\n"
           "        ON (L.PRLT_CTLR_NMBR = M.PRLT_CTLR_NMBR)            \r\n"
           "WHEN MATCHED THEN                                           \r\n"
           "   UPDATE SET L.PRLT_CTLR_ID       = M.PRLT_CTLR_ID,        \r\n"
           "              L.PRLT_NM            = M.PRLT_NM,             \r\n"
           "              L.PRLT_CTLR_IP       = M.PRLT_CTLR_IP,        \r\n"
           "              L.PRLT_CTLR_PORT     = M.PRLT_CTLR_PORT,      \r\n"
           "              L.PRLT_CTLR_LOCAL_NO = M.PRLT_CTLR_LOCAL_NO,  \r\n"
           "              L.PRLT_OPER_DVSN_CD  = M.PRLT_OPER_DVSN_CD,   \r\n"
           "              L.PRLT_TYPE_DVSN_CD  = M.PRLT_TYPE_DVSN_CD,   \r\n"
           "              L.PRLT_TEL           = M.PRLT_TEL,            \r\n"
           "              L.ISTL_LCTN_NM       = M.ISTL_LCTN_NM,        \r\n"
           "              L.X_CRDN             = M.X_CRDN,              \r\n"
           "              L.Y_CRDN             = M.Y_CRDN,              \r\n"
           "              L.DEL_YN             = M.DEL_YN,              \r\n"
           "              L.ISTL_LCTN_ADDR     = M.ISTL_LCTN_ADDR,      \r\n"
           "              L.WHOL_NPLS          = M.WHOL_NPLS,           \r\n"
           "              L.GNRL_NPLS          = M.GNRL_NPLS,           \r\n"
           "              L.LGVH_NPLS          = M.LGVH_NPLS,           \r\n"
           "              L.HVVH_NPLS          = M.HVVH_NPLS,           \r\n"
           "              L.EMVH_NPLS          = M.EMVH_NPLS,           \r\n"
           "              L.HNDC_NPLS          = M.HNDC_NPLS,           \r\n"
           "              L.WMON_NPLS          = M.WMON_NPLS,           \r\n"
           "              L.ELVH_NPLS          = M.ELVH_NPLS,           \r\n"
           "              L.ETC_NPLS           = M.ETC_NPLS,            \r\n"
           "              L.PRLT_CHARGE_DESC   = M.PRLT_CHARGE_DESC,    \r\n"
           "              L.PRLT_OPER_DESC     = M.PRLT_OPER_DESC,      \r\n"
           "              L.PRLT_ETC_DESC      = M.PRLT_ETC_DESC,       \r\n"
           "              L.CTLR_CLCT_CYCL     = M.CTLR_CLCT_CYCL,      \r\n"
           "              L.CTLR_STTS_CYCL     = M.CTLR_STTS_CYCL,      \r\n"
           "              L.REAL_DATA_YN       = M.REAL_DATA_YN,        \r\n"
           "              L.CHNG_DT            = M.CHNG_DT,             \r\n"
           "              L.CNGT_NPLS          = M.CNGT_NPLS            \r\n"
           "WHEN NOT MATCHED THEN                                       \r\n"
           "   INSERT (PRLT_CTLR_NMBR,                                  \r\n"
           "           PRLT_CTLR_ID,                                    \r\n"
           "           PRLT_NM,                                         \r\n"
           "           PRLT_CTLR_IP,                                    \r\n"
           "           PRLT_CTLR_PORT,                                  \r\n"
           "           PRLT_CTLR_LOCAL_NO,                              \r\n"
           "           PRLT_OPER_DVSN_CD,                               \r\n"
           "           PRLT_TYPE_DVSN_CD,                               \r\n"
           "           PRLT_TEL,                                        \r\n"
           "           ISTL_LCTN_NM,                                    \r\n"
           "           X_CRDN,                                          \r\n"
           "           Y_CRDN,                                          \r\n"
           "           DEL_YN,                                          \r\n"
           "           ISTL_LCTN_ADDR,                                  \r\n"
           "           WHOL_NPLS,                                       \r\n"
           "           GNRL_NPLS,                                       \r\n"
           "           LGVH_NPLS,                                       \r\n"
           "           HVVH_NPLS,                                       \r\n"
           "           EMVH_NPLS,                                       \r\n"
           "           HNDC_NPLS,                                       \r\n"
           "           WMON_NPLS,                                       \r\n"
           "           ELVH_NPLS,                                       \r\n"
           "           ETC_NPLS,                                        \r\n"
           "           PRLT_CHARGE_DESC,                                \r\n"
           "           PRLT_OPER_DESC,                                  \r\n"
           "           PRLT_ETC_DESC,                                   \r\n"
           "           CTLR_CLCT_CYCL,                                  \r\n"
           "           CTLR_STTS_CYCL,                                  \r\n"
           "           REAL_DATA_YN,                                    \r\n"
           "           CNGT_NPLS,                                       \r\n"
           "           CHNG_DT )                                        \r\n"
           "   VALUES (M.PRLT_CTLR_NMBR,                                \r\n"
           "           M.PRLT_CTLR_ID,                                  \r\n"
           "           M.PRLT_NM,                                       \r\n"
           "           M.PRLT_CTLR_IP,                                  \r\n"
           "           M.PRLT_CTLR_PORT,                                \r\n"
           "           M.PRLT_CTLR_LOCAL_NO,                            \r\n"
           "           M.PRLT_OPER_DVSN_CD,                             \r\n"
           "           M.PRLT_TYPE_DVSN_CD,                             \r\n"
           "           M.PRLT_TEL,                                      \r\n"
           "           M.ISTL_LCTN_NM,                                  \r\n"
           "           M.X_CRDN,                                        \r\n"
           "           M.Y_CRDN,                                        \r\n"
           "           M.DEL_YN,                                        \r\n"
           "           M.ISTL_LCTN_ADDR,                                \r\n"
           "           M.WHOL_NPLS,                                     \r\n"
           "           M.GNRL_NPLS,                                     \r\n"
           "           M.LGVH_NPLS,                                     \r\n"
           "           M.HVVH_NPLS,                                     \r\n"
           "           M.EMVH_NPLS,                                     \r\n"
           "           M.HNDC_NPLS,                                     \r\n"
           "           M.WMON_NPLS,                                     \r\n"
           "           M.ELVH_NPLS,                                     \r\n"
           "           M.ETC_NPLS,                                      \r\n"
           "           M.PRLT_CHARGE_DESC,                              \r\n"
           "           M.PRLT_OPER_DESC,                                \r\n"
           "           M.PRLT_ETC_DESC,                                 \r\n"
           "           M.CTLR_CLCT_CYCL,                                \r\n"
           "           M.CTLR_STTS_CYCL,                                \r\n"
           "           M.REAL_DATA_YN,                                  \r\n"
           "           M.CNGT_NPLS,                                     \r\n"
           "           M.CHNG_DT )                                      \r\n";

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

            AObj->CHNG_DT = Now().FormatString("yyyymmddhhnnss");

            pADO->Parameters->ParamByName("p01")->Value = AObj->PRLT_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->PRLT_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->PRLT_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->PRLT_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->PRLT_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->PRLT_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->PRLT_OPER_DVSN_CD;
            pADO->Parameters->ParamByName("p08")->Value = AObj->PRLT_TYPE_DVSN_CD;
            pADO->Parameters->ParamByName("p09")->Value = AObj->PRLT_TEL;
            pADO->Parameters->ParamByName("p10")->Value = AObj->ISTL_LCTN_NM;
            pADO->Parameters->ParamByName("p11")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p12")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p13")->Value = AObj->DEL_YN;
            pADO->Parameters->ParamByName("p14")->Value = AObj->ISTL_LCTN_ADDR;
            pADO->Parameters->ParamByName("p15")->Value = AObj->WHOL_NPLS;
            pADO->Parameters->ParamByName("p16")->Value = AObj->GNRL_NPLS;
            pADO->Parameters->ParamByName("p17")->Value = AObj->LGVH_NPLS;
            pADO->Parameters->ParamByName("p18")->Value = AObj->HVVH_NPLS;
            pADO->Parameters->ParamByName("p19")->Value = AObj->EMVH_NPLS;
            pADO->Parameters->ParamByName("p20")->Value = AObj->HNDC_NPLS;
            pADO->Parameters->ParamByName("p21")->Value = AObj->WMON_NPLS;
            pADO->Parameters->ParamByName("p22")->Value = AObj->ELVH_NPLS;
            pADO->Parameters->ParamByName("p23")->Value = AObj->ETC_NPLS;
            pADO->Parameters->ParamByName("p24")->Value = AObj->PRLT_CHARGE_DESC;
            pADO->Parameters->ParamByName("p25")->Value = AObj->PRLT_OPER_DESC;
            pADO->Parameters->ParamByName("p26")->Value = AObj->PRLT_ETC_DESC;
            pADO->Parameters->ParamByName("p27")->Value = AObj->CTLR_CLCT_CYCL;
            pADO->Parameters->ParamByName("p28")->Value = AObj->CTLR_STTS_CYCL;
            pADO->Parameters->ParamByName("p29")->Value = AObj->REAL_DATA_YN;
            pADO->Parameters->ParamByName("p30")->Value = AObj->CHNG_DT;
            pADO->Parameters->ParamByName("p31")->Value = AObj->CNGT_NPLS;

			pADO->ExecSQL();

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

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

bool TPrltCtlrManager::InsertPrlt(TPrltCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_PRLT_CTLR (      \r\n"
           "            PRLT_CTLR_NMBR,     \r\n"
           "            PRLT_CTLR_ID,       \r\n"
           "            PRLT_NM,            \r\n"
           "            PRLT_CTLR_IP,       \r\n"
           "            PRLT_CTLR_PORT,     \r\n"
           "            PRLT_CTLR_LOCAL_NO, \r\n"
           "            PRLT_OPER_DVSN_CD,  \r\n"
           "            PRLT_TYPE_DVSN_CD,  \r\n"
           "            PRLT_TEL,           \r\n"
           "            ISTL_LCTN_NM,       \r\n"
           "            X_CRDN,             \r\n"
           "            Y_CRDN,             \r\n"
           "            DEL_YN,             \r\n"
           "            ISTL_LCTN_ADDR,     \r\n"
           "            WHOL_NPLS,          \r\n"
           "            GNRL_NPLS,          \r\n"
           "            LGVH_NPLS,          \r\n"
           "            HVVH_NPLS,          \r\n"
           "            EMVH_NPLS,          \r\n"
           "            HNDC_NPLS,          \r\n"
           "            WMON_NPLS,          \r\n"
           "            ELVH_NPLS,          \r\n"
           "            ETC_NPLS,           \r\n"
           "            PRLT_CHARGE_DESC,   \r\n"
           "            PRLT_OPER_DESC,     \r\n"
           "            PRLT_ETC_DESC,      \r\n"
           "            CTLR_CLCT_CYCL,     \r\n"
           "            CTLR_STTS_CYCL,     \r\n"
           "            REAL_DATA_YN,       \r\n"
           "            CNGT_NPLS,          \r\n"
           "            CHNG_DT )           \r\n"
           "   VALUES (:p01,                \r\n"
           "           :p02,                \r\n"
           "           :p03,                \r\n"
           "           :p04,                \r\n"
           "           :p05,                \r\n"
           "           :p06,                \r\n"
           "           :p07,                \r\n"
           "           :p08,                \r\n"
           "           :p09,                \r\n"
           "           :p10,                \r\n"
           "           :p11,                \r\n"
           "           :p12,                \r\n"
           "           :p13,                \r\n"
           "           :p14,                \r\n"
           "           :p15,                \r\n"
           "           :p16,                \r\n"
           "           :p17,                \r\n"
           "           :p18,                \r\n"
           "           :p19,                \r\n"
           "           :p20,                \r\n"
           "           :p21,                \r\n"
           "           :p22,                \r\n"
           "           :p23,                \r\n"
           "           :p24,                \r\n"
           "           :p25,                \r\n"
           "           :p26,                \r\n"
           "           :p27,                \r\n"
           "           :p28,                \r\n"
           "           :p29,                \r\n";
           "           :p30,                \r\n";
           "           :p31 )               \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;

            AObj->CHNG_DT = Now().FormatString("yyyymmddhhnnss");

            pADO->Parameters->ParamByName("p01")->Value = AObj->PRLT_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->PRLT_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->PRLT_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->PRLT_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->PRLT_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->PRLT_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->PRLT_OPER_DVSN_CD;
            pADO->Parameters->ParamByName("p08")->Value = AObj->PRLT_TYPE_DVSN_CD;
            pADO->Parameters->ParamByName("p09")->Value = AObj->PRLT_TEL;
            pADO->Parameters->ParamByName("p10")->Value = AObj->ISTL_LCTN_NM;
            pADO->Parameters->ParamByName("p11")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p12")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p13")->Value = AObj->DEL_YN;
            pADO->Parameters->ParamByName("p14")->Value = AObj->ISTL_LCTN_ADDR;
            pADO->Parameters->ParamByName("p15")->Value = AObj->WHOL_NPLS;
            pADO->Parameters->ParamByName("p16")->Value = AObj->GNRL_NPLS;
            pADO->Parameters->ParamByName("p17")->Value = AObj->LGVH_NPLS;
            pADO->Parameters->ParamByName("p18")->Value = AObj->HVVH_NPLS;
            pADO->Parameters->ParamByName("p19")->Value = AObj->EMVH_NPLS;
            pADO->Parameters->ParamByName("p20")->Value = AObj->HNDC_NPLS;
            pADO->Parameters->ParamByName("p21")->Value = AObj->WMON_NPLS;
            pADO->Parameters->ParamByName("p22")->Value = AObj->ELVH_NPLS;
            pADO->Parameters->ParamByName("p23")->Value = AObj->ETC_NPLS;
            pADO->Parameters->ParamByName("p24")->Value = AObj->PRLT_CHARGE_DESC;
            pADO->Parameters->ParamByName("p25")->Value = AObj->PRLT_OPER_DESC;
            pADO->Parameters->ParamByName("p26")->Value = AObj->PRLT_ETC_DESC;
            pADO->Parameters->ParamByName("p27")->Value = AObj->CTLR_CLCT_CYCL;
            pADO->Parameters->ParamByName("p28")->Value = AObj->CTLR_STTS_CYCL;
            pADO->Parameters->ParamByName("p29")->Value = AObj->REAL_DATA_YN;
            pADO->Parameters->ParamByName("p30")->Value = AObj->CNGT_NPLS;
            pADO->Parameters->ParamByName("p31")->Value = AObj->CHNG_DT;

			pADO->ExecSQL();

            MergeFcltInfo(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrltCtlrManager::InsertPrlt", 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("TPrltCtlrManager::InsertPrlt", 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 TPrltCtlrManager::MergeFcltInfo(TPrltCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_FCLT_INFR L                                         \r\n"
           "USING (SELECT :p01 AS FCLT_ID,                                    \r\n"
           "              :p02 AS FCLT_TYPE,                                  \r\n"
           "              :p03 AS FCLT_LCTN                                   \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.FCLT_ID = M.FCLT_ID AND L.FCLT_TYPE = M.FCLT_TYPE)  \r\n"
           "WHEN MATCHED                                                      \r\n"
           "THEN                                                              \r\n"
           "   UPDATE SET L.FCLT_LCTN = M.FCLT_LCTN,                          \r\n"
           "              L.DEL_YN    = 'N'                                   \r\n"
           "WHEN NOT MATCHED                                                  \r\n"
           "THEN                                                              \r\n"
           "   INSERT (  FCLT_ID,   FCLT_TYPE,   FCLT_LCTN)                   \r\n"
           "   VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN)                   \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->PRLT_CTLR_ID;
			pADO->Parameters->ParamByName("p02")->Value = "PARK";
			pADO->Parameters->ParamByName("p03")->Value = AObj->PRLT_NM;
			pADO->ExecSQL();

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

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

