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

#pragma hdrstop

#include "CDSCrossF.h"

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

#pragma package(smart_init)


//---------------------------------------------------------------------------
String GetDrctDvsnCdNm(int ADrctDvsnCd) {
    // 방향 구분 코드(1:북,2:동,3:남,4:서,5:북동,6:남동,7:남서,8:북서)
    switch(ADrctDvsnCd) {
    case 1: return "북";
    case 2: return "동";
    case 3: return "남";
    case 4: return "서";
    case 5: return "북동";
    case 6: return "남동";
    case 7: return "남서";
    case 8: return "북서";
    default: break;
    }
    return "?";
}
void TCrossCam::SetInfo()
{
    DRCT_DVSN_NM = GetDrctDvsnCdNm(DRCT_DVSN_CD);
    //CMRA_NM  = "[" + DRCT_DVSN_NM + "] " + CMRA_NM;
    CMRA_NM  = CMRA_NM + " [" + DRCT_DVSN_NM + "]";
}
//---------------------------------------------------------------------------

void TCrossCam::Copy(TCrossCam *AObj)
{
    if (AObj)
    {
        AObj->IXR_ID  = IXR_ID;
        AObj->CMRA_ID = CMRA_ID;
        AObj->CMRA_NM = CMRA_NM;
        AObj->ISTL_LCTN     = ISTL_LCTN;
        AObj->DRCT_DVSN_CD  = DRCT_DVSN_CD;
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCross::TCross()
{
}
//---------------------------------------------------------------------------

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

//---------------------------------------------------------------------------
TCrossManager *CrossManager = NULL;
//---------------------------------------------------------------------------

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

    sQry = "SELECT A.*              \r\n"
           "  FROM TB_SC_IXR_MNGM A \r\n"
           " WHERE A.USE_EN = 1     \r\n";

    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())
            {
				TCross *pObj = new TCross();

                pObj->IXR_ID       = pADO->FieldByName("IXR_ID")->AsString;
                pObj->IXR_NM       = pADO->FieldByName("IXR_NM")->AsString.Trim();
                pObj->IXR_TYPE     = pADO->FieldByName("IXR_TYPE")->AsString.Trim();

				FLists.Push(pObj->IXR_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return LoadCrossCam(ADbConn);
}
//---------------------------------------------------------------------------

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

    FLists.Lock();
    try
    {
        FOR_STL(TCross*, pObj, FLists)
        {
            pObj->FLists.RemoveAll();
        }
    }
    __finally
    {
        FLists.UnLock();
    }

    sQry = "SELECT A.IXR_ID, B.CMRA_ID, B.ISTL_LCTN, C.DRCT_LCTN AS CMRA_NM, C.DRCT_DVSN_CD \r\n"
           "  FROM TB_SC_IXR_MNGM A, TB_SC_CMRA_MNGM B, TB_SC_CMRA_DRCT_MNGM C              \r\n"
           " WHERE A.IXR_ID = B.IXR_ID                                                      \r\n"
           "   AND B.CMRA_ID = C.CMRA_ID                                                    \r\n"
           "   AND B.IXR_ID = C.IXR_ID                                                      \r\n"
           "   AND A.USE_EN = 1                                                             \r\n"
           "   AND B.USE_EN = 1                                                             \r\n"
           " ORDER BY A.IXR_ID, B.CMRA_ID                                                   \r\n";

    try
    {
        String WCAM_CTLR_NMBR;

        FLists.Lock();
        FCamLists.Lock();
        FCamLists.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())
            {
                String IXR_ID = pADO->FieldByName("IXR_ID")->AsString;
				TCross *pCross = FLists.Find(String(IXR_ID));
                if (!pCross) continue;

                TCrossCam *pObj = new TCrossCam();
                if (pObj)
                {
                    pObj->IXR_ID    = IXR_ID;
                    pObj->CMRA_ID   = pADO->FieldByName("CMRA_ID")->AsString;
                    pObj->CMRA_NM   = pADO->FieldByName("CMRA_NM")->AsString;

                    pObj->ISTL_LCTN    = pADO->FieldByName("ISTL_LCTN")->AsString.Trim();
                    pObj->DRCT_DVSN_CD = pADO->FieldByName("DRCT_DVSN_CD")->AsInteger;
                    pObj->SetInfo();

                    pCross->FLists.Push(pObj->CMRA_ID, pObj);

                    TCrossCam *pObj2 = new TCrossCam();
                    pObj->Copy(pObj2);
                    pObj2->SetInfo();

                    FCamLists.Push(pObj2->CMRA_ID, pObj2);
                }
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
        FCamLists.UnLock();
    }

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

