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


#pragma hdrstop

#include "CDSDsrcF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsDsrc::TItsDsrc()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsDsrc::Clear()
{
    ID           = "";
    RSE_ID       = "";
    ROAD_SPOT_ID = "";
    LOG_CNNC_ID  = "";
    LOG_CNNC_PW  = "";
    ISTL_LCTN_NM = "";
    LCTN_X       = "";
    LCTN_Y       = "";
    IP           = "";
    PORT         = "";
    MNFC_CMPY_CD = "";
    DEL_YN       = "";
    MEM_DEL      = "";
    CLCT_ABNR_BASI = 0;
}
//---------------------------------------------------------------------------
TItsDsrc::~TItsDsrc()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsDsrcManager *ItsDsrcManager = NULL;
//---------------------------------------------------------------------------

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

    sQry = "SELECT ID,              \r\n"
           "       RSE_ID,          \r\n"
           "       ROAD_SPOT_ID,    \r\n"
           "       LOG_CNNC_ID,     \r\n"
           "       LOG_CNNC_PW,     \r\n"
           "       ISTL_LCTN_NM,    \r\n"
           "       LCTN_X,          \r\n"
           "       LCTN_Y,          \r\n"
           "       IP,              \r\n"
           "       PORT,            \r\n"
           "       MNFC_CMPY_CD,    \r\n"
           "       DEL_YN,          \r\n"
           "       CLCT_ABNR_BASI   \r\n"
           "  FROM TB_RSE_MSTR      \r\n";
           //" WHERE DEL_YN = 'N'     \r\n";
           //" ORDER BY TO_NUMBER(ID) \r\n";
    try
    {
        FLists.Lock();
        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())
            {
				TItsDsrc *pObj = new TItsDsrc();

				pObj->ID            = pADO->FieldByName("ID")->AsString;
				pObj->RSE_ID        = pADO->FieldByName("RSE_ID")->AsString;
				pObj->ROAD_SPOT_ID  = pADO->FieldByName("ROAD_SPOT_ID")->AsString;
				pObj->LOG_CNNC_ID   = pADO->FieldByName("LOG_CNNC_ID")->AsString;
				pObj->LOG_CNNC_PW   = pADO->FieldByName("LOG_CNNC_PW")->AsString;
				pObj->ISTL_LCTN_NM  = pADO->FieldByName("ISTL_LCTN_NM")->AsString;
				pObj->LCTN_X        = pADO->FieldByName("LCTN_X")->AsString;
				pObj->LCTN_Y        = pADO->FieldByName("LCTN_Y")->AsString;
				pObj->IP            = pADO->FieldByName("IP")->AsString;
				pObj->PORT          = pADO->FieldByName("PORT")->AsString;
				pObj->MNFC_CMPY_CD  = pADO->FieldByName("MNFC_CMPY_CD")->AsString;
				pObj->DEL_YN        = pADO->FieldByName("DEL_YN")->AsString;
                pObj->CLCT_ABNR_BASI = pADO->FieldByName("CLCT_ABNR_BASI")->AsInteger;

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

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("RSE정보조회", 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("RSE정보조회", 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 TItsDsrcManager::GetNextRseId(String &ANewId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_RSE_MSTR)          \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(ID)) + 1) \r\n"
           "                                     FROM TB_RSE_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("RSE 신규ID조회", 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("RSE 신규ID조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsDsrcManager::DeleteDsrc(TItsDsrc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_RSE_MSTR   \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->ID;
			pADO->ExecSQL();

        //TODO: 세종시
        sQry = "UPDATE TB_FCLT_INFR   \r\n"
               "   SET DEL_YN = 'Y'   \r\n"
               " WHERE FCLT_ID = :p01 \r\n";
#if 0
        //TODO: 기타지역
        sQry = "DELETE TB_FCLT_INFR      \r\n"
               " WHERE FCLT_ID = :p01    \r\n";
               //"   AND FCLT_TYPE = 'DSRC' \r\n";
#endif

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

            //TODO: 세종시
			pADO->Parameters->ParamByName("p01")->Value = AObj->RSE_ID;
#if 0
            //TODO: 기타지역
			pADO->Parameters->ParamByName("p01")->Value = AObj->ID;
#endif
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("RSE정보삭제", 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("RSE정보삭제", 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 TItsDsrcManager::UpdateDsrc(String AOldRseId, TItsDsrc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_RSE_MSTR            \r\n"
           "   SET RSE_ID         = :p02, \r\n"
           "       ROAD_SPOT_ID   = :p03, \r\n"
           "       LOG_CNNC_ID    = :p04, \r\n"
           "       LOG_CNNC_PW    = :p05, \r\n"
           "       ISTL_LCTN_NM   = :p06, \r\n"
           "       LCTN_X         = :p07, \r\n"
           "       LCTN_Y         = :p08, \r\n"
           "       IP             = :p09, \r\n"
           "       PORT           = :p10, \r\n"
           "       MNFC_CMPY_CD   = :p11, \r\n"
           "       DEL_YN         = :p12, \r\n"
           "       CLCT_ABNR_BASI = :p13  \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;

            if (AObj->DEL_YN == "") AObj->DEL_YN = "N";
			pADO->Parameters->ParamByName("p01")->Value = AObj->ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->RSE_ID;
			pADO->Parameters->ParamByName("p03")->Value = AObj->ROAD_SPOT_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->LOG_CNNC_ID;
			pADO->Parameters->ParamByName("p05")->Value = AObj->LOG_CNNC_PW;
			pADO->Parameters->ParamByName("p06")->Value = AObj->ISTL_LCTN_NM;
			pADO->Parameters->ParamByName("p07")->Value = AObj->LCTN_X;
			pADO->Parameters->ParamByName("p08")->Value = AObj->LCTN_Y;
			pADO->Parameters->ParamByName("p09")->Value = AObj->IP;
			pADO->Parameters->ParamByName("p10")->Value = AObj->PORT;
			pADO->Parameters->ParamByName("p11")->Value = AObj->MNFC_CMPY_CD;
			pADO->Parameters->ParamByName("p12")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p13")->Value = AObj->CLCT_ABNR_BASI;
			pADO->ExecSQL();

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

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("RSE정보업데이트", 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("RSE정보업데이트", 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 TItsDsrcManager::InsertDsrc(TItsDsrc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_RSE_MSTR(ID,           RSE_ID, ROAD_SPOT_ID, LOG_CNNC_ID, LOG_CNNC_PW,  \r\n"
           "                        ISTL_LCTN_NM, LCTN_X, LCTN_Y,       IP,          PORT,         \r\n"
           "                        MNFC_CMPY_CD, DEL_YN, CLCT_ABNR_BASI)                          \r\n"
           "                 VALUES(:p01,         :p02,   :p03,         :p04,        :p05,         \r\n"
           "                        :p06,         :p07,   :p08,         :p09,        :p10,         \r\n"
           "                        :p11,         :p12,   :p13)                                    \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->ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->RSE_ID;
			pADO->Parameters->ParamByName("p03")->Value = AObj->ROAD_SPOT_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->LOG_CNNC_ID;
			pADO->Parameters->ParamByName("p05")->Value = AObj->LOG_CNNC_PW;
			pADO->Parameters->ParamByName("p06")->Value = AObj->ISTL_LCTN_NM;
			pADO->Parameters->ParamByName("p07")->Value = AObj->LCTN_X;
			pADO->Parameters->ParamByName("p08")->Value = AObj->LCTN_Y;
			pADO->Parameters->ParamByName("p09")->Value = AObj->IP;
			pADO->Parameters->ParamByName("p10")->Value = AObj->PORT;
			pADO->Parameters->ParamByName("p11")->Value = AObj->MNFC_CMPY_CD;
			pADO->Parameters->ParamByName("p12")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p13")->Value = AObj->CLCT_ABNR_BASI;
			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,    'DSRC',    :p03,      'N',     'N',    '0',      '0') \r\n";

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

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("RSE정보입력", 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("RSE정보입력", 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 TItsDsrcManager::UpdateClctAbnrBasi(int AClctAbnrBasi, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_RSE_MSTR            \r\n"
           "   SET CLCT_ABNR_BASI = :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 = AClctAbnrBasi;
			pADO->ExecSQL();

            FOR_STL(TItsDsrc*, pObj, FLists)
            {
                if (pObj->DEL_YN == "Y") continue;

                pObj->CLCT_ABNR_BASI = AClctAbnrBasi;
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("RSE정보업데이트", 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("RSE정보업데이트", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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


