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


#pragma hdrstop

#include "CDSParkF.h"
#include "CDSCtlrF.h"
#include "VmsUtilsF.h"
#include "VMSCommLibF.h"

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TCDSPark::TCDSPark()
{
    WHOL_NPLS = 0;
    GNRL_NPLS = 0;
    LGVH_NPLS = 0;
    HVVH_NPLS = 0;
    EMVH_NPLS = 0;
    HNDC_NPLS = 0;
    WMON_NPLS = 0;
    ELVH_NPLS = 0;
    ETC_NPLS  = 0;

    Init();
}
//---------------------------------------------------------------------------

TCDSPark::~TCDSPark()
{
}
//---------------------------------------------------------------------------
void TCDSPark::Init()
{
    SUCCESS = false;

    TOTAL              = 0;
    GNRL_RMND_PRZN_NUM = 0;
    LGVH_RMND_PRZN_NUM = 0;
    HVVH_RMND_PRZN_NUM = 0;
    EMVH_RMND_PRZN_NUM = 0;
    HNDC_RMND_PRZN_NUM = 0;
    WMON_RMND_PRZN_NUM = 0;
    ELVH_RMND_PRZN_NUM = 0;
    ETC_RMND_PRZN_NUM  = 0;

    CNGT_DESC = "-";
    CNGT_LEVL = 0;
}
//---------------------------------------------------------------------------
void TCDSPark::CalcTotal()
{
    if (!SUCCESS) return;

    TOTAL = GNRL_RMND_PRZN_NUM +
            LGVH_RMND_PRZN_NUM +
            HVVH_RMND_PRZN_NUM +
            EMVH_RMND_PRZN_NUM +
            HNDC_RMND_PRZN_NUM +
            WMON_RMND_PRZN_NUM +
            ELVH_RMND_PRZN_NUM +
            ETC_RMND_PRZN_NUM;

    if (TOTAL == 0)
    {
        CNGT_DESC = "만차";
        CNGT_LEVL = 3;
    }
    else
    if (TOTAL > CNGT_NPLS)
    {
        CNGT_DESC = "원활";
        CNGT_LEVL = 1;
    }
    else
    {
        CNGT_DESC = "혼잡";
        CNGT_LEVL = 2;
    }
}
//---------------------------------------------------------------------------
int TCDSPark::GetGradeColor()
{
    switch(CNGT_LEVL)
    {
    case 1: return 2;//밝은녹색(Lime)
    case 2: return 4;//주황색(Orange)
    case 3: return 1;//적색(Red)
    }
    return 3;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCDSParkManager *CDSParkManager = NULL;
//---------------------------------------------------------------------------

TCDSParkManager::TCDSParkManager()
{
}
//---------------------------------------------------------------------------
TCDSParkManager::~TCDSParkManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
int TCDSParkManager::LoadFromDb(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    int        nSelCnt = 0;

    DDBSTART;

    sQry = "SELECT B.*,                                                                 \r\n"
           "       A.WHOL_NPLS,                                                         \r\n"
           "       A.GNRL_NPLS,                                                         \r\n"
           "       A.LGVH_NPLS,                                                         \r\n"
           "       A.HVVH_NPLS,                                                         \r\n"
           "       A.EMVH_NPLS,                                                         \r\n"
           "       A.HNDC_NPLS,                                                         \r\n"
           "       A.WMON_NPLS,                                                         \r\n"
           "       A.ELVH_NPLS,                                                         \r\n"
           "       A.ETC_NPLS,                                                          \r\n"
           "       A.CNGT_NPLS                                                          \r\n"
           "  FROM TB_PRLT_CTLR    A,                                                   \r\n"
           "       (SELECT PRLT_CTLR_NMBR,                                              \r\n"
           "               SUM(GNRL_RMND_PRZN_NUM) AS GNRL_RMND_PRZN_NUM,               \r\n"
           "               SUM(LGVH_RMND_PRZN_NUM) AS LGVH_RMND_PRZN_NUM,               \r\n"
           "               SUM(HVVH_RMND_PRZN_NUM) AS HVVH_RMND_PRZN_NUM,               \r\n"
           "               SUM(EMVH_RMND_PRZN_NUM) AS EMVH_RMND_PRZN_NUM,               \r\n"
           "               SUM(HNDC_RMND_PRZN_NUM) AS HNDC_RMND_PRZN_NUM,               \r\n"
           "               SUM(WMON_RMND_PRZN_NUM) AS WMON_RMND_PRZN_NUM,               \r\n"
           "               SUM(ELVH_RMND_PRZN_NUM) AS ELVH_RMND_PRZN_NUM,               \r\n"
           "               SUM(ETC_RMND_PRZN_NUM)  AS ETC_RMND_PRZN_NUM                 \r\n"
           "          FROM TB_PRLT_RT_INFR                                              \r\n"
           "         WHERE UPDT_DT >= TO_CHAR(SYSDATE - 10/1440, 'YYYYMMDDHH24MISS')    \r\n"
           "         GROUP BY PRLT_CTLR_NMBR                                            \r\n"
           "       ) B                                                                  \r\n"
           " WHERE A.REAL_DATA_YN = 'Y'                                                 \r\n"
           "   AND A.DEL_YN       = 'N'                                                 \r\n"
           "   AND A.PRLT_CTLR_NMBR = B.PRLT_CTLR_NMBR                                  \r\n";

    try
    {
        FOR_STL(TCDSPark*, pTmp, FLists)
        {
            pTmp->Init();
        }
        
        TCDSPark *pObj;
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

			SQLOpen(pADO);
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString PRLT_CTLR_NMBR = pADO->FieldByName("PRLT_CTLR_NMBR")->AsString;

				pObj = FLists.Find(PRLT_CTLR_NMBR);
                if (!pObj)
                {
                    pObj = new TCDSPark();
                    pObj->PRLT_CTLR_NMBR = PRLT_CTLR_NMBR;
                    FLists.Push(pObj->PRLT_CTLR_NMBR, pObj);
                }

                pObj->GNRL_RMND_PRZN_NUM = pADO->FieldByName("GNRL_RMND_PRZN_NUM")->AsInteger;
                pObj->LGVH_RMND_PRZN_NUM = pADO->FieldByName("LGVH_RMND_PRZN_NUM")->AsInteger;
                pObj->HVVH_RMND_PRZN_NUM = pADO->FieldByName("HVVH_RMND_PRZN_NUM")->AsInteger;
                pObj->EMVH_RMND_PRZN_NUM = pADO->FieldByName("EMVH_RMND_PRZN_NUM")->AsInteger;
                pObj->HNDC_RMND_PRZN_NUM = pADO->FieldByName("HNDC_RMND_PRZN_NUM")->AsInteger;
                pObj->WMON_RMND_PRZN_NUM = pADO->FieldByName("WMON_RMND_PRZN_NUM")->AsInteger;
                pObj->ELVH_RMND_PRZN_NUM = pADO->FieldByName("ELVH_RMND_PRZN_NUM")->AsInteger;
                pObj->ETC_RMND_PRZN_NUM  = pADO->FieldByName("ETC_RMND_PRZN_NUM")->AsInteger;

                pObj->WHOL_NPLS = pADO->FieldByName("WHOL_NPLS")->AsInteger;
                pObj->GNRL_NPLS = pADO->FieldByName("GNRL_NPLS")->AsInteger;
                pObj->LGVH_NPLS = pADO->FieldByName("LGVH_NPLS")->AsInteger;
                pObj->HVVH_NPLS = pADO->FieldByName("HVVH_NPLS")->AsInteger;
                pObj->EMVH_NPLS = pADO->FieldByName("EMVH_NPLS")->AsInteger;
                pObj->HNDC_NPLS = pADO->FieldByName("HNDC_NPLS")->AsInteger;
                pObj->WMON_NPLS = pADO->FieldByName("WMON_NPLS")->AsInteger;
                pObj->ELVH_NPLS = pADO->FieldByName("ELVH_NPLS")->AsInteger;
                pObj->ETC_NPLS  = pADO->FieldByName("ETC_NPLS")->AsInteger;
                pObj->CNGT_NPLS = pADO->FieldByName("CNGT_NPLS")->AsInteger;

                pObj->SUCCESS = true;

                pObj->CalcTotal();

                nSelCnt++;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return -1;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return -1;
        }
    }
    __finally
    {
        SQLFree(pADO);
    }

    IDBSTOP(nSelCnt);
    return nSelCnt;
}
//---------------------------------------------------------------------------

