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

#pragma hdrstop

#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "CDSItsCrossInfoF.h"

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

#pragma package(smart_init)

String __fastcall STCondition::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 STCondition::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;
};
//------------------------------------------------------------------------------

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

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

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

//---------------------------------------------------------------------------
TRCrossManager *RCrossManager = NULL;
//---------------------------------------------------------------------------

TRCrossManager::TRCrossManager()
{
    FLoadLink = false;
}
//---------------------------------------------------------------------------
TRCrossManager::~TRCrossManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TRCrossManager::LoadCrossRoad(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT B.ROAD_NAME                              \r\n"
           "  FROM TB_VDS_CTLR A                            \r\n"
           "  INNER JOIN TB_VDS_CMRA B                      \r\n"
           "          ON A.DEL_YN = 'N'                     \r\n"
           "         AND B.DEL_YN = 'N'                     \r\n"
           "         AND A.VDS_TYPE_CD = 'C'                \r\n"
           "         AND A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR  \r\n"
           " GROUP BY B.ROAD_NAME                           \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())
            {
				TRCrossRoad *pCrsRoad = new TRCrossRoad();

                pCrsRoad->ROAD_NAME = pADO->FieldByName("ROAD_NAME")->AsString;

				FLists.Push(pCrsRoad->ROAD_NAME, pCrsRoad);
            }
        }
        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();
    }

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

    sQry = "SELECT B.ROAD_NAME, B.CROSS_NAME                \r\n"
           "  FROM TB_VDS_CTLR A                            \r\n"
           "  INNER JOIN TB_VDS_CMRA B                      \r\n"
           "          ON A.DEL_YN = 'N'                     \r\n"
           "         AND B.DEL_YN = 'N'                     \r\n"
           "         AND A.VDS_TYPE_CD = 'C'                \r\n"
           "         AND A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR  \r\n"
           " GROUP BY B.ROAD_NAME, B.CROSS_NAME             \r\n";

    TRCrossRoad *pCrsRoad;
    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 ROAD_NAME = pADO->FieldByName("ROAD_NAME")->AsString;
                pCrsRoad = FLists.Find(ROAD_NAME);
                if (!pCrsRoad) continue;

				TRCross *pCrs = new TRCross();
                pCrs->ROAD_NAME  = ROAD_NAME;
                pCrs->CROSS_NAME = pADO->FieldByName("CROSS_NAME")->AsString;

				pCrsRoad->FCrosss.Push(pCrs->CROSS_NAME, pCrs);
            }
        }
        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 TRCrossManager::LoadCrossDir(TADOConnection *ADbConn/*=NULL*/)
{
    if (FLoadLink) return true;

    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT B.ROAD_NAME, B.CROSS_NAME, B.DIR_NAME, B.VDS_CTLR_NMBR, B.VDS_CMRA_NO    \r\n"
           "  FROM TB_VDS_CTLR A                                                            \r\n"
           "  INNER JOIN TB_VDS_CMRA B                                                      \r\n"
           "          ON A.DEL_YN = 'N'                                                     \r\n"
           "         AND B.DEL_YN = 'N'                                                     \r\n"
           "         AND A.VDS_TYPE_CD = 'C'                                                \r\n"
           "         AND A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR                                  \r\n"
           " GROUP BY B.ROAD_NAME, B.CROSS_NAME, B.DIR_NAME, B.VDS_CTLR_NMBR, B.VDS_CMRA_NO \r\n";

    TRCrossRoad *pCrsRoad;
    TRCross     *pCrs;
    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 ROAD_NAME = pADO->FieldByName("ROAD_NAME")->AsString;
                pCrsRoad = FLists.Find(ROAD_NAME);
                if (!pCrsRoad) continue;

                String CROSS_NAME = pADO->FieldByName("CROSS_NAME")->AsString;
                pCrs = pCrsRoad->FCrosss.Find(CROSS_NAME);
                if (!pCrs) continue;

				TRCrossDir *pDir = new TRCrossDir();

                pDir->DIR_NAME      = pADO->FieldByName("DIR_NAME")->AsString;
                pDir->VDS_CTLR_NMBR = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pDir->VDS_CMRA_NO   = pADO->FieldByName("VDS_CMRA_NO")->AsString;

				pCrs->FLinks.Push(pDir->DIR_NAME, pDir);
            }
        }
        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;
}
//---------------------------------------------------------------------------


