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

#pragma hdrstop

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

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TCDSPark::TCDSPark()
{
    REG_PRK_CMPRT_CO    = 0;
    RUN_PRK_CMPRT_CO    = 0;
    PARKING_CGS_STTUS   = 9;
    REMNDR_PRK_CMPRT_CO = 0;

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

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

    CMNC_STTS_CD = "CMS1";
    PARKING_CGS_STTUS_DESC = "-";

    PARKING_CGS_STTUS   = 9;
    REMNDR_PRK_CMPRT_CO = 0;
}
//---------------------------------------------------------------------------
void TCDSPark::CalcCgsSttus()
{
    if (!SUCCESS) {
        PARKING_CGS_STTUS_DESC = "점검중";
        PARKING_CGS_STTUS = 9;
        return;
    }

    // 주차장 상태 코드 (0 : 여유 (점유 50% 미만), 1 : 보통 (점유 70% 미만), 2 : 혼잡 (점유 90% 미만), 3 : 만차 (점유 90% 초과))
    switch(PARKING_CGS_STTUS)
    {
    case 0:  PARKING_CGS_STTUS_DESC = "여유"; break;
    case 1:  PARKING_CGS_STTUS_DESC = "보통"; break;
    case 2:  PARKING_CGS_STTUS_DESC = "혼잡"; break;
    case 3:  PARKING_CGS_STTUS_DESC = "만차"; break;
    default: PARKING_CGS_STTUS_DESC = "점검중";
        SUCCESS = false;
        break;
    }

}
//---------------------------------------------------------------------------
int TCDSPark::GetGradeColor()
{
    if (!SUCCESS) {
        return 3;//황색(Yellow)
    }

    switch(PARKING_CGS_STTUS)
    {
    case 0:  return 2;//밝은녹색(Lime)
    case 1:  return 2;//밝은녹색(Lime)
    case 2:  return 3;//황색(Yellow)
    case 3:  return 1;//적색(Red)
    default: return grade_delay;;
    }
}
//---------------------------------------------------------------------------
AnsiString TCDSPark::GetParkName() {
    return VMS_DSPL_NM;
}
//---------------------------------------------------------------------------
AnsiString TCDSPark::GetCountText(AnsiString ATxt, int ACount) {
    if (!SUCCESS) {
        return "  ";
    }

    bool isSpace = false;
    String sType = "";
    if (ATxt.Pos(" ")) {
        isSpace = true;
    }
    if (ATxt.Pos("대")) {
        sType = "대";
    }
    if (ATxt.Pos("면")) {
        sType = "면";
    }

    if (sType != "") {
        if (isSpace) {
            return AnsiString(ACount) + " " + sType;
        }
        return AnsiString(ACount) + sType;
    }
    return AnsiString(ACount);
}
//---------------------------------------------------------------------------
AnsiString TCDSPark::GetRemainCo(AnsiString ATxt) {
    return GetCountText(ATxt, REMNDR_PRK_CMPRT_CO);
}
//---------------------------------------------------------------------------
AnsiString TCDSPark::GetTotalCo(AnsiString ATxt) {
    return GetCountText(ATxt, REG_PRK_CMPRT_CO);
}
//---------------------------------------------------------------------------
AnsiString TCDSPark::GetCgsSttus() {
    return PARKING_CGS_STTUS_DESC;
}
//---------------------------------------------------------------------------


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

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

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT A.PRK_PLCE_NMBR, A.PRK_PLCE_MANAGE_NO,                       \r\n"
           "       A.PRK_PLCE_NM, A.VMS_DSPL_NM,                                \r\n"
           "       NVL(B.CMNC_STTS_CD, 'CMS1')         AS CMNC_STTS_CD,         \r\n"
           "       A.PRK_CMPRT_CO                      AS REG_PRK_CMPRT_CO,     \r\n"
           "       NVL(C.PRK_CMPRT_CO, A.PRK_CMPRT_CO) AS RUN_PRK_CMPRT_CO,     \r\n"
           "       NVL(C.PARKING_CGS_STTUS, 9)         AS PARKING_CGS_STTUS,    \r\n"
           "       NVL(C.REMNDR_PRK_CMPRT_CO, 0)       AS REMNDR_PRK_CMPRT_CO   \r\n"
           "  FROM TB_PRK_PLCE A, TB_PRK_PLCE_STTS B, TB_PRK_PLCE_RT C          \r\n"
           " WHERE A.DEL_YN    = 'N'                                            \r\n"
           "   AND A.PIS_TYPE != 'N'                                            \r\n"
           "   AND A.PRK_PLCE_NMBR = B.PRK_PLCE_NMBR(+)                         \r\n"
           "   AND A.PRK_PLCE_NMBR = C.PRK_PLCE_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 PRK_PLCE_NMBR = pADO->FieldByName("PRK_PLCE_NMBR")->AsString;
				pObj = FLists.Find(PRK_PLCE_NMBR);
                if (!pObj)
                {
                    pObj = new TCDSPark();
                    pObj->PRK_PLCE_NMBR = PRK_PLCE_NMBR;
                    pObj->PRK_PLCE_MANAGE_NO = pADO->FieldByName("PRK_PLCE_MANAGE_NO")->AsString;
                    FLists.Push(PRK_PLCE_NMBR, pObj);
                }

                pObj->PRK_PLCE_NM = pADO->FieldByName("PRK_PLCE_NM")->AsString;
                pObj->VMS_DSPL_NM = pADO->FieldByName("VMS_DSPL_NM")->AsString;
                pObj->CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;


                pObj->REG_PRK_CMPRT_CO    = pADO->FieldByName("REG_PRK_CMPRT_CO")->AsInteger;
                pObj->RUN_PRK_CMPRT_CO    = pADO->FieldByName("RUN_PRK_CMPRT_CO")->AsInteger;
                pObj->PARKING_CGS_STTUS   = pADO->FieldByName("PARKING_CGS_STTUS")->AsInteger;
                pObj->REMNDR_PRK_CMPRT_CO = pADO->FieldByName("REMNDR_PRK_CMPRT_CO")->AsInteger;

                if (pObj->VMS_DSPL_NM == "") {
                    pObj->VMS_DSPL_NM = pObj->PRK_PLCE_NM;
                }
                if (pObj->CMNC_STTS_CD == "CMS0") {
                    pObj->SUCCESS = true;
                }

                pObj->CalcCgsSttus();

                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);
    }

    SELEND(nSelCnt, dwTick);
    if (JOBTIME(dwTick) > g_AppCfg.JobTimeout) {
        TIMEDELAY(nSelCnt, dwTick);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

