package com.eactive.eai.rms.bat.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 BatLogTableDeleteJob implements Job {
    /**
     * Logger for this class
     */
     private static final Logger logger = Logger.getLogger(BatLogTableDeleteJob.class);

     private transient BatLogTableDeleteService batLogTableDeleteService;
     
     
 	@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 ;
		}  
		batLogTableDeleteService = (BatLogTableDeleteService)appContext.getBean("batLogTableDeleteService");	
 		
  		logger.info("START BAT_EMPTY_LOG_TABLE_SCHEDULER...");
  		
    	int keyLevel = 2;
    	int logStart = 40;
    	int logEnd = 30;

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

    	try{
    		long [] cnt = batLogTableDeleteService.clearLogTable(paramMap, keyLevel );
    		logger.info("[BAT_EMPTY_LOG_TABLE_SCHEDULER]Number of [Interface] log deletions:" + cnt[0]);
    		logger.info("[BAT_EMPTY_LOG_TABLE_SCHEDULER]Number of [File] log deletions:" + cnt[1]);
    		logger.info("[BAT_EMPTY_LOG_TABLE_SCHEDULER]Number of [DB] log deletions:" + cnt[2]);
    		logger.info("[BAT_EMPTY_LOG_TABLE_SCHEDULER]Number of [Error] log deletions:" + cnt[3]);
    	}catch ( Exception e){
    		logger.error("...BAT_EMPTY_LOG_TABLE_SCHEDULER error="+e.getMessage(),e);
    	}
	  	logger.info("...BAT_EMPTY_LOG_TABLE_SCHEDULER END");

 	}
}