package com.eactive.eai.rms.bat.manage.rule.inter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.bat.manage.rule.schedule.vo.SchedulerMonitorVO;
import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;

@Repository("batInterfaceDao")
@SuppressWarnings({"deprecation","unchecked"})
public class BatInterfaceDao extends SqlMapClientTemplateDao {

	public int selectListCount(HashMap<String, Object> param) throws Exception {
    	return (Integer)template.queryForObject("BatInterface.selectListCount", param);
    }	
	
	public List<Map<String, Object>> selectList(HashMap<String, Object> param) throws Exception {
    	return (List<Map<String, Object>>)template.queryForList("BatInterface.selectList", param);
    }	

	public HashMap<String, Object> selectDetail(HashMap<String, Object> param) throws Exception {
    	return (HashMap<String, Object>)template.queryForObject("BatInterface.selectDetail", param);
    }	

	public List<Map<String, Object>> dbAdapterList(HashMap<String, Object> param) throws Exception {
    	return (List<Map<String, Object>>)template.queryForList("BatInterface.dbAdapterList", param);
    }	

	public List<Map<String, Object>> fileAdapterList(HashMap<String, Object> param) throws Exception {
    	return (List<Map<String, Object>>)template.queryForList("BatInterface.fileAdapterList", param);
    }	

    public void insert(HashMap<String, Object> paramMap) throws Exception{
        this.template.insert("BatInterface.insert", paramMap);
    }      

    public void update(HashMap<String, Object> paramMap) throws Exception{
        this.template.update("BatInterface.update", paramMap);
    }

	public void delete(HashMap<String, Object> paramMap) throws Exception {
		this.template.delete("BatInterface.delete", paramMap);
	}
	
	public void deleteWholeInterface(String interfaceId) throws Exception {
    	//TSEAIEA04 인터페이스   batIntfDao.deleteWholeInterface(intfName);
    	//TSEAIEA03 DB아답터
    	//TSEAIEA05 file아답터
    	//TSEAIES07 스케쥴
    	//TSEAIES09 스케쥴-인터페이스
		//2.1 인터페이스에 연결되어 있는 자료 삭제(TSEAIEA03)
		//2.2 인터페이스에 연결되어 있는 자료 삭제(TSEAIEA05)
		//2.3 인터페이스에 연결되어 있는 자료 삭제(TSEAIES07)
		//2.4 인터페이스에 연결되어 있는 자료 삭제(TSEAIES09)
		
		Map<String, Object> paramMap = new HashMap<String, Object>(); 
		paramMap.put("intfId", interfaceId);
		Object queryObject = template.queryForObject("BatInterface.selectBatchType", paramMap);
		String batchType = null;
		if(queryObject != null) {
			batchType = (String) queryObject;
		}
		
		if (batchType == null) {
			// 존재하지 않는 인터페이스
		} else if (batchType.equalsIgnoreCase("DD") || batchType.equalsIgnoreCase("FF")) {
			template.delete("BatInterface.deleteAdapter03ByIntfId", paramMap);
			template.delete("BatInterface.deleteAdapter05ByIntfId", paramMap);
			template.delete("BatInterface.deleteScheduleES07ByIntfId", paramMap); //ES09삭제하기 전에 ES07을 삭제해야됨
			template.delete("BatInterface.deleteScheduleES09", paramMap);
			template.delete("BatInterface.delete", paramMap);
		} else {
			throw new Exception("배치 인터페이스 ["+interfaceId+"] 의 BatchType을 알 수 없어 초기화에 실패했습니다.");
		}
		
	}

	public int adapterListCount(HashMap<String, Object> param) throws Exception {
    	return (Integer)template.queryForObject("BatInterface.adapterListCount", param);
    }	

	public List<Map<String, Object>> adapterList(HashMap<String, Object> param) throws Exception {
    	return (List<Map<String, Object>>)template.queryForList("BatInterface.adapterList", param);
    }	
	
    public void sndAdptDbUpdate(HashMap<String, Object> paramMap) throws Exception{
        this.template.update("BatInterface.sndAdptDbUpdate", paramMap);
    }
    
    public void sndAdptFileUpdate(HashMap<String, Object> paramMap) throws Exception{
        this.template.update("BatInterface.sndAdptFileUpdate", paramMap);
    }

    public void rcvAdptDbUpdate(HashMap<String, Object> paramMap) throws Exception{
    	this.template.update("BatInterface.rcvAdptDbUpdate", paramMap);
    }
    
    public void rcvAdptFileUpdate(HashMap<String, Object> paramMap) throws Exception{
    	this.template.update("BatInterface.rcvAdptFileUpdate", paramMap);
    }
	public HashMap<String, Object> adapterDetail(HashMap<String, Object> param) throws Exception {
    	return (HashMap<String, Object>)template.queryForObject("BatInterface.adapterDetail", param);
    }	
	public String selectScheduleId(HashMap<String, Object> param) throws Exception {
    	return (String)template.queryForObject("BatInterface.selectScheduleId", param);
    }

	public List<SchedulerMonitorVO> selectSchedulerMonitorVOList(Map<String, Object> paramMap) {
		return template.queryForList("BatInterface.selectSchedulerMonitorVOList", paramMap);
	}

	public Integer selectSchedulerMonitorVOListCount(Map<String, Object> paramMap) {
		return (Integer) template.queryForObject("BatInterface.selectSchedulerMonitorVOListCount", paramMap);
	}	
	
}