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

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;
import com.eactive.eai.rms.onl.common.util.DateUtil;
import com.eactive.eai.rms.onl.statistics.eaisvcname.vo.EaiSvcNameErrMonthlyVO;
import com.eactive.eai.rms.onl.statistics.eaisvcname.vo.StatisticsForm;
import com.ibatis.sqlmap.client.event.RowHandler;

/**
 * <pre>
 * 1. 기능 : 
 * 2. 처리 개요 : 
 *     - 
 * 3. 주의사항 : 
 * </pre>
 * @author  : music75
 * @version : $Revision: 1.1 $
 * @since : rts 1.0
 * @spring.bean id="eaiSvcNameErrMonthlyDAO"
 * @spring.property name="dataSource" ref="dataSource"
 */
@Repository("eaiSvcNameErrMonthlyDAO")
public class EaiSvcNameErrMonthlyDAOImpl
        extends SqlMapClientTemplateDao
        implements EaiSvcNameErrMonthlyDAO {

    /**
     * <pre>
     * 1. 기능 : 서비스별 장애발생 현황 리스트 조회
     * 2. 처리 개요 : 
     *     - 
     * 3. 주의사항 : Overriden 
     * </pre>
     * @author  : music75
     * @version : $Revision: 1.1 $
     * @since : rts 1.0
     * @see com.eactive.eai.rts.biz.statistics.eaisvcname.dao.EaiSvcNameErrMonthlyDAO#findByErrEaiSvcNameMonthly(com.eactive.eai.rts.biz.statistics.eaisvcname.vo.StatisticsForm)
     * @param statisticsForm
     * @return
     */
    public List findByErrEaiSvcNameMonthly(StatisticsForm statisticsForm) throws Exception{        
        Calendar cal = Calendar.getInstance();

        int thisYear = 0;
        int thisMonth = 0;
        try {
            thisYear = Integer.parseInt(statisticsForm.getStartDate().substring(0, 4));
            thisMonth = Integer.parseInt(statisticsForm.getStartDate().substring(4, 6));  //hd 수정 5 -> 6 (2007.11.2)
        } catch (Exception e) {
            thisYear = Integer.parseInt(DateUtil.getYear());
            thisMonth = Integer.parseInt(DateUtil.getMonth());
        }
        cal.set(thisYear, thisMonth - 1, 10);

        cal.add(Calendar.MONTH, -1);

        int lastYear = cal.get(Calendar.YEAR);
        int lastMonth = cal.get(Calendar.MONTH) + 1;

    	final HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("thisMonthErrColumnName", thisMonth + "MnTrsmtNoitm");
        paramMap.put("lastMonthErrColumnName", lastMonth + "MnTrsmtNoitm");
        paramMap.put("thisYear", String.valueOf(thisYear));
        paramMap.put("lastYear", String.valueOf(lastYear));

    	final ArrayList<EaiSvcNameErrMonthlyVO> al = new ArrayList<EaiSvcNameErrMonthlyVO>();
    	
    	template.queryWithRowHandler("EaiSvcNameErrMonthly.findByErrEaiSvcNameMonthly", paramMap,
    			new RowHandler(){
            		@SuppressWarnings("unchecked")
                    public void handleRow(Object valueObject) {
            	    	EaiSvcNameErrMonthlyVO vo = (EaiSvcNameErrMonthlyVO)valueObject;
                        al.add(vo);
        }});
        return al;
    }
}
