package com.eactive.eai.rms.onl.manage.adapter.spcftmadapter;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;


@Repository("spcfTmAdapterDao")
public class SpcfTmAdapterDao extends SqlMapClientTemplateDao {
	
	@Autowired
	private MonitoringContext monitoringContext;
	
    public int selectListCount(HashMap param) throws Exception {
    	return (Integer)template.queryForObject("SpcfTmAdapter.selectListCount", param);
    }	
    public List selectList(HashMap param) throws Exception {
    	return template.queryForList("SpcfTmAdapter.selectList", param);
    }	

	public List selectListToExcel(HashMap param) throws Exception {
		return template.queryForList("SpcfTmAdapter.selectListToExcel", param);
	}	
    public HashMap selectDetail(HashMap param) throws Exception {
    	return (HashMap)template.queryForObject("SpcfTmAdapter.selectDetail", param);
    }

    public void insert(HashMap paramMap) throws Exception{
        this.template.insert("SpcfTmAdapter.insert", paramMap);
    }      
    public void update(HashMap paramMap) throws Exception{
        this.template.update("SpcfTmAdapter.update", paramMap);
    }      
    public void delete(HashMap paramMap) throws Exception{
        this.template.delete("SpcfTmAdapter.delete", paramMap);
    }	

    @SuppressWarnings("unchecked")
	public HashMap<String, AdapterJobInfo> getAdapterSchedule(HashMap<String, Object> paramMap) throws Exception {
    	
    	HashMap<String, AdapterJobInfo> infos = new HashMap<String, AdapterJobInfo>();
    	String JobName = "";
    	
    	//스케줄러 동작 해당 인스턴스명
		String adtrSvrInstNm = "";
		String keyVal = "adptr.scheduler.inst";
		
		try {
			adtrSvrInstNm = monitoringContext.getProperty(keyVal);
			logger.info("스케줄러 동작 해당 인스턴스명 : "+adtrSvrInstNm);
		} catch (Exception e1) {
			logger.error("어댑터 스케줄러를 동작 시킬 해당 인스턴스명이 등록되지 않았습니다."+e1);
		}
    	
    	for(DataSourceType d : DataSourceTypeManager.getOnlineDataSourceTypes() ) {
	  		DataSourceContextHolder.setDataSourceType(d);
	  		
	  		paramMap.put("schemaId", d.getSchema());
	  		List<HashMap<String, String>> list = null;
	  		
			list = this.template.queryForList("SpcfTmAdapter.selectAdapterSchedule", paramMap);
			
			String className = "com.eactive.eai.rms.onl.manage.adapter.spcftmadapter.AdapterSchedulerJob";
			String serverNm = DataSourceContextHolder.getDataSourceType().getName();
			
			for(HashMap<String,String> m : list){
				AdapterJobInfo info = null;
				
				m.put("serverNm", serverNm);
				m.put("adtrSvrInstNm", adtrSvrInstNm);
				
				if("0".equals(m.get("ADPTRMNGDSTCD"))) continue;
				
				if("1".equals(m.get("ADPTRMNGDSTCD"))) { //Daily 기동종료 둘다 등록
					JobName = makeJobName(m,"start");
					info = new AdapterJobInfo(JobName,m.get("ADPTRBZWKGROUPNAME"),m.get("ADPTRSTARTHMS").trim(),m.get("ADPTRMNGDSTCD"),"start",className,serverNm,adtrSvrInstNm);
					infos.put(JobName, info);
	
					JobName = makeJobName(m,"stop");
					info = new AdapterJobInfo(JobName,m.get("ADPTRBZWKGROUPNAME"),m.get("ADPTRSTOPHMS").trim(),m.get("ADPTRMNGDSTCD"),"stop",className,serverNm,adtrSvrInstNm);
					infos.put(JobName, info);
				}else{
					//기동시간 있을경우
					if(!"".equals(m.get("ADPTRSTARTHMS").trim())){
						JobName = makeJobName(m,"start");
						info = new AdapterJobInfo(JobName,m.get("ADPTRBZWKGROUPNAME"),m.get("ADPTRSTARTHMS").trim(),m.get("ADPTRMNGDSTCD"),"start",className,serverNm,adtrSvrInstNm);
						infos.put(JobName, info);
					}
					if(!"".equals(m.get("ADPTRSTOPHMS").trim())){ //종료시간있을경우
						JobName = makeJobName(m,"stop");
						info = new AdapterJobInfo(JobName,m.get("ADPTRBZWKGROUPNAME"),m.get("ADPTRSTOPHMS").trim(),m.get("ADPTRMNGDSTCD"),"stop",className,serverNm,adtrSvrInstNm);
						infos.put(JobName, info);
						
					}
				}
			

			}
		}
		
		return infos;
	}	
    
    public String makeJobName(HashMap<String,String> paramMap, String startstopTxt){
    	String JobName = ""; //JobName :  어댑터그룹명 + START/STOP + 서버인스턴스명		
    	
    	JobName = (String)paramMap.get("ADPTRBZWKGROUPNAME");
    	if("start".equals(startstopTxt)){
    		JobName += "-START";
    	}else{
    		JobName += "-STOP";
    	}
		
		JobName += "-"+paramMap.get("serverNm");    	
		
		return JobName;
    }
	

}
