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

#pragma hdrstop

#include "CDSDsrcSectF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsDsrcSectLink::TItsDsrcSectLink()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsDsrcSectLink::Clear()
{
    RSE_SECT_ID = "";
    LINK_ID     = "";
    LINK_ORD    = "";
}
//---------------------------------------------------------------------------
TItsDsrcSectLink::~TItsDsrcSectLink()
{
}
//---------------------------------------------------------------------------


TItsDsrcSect::TItsDsrcSect()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsDsrcSect::Clear()
{
    String RSE_SECT_ID  = "";
    String DSTC         = "";
    String RSE_SECT_NM  = "";
    String STRT_SPOT_NM = "";
    String END_SPOT_NM  = "";
    String DEL_YN       = "";
    String STRT_ID      = "";
    String END_ID       = "";
}
//---------------------------------------------------------------------------
TItsDsrcSect::~TItsDsrcSect()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsDsrcSectManager *ItsDsrcSectManager = NULL;
//---------------------------------------------------------------------------

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

    LoadSectMstr(ADbConn);
    LoadSectLink(ADbConn);
    return true;
}
//---------------------------------------------------------------------------
bool TItsDsrcSectManager::LoadSectLink(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT RSE_SECT_ID,     \r\n"
           "       LINK_ID,         \r\n"
           "       LINK_ORD         \r\n"
           "  FROM TB_RSE_SECT_LINK \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 sRseSectId = pADO->FieldByName("RSE_SECT_ID")->AsString;
                TItsDsrcSect *pObj = FLists.Find(sRseSectId);
                if (!pObj) continue;

				TItsDsrcSectLink *pLinkObj = new TItsDsrcSectLink();

				pLinkObj->RSE_SECT_ID = pADO->FieldByName("RSE_SECT_ID")->AsString;
				pLinkObj->LINK_ID     = pADO->FieldByName("LINK_ID")->AsString;
				pLinkObj->LINK_ORD    = pADO->FieldByName("LINK_ORD")->AsString;
				pLinkObj->DEL_YN      = "N";

				pObj->FLinkLists.Push(pLinkObj->LINK_ORD, pLinkObj);

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

    sQry = "SELECT RSE_SECT_ID ,             \r\n"
           "       DSTC        ,             \r\n"
           "       RSE_SECT_NM ,             \r\n"
           "       STRT_SPOT_NM,             \r\n"
           "       END_SPOT_NM ,             \r\n"
           "       DEL_YN      ,             \r\n"
           "       STRT_ID     ,             \r\n"
           "       END_ID                    \r\n"
           "  FROM TB_RSE_SECT_MSTR          \r\n";
           //" WHERE DEL_YN = 'N'              \r\n";
           //" ORDER BY TO_NUMBER(RSE_SECT_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())
            {
				TItsDsrcSect *pObj = new TItsDsrcSect();

				pObj->RSE_SECT_ID  = pADO->FieldByName("RSE_SECT_ID")->AsString;
				pObj->DSTC         = pADO->FieldByName("DSTC")->AsString;
				pObj->RSE_SECT_NM  = pADO->FieldByName("RSE_SECT_NM")->AsString;
				pObj->STRT_SPOT_NM = pADO->FieldByName("STRT_SPOT_NM")->AsString;
				pObj->END_SPOT_NM  = pADO->FieldByName("END_SPOT_NM")->AsString;
				pObj->DEL_YN       = pADO->FieldByName("DEL_YN")->AsString;
				pObj->STRT_ID      = pADO->FieldByName("STRT_ID")->AsString;
				pObj->END_ID       = pADO->FieldByName("END_ID")->AsString;

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

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_RSE_SECT_MSTR)              \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(RSE_SECT_ID)) + 1) \r\n"
           "                                     FROM TB_RSE_SECT_MSTR)                     \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 TItsDsrcSectManager::DeleteSect(String ASectId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    //수집구간목록
    sQry = "DELETE TB_RSE_SECT_LINK WHERE RSE_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 = ASectId;
			pADO->ExecSQL();

