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

import java.util.HashMap;

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

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



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

     private transient BapLogTableDeleteService bapLogTableDeleteService;
     
     
 	@Override
 	public void execute(JobExecutionContext context) throws JobExecutionException {
        JobKey jobKey = context.getJobDetail().getKey();
        String data ="";
        for(String key : context.getJobDetail().getJobDataMap().getKeys()){
        	data =data + key +"="+context.getJobDetail().getJobDataMap().getString(key) + ",";
        }
        logger.error("excute TestJob "+jobKey + "data ("+data+")");

		ApplicationContext appContext = null;
		try {
			appContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
		} catch (SchedulerException e) {
			logger.error("applicationContext get module error",e);
			return ;
		}  
		bapLogTableDeleteService = (BapLogTableDeleteService)appContext.getBean("bapLogTableDeleteService");	
 		
  		logger.info("START BAP_EMPTY_LOG_TABLE_SCHEDULER...");
  		
    	int keyLevel = 2;
    	int nofileStart = 7;
    	int nofileEnd = 3;
    	int stageStart = 37;
    	int stageEnd = 30;

    	try{
    		keyLevel = Integer.parseInt(context.getJobDetail().getJobDataMap().getString("key.level"));
    	}catch ( Exception e) {
    		logger.error("keyLevel error",e);
    	}
    	try{
      		nofileStart = Integer.parseInt(context.getJobDetail().getJobDataMap().getString("nofile.start"));
    	}catch ( Exception e) {
    		logger.error("nofileStart error",e);
    	}
    	try{
    		nofileEnd = Integer.parseInt(context.getJobDetail().getJobDataMap().getString("nofile.end"));
    	}catch ( Exception e) {
    		logger.error("nofileEnd error",e);
    	}
    	try{
    		stageStart = Integer.parseInt(context.getJobDetail().getJobDataMap().getString("stage.start"));
    	}catch ( Exception e) {
    		logger.error("stageStart error",e);
    	}
    	try{
    		stageEnd = Integer.parseInt(context.getJobDetail().getJobDataMap().getString("stage.end"));
    	}catch ( Exception e) {
    		logger.error("stageEnd error",e);
    	}
  		
//  		DataSourceType dataType = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.ISG);
  		DataSourceType dataType = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.BAP);  		
  
  		DataSourceContextHolder.setDataSourceType(dataType);
  		HashMap<String, String> paramMap = new HashMap<String, String>();
  		paramMap.put("nofileStart", DatetimeUtil.getDate(-1 * nofileStart)+"0000");
		paramMap.put("nofileEnd", DatetimeUtil.getDate(-1 * nofileEnd)+"9999");
		paramMap.put("stageStart", DatetimeUtil.getDate(-1 * stageStart)+"0000");
		paramMap.put("stageEnd", DatetimeUtil.getDate(-1 * stageEnd)+"9999");

    	try{
    		long [] cnt = bapLogTableDeleteService.clearLogTable(paramMap, keyLevel );
    		logger.info("[BAP_EMPTY_LOG_TABLE_SCHEDULER]스테이지 로그 삭제 건수:" + cnt[0]);
    		logger.info("[BAP_EMPTY_LOG_TABLE_SCHEDULER]파일없는 스테이지 로그 삭제 건수:" + cnt[1]);
    		logger.info("[BAP_EMPTY_LOG_TABLE_SCHEDULER]파일없는 마스터 로그 삭제  건수:" + cnt[2]);
    	}catch ( Exception e){
    		logger.error("...BAP_EMPTY_LOG_TABLE_SCHEDULER error="+e.getMessage(),e);
    	}
	  	logger.info("...BAP_EMPTY_LOG_TABLE_SCHEDULER END");

 	}
}