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

#pragma hdrstop

#include "CDSDsrcOfferSectF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsDsrcOfferSectInfr::TItsDsrcOfferSectInfr()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsDsrcOfferSectInfr::Clear()
{
    OFFR_SECT_ID = "";
    IFSC_ID      = "";
    ORD          = 0;
}
//---------------------------------------------------------------------------
TItsDsrcOfferSectInfr::~TItsDsrcOfferSectInfr()
{
}
//---------------------------------------------------------------------------

TItsDsrcOfferSect::TItsDsrcOfferSect()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsDsrcOfferSect::Clear()
{
    OFFR_SECT_ID       = "";
    OFFR_DRCT_NM       = "";
    CNGS_BASI_SPED     = "";
    DELY_BASI_SPED     = "";
    ID                 = "";
    OBU_ENTR_DRCT_NMBR = "";

    PRE_ID             = "";
    IXR_DRCT_NUM       = "";
    TARGETNM           = "";
    PRENM              = "";
}
//---------------------------------------------------------------------------
TItsDsrcOfferSect::~TItsDsrcOfferSect()
{
    FSectLists.RemoveAll();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsDsrcOfferSectManager *ItsDsrcOfferSectManager = NULL;
//---------------------------------------------------------------------------

/*
* DsrcOfferSect Manager
*/
TItsDsrcOfferSectManager::TItsDsrcOfferSectManager()
{
}
//---------------------------------------------------------------------------
TItsDsrcOfferSectManager::~TItsDsrcOfferSectManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsDsrcOfferSectManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    FLists.RemoveAll();

    LoadSectMngr(ADbConn);
    LoadSectInfr(ADbConn);
    return true;
}
bool TItsDsrcOfferSectManager::LoadSectInfr(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT OFFR_SECT_ID,         \r\n"
           "       IFSC_ID,              \r\n"
           "       ORD                   \r\n"
           "  FROM TB_RSE_OFFR_SECT_INFR \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->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String sOffrSectId = pADO->FieldByName("OFFR_SECT_ID")->AsString;
                TItsDsrcOfferSect *pObj = FLists.Find(sOffrSectId);
                if (!pObj) continue;

				TItsDsrcOfferSectInfr *pSectObj = new TItsDsrcOfferSectInfr();

				pSectObj->OFFR_SECT_ID = pADO->FieldByName("OFFR_SECT_ID")->AsString;
				pSectObj->IFSC_ID      = pADO->FieldByName("IFSC_ID")->AsString;
				pSectObj->ORD          = pADO->FieldByName("ORD")->AsInteger;
				pSectObj->DEL_YN       = "N";

				pObj->FSectLists.Push(pSectObj->ORD, pSectObj);

                pSectObj->Completed = true;
                pSectObj->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 TItsDsrcOfferSectManager::LoadSectMngr(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT OFFR_SECT_ID,         \r\n"
           "       OFFR_DRCT_NM,         \r\n"
           "       CNGS_BASI_SPED,       \r\n"
           "       DELY_BASI_SPED,       \r\n"
           "       ID,                   \r\n"
           "       OBU_ENTR_DRCT_NMBR    \r\n"
           "  FROM TB_RSE_OFFR_SECT_MNGM \r\n";
           //" ORDER BY OFFR_SECT_ID       \r\n";
#else
    sQry = "SELECT A.OFFR_SECT_ID, A.OFFR_DRCT_NM, A.CNGS_BASI_SPED, A.DELY_BASI_SPED,    \r\n"
           "       A.ID, A.OBU_ENTR_DRCT_NMBR, C.PRE_ID, C.IXR_DRCT_NUM,                  \r\n"
           "       (SELECT ISTL_LCTN_NM FROM TB_RSE_MSTR WHERE ID = A.ID)     TARGETNM,   \r\n"
           "       (SELECT ISTL_LCTN_NM FROM TB_RSE_MSTR WHERE ID = C.PRE_ID) PRENM       \r\n"
           "  FROM TB_RSE_OFFR_SECT_MNGM A,                                               \r\n"
           "       TB_RSE_OFFR_DRCT_INFR C                                                \r\n"
           " WHERE A.ID = C.ID(+)                                                         \r\n"
           "  AND A.OBU_ENTR_DRCT_NMBR = C.OBU_ENTR_DRCT_NMBR(+)                          \r\n";
           //" ORDER BY TO_NUMBER(A.OFFR_SECT_ID)                                           \r\n";
#endif
    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())
            {
				TItsDsrcOfferSect *pObj = new TItsDsrcOfferSect();

				pObj->OFFR_SECT_ID       = pADO->FieldByName("OFFR_SECT_ID")->AsString;
				pObj->OFFR_DRCT_NM       = pADO->FieldByName("OFFR_DRCT_NM")->AsString;
				pObj->CNGS_BASI_SPED     = pADO->FieldByName("CNGS_BASI_SPED")->AsString;
				pObj->DELY_BASI_SPED     = pADO->FieldByName("DELY_BASI_SPED")->AsString;
				pObj->ID                 = pADO->FieldByName("ID")->AsString;
				pObj->OBU_ENTR_DRCT_NMBR = pADO->FieldByName("OBU_ENTR_DRCT_NMBR")->AsString;

				pObj->PRE_ID             = pADO->FieldByName("PRE_ID")->AsString;
				pObj->IXR_DRCT_NUM       = pADO->FieldByName("IXR_DRCT_NUM")->AsString;
				pObj->TARGETNM           = pADO->FieldByName("TARGETNM")->AsString;
				pObj->PRENM              = pADO->FieldByName("PRENM")->AsString;

				pObj->DEL_YN             = "N";

				FLists.Push(pObj->OFFR_SECT_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 TItsDsrcOfferSectManager::GetNextOffrSectId(String &ANewId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_RSE_OFFR_SECT_MNGM)          \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(OFFR_SECT_ID)) + 1) \r\n"
           "                                     FROM TB_RSE_OFFR_SECT_MNGM)                 \r\n"
           "                             ELSE '1' END) AS 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 TItsDsrcOfferSectManager::GetAutoRseDrctNum(String ARseId, int &AAutoNum, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_RSE_OFFR_SECT_MNGM)                 \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(OBU_ENTR_DRCT_NMBR)) + 1)  \r\n"
           "                                     FROM TB_RSE_OFFR_SECT_MNGM                         \r\n"
           "                                    WHERE ID = :p01)                                    \r\n"
           "                             ELSE '1' END) AS AUTONUM                                   \r\n"
           "  FROM DUAL                                                                             \r\n";

    AAutoNum = 0;
    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 = ARseId;

            pADO->Prepared = true;
			pADO->Open();
            AAutoNum = pADO->FieldByName("AUTONUM")->AsInteger;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("RSE제공구간 신규DRCT 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제공구간 신규DRCT ID조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsDsrcOfferSectManager::DeleteOfferSect(String AOfferSectId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    //정보제공구간목록
    sQry = "DELETE TB_RSE_OFFR_SECT_INFR WHERE OFFR_SECT_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 = AOfferSectId;
			pADO->ExecSQL();

    //정보제공이력-->외래키를 삭제하는것이 맞음
    sQry = "DELETE TB_RSE_OFFR_INFR_HS	 WHERE OFFR_SECT_ID = :p01 \r\n";

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

    //정보제공구간 마스터
    sQry = "DELETE TB_RSE_OFFR_SECT_MNGM WHERE OFFR_SECT_ID = :p01 \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AOfferSectId;
			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 TItsDsrcOfferSectManager::SaveOfferSect(TItsDsrcOfferSect *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    //정보제공구간 링크구성정보 삭제
    sQry = "DELETE FROM TB_RSE_OFFR_SECT_INFR WHERE OFFR_SECT_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->OFFR_SECT_ID;
			pADO->ExecSQL();

    //RSE 제공 방향 정보
    sQry = "MERGE INTO TB_RSE_OFFR_DRCT_INFR L                            \r\n"
           "USING (SELECT :p01 AS ID,                                     \r\n"
           "              :p02 AS OBU_ENTR_DRCT_NMBR,                     \r\n"
           "              :p03 AS PRE_ID,                                 \r\n"
           "              :p04 AS IXR_DRCT_NUM                            \r\n"
           "         FROM DUAL) M                                         \r\n"
           "   ON (L.ID = M.ID                                            \r\n"
           "   AND L.OBU_ENTR_DRCT_NMBR = M.OBU_ENTR_DRCT_NMBR)           \r\n"
           "WHEN MATCHED THEN                                             \r\n"
           "UPDATE SET PRE_ID       = M.PRE_ID,                           \r\n"
           "           IXR_DRCT_NUM = M.IXR_DRCT_NUM                      \r\n"
           "WHEN NOT MATCHED THEN                                         \r\n"
           "INSERT (ID, OBU_ENTR_DRCT_NMBR, PRE_ID, IXR_DRCT_NUM)         \r\n"
           "VALUES (M.ID, M.OBU_ENTR_DRCT_NMBR, M.PRE_ID, M.IXR_DRCT_NUM) \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->OBU_ENTR_DRCT_NMBR;
			pADO->Parameters->ParamByName("p03")->Value = AObj->PRE_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->IXR_DRCT_NUM;
			pADO->ExecSQL();

    //정보제공구간 마스터
    sQry = "MERGE INTO TB_RSE_OFFR_SECT_MNGM L                         \r\n"
           "USING (SELECT :p01 AS OFFR_SECT_ID,                        \r\n"
           "              :p02 AS OFFR_DRCT_NM,                        \r\n"
           "              :p03 AS CNGS_BASI_SPED,                      \r\n"
           "              :p04 AS DELY_BASI_SPED,                      \r\n"
           "              :p05 AS ID,                                  \r\n"
           "              :p06 AS OBU_ENTR_DRCT_NMBR                   \r\n"
           "         FROM DUAL) M                                      \r\n"
           "  ON (L.OFFR_SECT_ID = M.OFFR_SECT_ID)                     \r\n"
           "WHEN MATCHED THEN                                          \r\n"
           "   UPDATE SET OFFR_DRCT_NM       = M.OFFR_DRCT_NM,         \r\n"
           "              CNGS_BASI_SPED     = M.CNGS_BASI_SPED,       \r\n"
           "              DELY_BASI_SPED     = M.DELY_BASI_SPED,       \r\n"
           "              ID                 = M.ID,                   \r\n"
           "              OBU_ENTR_DRCT_NMBR = M.OBU_ENTR_DRCT_NMBR    \r\n"
           "WHEN NOT MATCHED THEN                                      \r\n"
           "INSERT (OFFR_SECT_ID, OFFR_DRCT_NM, CNGS_BASI_SPED,        \r\n"
           "       DELY_BASI_SPED,ID, OBU_ENTR_DRCT_NMBR)              \r\n"
           "VALUES (M.OFFR_SECT_ID, M.OFFR_DRCT_NM, M.CNGS_BASI_SPED,  \r\n"
           "       M.DELY_BASI_SPED, M.ID, M.OBU_ENTR_DRCT_NMBR)       \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->OFFR_SECT_ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->OFFR_DRCT_NM;
			pADO->Parameters->ParamByName("p03")->Value = AObj->CNGS_BASI_SPED;
			pADO->Parameters->ParamByName("p04")->Value = AObj->DELY_BASI_SPED;
			pADO->Parameters->ParamByName("p05")->Value = AObj->ID;
			pADO->Parameters->ParamByName("p06")->Value = AObj->OBU_ENTR_DRCT_NMBR;
			pADO->ExecSQL();

    //정보제공구간 링크구성정보 입력
    sQry = "INSERT INTO TB_RSE_OFFR_SECT_INFR (OFFR_SECT_ID, IFSC_ID, ORD)  \r\n"
           "                           VALUES (:p01,         :p02,    :p03) \r\n";

            int nOrd = 1;
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            FOR_STL(TItsDsrcOfferSectInfr *, pObj, AObj->FSectLists)
            {
                pADO->Parameters->ParamByName("p01")->Value = AObj->OFFR_SECT_ID;
                pADO->Parameters->ParamByName("p02")->Value = pObj->IFSC_ID;
                pADO->Parameters->ParamByName("p03")->Value = nOrd;
                pADO->ExecSQL();
                nOrd++;
            }
            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;
}
//---------------------------------------------------------------------------