#if 0
    //정보제공이력-->외래키를 삭제하는것이 맞음
    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();
#endif

    //수집구간 마스터
    //sQry = "DELETE TB_RSE_SECT_MSTR WHERE RSE_SECT_ID = :p01 \r\n";
    sQry = "UPDATE TB_RSE_SECT_MSTR SET DEL_YN = 'Y' WHERE RSE_SECT_ID = :p01 \r\n";

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

    //수집구간 링크구성정보 삭제
    sQry = "DELETE FROM TB_RSE_SECT_LINK WHERE RSE_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->RSE_SECT_ID;
			pADO->ExecSQL();

    //수집구간 마스터
    sQry = "MERGE INTO TB_RSE_SECT_MSTR L                  \r\n"
           "USING (SELECT :p01 AS RSE_SECT_ID,             \r\n"
           "              :p02 AS DSTC,                    \r\n"
           "              :p03 AS RSE_SECT_NM,             \r\n"
           "              :p04 AS STRT_SPOT_NM,            \r\n"
           "              :p05 AS END_SPOT_NM,             \r\n"
           "              :p06 AS DEL_YN,                  \r\n"
           "              :p07 AS STRT_ID,                 \r\n"
           "              :p08 AS END_ID                   \r\n"
           "         FROM DUAL) M                          \r\n"
           "  ON (L.RSE_SECT_ID = M.RSE_SECT_ID)           \r\n"
           "WHEN MATCHED THEN                              \r\n"
           "   UPDATE SET DSTC         = M.DSTC,           \r\n"
           "              RSE_SECT_NM  = M.RSE_SECT_NM,    \r\n"
           "              STRT_SPOT_NM = M.STRT_SPOT_NM,   \r\n"
           "              END_SPOT_NM  = M.END_SPOT_NM,    \r\n"
           "              DEL_YN       = M.DEL_YN,         \r\n"
           "              STRT_ID      = M.STRT_ID,        \r\n"
           "              END_ID       = M.END_ID          \r\n"
           "WHEN NOT MATCHED THEN                          \r\n"
           "INSERT (  RSE_SECT_ID,    DSTC,   RSE_SECT_NM, \r\n"
           "          STRT_SPOT_NM,   END_SPOT_NM,         \r\n"
           "          DEL_YN,   STRT_ID,   END_ID)         \r\n"
           "VALUES (M.RSE_SECT_ID,  M.DSTC, M.RSE_SECT_NM, \r\n"
           "        M.STRT_SPOT_NM, M.END_SPOT_NM,         \r\n"
           "        M.DEL_YN, M.STRT_ID, M.END_ID)         \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->RSE_SECT_ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->DSTC;
			pADO->Parameters->ParamByName("p03")->Value = AObj->RSE_SECT_NM;
			pADO->Parameters->ParamByName("p04")->Value = AObj->STRT_SPOT_NM;
			pADO->Parameters->ParamByName("p05")->Value = AObj->END_SPOT_NM;
			pADO->Parameters->ParamByName("p06")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p07")->Value = AObj->STRT_ID;
			pADO->Parameters->ParamByName("p08")->Value = AObj->END_ID;
			pADO->ExecSQL();

    //정보제공구간 링크구성정보 입력
    sQry = "INSERT INTO TB_RSE_SECT_LINK (RSE_SECT_ID, LINK_ID, LINK_ORD) \r\n"
           "                      VALUES (:p01,        :p02,    :p03)     \r\n";

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

            FOR_STL(TItsDsrcSectLink *, pObj, AObj->FLinkLists)
            {
                pADO->Parameters->ParamByName("p01")->Value = AObj->RSE_SECT_ID;
                pADO->Parameters->ParamByName("p02")->Value = pObj->LINK_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;
}
//---------------------------------------------------------------------------

