package com.eactive.eai.inbound.remote;

import java.sql.ResultSet;

import com.eactive.eai.batch.common.StringUtil;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;

public class RemoteTransferLogDAO extends BaseDAO implements RemoteTransferLogQuery
{

    //-------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------
    public int insertRemoteTransferLog(RemoteTransferLogVO logVO) throws DAOException {

        int retValue = 0;
        int index    = 0;

        try {

            this.connect(INSERT_REMOTE_TRANSFER_LOG);
            //logger.debug(">>> 리모트 파일 전송로그 등록 SQL : \n"+ INSERT_REMOTE_TRANSFER_LOG);

            index = 1;
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtLogID         (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getFileTrsmtLogID         (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtStartPtrnDstcd(), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getFileTrsmtStartPtrnDstcd(), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtStartHMS      (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getFileTrsmtStartHMS      (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtEndHMS        (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getFileTrsmtEndHMS        (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getBjobBzwkDstcd          (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getBjobBzwkDstcd          (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getOsidInstiDstcd         (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getOsidInstiDstcd         (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getBjobMsgDstcd           (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getBjobMsgDstcd           (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtPathName      (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getFileTrsmtPathName      (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getTrsmtFileName          (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getTrsmtFileName          (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getRmtFileTrsmtPathName   (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getRmtFileTrsmtPathName   (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getRmtTrsmtFileName       (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getRmtTrsmtFileName       (), " ") +"]");
            this.preparedStatement.setLong  (index++, logVO.getFileTrsmtSize()                                   );    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getFileTrsmtSize          (), "0") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getThisMsgRegsntID        (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getThisMsgRegsntID        (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getThisStgePrcssRsultCd   (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getThisStgePrcssRsultCd   (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getSendRecvYn             (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getThisStgePrcssRsultCd   (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getObstclOccurCausCtnt    (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.getObstclOccurCausCtnt    (), " ") +"]");

            retValue = this.executeUpdate();
            //logger.info(">>> 리모트 파일 전송로그 등록 건수 : "+ retValue);

            return retValue;

        } catch (DAOException ex) {
            throw ex;
        } catch (Exception e) {
            String errMsg = ExceptionUtil.getErrorCode(e, "UNDEFINEDCD", new String[] {});
            throw new DAOException(errMsg);    //<--------------
        } finally {
            disconnect();
        }
    }

    public int updateRemoteTransferLog(RemoteTransferLogVO logVO) throws DAOException {

        int retValue = 0;
        int index    = 0;

        try {

            this.connect(UPDATE_REMOTE_TRANSFER_LOG);
            //logger.debug(">>> 리모트 파일 전송로그 수정 SQL : \n"+ UPDATE_REMOTE_TRANSFER_LOG);

            index = 1;
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtStartPtrnDstcd(), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geFileTrsmtStartPtrnDstcd(), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtStartHMS      (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geFileTrsmtStartHMS      (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtEndHMS        (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geFileTrsmtEndHMS        (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getBjobBzwkDstcd          (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geBjobBzwkDstcd          (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getOsidInstiDstcd         (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geOsidInstiDstcd         (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getBjobMsgDstcd           (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geBjobMsgDstcd           (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtPathName      (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geFileTrsmtPathName      (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getTrsmtFileName          (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geTrsmtFileName          (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getRmtFileTrsmtPathName   (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geRmtFileTrsmtPathName   (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getRmtTrsmtFileName       (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geRmtTrsmtFileName       (), " ") +"]");
            this.preparedStatement.setLong  (index++, logVO.getFileTrsmtSize()                                   );    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geFileTrsmtSize          (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getThisMsgRegsntID        (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geThisMsgRegsntID        (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getThisStgePrcssRsultCd   (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geThisStgePrcssRsultCd   (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getSendRecvYn             (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geThisStgePrcssRsultCd   (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getObstclOccurCausCtnt    (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geObstclOccurCausCtnt    (), " ") +"]");
            this.preparedStatement.setString(index++, StringUtil.nvlTrim(logVO.getFileTrsmtLogID         (), " "));    //logger.debug(">>> 바인드("+(index-1)+") : ["+ StringUtil.nvlTrim(logVO.geFileTrsmtLogID         (), " ") +"]");

            retValue = this.executeUpdate();
            //logger.info(">>> 리모트 파일 전송로그 등록 수정 : "+ retValue);

            if ( retValue == 0 ){
            	retValue = insertRemoteTransferLog(logVO);
            }
            return retValue;

        } catch (DAOException ex) {
            throw ex;
        } catch (Exception e) {
            String errMsg = ExceptionUtil.getErrorCode(e, "UNDEFINEDCD", new String[] {});
            throw new DAOException(errMsg);    //<--------------
        } finally {
            disconnect();
        }
    }
    public RemoteTransferLogVO getRemoteTransferLog( String fileTrsmtLogID ) throws DAOException {
    	RemoteTransferLogVO vo = null;
        ResultSet   rs = null;
        try {
            this.connect(SELECT_REMOTE_TRANSFER_LOG);
            this.preparedStatement.setString(1, fileTrsmtLogID);
            rs = executeQuery();
            vo = new RemoteTransferLogVO();
            while (rs.next())
            {
                vo.setFileTrsmtStartPtrnDstcd(rs.getString("FileTrsmtStartPtrnDstcd"));
                vo.setFileTrsmtStartHMS      (rs.getString("FileTrsmtStartHMS"      ));
                vo.setFileTrsmtEndHMS        (rs.getString("FileTrsmtEndHMS"        ));
                vo.setBjobBzwkDstcd          (rs.getString("BjobBzwkDstcd"          ));
                vo.setOsidInstiDstcd         (rs.getString("OsidInstiDstcd"         ));
                vo.setBjobMsgDstcd           (rs.getString("BjobMsgDstcd"           ));
                vo.setFileTrsmtPathName      (rs.getString("FileTrsmtPathName"      ));
                vo.setTrsmtFileName          (rs.getString("TrsmtFileName"          ));
                vo.setRmtFileTrsmtPathName   (rs.getString("RmtFileTrsmtPathName"   ));
                vo.setRmtTrsmtFileName       (rs.getString("RmtTrsmtFileName"       ));
                vo.setFileTrsmtSize          (rs.getInt   ("FileTrsmtSize"          ));
                vo.setThisMsgRegsntID        (rs.getString("ThisMsgRegsntID"        ));
                vo.setThisStgePrcssRsultCd   (rs.getString("ThisStgePrcssRsultCd"   ));
                vo.setSendRecvYn             (rs.getString("SendRecvYn"             ));
                vo.setObstclOccurCausCtnt    (rs.getString("ObstclOccurCausCtnt"    ));
            }
        } catch(Exception e) {
        	logger.error(e.getMessage(), e);
//            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR017", new String[] {strRuleCode, strPhaseCode}));
        } finally {
            this.disconnect();
        }
        return vo;
    }
}