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

#pragma hdrstop

#include "ITSDbF.h"
#include "ITS_OPLibF.h"
#include "CDSItsRoadInfoF.h"

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

String REGCODE = "355";
String REGNAME = "±¸¹Ì½Ã";

#pragma package(smart_init)

String __fastcall STCodeCondition::ToSQLOrString(String strField)
{
	String strRet;
	int nCount = 0;
	if(true == bUse)
	{
		strRet += "( ";
		vector<String>::iterator it, itEnd = Codes.end();
		for(it = Codes.begin(); it != itEnd; ++it)
		{
			if(Codes.begin() != it)
			{
				strRet += " or ";
			}

			if(nCount >= 100)
			{
				strRet += " \n";
				nCount = 0;
			}
			strRet += strField + " = '";
			strRet += *it;
			strRet += "'";
		}
		strRet += ") ";
	}
	return strRet;
}
//------------------------------------------------------------------------------

String __fastcall STCodeCondition::ToSQLInString(String strField)
{
	String strRet;
	if(true == bUse)
	{
		if(strField.IsEmpty() == false)
		{
			strRet.sprintf(L" %s IN ( ",	strField);
		}
		else
		{
			strRet = " IN ( ";
		}

		vector<String>::iterator it, itEnd = Codes.end();
		for(it = Codes.begin(); it != itEnd; ++it)
		{

			if(Codes.begin() != it)
			{
				strRet += ", ";
			}
			strRet += "'";
			strRet += *it;
			strRet += "'";
		}
		strRet += ") ";
	}
	return strRet;
};
//------------------------------------------------------------------------------


TRLink::TRLink()
{
    FShow = true;
}
//---------------------------------------------------------------------------
void TRLink::Clear()
{
}
//---------------------------------------------------------------------------
TRLink::~TRLink()
{
}
//---------------------------------------------------------------------------

TRRoad::TRRoad()
{
    FShow = true;
}
//---------------------------------------------------------------------------
void TRRoad::Clear()
{
}
//---------------------------------------------------------------------------
TRRoad::~TRRoad()
{
}
//---------------------------------------------------------------------------

TRSido::TRSido()
{
    FShow = true;
}
//---------------------------------------------------------------------------
void TRSido::Clear()
{
}
//---------------------------------------------------------------------------
TRSido::~TRSido()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TRLinkManager *RLinkManager = NULL;
//---------------------------------------------------------------------------

/*
* User Manager
*/
TRLinkManager::TRLinkManager()
{
    FLoadLink = false;
}
//---------------------------------------------------------------------------
TRLinkManager::~TRLinkManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TRLinkManager::LoadSido(TADOConnection *ADbConn/*=NULL*/)
{
#if 0
    String     sQry;
    TADOQuery *pADO = NULL;
    String     sSido = REGCODE;

    sQry = "SELECT VALUE, CODE, PCODE "
           "  FROM CODEDEFINE         "
           " WHERE PCODEKIND = 'CTY'  "
           "   AND CODEKIND  = 'RGN'  "
           "   AND PCODE = " + sSido +
           "   AND USEYN = 1          "
           " ORDER BY VALUE           ";

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

                pObj->LINKID = pADO->FieldByName("CODE")->AsString;
                pObj->NAME   = pADO->FieldByName("VALUE")->AsString;

				FLists.Push(pObj->LINKID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }
#else
    try
    {
        FLists.Lock();
        FLists.RemoveAll();

        TRSido *pObj = new TRSido();

        pObj->LINKID = REGCODE;
        pObj->NAME   = REGNAME;

        FLists.Push(pObj->LINKID, pObj);
    }
    __finally
    {
        FLists.UnLock();
    }
#endif

    return true;
}
//---------------------------------------------------------------------------
bool TRLinkManager::LoadRoad(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    String     sSido = "8";

    sQry = "SELECT SUBSTR(A.LINK_ID, 1, 3) AS LINKID,          \r\n"
           "       A.ROAD_NAME AS ROADNAME                     \r\n"
           "  FROM TB_LINK A                                   \r\n"
           " WHERE SUBSTR(A.LINK_ID, 1, 3) = '" + REGCODE + "' \r\n"
           " GROUP BY SUBSTR(A.LINK_ID, 1, 3), ROAD_NAME       \r\n";

    TRSido *pSido;
    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();
            for( ; !pADO->Eof; pADO->Next())
            {
                String sLinkId = pADO->FieldByName("LINKID")->AsString;
                pSido = FLists.Find(sLinkId);
                if (!pSido) continue;

				TRRoad *pObj = new TRRoad();
                pObj->LINKID = sLinkId;
                pObj->NAME   = pADO->FieldByName("ROADNAME")->AsString;

				pSido->FRoads.Push(pObj->NAME, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------
bool TRLinkManager::LoadLink(TADOConnection *ADbConn/*=NULL*/)
{
    if (FLoadLink) return true;

    String     sQry;
    TADOQuery *pADO = NULL;
    String     sSido = REGCODE;

    sQry = "SELECT SUBSTR(A.LINK_ID, 1, 3) AS SIDOID,              \r\n"
           "       A.ROAD_NAME AS ROADNAME, A.LINK_ID AS LINKID,   \r\n"
           "       C.NODE_NAME AS STNODENAME,                      \r\n"
           "       D.NODE_NAME AS EDNODENAME                       \r\n"
           "  FROM TB_LINK A,                                      \r\n"
           "       TB_NODE C, TB_NODE D                            \r\n"
           " WHERE SUBSTR(A.LINK_ID, 1, 3) = '" + REGCODE + "'     \r\n"
           "   AND A.F_NODE_ID = C.NODE_ID                         \r\n"
           "   AND A.T_NODE_ID = D.NODE_ID                         \r\n"
           " ORDER BY A.ROAD_NAME, A.LINK_ID                       \r\n";

    TRSido *pSido;
    TRRoad *pRoad;
    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();
            for( ; !pADO->Eof; pADO->Next())
            {
                String sSidoId = pADO->FieldByName("SIDOID")->AsString;
                pSido = FLists.Find(sSidoId);
                if (!pSido) continue;

                String sRoadName = pADO->FieldByName("ROADNAME")->AsString;
                pRoad = pSido->FRoads.Find(sRoadName);
                if (!pRoad) continue;

				TRLink *pObj = new TRLink();

                pObj->LINKID     = pADO->FieldByName("LINKID")->AsString;
                pObj->STNODENAME = pADO->FieldByName("STNODENAME")->AsString;
                pObj->EDNODENAME = pADO->FieldByName("EDNODENAME")->AsString;

				pRoad->FLinks.Push(pObj->LINKID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    FLoadLink = true;
    return true;
}
//---------------------------------------------------------------------------


