package com.eactive.eai.rms.onl.common.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.CommonUtil;


public class LogTableDeleteJob implements Job {
    /**
     * Logger for this class
     */
     private static final Logger logger = Logger.getLogger(LogTableDeleteJob.class);

     private transient LogTableDeleteService logTableDeleteService;
     
     // 프로퍼티로 설정함
     private int TARGET_RANGE_DAY = 0 ;
     
     /**
     *  1.  rms.EMPTY_LOG_CRON_YN 파라메터를 통해서 실행여부 판단
     *  2.  rms.EMPTY_LOG_SCHD_TIME*24 시간만큼  (-) 해서 기간만큼 데이터 DELETE
     *  
     *  ////////////////////////////////////////////////////////////////////////
     *  // 2013-07-10 로직 변경  
     *  ////////////////////////////////////////////////////////////////////////
     *  2.  오늘일자 +1 일에 해당하는 요일 테이블 삭제 (TRUNCATE)
     *  예)
     *  1/29 -> 1/30 -> 30 테이블 TRUNCATE
     *  1/30 -> 1/31 -> 31 테이블 TRUNCATE
     *  1/31 -> 2/1 -> 01 테이블 TRUNCATE
     *  2/1 -> 2/2 -> 02 테이블 TRUNCATE
     *  …
     *  2/27 -> 2/28 -> 28 테이블 TRUNCATE
     *  2/28 -> 3/1 -> 01 테이블 TRUNCATE ( 1/29, 1/30, 1/31 데이터 누적 )
     *  3/1 -> 3/2 -> 02 테이블 TRUNCATE
     *  ...
     *  3/29 -> 3/30 -> 30 테이블 TRUNCATE (1/30 데이터 삭제)
     *  3/30 -> 3/31 -> 31 테이블 TRUNCATE (1/31 데이터 삭제)
     *  3/31 -> 4/1 -> 01 테이블 TRUNCATE
     *  
     *  
     */
     //@ScheduleMethod(type = ScheduleType.CRON, cronExpression = "0 10 0 * * ?")
 	@Override
 	public void execute(JobExecutionContext context) throws JobExecutionException {
        //JobKey jobKey = context.getJobDetail().getKey();

		ApplicationContext appContext = null;
		try {
			appContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
		} catch (SchedulerException e) {
			logger.error("applicationContext get module error",e);
			return ;
		}  
		logTableDeleteService = (LogTableDeleteService)appContext.getBean("logTableDeleteService");	
 		
 		
 		// 프로퍼티로 뺄 것
      	//String cronYn = monitoringContext.getStringProperty("rms.EMPTY_LOG_CRON_YN", "N"); // 실행여부
      	
 		//String times = monitoringContext.getStringProperty("rms.schedule.EmptyLogTableScheduler", "0000"); // 실행시간
          	
  		logger.info("START EMPTY_LOG_TABLE_SCHEDULER...");
  		String today = CommonUtil.getToday("yyyyMMdd");
  		String targetDay = CommonUtil.getDiffHourDate(today, +24, "yyyyMMdd");

  		logger.info("today : " + today + ", TARGET_RANGE_DAY : " + TARGET_RANGE_DAY + ", targetDay : " + targetDay);

  		List<DataSourceType> dataTypeList = DataSourceTypeManager.getOnlineDataSourceTypes();

	  	for( DataSourceType d : dataTypeList ) {
	  		
			if(DataSourceTypeManager.APIGW.equals(d.getName())) {	  		
		  		DataSourceContextHolder.setDataSourceType(d);
		  		logTableDeleteService.truncLogTable(d, CommonUtil.getLogTable(targetDay, true));
		  		logTableDeleteService.truncLogTable(d, CommonUtil.getLogTable(targetDay, false));
		  		logTableDeleteService.truncLogTable(d, CommonUtil.getLogErrTable(targetDay));
			}
	  	}
	  	logger.info("...EMPTY_LOG_TABLE_SCHEDULER END");
	  	
	  	logger.info("START EMPTY_INBOUNDERROR_LOG_TABLE...");
	  	targetDay = CommonUtil.getDiffDayDate(today, -30, "yyyyMMdd") + "00000000";

  		logger.info("today : " + today + ", TARGET_RANGE_DAY : " + TARGET_RANGE_DAY + " targetDay < " + targetDay);
  		
	  	for( DataSourceType d : dataTypeList ) {
	  		if(DataSourceTypeManager.APIGW.equals(d.getName())) {	  		
		  		DataSourceContextHolder.setDataSourceType(d);
		  		logTableDeleteService.clearInboundErrorLogTable(d, targetDay);
	  		}
	  	}
	  	
	  	logger.info("...EMPTY_INBOUNDERROR_LOG_TABLE END");
	  	
 	}
 	
}
