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

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.onl.statistics.backup.vo.Tseaist02VO;
import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
 * 사용목적 : Daily Backup에서 실행 하는 DAO<br>
 * 사용법 : <br>
 * 작성 날짜 :
 * @author  박기섭 
 * @version $Revision: 1.5 $
 */
@Repository
public class DailyBackupDAO extends SqlMapClientTemplateDao {
    /**
    * Logger for this class
    */
    private static final Logger logger = Logger.getLogger(DailyBackupDAO.class);

    @SuppressWarnings("unchecked")
    public Set<Tseaist02VO> findDailyTseaist02(String yyyyMMdd) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("date", yyyyMMdd);
        paramMap.put("tableName", CommonUtil.getLogTable(yyyyMMdd, true));

        List<Tseaist02VO> hourlyList = new ArrayList<Tseaist02VO>();

        List<Tseaist02VO> procList = this.template.queryForList("DailyBackup.findDailyProcData", paramMap);

        List<Tseaist02VO> errorList = this.template.queryForList("DailyBackup.findDailyErrorData", paramMap);

        if (procList != null && procList.size() > 0) {
            hourlyList.addAll(procList);
        }

        if (errorList != null && errorList.size() > 0) {
            hourlyList.addAll(errorList);
        }

        for (Tseaist02VO vo : hourlyList) {
            vo.toHourlyValue();
        }

        Set<Tseaist02VO> dailySet = merge(hourlyList);

        return dailySet;
    }

    public Set<Tseaist02VO> merge(List<Tseaist02VO> hourlyList) throws Exception {

        // proc list의 list와 동일 한 날짜의 데이터가 있는지 확인 한다.
        Set<Tseaist02VO> dailySet = new HashSet<Tseaist02VO>();

        for (Tseaist02VO vo : hourlyList) {
            if (dailySet.contains(vo)) {
                for (Tseaist02VO dailyVo : dailySet) {
                    if (dailyVo.equals(vo)) {
                        dailyVo.merge(vo);
                    }
                }
            } else {
                dailySet.add(vo);
            }
        }
        return dailySet;
    }
     
    public int insertDailyBackupData(final String date,
            final Set<Tseaist02VO> list) throws Exception{
		Integer batchCount = (Integer) this.template.execute(new SqlMapClientCallback() {
        public Object doInSqlMapClient(SqlMapExecutor executor)
                    throws SQLException {
                executor.startBatch();

                for (Tseaist02VO vo : list) {
                    vo.setStatcYmd(date);
                    executor.insert("DailyBackup.insertDailyData", vo);
                }

                return executor.executeBatch();
            }
        });

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

        return batchCount;
    }

	public void deleteDailBackupData(final String date) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("date", date);
		template.delete("DailyBackup.deleteDailyData", paramMap);
	}
    @SuppressWarnings("unchecked")
    public BigDecimal selectLogCount(String yyyyMMdd) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("date", yyyyMMdd);
        paramMap.put("tableName", CommonUtil.getLogTable(yyyyMMdd, true));

        return (BigDecimal) this.template.queryForObject("DailyBackup.selectLogCount", paramMap);
    }

}
