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

#pragma hdrstop

#include "CDSPrcsParamF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

/*
* Info Class
*/
TPrcsParam::TPrcsParam()
{
}
//---------------------------------------------------------------------------
void TPrcsParam::Clear()
{
}
//---------------------------------------------------------------------------
TPrcsParam::~TPrcsParam()
{
}
//---------------------------------------------------------------------------
String TPrcsParam::ToString()
{
    String SubIds = "";
    String sTemp = "";
    FOR_STL(TPrcsParamClct*, pSub, CLCTS)
    {
        sTemp.printf(L"                            PRRT: %2d, WGHT: %5.2f, CLCT: %s\r\n", pSub->FUSN_PRRT, pSub->FUSN_WGHT, pSub->CLCT_SYST_CD.c_str());
        SubIds += sTemp;
    }

    String     sFltr1, sFltr2, sFltr3;
    String     sAdjs1, sAdjs2, sAdjs3;
    String     sSmth1, sSmth2, sSmth3;
    String     sFusn1;

    sFltr1.printf(L"                       FILTERING: %s\r\n", FLTR_MTHD_CD.c_str());
    sFltr2.printf(L"                    VOL(MIN/MAX): %6d/%6d\r\n", FLTR_MIN_TFVL, FLTR_MAX_TFVL);
    sFltr3.printf(L"                    SPD(LOW/HIG): %6d/%6d\r\n", FLTR_LWST_SPED, FLTR_HGHS_SPED);

    sAdjs1.printf(L"                      ADJUSTMENT: %s\r\n", ADJS_MTHD_CD.c_str());
    sAdjs2.printf(L"        WGHT_AVRG_PRE(1/2/3CYCL): %5.2f/%5.2f/%5.2f\r\n", ADJS_WGHT_AVRG_PRE_1CYCL, ADJS_WGHT_AVRG_PRE_2CYCL, ADJS_WGHT_AVRG_PRE_3CYCL);
    sAdjs3.printf(L"     15M_PTRN_YN, MISS_DECS_CYCL: %s, %d\r\n", ADJS_15M_PTRN_APLY_YN.c_str(), ADJS_SHTR_MISS_DECS_CYCL);

    sSmth1.printf(L"                       SMOOTHING: %s\r\n", SMTH_MTHD_CD.c_str());
    sSmth2.printf(L"            SPED_FCTR(LWST/HGHS): %3.2f/%3.2f\r\n", SMTH_LWST_SPED_FCTR, SMTH_HGHS_SPED_FCTR);
    sSmth3.printf(L"     WGHT_AVRG(PRST/1CYCL/2CYCL): %5.2f/%5.2f/%5.2f\r\n", SMTH_WGHT_AVRG_PRST, SMTH_WGHT_AVRG_PRE_1CYCL, SMTH_WGHT_AVRG_PRE_2CYCL);

    sFusn1.printf(L"                          FUSION: %s\r\n", FUSN_MTHD_CD.c_str());

    return "ID: " + PARA_ID + "\r\n" +
            sFltr1 + sFltr2 + sFltr3 +
            sAdjs1 + sAdjs2 + sAdjs3 +
            sSmth1 + sSmth2 + sSmth3 +
            sFusn1 + SubIds;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TPrcsParamManager *PrcsParamManager = NULL;
//---------------------------------------------------------------------------

/*
* Manager
*/
TPrcsParamManager::TPrcsParamManager()
{
}
//---------------------------------------------------------------------------
TPrcsParamManager::~TPrcsParamManager()
{
    Clear();
}
//---------------------------------------------------------------------------
void TPrcsParamManager::Clear()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

bool TPrcsParamManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    LoadParam(ADbConn);
    return LoadParamDetl(ADbConn);
}
//---------------------------------------------------------------------------

