package com.dbs.mplus.service.batch;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dbs.mplus.framework.FrameworkConstants;
import com.dbs.mplus.framework.service.batch.BatchJobManager;
import com.dbs.mplus.framework.service.batch.job.BatchJob;
import com.dbs.mplus.framework.util.DateUtil;
import com.dbs.mplus.service.MPlusConfigureService;

@Component 
@EnableScheduling 
public class MPlusBatchJobManager extends BatchJobManager {
	private static final Logger logger = LoggerFactory.getLogger(BatchJobManager.class); 
	
	@Autowired 
	private MPlusConfigureService configureService;
	// 연령별/지역별 통계 
	private BatchJob userInfoJob; 

	/**
	 * 생성자 
	 */
	public MPlusBatchJobManager() { 
		
	}	
	
	@Scheduled(cron="${agg.user.info.batch.cron}")
	public void workUserInfo() { 
		// 사용자 배치 활성화 여부 
		if(!this.configureService.isAggUserInfoWork()) {
			logger.info("사용자 정보 통계배치는 비활성화되이 있음. ");
			return; 
		}
		
		final Date aggDt = DateUtil.getStartDate(1); 	
		if(logger.isInfoEnabled()) { 
			StringBuilder builder = new StringBuilder(FrameworkConstants.NEW_LINE); 
			
			builder.append("사용자 정보 통계 배치작업시작 =========="); 
			builder.append("- 기준시간 : ").append(aggDt).append(FrameworkConstants.NEW_LINE); 
			builder.append("- 대상작업 : ").append(this.userInfoJob).append(FrameworkConstants.NEW_LINE); 
			
			logger.info(builder.toString());
		}
		
		// 실행 
		this.executeBatchJob(aggDt, this.userInfoJob);		
	}

	/**
	 * 사용자 연령별/지역별 배치작업 
	 * @param job
	 */
	public void setUserInfoBatchJob(BatchJob job) { 
		this.userInfoJob = job; 
	}
}
