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

#pragma hdrstop

#include "CDSTrafficGradeF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsTrafficSubGrade::TItsTrafficSubGrade()
{
    TItsTrafficSubGrade("", "");
}
//---------------------------------------------------------------------------
TItsTrafficSubGrade::TItsTrafficSubGrade(String ACode, String ASubCode)
{
    SECT_GRAD_CD = ACode;
    CMTR_GRAD_CD = ASubCode;

    FModified = false;
    FCompleted= false;
}
//---------------------------------------------------------------------------
TItsTrafficSubGrade::~TItsTrafficSubGrade()
{
}
//---------------------------------------------------------------------------

TItsTrafficGrade::TItsTrafficGrade()
{
    TItsTrafficGrade("");
}
//---------------------------------------------------------------------------
TItsTrafficGrade::TItsTrafficGrade(String ACode)
{
    SECT_GRAD_CD = ACode;

    FModified = false;
    FCompleted= false;
}
//---------------------------------------------------------------------------
TItsTrafficGrade::~TItsTrafficGrade()
{
    FSubLists.RemoveAll();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsTrafficGradeManager *ItsTrafficGradeManager = NULL;
//---------------------------------------------------------------------------

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

    return (LoadTrafficGrade(ADbConn) && LoadTrafficSubGrade(ADbConn));
}
//---------------------------------------------------------------------------
bool TItsTrafficGradeManager::LoadTrafficGrade(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT SECT_GRAD_CD,     \r\n"
           "       SECT_GRAD_NM      \r\n"
           "  FROM TB_SECT_GRAD_CLSF \r\n";
//           " ORDER BY SECT_GRAD_CD   \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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
				TItsTrafficGrade *pCode = new TItsTrafficGrade();

                pCode->SECT_GRAD_CD = pADO->FieldByName("SECT_GRAD_CD")->AsString; //'구간 등급 코드';
                pCode->SECT_GRAD_NM = pADO->FieldByName("SECT_GRAD_NM")->AsString; //'구간 등급 명';

				FLists.Push(pCode->SECT_GRAD_CD, pCode);
                pCode->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsTrafficGradeManager::LoadTrafficGrade", 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("TItsTrafficGradeManager::LoadTrafficGrade", 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 TItsTrafficGradeManager::LoadTrafficSubGrade(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.SECT_GRAD_CD,                 \r\n"
           "       CMTR_GRAD_CD  ,                 \r\n"
           "       LWST_TRVL_SPED,                 \r\n"
           "       HGHS_TRVL_SPED,                 \r\n"
           "       CNGS_GRAD_YN                    \r\n"
           "  FROM TB_CMTR_GRAD_CLSF A,            \r\n"
           "       TB_SECT_GRAD_CLSF B             \r\n"
           " WHERE A.SECT_GRAD_CD = B.SECT_GRAD_CD \r\n";
//           " ORDER BY SECT_GRAD_CD, CMTR_GRAD_CD   \r\n";
    try
    {
        ItsTrafficGradeManager->FLists.Lock();
        TItsTrafficGrade *pCode = NULL;

        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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sGradCd = pADO->FieldByName("SECT_GRAD_CD")->AsString;
                pCode = ItsTrafficGradeManager->FLists.Find(sGradCd);
                if (!pCode) continue;

				TItsTrafficSubGrade *pSubCode = new TItsTrafficSubGrade();

                pSubCode->SECT_GRAD_CD   = sGradCd;
                pSubCode->CMTR_GRAD_CD   = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                pSubCode->LWST_TRVL_SPED = pADO->FieldByName("LWST_TRVL_SPED")->AsInteger;
                pSubCode->HGHS_TRVL_SPED = pADO->FieldByName("HGHS_TRVL_SPED")->AsInteger;
                pSubCode->CNGS_GRAD_YN   = pADO->FieldByName("CNGS_GRAD_YN")->AsString;

                pCode->FSubLists.Push(pSubCode->CMTR_GRAD_CD, pSubCode);
                pSubCode->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsTrafficGradeManager::LoadTrafficSubGrade", 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("TItsTrafficGradeManager::LoadTrafficSubGrade", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        ItsTrafficGradeManager->FLists.UnLock();
    }

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