bool TPrcsParamManager::LoadParam(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.PARA_ID,  A.PARA_NM,                                                               \r\n"
           "       A.FLTR_MTHD_CD, A.FUSN_MTHD_CD, A.ADJS_MTHD_CD, A.SMTH_MTHD_CD,                      \r\n"
           "       B.FLTR_MIN_TFVL, B.FLTR_MAX_TFVL,                                                    \r\n"
           "       B.FLTR_LWST_SPED, B.FLTR_HGHS_SPED,                                                  \r\n"
           "       B.ADJS_WGHT_AVRG_PRE_1CYCL, B.ADJS_WGHT_AVRG_PRE_2CYCL, B.ADJS_WGHT_AVRG_PRE_3CYCL,  \r\n"
           "       B.ADJS_15M_PTRN_APLY_YN, B.ADJS_SHTR_MISS_DECS_CYCL,                                 \r\n"
           "       B.SMTH_LWST_SPED_FCTR, B.SMTH_HGHS_SPED_FCTR,                                        \r\n"
           "       B.SMTH_WGHT_AVRG_PRST, B.SMTH_WGHT_AVRG_PRE_1CYCL, B.SMTH_WGHT_AVRG_PRE_2CYCL        \r\n"
           "  FROM TB_LINK_PARA_STUP A                                                                  \r\n"
           " INNER JOIN TB_LINK_PARA_DETL B                                                             \r\n"
           "         ON A.PARA_ID = B.PARA_ID                                                           \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())
            {
                AnsiString PARA_ID = pADO->FieldByName("PARA_ID")->AsString;

                TPrcsParam* pObj = new TPrcsParam();

                pObj->PARA_ID                  = pADO->FieldByName("PARA_ID")->AsString;
                pObj->PARA_NM                  = pADO->FieldByName("PARA_NM")->AsString;
                pObj->FLTR_MTHD_CD             = pADO->FieldByName("FLTR_MTHD_CD")->AsString;               //	N	VARCHAR2(7)	Y			필터링 방법 코드
                pObj->FUSN_MTHD_CD             = pADO->FieldByName("FUSN_MTHD_CD")->AsString;               //	N	VARCHAR2(7)	Y			퓨전 방법 코드
                pObj->ADJS_MTHD_CD             = pADO->FieldByName("ADJS_MTHD_CD")->AsString;               //	N	VARCHAR2(7)	Y			보정 방법 코드
                pObj->SMTH_MTHD_CD             = pADO->FieldByName("SMTH_MTHD_CD")->AsString;               //	N	VARCHAR2(7)	Y			평활화 방법 코드

                pObj->FLTR_MIN_TFVL            = pADO->FieldByName("FLTR_MIN_TFVL")->AsInteger;             //	N	NUMBER(6)	Y	0		필터링 최소 교통량
                pObj->FLTR_MAX_TFVL            = pADO->FieldByName("FLTR_MAX_TFVL")->AsInteger;             //	N	NUMBER(6)	Y	0		필터링 최대 교통량
                pObj->FLTR_LWST_SPED           = pADO->FieldByName("FLTR_LWST_SPED")->AsInteger;            //	N	NUMBER(3)	Y	0		필터링 최저 속도
                pObj->FLTR_HGHS_SPED           = pADO->FieldByName("FLTR_HGHS_SPED")->AsInteger;            //	N	NUMBER(3)	Y	0		필터링 최고 속도
                pObj->ADJS_WGHT_AVRG_PRE_1CYCL = pADO->FieldByName("ADJS_WGHT_AVRG_PRE_1CYCL")->AsFloat;    //	N	NUMBER(5,2)	Y	0		보정 가중치 평균 이전 1주기
                pObj->ADJS_WGHT_AVRG_PRE_2CYCL = pADO->FieldByName("ADJS_WGHT_AVRG_PRE_2CYCL")->AsFloat;    //	N	NUMBER(5,2)	Y	0		보정 가중치 평균 이전 2주기
                pObj->ADJS_WGHT_AVRG_PRE_3CYCL = pADO->FieldByName("ADJS_WGHT_AVRG_PRE_3CYCL")->AsFloat;    //	N	NUMBER(5,2)	Y	0		보정 가중치 평균 이전 3주기
                pObj->ADJS_15M_PTRN_APLY_YN    = pADO->FieldByName("ADJS_15M_PTRN_APLY_YN")->AsString;      //	N	CHAR(1)	    Y	'N'		보정 15분 패턴 적용 여부
                pObj->ADJS_SHTR_MISS_DECS_CYCL = pADO->FieldByName("ADJS_SHTR_MISS_DECS_CYCL")->AsInteger;  //	N	NUMBER(2)	Y			보정 단기 결측 판단 주기
                pObj->SMTH_LWST_SPED_FCTR      = pADO->FieldByName("SMTH_LWST_SPED_FCTR")->AsFloat;         //	N	NUMBER(3,2)	Y	0		평활화 최저 속도 계수
                pObj->SMTH_HGHS_SPED_FCTR      = pADO->FieldByName("SMTH_HGHS_SPED_FCTR")->AsFloat;         //	N	NUMBER(3,2)	Y	0		평활화 최고 속도 계수
                pObj->SMTH_WGHT_AVRG_PRST      = pADO->FieldByName("SMTH_WGHT_AVRG_PRST")->AsFloat;         //	N	NUMBER(5,2)	Y	0		평활화 가중치 평균 현재
                pObj->SMTH_WGHT_AVRG_PRE_1CYCL = pADO->FieldByName("SMTH_WGHT_AVRG_PRE_1CYCL")->AsFloat;    //	N	NUMBER(5,2)	Y	0		평활화 가중치 평균 이전 1주기
                pObj->SMTH_WGHT_AVRG_PRE_2CYCL = pADO->FieldByName("SMTH_WGHT_AVRG_PRE_2CYCL")->AsFloat;    //	N	NUMBER(5,2)	Y	0		평활화 가중치 평균 이전 2주기

                pObj->DEL_YN = "N";

                FLists.Push(pObj->PARA_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::LoadParam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::LoadParam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TPrcsParamManager::LoadParamDetl(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.PARA_ID,                               \r\n"
           "       B.CLCT_SYST_CD, B.FUSN_PRRT, B.FUSN_WGHT \r\n"
           "  FROM TB_LINK_PARA_STUP A                      \r\n"
           " INNER JOIN TB_LINK_PARA_CLCT_SYST B            \r\n"
           "         ON A.PARA_ID = B.PARA_ID               \r\n"
           " ORDER BY A.PARA_ID, B.FUSN_PRRT                \r\n";

    //기존 파라미터의 수집원별 우선순위, 가중치 정보를 삭제한다.
    FOR_STL(TPrcsParam*, pObj, FLists)
    {
        pObj->CLCTS.RemoveAll();
    }

    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();
            for( ; !pADO->Eof; pADO->Next())
            {
                AnsiString PARA_ID = pADO->FieldByName("PARA_ID")->AsString;
                TPrcsParam* pObj = FLists.Find(PARA_ID);
                if (pObj)
                {
                    // 메모리에 파라미터가 존재해야 한다
                    TPrcsParamClct* pSub = new TPrcsParamClct();
                    if (pSub)
                    {
                        pSub->CLCT_SYST_CD = pADO->FieldByName("CLCT_SYST_CD")->AsString;
                        pSub->FUSN_PRRT    = pADO->FieldByName("FUSN_PRRT")->AsInteger;
                        pSub->FUSN_WGHT    = pADO->FieldByName("FUSN_WGHT")->AsFloat;

                        pObj->CLCTS.Push(pSub->FUSN_PRRT, pSub);
                    }
                }
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::LoadParamDetl", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::LoadParamDetl", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TPrcsParamManager::IsUsing(String AParamId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT COUNT(1) AS USE_CNT   \r\n"
           "  FROM TB_LINK               \r\n"
           " WHERE PARA_ID = :p01        \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AParamId;

            pADO->Prepared = true;
			pADO->Open();
            int nUseCnt = pADO->FieldByName("USE_CNT")->AsInteger;
            if (nUseCnt == 0) return false;

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::IsUsing", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::IsUsing", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TPrcsParamManager::GetNewPrcsParamId(String &ANewPraramId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(PARA_ID), 0) + 1 AS NEWID    \r\n"
           "  FROM TB_LINK_PARA_STUP                    \r\n";

    ANewPraramId = "";
    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();
            ANewPraramId = pADO->FieldByName("NEWID")->AsString;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::GetNewPrcsParamId", 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("TPrcsParamManager::GetNewPrcsParamId", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TPrcsParamManager::DeleteData(TPrcsParam* AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_LINK_PARA_CLCT_SYST  \r\n"
           " WHERE PARA_ID = :p01          \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->PARA_ID;
			pADO->ExecSQL();

        sQry = "DELETE TB_LINK_PARA_DETL  \r\n"
               " WHERE PARA_ID = :p01     \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->PARA_ID;
			pADO->ExecSQL();

        sQry = "DELETE TB_LINK_PARA_STUP  \r\n"
               " WHERE PARA_ID = :p01     \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->PARA_ID;
			pADO->ExecSQL();

            AObj->DEL_YN = "Y";

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TPrcsParamManager::DeleteData", 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("TPrcsParamManager::DeleteData", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

