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

#pragma hdrstop

#include "CDSUserF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsUser::TItsUser()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TItsUser::Clear()
{
    USER_ID      = "";  //VARCHAR2(20)	N			사용자ID
    PWD          = "";  //VARCHAR2(64)	Y			암호
    NAME         = "";  //VARCHAR2(30)	Y			성명
    COMP         = "";  //VARCHAR2(50)	Y			회사
    TEL          = "";  //VARCHAR2(128)	Y			전화번호
    ADDR         = "";  //VARCHAR2(200)	Y			주소
    RESD_NMBR    = "";  //VARCHAR2(14)	Y			주민번호
    MOBILE       = "";  //VARCHAR2(128)	Y			이동전화
    EMAL         = "";  //VARCHAR2(128)	Y			이메일
    DEL_YN       = "";  //CHAR(1)	Y	'N'		삭제 여부
    RGSTYMD      = "";  //VARCHAR2(14)	Y			등록일자
    CRCTYMD      = "";  //VARCHAR2(14)	Y			수정일자
    HINT_QUES    = "";  //VARCHAR2(7)	Y			힌트질문
    HINT_ANS     = "";  //VARCHAR2(100)	Y			힌트질문답
    GROP_ID      = "";  //VARCHAR2(30)	Y			그룹ID
    OPER_SYST_ID = "";  //VARCHAR2(30)	Y			운영시스템ID
}
//---------------------------------------------------------------------------
TItsUser::~TItsUser()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsUserManager *ItsUserManager = NULL;
//---------------------------------------------------------------------------

/*
* User Manager
*/
TItsUserManager::TItsUserManager()
{
}
//---------------------------------------------------------------------------
TItsUserManager::~TItsUserManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsUserManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT *            \r\n"
           "  FROM TB_USER_INFR \r\n";
           //" ORDER BY USER_ID   \r\n";
#else
    sQry = "SELECT A.USER_ID      ,                   \r\n"
           "       A.PWD          ,                   \r\n"
           "       A.NAME         ,                   \r\n"
           "       A.COMP         ,                   \r\n"
           "       A.TEL          ,                   \r\n"
           "       A.ADDR         ,                   \r\n"
//           "       A.RESD_NMBR    ,                   \r\n"
           "       A.MOBILE       ,                   \r\n"
           "       A.EMAL         ,                   \r\n"
           "       A.DEL_YN       ,                   \r\n"
           "       A.RGSTYMD      ,                   \r\n"
           "       A.CRCTYMD      ,                   \r\n"
           "       A.HINT_QUES    ,                   \r\n"
           "       A.HINT_ANS     ,                   \r\n"
           "       A.GROP_ID      ,                   \r\n"
           "       A.OPER_SYST_ID ,                   \r\n"
           "       B.GROP_NM AS GROP_NM,              \r\n"
           "       C.OPER_SYST_NAME AS OPER_SYST_NM   \r\n"
           "  FROM TB_USER_INFR      A,               \r\n"
           "       TB_USER_GROP_INFR B,               \r\n"
           "       TB_OPERSYST       C                \r\n"
           " WHERE A.GROP_ID      = B.GROP_ID(+)      \r\n"
           "   AND A.OPER_SYST_ID = C.OPER_SYST_ID(+) \r\n"
           " ORDER BY A.USER_ID                       \r\n";
#endif
    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())
            {
				TItsUser *pObj = new TItsUser();

                pObj->USER_ID       = pADO->FieldByName("USER_ID")->AsString;

                pObj->PWD           = pADO->FieldByName("PWD")->AsString;

                pObj->NAME          = pADO->FieldByName("NAME")->AsString;
                pObj->COMP          = pADO->FieldByName("COMP")->AsString;
                pObj->TEL           = pADO->FieldByName("TEL")->AsString;
                pObj->ADDR          = pADO->FieldByName("ADDR")->AsString;
                //pObj->RESD_NMBR     = pADO->FieldByName("RESD_NMBR")->AsString;
                pObj->MOBILE        = pADO->FieldByName("MOBILE")->AsString;
                pObj->EMAL          = pADO->FieldByName("EMAL")->AsString;
                pObj->DEL_YN        = pADO->FieldByName("DEL_YN")->AsString;
                pObj->RGSTYMD       = pADO->FieldByName("RGSTYMD")->AsString;
                pObj->CRCTYMD       = pADO->FieldByName("CRCTYMD")->AsString;
                pObj->HINT_QUES     = pADO->FieldByName("HINT_QUES")->AsString;
                pObj->HINT_ANS      = pADO->FieldByName("HINT_ANS")->AsString;
                pObj->GROP_ID       = pADO->FieldByName("GROP_ID")->AsString;
                pObj->OPER_SYST_ID  = pADO->FieldByName("OPER_SYST_ID")->AsString;

                pObj->GROP_NM       = "[" + pObj->GROP_ID + "] " + pADO->FieldByName("GROP_NM")->AsString;
                pObj->OPER_SYST_NM  = "[" + pObj->OPER_SYST_ID + "] " + pADO->FieldByName("OPER_SYST_NM")->AsString;

                if (pObj->GROP_NM      == "[] ") pObj->GROP_NM = "";
                if (pObj->OPER_SYST_NM == "[] ") pObj->OPER_SYST_NM = "";

				FLists.Push(pObj->USER_ID, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsUserManager::LoadFromDb", 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("TItsUserManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

