package com.eactive.eai.rms.onl.statistics.backup.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;
import com.eactive.eai.rms.onl.statistics.backup.vo.Tseaist03VO;
import com.eactive.eai.rms.onl.statistics.backup.vo.Tseaist04VO;
import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
 * <pre>
 * 1. 기능 : 
 * 2. 처리 개요 : 
 *     - 
 * 3. 주의사항 : 
 * </pre>
 * @author  : 박기섭
 * @version : $Revision: 1.4 $
 * @since : rts 1.0
 * @spring.bean id="monthlyBackupDAO"
 * @spring.property name="dataSource" 
 *      ref="dataSource"
 */
@Repository("monthlyBackupDAO")
public class MonthlyBackupDAO extends SqlMapClientTemplateDao {
    /**
     * Default Logger
     */
	protected final Logger logger = Logger.getLogger(getClass());

	public List findMonthlyTseaist03(String month) {
	   	List<Tseaist03VO> al = new ArrayList<Tseaist03VO>();
	   	try{
			final HashMap<String, String> paramMap = new HashMap<String, String>();
	        paramMap.put("month", month);
	
	    	al = template.queryForList("MonthlyBackup.findMonthlyTseaist03", paramMap);
	   	}catch(DataAccessException e){
	   		logger.error("findMonthlyTseaist03 error", e); 
	   	}
    	return al;
	}
	public void deleteMonthlyData(String month) {
        final HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("month", month);
		template.delete("MonthlyBackup.deleteMonthlyData", paramMap);
	}

	public void insertMonthlyBackupData(final List list ) throws Exception {
		try {			
			Integer batchCount = (Integer) this.template.execute(new SqlMapClientCallback() {
	        public Object doInSqlMapClient(SqlMapExecutor executor)
	                    throws SQLException {
	                executor.startBatch();
	                for (Iterator it = list.iterator(); it.hasNext();) {
	    				Tseaist03VO vo = (Tseaist03VO) it.next();
	    				executor.insert("MonthlyBackup.insertMonthlyBackupData", vo);
	    			}
	                return executor.executeBatch();
	            }
	        });

	        if (logger.isInfoEnabled()) {
	            logger.info("총 " + list.size()
	                    + "의 크기를 가진 List<Tseaist03VO> 를 저장 하여 " + batchCount + " 개의 배치 작업 결과를 리턴 받았습니다. ");
	        }
		} catch (Exception e) {
			throw e;
		}
	}

	public List findYearlyTseaist04(String year ) {
		List<Tseaist04VO> al = new ArrayList<Tseaist04VO>();
		try{
			final HashMap<String, String> paramMap = new HashMap<String, String>();
	        paramMap.put("year", year);
	
	        al = template.queryForList("MonthlyBackup.findYearlyTseaist04", paramMap);
		} catch (DataAccessException e) {
			logger.error("findYearlyTseaist04 error", e); 
		}
        return al;
	}

	public void insertYearlyBackupData(final List list, String yyyy) throws Exception {
		try {
	        final HashMap<String, String> paramMap = new HashMap<String, String>();
	        paramMap.put("year", yyyy);
	        
			template.delete("MonthlyBackup.deleteTseaist04", paramMap);
	        
			Integer batchCount = (Integer) this.template.execute(new SqlMapClientCallback() {
	        public Object doInSqlMapClient(SqlMapExecutor executor)
	                    throws SQLException {
	                executor.startBatch();
	                for (Iterator it = list.iterator(); it.hasNext();) {
	                	Tseaist04VO vo = (Tseaist04VO) it.next();
	    				executor.insert("MonthlyBackup.insertYearlyTseaist04", vo);
	    			}
	                return executor.executeBatch();
	            }
	        });
			logger.debug("MonthlyBackup] batchCount=" +  batchCount);
	
		} catch (Exception e) {
			throw e;
		}
	}

	public int getBackupCount(String month) {
    	//DataSourceType dt = DataSourceContextHolder.getDataSourceType();
        final HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("month", month);
        
		return (Integer) template.queryForObject("MonthlyBackup.getBackupCount", paramMap);
	}
}
