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

#pragma hdrstop

#include "CDSAtrdF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsAtrdRoad::TItsAtrdRoad()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsAtrdRoad::Clear()
{
    ATRD_ID  = "";
    ROAD_ID  = "";
    ORD      = 0;
    MEM_DEL  = "";
}
//---------------------------------------------------------------------------
TItsAtrdRoad::~TItsAtrdRoad()
{
}
//---------------------------------------------------------------------------

TItsAtrd::TItsAtrd()
{
	Completed = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";

    LENGTH = 0;
    //Clear();
}
//---------------------------------------------------------------------------
void TItsAtrd::Clear()
{
    ATRD_ID = "";
    ATRD_NM = "";
    DRCT_CD = "";
    AREA_CD = "";
    DEL_YN  = "";
    MEM_DEL = "";

    LENGTH = 0;
}
//---------------------------------------------------------------------------
TItsAtrd::~TItsAtrd()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsAtrdManager *ItsAtrdManager = NULL;
//---------------------------------------------------------------------------

/*
* Atrd Manager
*/
TItsAtrdManager::TItsAtrdManager()
{
}
//---------------------------------------------------------------------------
TItsAtrdManager::~TItsAtrdManager()
{
    FLists.RemoveAll();
    FNameLists.RemoveAll();
    FCDSNameLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsAtrdManager::LoadFromDbAtrdName(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT DISTINCT TRIM(ATRD_NM) AS ATRD_NM  \r\n"
           "  FROM TB_ATRD                            \r\n";
    try
    {
        FCDSNameSelCnt = 0;
        FCDSNameLists.Lock();
        FCDSNameLists.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())
            {
				TCDSAtrdName *pObj = new TCDSAtrdName();

                pObj->ATRD_NM = pADO->FieldByName("ATRD_NM")->AsString;
                pObj->IsSelected = true;
                FCDSNameSelCnt++;
				FCDSNameLists.Push(pObj->ATRD_NM, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAtrdManager::LoadFromDbAtrdName", 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("TItsAtrdManager::LoadFromDbAtrdName", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FCDSNameLists.UnLock();
    }

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

bool TItsAtrdManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    LoadFromAtrdDb(ADbConn);
    LoadFromAtrdRoadDb(ADbConn);
    return true;
}
//---------------------------------------------------------------------------
bool TItsAtrdManager::LoadFromAtrdDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT ATRD_ID,                     \r\n"
           "       TRIM(ATRD_NM) AS ATRD_NM,    \r\n"
           "       DRCT_CD,                     \r\n"
           "       TRIM(DRCT_NM) AS DRCT_NM,    \r\n"
           "       AREA_CD,                     \r\n"
           "       DEL_YN                       \r\n"
           "  FROM TB_ATRD                      \r\n";
           //" WHERE DEL_YN = 'N' \r\n";
           //" ORDER BY ATRD_ID   \r\n";
    try
    {
        FLists.Lock();
        FLists.RemoveAll();

        FNameLists.Lock();
        FNameLists.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())
            {
				TItsAtrd *pObj = new TItsAtrd();

                pObj->ATRD_ID = pADO->FieldByName("ATRD_ID")->AsString;
                pObj->ATRD_NM = pADO->FieldByName("ATRD_NM")->AsString;
                pObj->DRCT_CD = pADO->FieldByName("DRCT_CD")->AsString;
                pObj->DRCT_NM = pADO->FieldByName("DRCT_NM")->AsString;
                pObj->AREA_CD = pADO->FieldByName("AREA_CD")->AsString;
                pObj->DEL_YN  = pADO->FieldByName("DEL_YN")->AsString;
                pObj->LENGTH  = 0;

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

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

//////////////////////////////////////////////
                String AtrdName = pObj->ATRD_NM;

                TItsAtrdName *pAtrd = FNameLists.Find(AtrdName);
                if (pAtrd)
                {
                    if (pObj->DRCT_CD.Trim() == "0")
                    {
                        pAtrd->UpAtrdNm = pObj->DRCT_NM;
                        pAtrd->UpAtrdId = pObj->ATRD_ID;
                        pAtrd->IsUpAtrd = true;
                    }
                    else
                    {
                        pAtrd->DnAtrdNm = pObj->DRCT_NM;
                        pAtrd->DnAtrdId = pObj->ATRD_ID;
                        pAtrd->IsDnAtrd = true;
                    }
                }
                else
                {
                    pAtrd = new TItsAtrdName();
                    pAtrd->Name = AtrdName;
                    pAtrd->RowNo = FNameLists.Size();

                    if (pObj->DRCT_CD.Trim() == "0")
                    {
                        pAtrd->UpAtrdNm = pObj->DRCT_NM;
                        pAtrd->UpAtrdId = pObj->ATRD_ID;
                        pAtrd->IsUpAtrd = true;
                    }
                    else
                    {
                        pAtrd->DnAtrdNm = pObj->DRCT_NM;
                        pAtrd->DnAtrdId = pObj->ATRD_ID;
                        pAtrd->IsDnAtrd = true;
                    }
                    FNameLists.Push(AtrdName, pAtrd);
                }
//////////////////////////////////////////////
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAtrdManager::LoadFromAtrdDb", 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("TItsAtrdManager::LoadFromAtrdDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
        FNameLists.UnLock();
    }

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

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

    sQry = "SELECT ATRD_ID,           \r\n"
           "       ROAD_ID,           \r\n"
           "       ORD                \r\n"
           "  FROM TB_ATRD_RLTN_ROAD  \r\n";
           " ORDER BY ATRD_ID, ORD    \r\n";
    try
    {
        TItsAtrd *pAtrd;
        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 ATRD_ID = pADO->FieldByName("ATRD_ID")->AsString;
                pAtrd = FLists.Find(ATRD_ID);
                if (!pAtrd) continue;

				TItsAtrdRoad *pObj = new TItsAtrdRoad();

                pObj->ATRD_ID   = ATRD_ID;
                pObj->ROAD_ID   = pADO->FieldByName("ROAD_ID")->AsString;
                pObj->ORD       = pADO->FieldByName("ORD")->AsInteger;

				pAtrd->FRoads.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("TItsAtrdManager::LoadFromAtrdRoadDb", 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("TItsAtrdManager::LoadFromAtrdRoadDb", 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 TItsAtrdManager::LoadAtrdLength(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.ATRD_ID, SUM(B.SECT_LNGT) AS ATRD_LNGT  \r\n"
           "  FROM TB_ATRD_RLTN_ROAD A, TB_ROAD B            \r\n"
           " WHERE A.ROAD_ID = B.ROAD_ID                     \r\n"
           " GROUP BY A.ATRD_ID                              \r\n";

    try
    {
        TItsAtrd *pAtrd;
        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 ATRD_ID = pADO->FieldByName("ATRD_ID")->AsString;
                pAtrd = FLists.Find(ATRD_ID);
                if (!pAtrd) continue;

                pAtrd->LENGTH = pADO->FieldByName("ATRD_LNGT")->AsInteger;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAtrdManager::LoadAtrdLength", 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("TItsAtrdManager::LoadAtrdLength", 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 TItsAtrdManager::DeleteAtrdRoad(TItsAtrd *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_ATRD_RLTN_ROAD  \r\n"
           " WHERE ATRD_ID = :p01     \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->ATRD_ID;
			pADO->ExecSQL();

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

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

bool TItsAtrdManager::DeleteAtrd(TItsAtrd *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_ATRD        \r\n"
           "   SET DEL_YN = 'Y'   \r\n"
           " WHERE ATRD_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->ATRD_ID;
			pADO->ExecSQL();

            DeleteAtrdRoad(AObj, ADbConn);

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

    //링크구성정보 입력
    sQry = "INSERT INTO TB_ATRD_RLTN_ROAD (ATRD_ID, ROAD_ID, ORD ) \r\n"
           "                       VALUES (:p01,    :p02,    :p03) \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();

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

            FOR_STL(TItsAtrdRoad *, pObj, AObj->FRoads)
            {
                pADO->Parameters->ParamByName("p01")->Value = AObj->ATRD_ID;
                pADO->Parameters->ParamByName("p02")->Value = pObj->ROAD_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("TItsAtrdManager::InsertAtrdRoad", 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("TItsAtrdManager::InsertAtrdRoad", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TItsAtrdManager::UpdateAtrd(TItsAtrd *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;


    sQry = "UPDATE TB_ATRD          \r\n"
           "   SET ATRD_NM  = :p02, \r\n"
           "       DRCT_CD  = :p03, \r\n"
           "       AREA_CD  = :p04, \r\n"
           "       DEL_YN   = :p05, \r\n"
           "       DRCT_NM  = :p06  \r\n"
           " WHERE ATRD_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->ATRD_ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->ATRD_NM;
			pADO->Parameters->ParamByName("p03")->Value = AObj->DRCT_CD;
			pADO->Parameters->ParamByName("p04")->Value = AObj->AREA_CD;
			pADO->Parameters->ParamByName("p05")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p06")->Value = AObj->DRCT_NM;

			pADO->ExecSQL();

            DeleteAtrdRoad(AObj, ADbConn);
            InsertAtrdRoad(AObj, ADbConn);

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

    sQry = "INSERT INTO TB_ATRD(ATRD_ID, ATRD_NM, DRCT_CD, AREA_CD, DEL_YN, DRCT_NM) \r\n"
           "             VALUES(:p01,    :p02,    :p03,    :p04,    :p05,   :p06)    \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->ATRD_ID;
			pADO->Parameters->ParamByName("p02")->Value = AObj->ATRD_NM;
			pADO->Parameters->ParamByName("p03")->Value = AObj->DRCT_CD;
			pADO->Parameters->ParamByName("p04")->Value = AObj->AREA_CD;
			pADO->Parameters->ParamByName("p05")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p06")->Value = AObj->DRCT_NM;

			pADO->ExecSQL();

            DeleteAtrdRoad(AObj, ADbConn);
            InsertAtrdRoad(AObj, ADbConn);

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

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


