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


#pragma hdrstop

#include "AppGlobalF.h"
#include "CDSMonitoringObjF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TMonitoringScreenManager *ObjScreenManager = NULL;
//---------------------------------------------------------------------------

TMonitoringScreenManager::TMonitoringScreenManager()
{
}
//---------------------------------------------------------------------------
TMonitoringScreenManager::~TMonitoringScreenManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TMonitoringScreenManager::Init()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TMonitoringCtlrManager *ObjCtlrManager = NULL;
//---------------------------------------------------------------------------

TMonitoringCtlrManager::TMonitoringCtlrManager(int AMonitoringType)
{
    FMonitoringType = AMonitoringType;
}
//---------------------------------------------------------------------------
TMonitoringCtlrManager::~TMonitoringCtlrManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

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

    try
    {
        FLists.Lock();
        FLists.RemoveAll();
    }
    __finally
    {
        FLists.UnLock();
    }

    sQry = "SELECT A.*,                               \r\n"
           "       B.VMS_TYPE_NM,                     \r\n"
           "       B.VMS_WDTH,     B.VMS_HGHT,        \r\n"
           "       B.ROW_NUM,      B.COL_NUM,         \r\n"
           "       B.MODL_ROW_NUM, B.MODL_COL_NUM,    \r\n"
           "       B.POWR_ROW_NUM, B.POWR_COL_NUM     \r\n"
           "  FROM TB_VMS_CTLR A,                     \r\n"
           "       TB_VMS_TYPE B                      \r\n"
           " WHERE A.VMS_TYPE_CD = B.VMS_TYPE_CD      \r\n"
           "   AND A.DEL_YN      = 'N'                \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->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TMonitoringCtlr *pObj = new TMonitoringCtlr();

                pObj->CTLR_NMBR = pADO->FieldByName("VMS_CTLR_NMBR")->AsInteger;
                pObj->ID        = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                pObj->CTLR_ID   = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                pObj->NAME      = pADO->FieldByName("VMS_NM")->AsString;
                pObj->CTLR_IP   = pADO->FieldByName("VMS_CTLR_IP")->AsString;
                pObj->INFO1     = pADO->FieldByName("VMS_TYPE_NM")->AsString;

                pObj->STRM_IP   = pADO->FieldByName("WEB_CMRA_IP")->AsString;
                pObj->STRM_ID   = pADO->FieldByName("WEB_CMRA_ID")->AsString;
                pObj->STRM_PSWD = pADO->FieldByName("WEB_CMRA_PWD")->AsString;
                pObj->STRM_ADDR = pADO->FieldByName("STRM_SESN_NM")->AsString;
                pObj->FULL_STRM_ADDR = pObj->STRM_ADDR;

				FLists.Push(pObj->CTLR_NMBR, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TMonitoringCtlrManager::LoadCtlrFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TMonitoringCtlrManager::LoadCtlrFromDb", 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 TMonitoringCtlrManager::InitMonitoringFormFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    // 모니터링유형(1:현시,2,시나리오,3,영상)
    sQry = "SELECT COUNT(1) AS CNT        \r\n"
           "  FROM TB_VMS_MONITORING      \r\n"
           " WHERE MONITORING_TYPE = :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 = String(FMonitoringType);

			pADO->Open();
            int nFormCnt = pADO->FieldByName("CNT")->AsInteger;
            if (nFormCnt > 0) return true;

    sQry = "INSERT INTO TB_VMS_MONITORING (     \r\n"
           "                MONITORING_TYPE,    \r\n"
           "                MONITORING_NM,      \r\n"
           "                MONITORING_SEQ,     \r\n"
           "                VMS_CTLR_NMBR,      \r\n"
           "                VIEW_MODE           \r\n"
           "                )                   \r\n"
           "  VALUES (                          \r\n"
           "                :p01,               \r\n"
           "                :p02,               \r\n"
           "                :p03,               \r\n"
           "                :p04,               \r\n"
           "                0                   \r\n"
           "                )                   \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            String sFormName = "Default";
            nFormCnt = 0;
            FOR_STL(TMonitoringCtlr*, pObj, FLists)
            {
                if (nFormCnt++ > 64) break; /* 한화면에 최대 64개 까지 설정 */

                pADO->Parameters->ParamByName("p01")->Value = String(FMonitoringType);
                pADO->Parameters->ParamByName("p02")->Value = sFormName;
                pADO->Parameters->ParamByName("p03")->Value = String(nFormCnt);
                pADO->Parameters->ParamByName("p04")->Value = String(pObj->CTLR_NMBR);

                pADO->ExecSQL();
                //pADO->RowsAffected;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TMonitoringCtlrManager::InitMonitoringFormFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TMonitoringCtlrManager::InitMonitoringFormFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

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

    if (ObjScreenManager == NULL)
    {
        ObjScreenManager = new TMonitoringScreenManager();
    }
    ObjScreenManager->Init();

    sQry = "SELECT TRIM(A.MONITORING_NM) AS MONITORING_NM,  \r\n"
           "       A.MONITORING_SEQ,                        \r\n"
           "       A.VMS_CTLR_NMBR,                         \r\n"
           "       A.VIEW_MODE                              \r\n"
           "  FROM TB_VMS_MONITORING A, TB_VMS_CTLR B       \r\n"
           " WHERE B.DEL_YN = 'N'                           \r\n"
           "   AND B.VMS_CTLR_NMBR = A.VMS_CTLR_NMBR        \r\n"
           "   AND A.MONITORING_TYPE = :p01                 \r\n"
           " ORDER BY A.MONITORING_NM, A.MONITORING_SEQ     \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->Parameters->ParamByName("p01")->Value = String(FMonitoringType);

			pADO->Open();

            for( ; !pADO->Eof; pADO->Next())
            {
                String MONITORING_NM  = pADO->FieldByName("MONITORING_NM")->AsString;
                int    MONITORING_SEQ = pADO->FieldByName("MONITORING_SEQ")->AsInteger;
                int    VMS_CTLR_NMBR  = pADO->FieldByName("VMS_CTLR_NMBR")->AsInteger;
                int    VIEW_MODE      = pADO->FieldByName("VIEW_MODE")->AsInteger;

                TMonitoringScreen *pObj = ObjScreenManager->FLists.Find(MONITORING_NM);
                if (!pObj)
                {
                    pObj = new TMonitoringScreen();
                    pObj->NAME = MONITORING_NM;
                    ObjScreenManager->FLists.Push(pObj->NAME, pObj);
                }
                TScreenCtlr *pScr = new TScreenCtlr();
                pScr->SEQ       = pObj->FLists.Size()+1;
                pScr->CTLR_NMBR = VMS_CTLR_NMBR;
                pScr->VIEW_MODE = VIEW_MODE;

				pObj->FLists.Push(pScr->SEQ, pScr);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TMonitoringCtlrManager::LoadMonitoringFormFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TMonitoringCtlrManager::LoadMonitoringFormFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();

        FOR_STL(TMonitoringScreen*, pObj, ObjScreenManager->FLists)
        {
            int nCtlrCount = pObj->FLists.Size();
            switch(nCtlrCount)
            {
            case  0: case  1:                   pObj->LAYOUT = 0; break;  //1*1
            case  2: case  3: case  4:          pObj->LAYOUT = 1; break;  //2*2
            case  5: case  6:                   pObj->LAYOUT = 3; break;  //3*2
            case  7: case  8: case  9:          pObj->LAYOUT = 2; break;  //3*3
            case 10: case 11: case 12:          pObj->LAYOUT = 4; break;  //4*3
            case 13: case 14: case 15: case 16: pObj->LAYOUT = 5; break;  //4*4
            case 17: case 18: case 19: case 20: pObj->LAYOUT = 7; break;  //5*4
            case 21: case 22: case 23: case 24: pObj->LAYOUT = 8; break;  //6*4
            case 25: case 26: case 27: case 28:
            case 29: case 30:                   pObj->LAYOUT = 9; break;  //6*5
            default:
                if      (nCtlrCount <= 42)      pObj->LAYOUT = 10;        //7*6
                else if (nCtlrCount <= 56)      pObj->LAYOUT = 11;        //8*7
                else                            pObj->LAYOUT = 12;        //8*8
                break;
            }
        }
    }

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

bool TMonitoringCtlrManager::DelMonitoringFormFromDb(String AMonitoringName, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_VMS_MONITORING      \r\n"
           " WHERE MONITORING_TYPE = :p01 \r\n"
           "   AND MONITORING_NM   = :p02 \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 = String(FMonitoringType);
            pADO->Parameters->ParamByName("p02")->Value = AMonitoringName;

            pADO->ExecSQL();
            //pADO->RowsAffected;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TMonitoringCtlrManager::DelMonitoringFormFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TMonitoringCtlrManager::DelMonitoringFormFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TMonitoringCtlrManager::InsMonitoringFormFromDb(String AMonitoringName, int ASeq, String ACtlrNmbr, int AViewMode, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_VMS_MONITORING (     \r\n"
           "                MONITORING_TYPE,    \r\n"
           "                MONITORING_NM,      \r\n"
           "                MONITORING_SEQ,     \r\n"
           "                VMS_CTLR_NMBR,      \r\n"
           "                VIEW_MODE           \r\n"
           "                )                   \r\n"
           "  VALUES (                          \r\n"
           "                :p01,               \r\n"
           "                :p02,               \r\n"
           "                :p03,               \r\n"
           "                :p04,               \r\n"
           "                :p05                \r\n"
           "                )                   \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 = String(FMonitoringType);
            pADO->Parameters->ParamByName("p02")->Value = AMonitoringName;
            pADO->Parameters->ParamByName("p03")->Value = ASeq;
            pADO->Parameters->ParamByName("p04")->Value = ACtlrNmbr;
            pADO->Parameters->ParamByName("p05")->Value = AViewMode;

            pADO->ExecSQL();
            //pADO->RowsAffected;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TMonitoringCtlrManager::InsMonitoringFormFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TMonitoringCtlrManager::InsMonitoringFormFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

