package com.eactive.eai.rms.bap.manage.schedule.procPeriod;

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

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

import com.eactive.eai.rms.bap.common.base.BapBaseService;

@Service("bapProcPeriodService")
public class BapProcPeriodService extends BapBaseService {

	@Autowired
	@Qualifier("BapProcPeriodDao")
	private BapProcPeriodDao dao;
	
	private boolean isNum(String s){
		try{
			Double.parseDouble(s);
			return true;
		}
		catch(Exception e)
		{
			return false;
		}
	}

	public HashMap<String, Object> selectList(int startNum,int endNum, HashMap<String, Object> paramMap) throws Exception {
 
		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramMap);

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

		List<Map<String, Object>> list = dao.selectList(paramMap);
		String strSndRcvCyclTypName = "";
		
		for(Map<String, Object> m : list){
			if( "D".equals(m.get("SNDRCVCYCLTYPNAME"))  ||
				"Y".equals(m.get("SNDRCVCYCLTYPNAME"))  ||
				"Y+".equals(m.get("SNDRCVCYCLTYPNAME")) ||
				"S".equals(m.get("SNDRCVCYCLTYPNAME")))
			{
				// 일반복
				strSndRcvCyclTypName = "C1";
			}
			else if(m.get("SNDRCVCYCLTYPNAME").toString().length() >= 4 &&
					isNum(m.get("SNDRCVCYCLTYPNAME").toString().substring(0,1))
			) {
				// 월반복
				strSndRcvCyclTypName = "C2";
			}
			else if(
				// 월 영업일 반복
				m.get("SNDRCVCYCLTYPNAME").toString().length() == 5 &&
				m.get("SNDRCVCYCLTYPNAME").toString().charAt(0) == 'Y'
			) {
				strSndRcvCyclTypName = "C3";
			}
			else if(
				// 주반복
				m.get("SNDRCVCYCLTYPNAME").toString().length() == 2 &&
				m.get("SNDRCVCYCLTYPNAME").toString().charAt(0) == 'D'
			) {
				strSndRcvCyclTypName = "C4";
			}
			else if(
				// 매주 영업일 반복
				m.get("SNDRCVCYCLTYPNAME").toString().length() >= 3 &&
				"DY".equals(m.get("SNDRCVCYCLTYPNAME").toString().substring(0,2))
			) {
				strSndRcvCyclTypName = "C5";
			}
			else if(
				// 주 요일 반복
				m.get("SNDRCVCYCLTYPNAME").toString().length() == 4 &&
				m.get("SNDRCVCYCLTYPNAME").toString().charAt(0) == 'W' &&
				m.get("SNDRCVCYCLTYPNAME").toString().charAt(2) == 'D'
			) {
				strSndRcvCyclTypName = "C6";
			}
			else if(
				// 주 영업일 반복
				m.get("SNDRCVCYCLTYPNAME").toString().length() >= 4 &&
				m.get("SNDRCVCYCLTYPNAME").toString().charAt(0) == 'W' &&
				m.get("SNDRCVCYCLTYPNAME").toString().charAt(2) == 'Y'
			) {
				strSndRcvCyclTypName = "C7";
			}
			
			m.put("SNDRCVCYCLTYPNAME", strSndRcvCyclTypName);
		}
		
		HashMap<String, Object> map = new HashMap<String, Object>();

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

		return map;
	}

	public HashMap<String, Object> selectDetail(String bJobMsgScheId) throws Exception {
		return dao.selectDetail( bJobMsgScheId );
	}

	public void update(HashMap<String, Object> paramMap) throws Exception {
		dao.update(paramMap);
	}

	public void insert(HashMap<String, Object> paramMap) throws Exception {
		paramMap.put( "bjobMsgScheID", dao.generatePK(paramMap));
		
		if(dao.checkCount(paramMap) > 0)
			throw new Exception("이미 등록된 거래처리 주기 입니다.");
		
		dao.insert(paramMap);
	}

	public void delete(HashMap<String, Object> paramMap) throws Exception {
		dao.delete(paramMap);
	}
}