package com.eactive.eai.batch.scheduler;

import java.util.ArrayList;
import java.util.TimerTask;

import com.eactive.eai.batch.logger.LogManager;
import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.UCMsgSenderUtil;
public class TransferJobChecker extends TimerTask{

    public static Logger logger = Logger.getLogger(Logger.LOGGER_SCHEDULER);

    public void run(){

    	logger.debug("[TransferJobChecker]시작");
    	try {
    		ArrayList<String> alInterval = ScheduleBasisManager.getInstance().getIntervalTypes( DatetimeUtil.getCurrentDate() );
    		ArrayList<JobFileTransferVO> alScheduleList = SchedulerMessageManager.getInstance().getScheduledJobList(alInterval);
    		ArrayList<JobFileTransferVO> alLogList = LogManager.getInstance().getFileLogSuccess();
    		JobFileTransferVO svo = null;
    		JobFileTransferVO lvo = null;
    		int sstart = 0;
    		int lstart = 0;
    		for ( int inx = 0; inx < alScheduleList.size(); inx++){
    			svo = alScheduleList.get(inx);
    			logger.debug("[TransferJobChecker]svo(" + inx + ")-->" + svo);
    			try {
					sstart = Integer.parseInt(svo.getSndrcvStartHMS());
				} catch (Exception e) {}
				
        		boolean logFlag = false;
    			for ( int jnx = 0; jnx < alLogList.size(); jnx++){
    				lvo = alLogList.get(jnx);
        			try {
    					lstart = Integer.parseInt(lvo.getSndrcvStartHMS());
    				} catch (Exception e) {}
    				if ( ( svo.getBjobMsgDstcd().equals(lvo.getBjobMsgDstcd()) ||          // 같은 파일인지 ( 작업메시지 구분이 같거나 ) 
    					   ( svo.getBjobBzwkDstcd().equals(lvo.getBjobBzwkDstcd()) &&      //     ""     ( 업무구분이 같고 
                             svo.getBjobTranDstcdName().equals(lvo.getBjobTranDstcdName()) //     ""       거래구분코드가 같은 경우 )
                            ) 
                          ) &&
                          svo.getSendRecvYn().equals(lvo.getSendRecvYn()) &&               //  송수신 구분이 같은지
                          sstart <= lstart                                                 //  송수신 시작 시간이 스케쥴된 시작 시간 이후이고
                        ){
    					logFlag = true;
    					break;
    				}
        		}
				if ( !logFlag ){
					sendUC(svo);
				}
    		}

		} catch (Exception e) {
			logger.error( e.getMessage(), e);
			return;
		} finally {
		}

		logger.debug("[TransferJobChecker]끝");

    	return;
    }

//    private void sendSms ( JobFileTransferVO vo ){
//    	
//    	String message = "[송신지연]";
//    	if ( "R".equals(vo.getSendRecvYn())){
//    		message = "[수신지연]";
//    	}
//
//    	// 지연된 시간 계산
//    	int scheduleT = Integer.parseInt(vo.getSndrcvEndHMS().substring(0,2));
//        int scheduleM = Integer.parseInt(vo.getSndrcvEndHMS().substring(2,4));
//
//        String currentTime = DatetimeUtil.getCurrentTime();
//        int currentT  = Integer.parseInt(currentTime.substring(0,2));
//        int currentM  = Integer.parseInt(currentTime.substring(2,4));
//
//        int diffM     = currentM-scheduleM;
//        int diffT     = currentT-scheduleT;
//        if (diffM < 0){
//            diffM = (currentM+60)-scheduleM;
//            diffT = (currentT-1)-scheduleT;
//        }
//
//    	message = message + "("+diffT+":"+diffM+")파일명:" + vo.getBjobTranDstcdName() 
//    	          + ",업무명:" + vo.getBjobBzwkName()
//    	          + ",기관명:" + vo.getOsidInstiName();
//    	ItmsSmsUtil.sendSMS(PropManager.getInstance().getProperty(ItmsSmsUtil.SMSINFO, ItmsSmsUtil.REQ_BY_GROUP), vo.getUapplCd(),  message);
//		if ( vo.getUapplCd() != null && !vo.getUapplCd().trim().equals("")){
//			if (!PropManager.getInstance().getProperty(ItmsSmsUtil.SMSINFO, ItmsSmsUtil.EAI_SEND_YN).trim().equals("")){
//				ItmsSmsUtil.sendSMS( PropManager.getInstance().getProperty(ItmsSmsUtil.SMSINFO, ItmsSmsUtil.REQ_BY_GROUP), "", message);
//			}
//		}
//    }

    private void sendUC ( JobFileTransferVO vo ){
    	
    	String message = "[송신지연]";
    	if ( "R".equals(vo.getSendRecvYn())){
    		message = "[수신지연]";
    	}

    	// 지연된 시간 계산
    	int scheduleT = Integer.parseInt(vo.getSndrcvEndHMS().substring(0,2));
        int scheduleM = Integer.parseInt(vo.getSndrcvEndHMS().substring(2,4));

        String currentTime = DatetimeUtil.getCurrentTime();
        int currentT  = Integer.parseInt(currentTime.substring(0,2));
        int currentM  = Integer.parseInt(currentTime.substring(2,4));

        int diffM     = currentM-scheduleM;
        int diffT     = currentT-scheduleT;
        if (diffM < 0){
            diffM = (currentM+60)-scheduleM;
            diffT = (currentT-1)-scheduleT;
        }

    	message = message + "("+diffT+":"+diffM+")파일명:" + vo.getBjobTranDstcdName() 
    	          + ",업무명:" + vo.getBjobBzwkName()
    	          + ",기관명:" + vo.getOsidInstiName();
    	logger.info("[TransferJobChecker][sendUC]" + message);
    	UCMsgSenderUtil.sendUCMsg( message);
    }

}
