package com.eactive.eai.rms.onl.statistics.eaibzwkdstcd.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.eaibzwkdstcd.vo.EaiBzwkDstcdCompareMonthlyVO;
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="eaiBzwkDstcdCompareMonthlyDAO"
 * @spring.property name="dataSource" ref="dataSource"
 */
@Repository("eaiBzwkDstcdCompareMonthlyDAO")
public class EaiBzwkDstcdCompareMonthlyDAOImpl
        extends SqlMapClientTemplateDao
        implements EaiBzwkDstcdCompareMonthlyDAO {

    /**
     * <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.EaiBzwkDstcdCompareMonthlyDAO#findByTransactionEaiBzwkDstcdMonthly(com.eactive.eai.rts.biz.statistics.eaisvcname.vo.StatisticsForm)
     * @param statisticsForm
     * @return
     */
    public List findByTransactionEaiBzwkDstcdMonthly (
            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("thisMonthProcColumnName", thisMonth + "MnTrsmtNoitm");
        paramMap.put("lastMonthProcColumnName", lastMonth + "MnTrsmtNoitm");
        paramMap.put("thisYear", String.valueOf(thisYear));
        paramMap.put("lastYear", String.valueOf(lastYear));

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