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

import java.util.ArrayList;
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.util.StringUtils;

@Service("spcfTmAdapterService")
public class SpcfTmAdapterService {


	@Autowired
	@Qualifier("spcfTmAdapterDao")
	private SpcfTmAdapterDao dao;

	public HashMap selectList(int startNum,int endNum, String searchAdptrBzwkGroupName, String searchAdptrMngDstCd) throws Exception {
		HashMap paramMap = new HashMap();
		//검색 조건 
		paramMap.put("searchAdptrBzwkGroupName", searchAdptrBzwkGroupName);
		paramMap.put("searchAdptrMngDstCd", searchAdptrMngDstCd);
		
		// 전체 목록수 얻기
		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);

		List<HashMap> resultList = new ArrayList<HashMap>();
		
		for(HashMap tmp : list){
			String adptrMngDstCd = (String)tmp.get("ADPTRMNGDSTCD");
			String adptrStartHms = (String)tmp.get("ADPTRSTARTHMS");
			String adptrStopHms = (String)tmp.get("ADPTRSTOPHMS");
			
			if( !"".equals(adptrStartHms.trim())){
				tmp.put("ADPTRSTARTHMS", makeCronDesc(adptrStartHms,adptrMngDstCd));
			}
			
			if( !"".equals(adptrStopHms.trim())){
				tmp.put("ADPTRSTOPHMS", makeCronDesc(adptrStopHms,adptrMngDstCd));
			}
			
			resultList.add(tmp);
		}
		
		map.put("list", resultList);

		return map;
	}	
	
	// 매월 특정일까지만 입력한다는 조건하에 생성 ( 월, 요일은 무조건 * ?로 가정)
	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];
				}
			}
			
		}else 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] +" ";
				}
		}else{
			for(int i =6 ; i >= 0; i--){ 
				if("*".equals(cron[6])){ 
					if(i==0) break;
					if(i == 2){
						Desc = cron[i] +gubun[i];
					}
					if(i == 1){
						Desc = "메일 "+Desc + cron[i] +gubun[i];
					}
				}else{
					if(i==0 ) break;
					if(i==5) continue;
					Desc = Desc + cron[i] +gubun[i] +" ";
				}
			}
		}

		return Desc;
	}

	public HashMap selectDetail(String adptrBzwkGroupName) throws Exception {
		HashMap paramMap = new HashMap();
		paramMap.put("adptrBzwkGroupName",adptrBzwkGroupName);
		return dao.selectDetail(paramMap);//어댑터그룹정보
	}	
	
	public void insert(HashMap paramMap) throws Exception {
		dao.insert(paramMap);
	}	
	
	public void update(HashMap paramMap) throws Exception {
		dao.update(paramMap);
	}	

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

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

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

		HashMap map = new HashMap();

		List<HashMap> resultList = new ArrayList<HashMap>();
		
		for(HashMap tmp : list){
			String adptrMngDstCd = (String)tmp.get("ADPTRMNGDSTCD");
			String adptrStartHms = (String)tmp.get("ADPTRSTARTHMS");
			String adptrStopHms = (String)tmp.get("ADPTRSTOPHMS");
			
			if( !"".equals(adptrStartHms.trim())){
				tmp.put("ADPTRSTARTHMS", makeCronDesc(adptrStartHms,adptrMngDstCd));
			}
			
			if( !"".equals(adptrStopHms.trim())){
				tmp.put("ADPTRSTOPHMS", makeCronDesc(adptrStopHms,adptrMngDstCd));
			}
			
			resultList.add(tmp);
		}
		
		map.put("list", resultList);

		return map;
	}


}
