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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.StringUtils;

@Service("spcfTmAdapterHistoryService")
public class SpcfTmAdapterHistoryService {


	@Autowired
	@Qualifier("spcfTmAdapterHistoryDao")
	private SpcfTmAdapterHistoryDao dao;

	public HashMap selectList(int startNum,int endNum,  String searchAdptrBzwkGroupName,String searchAdptrStartHms, String searchAdptrStopHms,
			String searchAdptrMngDstCd, String searchAdptrEvtStatus) throws Exception {
		HashMap paramMap = new HashMap();
		//검색 조건 
		paramMap.put("searchAdptrBzwkGroupName", searchAdptrBzwkGroupName);
		paramMap.put("searchAdptrStartHms", searchAdptrStartHms);
		paramMap.put("searchAdptrStopHms", searchAdptrStopHms);
		paramMap.put("searchAdptrMngDstCd", searchAdptrMngDstCd);
		paramMap.put("searchAdptrEvtStatus", searchAdptrEvtStatus);
 
		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramMap);

		paramMap.put("startNum" , startNum);// 시작 record
		paramMap.put("endNum"   , endNum);// 끝 record

		List<HashMap> list = dao.selectList(paramMap);

		HashMap map = new HashMap();

		map.put("totalCount", totalCount);
		map.put("list", list);

		return map;
	}	

	public HashMap selectDetail(String adptrBzwkGroupName,String adptrEventHms) throws Exception {
		HashMap paramMap = new HashMap();
		HashMap returnMap = new HashMap();
		paramMap.put("adptrBzwkGroupName", adptrBzwkGroupName);
		paramMap.put("adptrEventHms", adptrEventHms);
		
		returnMap = dao.selectDetail(paramMap);
		
		String adptrMngDstCd = (String)returnMap.get("ADPTRMNGDSTCD");
		String adptrStartHms = (String)returnMap.get("ADPTRSTARTHMS");
		String adptrStopHms = (String)returnMap.get("ADPTRSTOPHMS");
		
		if( !"".equals(adptrStartHms.trim())){
			returnMap.put("ADPTRSTARTHMS", makeCronDesc(adptrStartHms,adptrMngDstCd));
		}
		
		if( !"".equals(adptrStopHms.trim())){
			returnMap.put("ADPTRSTOPHMS", makeCronDesc(adptrStopHms,adptrMngDstCd));
		}
		
		return returnMap;
	}	
	
	public void insert(HashMap paramMap) throws Exception {
		//서버구분
		String instName = (String)paramMap.get("serverName");
		
		for(DataSourceType d : DataSourceTypeManager.getOnlineDataSourceTypes() ) {
	  		DataSourceContextHolder.setDataSourceType(d);
		  	if(instName.equals(d.getName())){
		  		dao.insert(paramMap);
		  		
		  	}
		}
	}
	

	public void delete(HashMap paramMap) throws Exception {
		//inboundErrorInfoDao.delete(paramMap);
	}	

	// 매월 특정일까지만 입력한다는 조건하에 생성 ( 월, 요일은 무조건 * ?로 가정)
	public String makeCronDesc(String cronExp,String adptrMngDstCd) throws Exception{
		
		String[] gubun = {"초","분","시","일","월"," ","년"};
		
		String Desc = "";
		String[] cron = StringUtils.split(cronExp," ");
			
		if("1".equals(adptrMngDstCd)){ //daily    0 0 10 * * ? *      
			for(int i =6 ; i >= 0; i--){  
				if(i==0) break;
				if(i == 2){
					Desc = cron[i] +gubun[i];
				}
				if(i == 1){
					Desc = "메일 "+Desc + cron[i] +gubun[i];
				}
			}
			
		}if("2".equals(adptrMngDstCd)){  // 0 0 10 5 6 ? 2019  
				for(int i =6 ; i >= 0; i--){   //0 0 7 5 * ? 2019
					if(i==0 ) break;
					if(i==5) continue;
					Desc = Desc + cron[i] +gubun[i] +" ";
				}
		}

		return Desc;
	}
	public HashMap selectListToExcel(HashMap<String, Object> paramMap ) throws Exception {

		List list = dao.selectListToExcel(paramMap);

		HashMap map = new HashMap();

		map.put("list", list);

		return map;
	}
}
