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

#pragma hdrstop

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

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TCDSAtmp::TCDSAtmp()
{
}
//---------------------------------------------------------------------------

TCDSAtmp::~TCDSAtmp()
{
}
//---------------------------------------------------------------------------
void TCDSAtmp::Init()
{
    SUCCESS         = false;
    MSRM_DT         = "";
    MSRM_SYST_NM    = "";
    SO2_VAL         = "";
    CO_VAL          = "";
    NO2_VAL         = "";
    O3_VAL          = "";
    PM10_VAL        = "";
    PM10_24HH_VAL   = "";
    PM25_VAL        = "";
    PM25_24HH_VAL   = "";
    INTG_ATMP_VAL   = "";
    INTG_ATMP_GRAD  = "";
    SO2_GRAD        = "";
    CO_GRAD         = "";
    O3_GRAD         = "";
    NO2_GRAD        = "";
    PM10_24HH_GRAD  = "";
    PM25_24HH_GRAD  = "";
    PM10_1HH_GRAD   = "";
    PM25_1HH_GRAD   = "";
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetPM10_VAL(bool AUnit/*=true*/)
{
    //미세먼지
    if (SUCCESS)
        if (AUnit)
            return PM10_VAL + " ㎍/㎥";
        else
            return PM10_VAL;

    return (AUnit) ? "- ㎍/㎥" : "-";
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetPM25_VAL(bool AUnit/*=true*/)
{
    //초미세먼지
    if (SUCCESS)
        if (AUnit)
            return PM25_VAL + " ㎍/㎥";
        else
            return PM25_VAL;

    return (AUnit) ? "- ㎍/㎥" : "-";
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetATMP_VAL(bool AUnit/*=true*/)
{
    //통합대기환경수치
    if (SUCCESS)
        if (AUnit)
            return INTG_ATMP_VAL;
        else
            return INTG_ATMP_VAL;

    return "-";
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetO3_VAL(bool AUnit/*=true*/)
{
    //오존농도
    if (SUCCESS)
        if (AUnit)
            return O3_VAL + " ppm";
        else
            return O3_VAL;

    return (AUnit) ? "- ppm" : "-";
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetGradeDesc(int AGrad)
{
    if (!SUCCESS) return "-";

    switch(AGrad)
    {
    case 1: return "좋음";
    case 2: return "보통";
    case 3: return "나쁨";
    case 4: return "매우나쁨";
    }
    return "-";
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetPM10_GRAD()
{
    return GetGradeDesc(PM10_1HH_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetPM25_GRAD()
{
    return GetGradeDesc(PM25_1HH_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetATMP_GRAD()
{
    return GetGradeDesc(INTG_ATMP_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
AnsiString TCDSAtmp::GetO3_GRAD()
{
    return GetGradeDesc(O3_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
int TCDSAtmp::GetGradeColor(int AGrad)
{
    if (!SUCCESS) return clGreen;

    switch(AGrad)
    {
    case 1: return 2;//밝은녹색(Lime)
    case 2: return 5;//하늘색(Aqua)
    case 3: return 4;//주황색(Orange)
    case 4: return 1;//적색(Red)
    }
    return 3;
}
//---------------------------------------------------------------------------
int TCDSAtmp::GetPM10_CLR()
{
    return GetGradeColor(PM10_1HH_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
int TCDSAtmp::GetPM25_CLR()
{
    return GetGradeColor(PM25_1HH_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
int TCDSAtmp::GetATMP_CLR()
{
    return GetGradeColor(INTG_ATMP_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------
int TCDSAtmp::GetO3_CLR()
{
    return GetGradeColor(O3_GRAD.ToIntDef(0));
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TCDSAtmpManager *CDSAtmpManager = NULL;
//---------------------------------------------------------------------------

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

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT A.*                  \r\n"
           "  FROM TB_ATMP_PLTN_STTN A  \r\n";
    try
    {
        FOR_STL(TCDSAtmp*, pTmp, FLists)
        {
            pTmp->DEL_YN = "Y";
        }

        TCDSAtmp *pObj;
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

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

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

                pObj->ATMP_STTN_NM = pADO->FieldByName("ATMP_STTN_NM")->AsString;
                pObj->VMS_DISP_NM  = pADO->FieldByName("VMS_DISP_NM")->AsString;
                pObj->DEL_YN       = pADO->FieldByName("DEL_YN")->AsString;
                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;
}
//---------------------------------------------------------------------------
int TCDSAtmpManager::LoadAtmpRtPnstInfo(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;
    int        nSelCnt = 0;

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT *                                                       \r\n"
           "  FROM TB_ATMP_PLTN_RT_PNST                                    \r\n"
           " WHERE MSRM_DT >= TO_CHAR(SYSDATE-120/1440,'YYYYMMDDHH24MISS') \r\n";

    int nAtmpCnt = 0;
    int nFailCnt = 0;
    try
    {
        FOR_STL(TCDSAtmp*, pTmp, FLists)
        {
            pTmp->Init();
        }

        TCDSAtmp *pObj;
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

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

				pObj = FLists.Find(ATMP_STTN_NMBR);
                if (!pObj)
                {
                    continue;
                }
                pObj->MSRM_DT        = pADO->FieldByName("MSRM_DT")->AsString.Trim();        //	N	VARCHAR2(14)	Y			측정 일시
                pObj->MSRM_SYST_NM   = pADO->FieldByName("MSRM_SYST_NM")->AsString.Trim();   //	N	VARCHAR2(10)	Y			측정 시스템 명
                pObj->CO_VAL         = pADO->FieldByName("CO_VAL")->AsString.Trim();         //	N	VARCHAR2(10)	Y			CO 값
                pObj->SO2_VAL        = pADO->FieldByName("SO2_VAL")->AsString.Trim();        //	N	VARCHAR2(10)	Y			SO2 값
                pObj->NO2_VAL        = pADO->FieldByName("NO2_VAL")->AsString.Trim();        //	N	VARCHAR2(10)	Y			NO2 값
                pObj->O3_VAL         = pADO->FieldByName("O3_VAL")->AsString.Trim();         //	N	VARCHAR2(10)	Y			O3 값
                pObj->PM10_VAL       = pADO->FieldByName("PM10_VAL")->AsString.Trim();       //	N	VARCHAR2(10)	Y			PM10 값
                pObj->PM10_24HH_VAL  = pADO->FieldByName("PM10_24HH_VAL")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM10 24시간 값
                pObj->PM25_VAL       = pADO->FieldByName("PM25_VAL")->AsString.Trim();       //	N	VARCHAR2(10)	Y			PM25 값
                pObj->PM25_24HH_VAL  = pADO->FieldByName("PM25_24HH_VAL")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM25 24시간 값
                pObj->INTG_ATMP_VAL  = pADO->FieldByName("INTG_ATMP_VAL")->AsString.Trim();  //	N	VARCHAR2(10)	Y			통합 대기 값
                pObj->INTG_ATMP_GRAD = pADO->FieldByName("INTG_ATMP_GRAD")->AsString.Trim(); //	N	VARCHAR2(10)	Y			통합 대기 등급
                pObj->SO2_GRAD       = pADO->FieldByName("SO2_GRAD")->AsString.Trim();       //	N	VARCHAR2(10)	Y			SO2 등급
                pObj->CO_GRAD        = pADO->FieldByName("CO_GRAD")->AsString.Trim();        //	N	VARCHAR2(10)	Y			CO 등급
                pObj->O3_GRAD        = pADO->FieldByName("O3_GRAD")->AsString.Trim();        //	N	VARCHAR2(10)	Y			O3 드급
                pObj->NO2_GRAD       = pADO->FieldByName("NO2_GRAD")->AsString.Trim();       //	N	VARCHAR2(10)	Y			NO2 등급
                pObj->PM10_24HH_GRAD = pADO->FieldByName("PM10_24HH_GRAD")->AsString.Trim(); //	N	VARCHAR2(10)	Y			PM10 24시간 등급
                pObj->PM25_24HH_GRAD = pADO->FieldByName("PM25_24HH_GRAD")->AsString.Trim(); //	N	VARCHAR2(10)	Y			PM25 24시간 등급
                pObj->PM10_1HH_GRAD  = pADO->FieldByName("PM10_1HH_GRAD")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM10 1시간 등급
                pObj->PM25_1HH_GRAD  = pADO->FieldByName("PM25_1HH_GRAD")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM25 1시간 등급

                pObj->SUCCESS = true;

                nAtmpCnt++;

                if (pObj->PM10_1HH_GRAD.ToIntDef(0)  == 0 &&
                    pObj->PM25_1HH_GRAD.ToIntDef(0)  == 0 &&
                    pObj->INTG_ATMP_GRAD.ToIntDef(0) == 0
                   )
                {
                    pObj->SUCCESS = false;
                    nFailCnt++;
                }

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

    // 대기정보가 존재하고 정보가 없는 대기환경 지점이 있는 경우 해당 정보를 보정(결측)
    if (nAtmpCnt > 0 && nFailCnt > 0)
    {
        nSelCnt = LoadAtmpRtPnstMainInfo(ADbConn);
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

int TCDSAtmpManager::LoadAtmpRtPnstMainInfo(TADOConnection *ADbConn)
{
#if 1
    int        nSelCnt = 0;

    TCDSAtmp *pSuccAtmp = NULL;
    FOR_STL(TCDSAtmp*, pTmp, FLists)
    {
        if (pTmp->SUCCESS)
        {
            pSuccAtmp = pTmp;
            break;
        }
    }

    if (pSuccAtmp)
    {
        FOR_STL(TCDSAtmp*, pFail, FLists)
        {
            if (pFail->SUCCESS == false)
            {
                pFail->MSRM_DT        = pSuccAtmp->MSRM_DT;
                pFail->MSRM_SYST_NM   = pSuccAtmp->MSRM_SYST_NM;
                pFail->CO_VAL         = pSuccAtmp->CO_VAL;
                pFail->SO2_VAL        = pSuccAtmp->SO2_VAL;
                pFail->NO2_VAL        = pSuccAtmp->NO2_VAL;
                pFail->O3_VAL         = pSuccAtmp->O3_VAL;
                pFail->PM10_VAL       = pSuccAtmp->PM10_VAL;
                pFail->PM10_24HH_VAL  = pSuccAtmp->PM10_24HH_VAL;
                pFail->PM25_VAL       = pSuccAtmp->PM25_VAL;
                pFail->PM25_24HH_VAL  = pSuccAtmp->PM25_24HH_VAL;
                pFail->INTG_ATMP_VAL  = pSuccAtmp->INTG_ATMP_VAL;
                pFail->INTG_ATMP_GRAD = pSuccAtmp->INTG_ATMP_GRAD;
                pFail->SO2_GRAD       = pSuccAtmp->SO2_GRAD;
                pFail->CO_GRAD        = pSuccAtmp->CO_GRAD;
                pFail->O3_GRAD        = pSuccAtmp->O3_GRAD;
                pFail->NO2_GRAD       = pSuccAtmp->NO2_GRAD;
                pFail->PM10_24HH_GRAD = pSuccAtmp->PM10_24HH_GRAD;
                pFail->PM25_24HH_GRAD = pSuccAtmp->PM25_24HH_GRAD;
                pFail->PM10_1HH_GRAD  = pSuccAtmp->PM10_1HH_GRAD;
                pFail->PM25_1HH_GRAD  = pSuccAtmp->PM25_1HH_GRAD;

                pFail->SUCCESS = true;

                nSelCnt++;
                MWARN("ATMP Missing atmp area correct: %s ==> %s", pFail->ATMP_STTN_NMBR.c_str(), pSuccAtmp->ATMP_STTN_NMBR.c_str());
            }
        }
    }
#else
    String     sQry;
    TADOQuery *pADO = NULL;
    int        nSelCnt = 0;

    DWORD dwTick = GetTickCount();
    SELSTART;

    sQry = "SELECT *                                                                                 \r\n"
           "  FROM TB_ATMP_PLTN_RT_PNST                                                              \r\n"
           " WHERE MSRM_DT >= TO_CHAR(SYSDATE-120/1440,'YYYYMMDDHH24MISS')                           \r\n"
           "   AND PM10_1HH_GRAD IS NOT NULL                                                         \r\n"
           "   AND PM25_1HH_GRAD IS NOT NULL                                                         \r\n"
           "   AND INTG_ATMP_GRAD IS NOT NULL                                                        \r\n"
           "   AND ATMP_STTN_NMBR = (SELECT MIN(ATMP_STTN_NMBR)                                      \r\n"
           "                          FROM TB_ATMP_PLTN_RT_PNST                                      \r\n"
           "                         WHERE MSRM_DT >= TO_CHAR(SYSDATE-120/1440,'YYYYMMDDHH24MISS')   \r\n"
           "                           AND PM10_1HH_GRAD IS NOT NULL                                 \r\n"
           "                           AND PM25_1HH_GRAD IS NOT NULL                                 \r\n"
           "                           AND INTG_ATMP_GRAD IS NOT NULL)                               \r\n";

    bool bFail = false;
    try
    {
        TCDSAtmp *pObj;
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = ADbConn;

			SQLText(pADO, sQry, true);

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

				pObj = FLists.Find(ATMP_STTN_NMBR);
                if (!pObj)
                {
                    continue;
                }
                pObj->MSRM_DT          = pADO->FieldByName("MSRM_DT")->AsString.Trim();        //	N	VARCHAR2(14)	Y			측정 일시
                pObj->MSRM_SYST_NM     = pADO->FieldByName("MSRM_SYST_NM")->AsString.Trim();   //	N	VARCHAR2(10)	Y			측정 시스템 명
                pObj->CO_VAL           = pADO->FieldByName("CO_VAL")->AsString.Trim();         //	N	VARCHAR2(10)	Y			CO 값
                pObj->SO2_VAL          = pADO->FieldByName("SO2_VAL")->AsString.Trim();        //	N	VARCHAR2(10)	Y			SO2 값
                pObj->NO2_VAL          = pADO->FieldByName("NO2_VAL")->AsString.Trim();        //	N	VARCHAR2(10)	Y			NO2 값
                pObj->O3_VAL           = pADO->FieldByName("O3_VAL")->AsString.Trim();         //	N	VARCHAR2(10)	Y			O3 값
                pObj->PM10_VAL         = pADO->FieldByName("PM10_VAL")->AsString.Trim();       //	N	VARCHAR2(10)	Y			PM10 값
                pObj->PM10_24HH_VAL    = pADO->FieldByName("PM10_24HH_VAL")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM10 24시간 값
                pObj->PM25_VAL         = pADO->FieldByName("PM25_VAL")->AsString.Trim();       //	N	VARCHAR2(10)	Y			PM25 값
                pObj->PM25_24HH_VAL    = pADO->FieldByName("PM25_24HH_VAL")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM25 24시간 값
                pObj->INTG_ATMP_VAL    = pADO->FieldByName("INTG_ATMP_VAL")->AsString.Trim();  //	N	VARCHAR2(10)	Y			통합 대기 값
                pObj->INTG_ATMP_GRAD   = pADO->FieldByName("INTG_ATMP_GRAD")->AsString.Trim(); //	N	VARCHAR2(10)	Y			통합 대기 등급
                pObj->SO2_GRAD         = pADO->FieldByName("SO2_GRAD")->AsString.Trim();       //	N	VARCHAR2(10)	Y			SO2 등급
                pObj->CO_GRAD          = pADO->FieldByName("CO_GRAD")->AsString.Trim();        //	N	VARCHAR2(10)	Y			CO 등급
                pObj->O3_GRAD          = pADO->FieldByName("O3_GRAD")->AsString.Trim();        //	N	VARCHAR2(10)	Y			O3 드급
                pObj->NO2_GRAD         = pADO->FieldByName("NO2_GRAD")->AsString.Trim();       //	N	VARCHAR2(10)	Y			NO2 등급
                pObj->PM10_24HH_GRAD   = pADO->FieldByName("PM10_24HH_GRAD")->AsString.Trim(); //	N	VARCHAR2(10)	Y			PM10 24시간 등급
                pObj->PM25_24HH_GRAD   = pADO->FieldByName("PM25_24HH_GRAD")->AsString.Trim(); //	N	VARCHAR2(10)	Y			PM25 24시간 등급
                pObj->PM10_1HH_GRAD    = pADO->FieldByName("PM10_1HH_GRAD")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM10 1시간 등급
                pObj->PM25_1HH_GRAD    = pADO->FieldByName("PM25_1HH_GRAD")->AsString.Trim();  //	N	VARCHAR2(10)	Y			PM25 1시간 등급

                pObj->SUCCESS = true;

                if (pObj->PM10_1HH_GRAD.ToIntDef(0)  == 0 &&
                    pObj->PM25_1HH_GRAD.ToIntDef(0)  == 0 &&
                    pObj->INTG_ATMP_GRAD.ToIntDef(0) == 0
                   )
                {
                    pObj->SUCCESS = false;
                    bFail = true;
                }

                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);
    }
#endif
    return nSelCnt;
}
//---------------------------------------------------------------------------

