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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "WindowMsgF.h"
#include "AppGlobalF.h"
#include "CDSFacilityF.h"
#include "CDSCompanyF.h"
#include "ITSLangTransF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "IFM0010MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_UnRegFacilityListF"
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSplitter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "FRAME_FacilityListF"
#pragma link "cxLabel"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TIFM0010M *IFM0010M = NULL;
extern LOGININFO *g_pLOGIN;

//---------------------------------------------------------------------------
__fastcall TIFM0010M::TIFM0010M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"시설물정보 관리";

    FRAMEFacilityList1->PnlBottom->Visible = false;
    FRAMEFacilityList1->PnlTop->Visible = false;
    FRAMEFacilityList1->TvList->OptionsData->Editing = true;

    FRAMEUnRegFacilityList1->PnlBottom->Visible = false;

    cxGroupBox2->Width   = 390;
    cxSplitter1->CloseSplitter();
    cxSplitter1->Visible = false;

    FDbUpdate = false;
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::FormInit()
{
    m_pGDC1 = FRAMEFacilityList1->TvList->DataController;
    m_pGDC2 = FRAMEUnRegFacilityList1->TvList->DataController;
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick((TObject*)BtnSearch);   
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::RefreshData()
{
    FUpdate = false;

    FRAMEFacilityList1->UpdateList();
    FRAMEUnRegFacilityList1->UpdateList();

    if (m_pGDC2->RecordCount == 0)
    {
        cxSplitter1->CloseSplitter();
        cxSplitter1->Visible = false;
    }
    else
    {
        cxSplitter1->Visible = true;
        cxSplitter1->OpenSplitter();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    IFM0010M = NULL;
    Action = caFree;    
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::BtnEditClick(TObject *Sender)
{
    TListFocus tvFocus(FRAMEFacilityList1->TvList);

    if (BtnEdit->Caption == FrmLang->lblCancel->Caption)//"취소")
    {
        FRAMEFacilityList1->CxList->SetFocus();
        Application->ProcessMessages();
        SaveData();
    }

    FrmLang->ITSSkin_ButtonClick(BtnSearch, BtnEdit, BtnInsert, BtnDelete, BtnApply);
    ChangeEditMode(BtnEdit->Down);
    BtnInsert->Enabled = !BtnEdit->Down;

    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::ChangeEditMode(bool AEdit)
{
    bool bEditing = AEdit;

    //int nFocusRow = FRAMEFacilityList1->TvList->Controller->FocusedRowIndex;
    try
    {
        FRAMEFacilityList1->TvList->BeginUpdate();

        FRAMEFacilityList1->Column01->Visible = bEditing;

        FRAMEFacilityList1->Column03->Options->Editing = bEditing;

        FRAMEFacilityList1->Column05->Options->Editing = bEditing;

        FRAMEFacilityList1->Column06->Options->Editing = bEditing;
        FRAMEFacilityList1->Column07->Options->Editing = bEditing;
        FRAMEFacilityList1->Column08->Options->Editing = bEditing;
        FRAMEFacilityList1->Column09->Options->Editing = bEditing;
        FRAMEFacilityList1->Column10->Options->Editing = bEditing;
        FRAMEFacilityList1->Column11->Options->Editing = bEditing;
        FRAMEFacilityList1->Column12->Options->Editing = bEditing;
        FRAMEFacilityList1->Column13->Options->Editing = bEditing;
        FRAMEFacilityList1->Column14->Options->Editing = bEditing;
        FRAMEFacilityList1->Column15->Options->Editing = bEditing;
        //FRAMEFacilityList1->Column16->Options->Editing = bEditing;
        //FRAMEFacilityList1->Column17->Options->Editing = bEditing;
        //FRAMEFacilityList1->Column18->Options->Editing = bEditing;
        //FRAMEFacilityList1->Column19->Options->Editing = bEditing;
        FRAMEFacilityList1->Column20->Options->Editing = bEditing;
        //FRAMEFacilityList1->Column21->Options->Editing = bEditing;
        FRAMEFacilityList1->Column22->Options->Editing = bEditing;
        FRAMEFacilityList1->Column23->Options->Editing = bEditing;
        FRAMEFacilityList1->Column24->Options->Editing = bEditing;
    }
    __finally
    {
        FRAMEFacilityList1->TvList->EndUpdate();
        if (bEditing)
        {
            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
        }
        else
        {
            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
        }
        //FRAMEFacilityList1->TvList->Controller->FocusedRowIndex = nFocusRow;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::BtnDeleteClick(TObject *Sender)
{
    int nIdx = m_pGDC1->FocusedRecordIndex;
    if (nIdx < 0) return;
    //m_pGDC1->Values[nIdx][FRAMEFacilityList1->Column01->Index] = FrmLang->lblDelete->Caption;//"삭제";
    m_pGDC1->Values[nIdx][FRAMEFacilityList1->Column01->Index] = FrmLang->lblDelete->Caption;
    int nMemPtr = m_pGDC1->Values[nIdx][FRAMEFacilityList1->Column99->Index];
    TItsFacility *pObj = (TItsFacility*)nMemPtr;
    pObj->EditMode = DB_DELETE;
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::BtnApplyClick(TObject *Sender)
{
    FUpdate = true;
    BtnEdit->Down = false;
    BtnEdit->Click();
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::BtnInsertClick(TObject *Sender)
{
    int nSelCount = CMM_GetCheckCount(FRAMEUnRegFacilityList1->TvList, FRAMEUnRegFacilityList1->ColumnSel->Index);
    if (nSelCount == 0)
    {
        Application->MessageBox(cxLabel1->Caption.c_str(), //L"미등록 시설물이 선택되지 않았습니다.\r\n추가 등록할 미등록 시설물을 체크하여 선택하십시요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    if (Application->MessageBox(cxLabel2->Caption.c_str(), //L"선택하신 미등록 시설물을 시설물 정보에 추가하여 저장 하시겠습니까?",
                                FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    TADOQuery *pADO = NULL;
	String sQry;
    try
    {
        sQry = "MERGE INTO TB_FCLT_INFR L                                     \r\n"
               "USING (SELECT :p01 AS FCLT_ID,                                \r\n"
               "              :p02 AS FCLT_TYPE,                              \r\n"
               "              :p03 AS FCLT_LCTN,                              \r\n"
               "              'N'  AS DEL_YN,                                 \r\n"
               "              0    AS USE_YEAR,                               \r\n"
               "              0    AS AMT,                                    \r\n"
               "              :p04 AS ADTN_NM,                                \r\n"
               "              TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS ADTN_DT \r\n"
               "         FROM DUAL ) M                                        \r\n"
               "ON (L.FCLT_ID = M.FCLT_ID)                                    \r\n"
               "WHEN MATCHED THEN                                             \r\n"
               "     UPDATE SET L.DEL_YN  = M.DEL_YN,                         \r\n"
               "                L.ADTN_NM = M.ADTN_NM,                        \r\n"
               "                L.ADTN_DT = M.ADTN_DT                         \r\n"
               "WHEN NOT MATCHED THEN                                         \r\n"
               "     INSERT (L.FCLT_ID, L.FCLT_TYPE, L.FCLT_LCTN, L.DEL_YN,   \r\n"
               "             L.USE_YEAR,L.AMT,       L.ADTN_NM,   L.ADTN_DT)  \r\n"
               "     VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN, M.DEL_YN,   \r\n"
               "             M.USE_YEAR,M.AMT,       M.ADTN_NM,   M.ADTN_DT)  \r\n";

        FRAMEUnRegFacilityList1->TvList->BeginUpdate();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);

            ITSDb_GetConnection()->BeginTrans();

            int nMemPtr;
            TItsRegFacility *pObj;
            int nRowCnt = FRAMEUnRegFacilityList1->TvList->ViewData->RecordCount;
            for (int ii = 0; ii < nRowCnt; ii++)
            {
                TcxCustomGridRow* ARow = FRAMEUnRegFacilityList1->TvList->ViewData->Rows[ii];
                if (((bool)ARow->Values[FRAMEUnRegFacilityList1->ColumnSel->Index]))
                {
                    nMemPtr = ARow->Values[FRAMEUnRegFacilityList1->Column99->Index];
                    pObj = (TItsRegFacility*)nMemPtr;

                    ITSDb_SQLBind(pADO, "p01", pObj->Id);
                    ITSDb_SQLBind(pADO, "p02", pObj->Type);
                    ITSDb_SQLBind(pADO, "p03", pObj->Name);
                    ITSDb_SQLBind(pADO, "p04", g_pLOGIN->sUserId);
                    ITSDb_SQLExec(pADO);
                }
            }
            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(cxLabel3->Caption.c_str(), //L"선택하신 미등록 시설물을 시설물 정보에 정상적으로 저장하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
            FDbUpdate = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FRAMEUnRegFacilityList1->TvList->EndUpdate();
    }

    ItsFacilityManager->LoadFromDb();
    HWND MainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::FRAMEFacilityList1TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[FRAMEFacilityList1->Column01->Index];
        if (sType == FrmLang->lblModify->Caption)//"수정")
        {
            ACanvas->Brush->Color = clOlive;
        }
        else if (sType == FrmLang->lblDelete->Caption)//"삭제")
        {
            ACanvas->Brush->Color = clRed;
        }
        else if (sType == FrmLang->lblNew->Caption)//"신규")
        {
            ACanvas->Brush->Color = clLime;
            if (AViewInfo->Focused) ACanvas->Font->Color = clBlack;
        }

        sType = AViewInfo->GridRecord->DisplayTexts[FRAMEFacilityList1->Column00->Index];   //삭제됨
        if (sType == "Y")
            ACanvas->Brush->Color = clSilver;
    }
}
//---------------------------------------------------------------------------

bool __fastcall TIFM0010M::IsUpdate()
{
    try
    {
        ItsFacilityManager->FLists.Lock();
        FOR_STL(TItsFacility*, pObj, ItsFacilityManager->FLists)
        {
            if (pObj->Completed && pObj->EditMode != DB_INIT)
                return true;

            if (!pObj->Completed && pObj->EditMode == DB_NEW)
                return true;
        }
    }
    __finally
    {
        ItsFacilityManager->FLists.UnLock();
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::SaveData()
{
    if (!IsUpdate()) return;

    if (!FUpdate)
    {
        if (Application->MessageBox(FrmLang->lblEidtConfirm->Caption.c_str(),//L"변경된 정보가 있습니다.\r\n변경된 정보를 저장 하시겠습니까?",
                                    FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
    }

    TADOQuery *pADOU = NULL;
    TADOQuery *pADOD = NULL;
	String sQryU;
	String sQryD;
    int    nEditMode;
    try
    {
        sQryU = "UPDATE TB_FCLT_INFR         \r\n"
                "   SET RFID_ID      = :p02, \r\n"
//                "       FCLT_TYPE    = :p03, \r\n"
                "       FCLT_LCTN    = :p04, \r\n"
                "       RSCE_YN      = :p05, \r\n"
                "       MDL_NM       = :p06, \r\n"
                "       USE_YEAR     = :p07, \r\n"
                "       INWR_DT      = :p08, \r\n"
                "       OTWR_DT      = :p09, \r\n"
                "       ISTL_DT      = :p10, \r\n"
                "       TAKT_DT      = :p11, \r\n"
                "       TAKT_RESN    = :p12, \r\n"
                "       TAKN_DT      = :p13, \r\n"
                "       ABDN_DT      = :p14, \r\n"
//                "       ADTN_NM      = :p15, \r\n"
//                "       ADTN_DT      = :p16, \r\n"
                "       CHNG_NM      = :p17, \r\n"
                "       CHNG_DT      = TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS'), \r\n"
                "       AMT          = :p18, \r\n"
//                "       DEL_YN       = :p19, \r\n"
                "       ISTL_CMPY_ID = :p20, \r\n"
                "       MNFC_CMPY_ID = :p21, \r\n"
                "       ISTL_LCTN_ADDR = :p22  \r\n"
                " WHERE FCLT_ID = :p01       \r\n";

        sQryD = "UPDATE TB_FCLT_INFR                                   \r\n"
                "   SET DEL_YN  = 'Y',                                 \r\n"
                "       CHNG_NM = :p02,                                \r\n"
                "       CHNG_DT = TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') \r\n"
                " WHERE FCLT_ID = :p01                                 \r\n";

        int nRow;
        FRAMEFacilityList1->TvList->BeginUpdate();
        try
        {
            pADOU = new TADOQuery(NULL);
			pADOU->Close();
            pADOU->Connection = ITSDb_GetConnection();

            pADOD = new TADOQuery(NULL);
			pADOD->Close();
            pADOD->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADOU, sQryU);
            ITSDb_SQLText(pADOD, sQryD);

            ITSDb_GetConnection()->BeginTrans();

            try
            {
                ItsFacilityManager->FLists.Lock();
                FOR_STL(TItsFacility*, pObj, ItsFacilityManager->FLists)
                {
                    if (pObj->GridIndex < 0) continue;

                    if (pObj->Completed && pObj->EditMode == DB_INIT)
                        continue;

                    if (!pObj->Completed && pObj->EditMode != DB_NEW)
                        continue;

                    nRow = pObj->GridIndex;

                    String FCLT_LCTN = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column05->Index]);

                    String sRfId     = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column03->Index]);   // RFID_ID VARCHAR(50 BYTE) NOT NULL,    '시설물 ID';
                    String sRsceYn   = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column06->Index]);   // RSCE_YN CHAR(1 BYTE) DEFAULT 'N',   '현장자산유무';
                    String sModelNm  = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column07->Index]);   // MDL_NM VARCHAR(30 BYTE)       '모델명';
                    String sUseYear  = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column08->Index]);   // USE_YEAR NUMBER(3)            '사용년수';
                    String sTaktResn = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column13->Index]);   // TAKT_RESN VARCHAR(100 BYTE)   '반출사유';
                    String sISTL_LCTN_ADDR = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column24->Index]);

                    String sInwr     = "";   // INWR_DT VARCHAR(14 BYTE)      '입고일시';
                    String sOtwr     = "";   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';
                    String sIstl     = "";   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';
                    String sTakt     = "";   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';
                    String sTakn     = "";   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';
                    String sAbdn     = "";   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';

                    if (!VarIsNull(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column09->Index]))   // INWR_DT VARCHAR(14 BYTE)      '입고일시';
                    {
                        TDateTime dtTmp = m_pGDC1->Values[nRow][FRAMEFacilityList1->Column09->Index];
                        sInwr = dtTmp.FormatString("YYYYMMDDHHNNSS");
                    }
                    if (!VarIsNull(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column10->Index]))   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';
                    {
                        TDateTime dtTmp = m_pGDC1->Values[nRow][FRAMEFacilityList1->Column10->Index];
                        sOtwr = dtTmp.FormatString("YYYYMMDDHHNNSS");
                    }
                    if (!VarIsNull(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column11->Index]))   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';
                    {
                        TDateTime dtTmp = m_pGDC1->Values[nRow][FRAMEFacilityList1->Column11->Index];
                        sIstl = dtTmp.FormatString("YYYYMMDDHHNNSS");
                    }
                    if (!VarIsNull(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column12->Index]))   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';
                    {
                        TDateTime dtTmp = m_pGDC1->Values[nRow][FRAMEFacilityList1->Column12->Index];
                        sTakt = dtTmp.FormatString("YYYYMMDDHHNNSS");
                    }
                    if (!VarIsNull(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column14->Index]))   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';
                    {
                        TDateTime dtTmp = m_pGDC1->Values[nRow][FRAMEFacilityList1->Column14->Index];
                        sTakn = dtTmp.FormatString("YYYYMMDDHHNNSS");
                    }
                    if (!VarIsNull(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column15->Index]))   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';
                    {
                        TDateTime dtTmp = m_pGDC1->Values[nRow][FRAMEFacilityList1->Column15->Index];
                        sAbdn = dtTmp.FormatString("YYYYMMDDHHNNSS");
                    }

                    String sAmt      = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column20->Index]);   // AMT NUMBER(15),               '금액';
                    String sInsCmpId = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column22->Index]);   // ISTL_CMPY_ID VARCHAR(30 BYTE) '설치업체ID';
                    String sPrcCmpId = VarToStr(m_pGDC1->Values[nRow][FRAMEFacilityList1->Column23->Index]);   // PRDC_CMPY_ID VARCHAR(30 BYTE) '제조업체ID';

                    sInsCmpId = ITSUtil_GetCode(sInsCmpId);
                    sPrcCmpId = ITSUtil_GetCode(sPrcCmpId);

                    //pObj->FFCLT_ID         = pADO->FieldByName("FCLT_ID")->AsString;
                    pObj->RFID_ID         = sRfId;
                    //pObj->FFCLT_TYPE       = pADO->FieldByName("FCLT_TYPE")->AsString;
                    pObj->FCLT_LCTN       = FCLT_LCTN;
                    pObj->RSCE_YN         = sRsceYn;
                    pObj->MDL_NM          = sModelNm;
                    pObj->USE_YEAR        = sUseYear;
                    pObj->INWR_DT         = sInwr;
                    pObj->OTWR_DT         = sOtwr;
                    pObj->ISTL_DT         = sIstl;
                    pObj->TAKT_DT         = sTakt;
                    pObj->TAKT_RESN       = sTaktResn;
                    pObj->TAKN_DT         = sTakn;
                    pObj->ABDN_DT         = sAbdn;
                    //pObj->ADTN_NM         = pADO->FieldByName("ADTN_NM")->AsString;
                    //pObj->ADTN_DT         = pADO->FieldByName("ADTN_DT")->AsString;
                    pObj->CHNG_NM         = g_pLOGIN->sUserId;
                    pObj->CHNG_DT         = Now().FormatString("YYYYMMDDHHNNSS");
                    pObj->AMT             = sAmt;
                    //pObj->FDEL_YN          = pADO->FieldByName("DEL_YN")->AsString;
                    pObj->ISTL_CMPY_ID    = sInsCmpId;
                    pObj->MNFC_CMPY_ID    = sPrcCmpId;
                    pObj->ISTL_LCTN_ADDR  = sISTL_LCTN_ADDR;

                    nEditMode = pObj->EditMode;
                    if (pObj->Completed)
                    {
                        if (pObj->EditMode == DB_DELETE)
                        {
                            //delete
                            ITSDb_SQLBind(pADOD, "p01", pObj->FCLT_ID);
                            ITSDb_SQLBind(pADOD, "p02", g_pLOGIN->sUserId);

                            ITSDb_SQLExec(pADOD);
                            pObj->DEL_YN = "Y";
                        }
                        else
                        if (pObj->EditMode == DB_UPDATE)
                        {
                            //update
                            ITSDb_SQLBind(pADOU, "p01", pObj->FCLT_ID);
                            ITSDb_SQLBind(pADOU, "p02", pObj->RFID_ID);
                            //ITSDb_SQLBind(pADOU, "p03", pObj->FCLT_TYPE);
                            ITSDb_SQLBind(pADOU, "p04", pObj->FCLT_LCTN);
                            ITSDb_SQLBind(pADOU, "p05", pObj->RSCE_YN);
                            ITSDb_SQLBind(pADOU, "p06", pObj->MDL_NM);
                            ITSDb_SQLBind(pADOU, "p07", pObj->USE_YEAR);
                            ITSDb_SQLBind(pADOU, "p08", pObj->INWR_DT);
                            ITSDb_SQLBind(pADOU, "p09", pObj->OTWR_DT);
                            ITSDb_SQLBind(pADOU, "p10", pObj->ISTL_DT);
                            ITSDb_SQLBind(pADOU, "p11", pObj->TAKT_DT);
                            ITSDb_SQLBind(pADOU, "p12", pObj->TAKT_RESN);
                            ITSDb_SQLBind(pADOU, "p13", pObj->TAKN_DT);
                            ITSDb_SQLBind(pADOU, "p14", pObj->ABDN_DT);
                            //ITSDb_SQLBind(pADOU, "p15", pObj->ADTN_NM);
                            //ITSDb_SQLBind(pADOU, "p16", pObj->ADTN_DT);
                            ITSDb_SQLBind(pADOU, "p17", g_pLOGIN->sUserId);
                            ITSDb_SQLBind(pADOU, "p18", pObj->AMT);
                            //ITSDb_SQLBind(pADOU, "p19", pObj->DEL_YN);
                            ITSDb_SQLBind(pADOU, "p20", pObj->ISTL_CMPY_ID);
                            ITSDb_SQLBind(pADOU, "p21", pObj->MNFC_CMPY_ID);
                            ITSDb_SQLBind(pADOU, "p22", pObj->ISTL_LCTN_ADDR);

                            ITSDb_SQLExec(pADOU);
                            pObj->DEL_YN = "N";
                        }
                    }
                    pObj->Completed = true;
                }
            }
            __finally
            {
                ItsFacilityManager->FLists.UnLock();
            }

            ITSDb_GetConnection()->CommitTrans();

            FDbUpdate = true;
            Application->MessageBox(cxLabel4->Caption.c_str(), //L"변경된 시설물 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryD);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryU);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryD);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryU);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADOU)
        {
            pADOU->Close();
            delete pADOU;
        }
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
        FRAMEFacilityList1->TvList->EndUpdate();
    }
    ItsFacilityManager->LoadFromDb();
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    SaveData();
    if (FDbUpdate)
    {
        POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_03, 0);
    }
    if (ItsCompanyManager)
    {
        ItsCompanyManager->FLists.RemoveAll();
        //delete ItsCompanyManager;
        //ItsCompanyManager = NULL;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM0010M::FRAMEFacilityList1ColumnPropertiesValidate(TObject *Sender, Variant &DisplayValue, TCaption &ErrorText,
          bool &Error)
{
    if (FRAMEFacilityList1->TvList->Controller->FocusedRow->IsFilterRow) return;

    int nIndex = m_pGDC1->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = FRAMEFacilityList1->TvList->DataController->Values[nIndex][FRAMEFacilityList1->Column99->Index];
    TItsFacility *pObj = (TItsFacility*)nMemPtr;
    if (pObj->Completed == false)
    {
        //신규이거나 삭제인 경우 편집 내용에 따른 변화가 필요없다.
        return;
    }

    String FCLT_LCTN = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column05->Index]);   // RFID_ID VARCHAR(50 BYTE) NOT NULL,    '시설물 ID';
    String sRfId     = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column03->Index]);   // RFID_ID VARCHAR(50 BYTE) NOT NULL,    '시설물 ID';
    String sRsceYn   = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column06->Index]);   // RSCE_YN CHAR(1 BYTE) DEFAULT 'N',   '현장자산유무';
    String sModelNm  = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column07->Index]);   // MDL_NM VARCHAR(30 BYTE)       '모델명';
    String sUseYear  = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column08->Index]);   // USE_YEAR NUMBER(3)            '사용년수';

    String sTaktResn = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column13->Index]);   // TAKT_RESN VARCHAR(100 BYTE)   '반출사유';
    String sISTL_LCTN_ADDR = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column24->Index]);

    String sInwr     = "";   // INWR_DT VARCHAR(14 BYTE)      '입고일시';
    String sOtwr     = "";   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';
    String sIstl     = "";   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';
    String sTakt     = "";   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';
    String sTakn     = "";   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';
    String sAbdn     = "";   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';

    if (!VarIsNull(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column09->Index]))   // INWR_DT VARCHAR(14 BYTE)      '입고일시';
    {
        TDateTime dtTmp = m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column09->Index];
        sInwr = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column10->Index]))   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';
    {
        TDateTime dtTmp = m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column10->Index];
        sOtwr = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column11->Index]))   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';
    {
        TDateTime dtTmp = m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column11->Index];
        sIstl = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column12->Index]))   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';
    {
        TDateTime dtTmp = m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column12->Index];
        sTakt = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column14->Index]))   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';
    {
        TDateTime dtTmp = m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column14->Index];
        sTakn = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    if (!VarIsNull(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column15->Index]))   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';
    {
        TDateTime dtTmp = m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column15->Index];
        sAbdn = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }

    String sAmt      = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column20->Index]);   // AMT NUMBER(15),               '금액';
    String sInsCmpId = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column22->Index]);   // ISTL_CMPY_ID VARCHAR(30 BYTE) '설치업체ID';
    String sPrcCmpId = VarToStr(m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column23->Index]);   // PRDC_CMPY_ID VARCHAR(30 BYTE) '제조업체ID';

    TcxGridColumn *pColumn = FRAMEFacilityList1->TvList->Controller->FocusedColumn;
    if (pColumn == FRAMEFacilityList1->Column05)
        FCLT_LCTN = DisplayValue;
    else
    if (pColumn == FRAMEFacilityList1->Column03)
        sRfId = DisplayValue;
    else
    if (pColumn == FRAMEFacilityList1->Column06)
        sRsceYn = DisplayValue;
    else
    if (pColumn == FRAMEFacilityList1->Column07)
        sModelNm = DisplayValue;
    else
    if (pColumn == FRAMEFacilityList1->Column08)
        sUseYear= DisplayValue;
    else
    if (pColumn == FRAMEFacilityList1->Column09)
    {
        TDateTime dtTmp = DisplayValue;
        sInwr = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == FRAMEFacilityList1->Column10)
    {
        TDateTime dtTmp = DisplayValue;
        sOtwr = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == FRAMEFacilityList1->Column11)
    {
        TDateTime dtTmp = DisplayValue;
        sIstl = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == FRAMEFacilityList1->Column12)
    {
        TDateTime dtTmp = DisplayValue;
        sTakt = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == FRAMEFacilityList1->Column13)
    {
        sTaktResn = DisplayValue;
    }
    else
    if (pColumn == FRAMEFacilityList1->Column14)
    {
        TDateTime dtTmp = DisplayValue;
        sTakn = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == FRAMEFacilityList1->Column15)
    {
        TDateTime dtTmp = DisplayValue;
        sAbdn = dtTmp.FormatString("YYYYMMDDHHNNSS");
    }
    else
    if (pColumn == FRAMEFacilityList1->Column20)
        sAmt = DisplayValue;
    else
    if (pColumn == FRAMEFacilityList1->Column22)
    {
        sInsCmpId = DisplayValue;
    }
    else
    if (pColumn == FRAMEFacilityList1->Column23)
    {
        sPrcCmpId = DisplayValue;
    }
    else
    if (pColumn == FRAMEFacilityList1->Column24)
    {
        sISTL_LCTN_ADDR = DisplayValue;
    }

    sInsCmpId = ITSUtil_GetCode(sInsCmpId); //마지막에 해준다.
    sPrcCmpId = ITSUtil_GetCode(sPrcCmpId); //마지막에 해준다.

    if (pObj->FCLT_LCTN       != FCLT_LCTN ||
        pObj->RFID_ID         != sRfId     ||
        pObj->RSCE_YN         != sRsceYn   ||
        pObj->MDL_NM          != sModelNm  ||
        pObj->USE_YEAR        != sUseYear  ||
        pObj->INWR_DT         != sInwr     ||
        pObj->OTWR_DT         != sOtwr     ||
        pObj->ISTL_DT         != sIstl     ||
        pObj->TAKT_DT         != sTakt     ||
        pObj->TAKT_RESN       != sTaktResn ||
        pObj->TAKN_DT         != sTakn     ||
        pObj->ABDN_DT         != sAbdn     ||
        pObj->AMT             != sAmt      ||
        pObj->ISTL_CMPY_ID    != sInsCmpId ||
        pObj->ISTL_LCTN_ADDR  != sISTL_LCTN_ADDR ||
        pObj->MNFC_CMPY_ID    != sPrcCmpId )
    {
        m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column01->Index] = FrmLang->lblModify->Caption;//"수정";
        pObj->EditMode = DB_UPDATE;
    }
    else
    {
        m_pGDC1->Values[nIndex][FRAMEFacilityList1->Column01->Index] = "-";
        pObj->EditMode = DB_INIT;
    }
}
//---------------------------------------------------------------------------

