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

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.bat.common.base.BatBaseService;

@Service("batInterfaceFileService")
public class BatInterfaceFileService extends BatBaseService {

	@Autowired
	@Qualifier("batInterfaceFileDao")
	private BatInterfaceFileDao dao;

	public List<HashMap<String, Object>> selectDetailCombo(HashMap<String,Object> paramap) throws Exception {
		return dao.selectDetailCombo(paramap);
	}
	
	public HashMap<String, Object> selectList(int startNum,int endNum, HashMap<String,Object> paramap) throws Exception {
		
		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramap);
		
		paramap.put("startNum" , startNum);// 시작 record
		paramap.put("endNum"   , endNum);// 끝 record
		
		List<Map<String, Object>> list	= dao.selectList(paramap);
		HashMap<String, Object> map		= new HashMap<String, Object>();
		
		map.put("totalCount", totalCount);
		map.put("list", list);
		
		return map;
	}	

	public HashMap<String, Object> selectDetail(String intfId, String fileNamePtrn) throws Exception {

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("intfId", intfId);
		paramMap.put("fileNamePtrn", fileNamePtrn);

		HashMap<String, Object> detail	= dao.selectDetail(paramMap);
		HashMap<String, Object> map		= new HashMap<String, Object>();

		map.put("detail"   , detail);

		return map;
	}	

	public void insert(HashMap<String, Object> paramMap) throws Exception {
		isValid(paramMap);
		
		HashMap<String, Object> detail	= dao.selectDetail(paramMap);
		if(detail != null) throw new Exception("이미 등록된 인터페이스ID 입니다.");
		dao.insert(paramMap);
	}

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

	public void delete(HashMap<String, Object> paramMap) throws Exception {
		isValid(paramMap);
		dao.delete(paramMap);
	}
	
	private void isValid(HashMap<String, Object> param) throws Exception{
		if("".equals(param.get("intfId")) || param.get("intfId") == null) throw new Exception("인터페이스ID를 선택하여 주십시요.");
		if("".equals(param.get("fileNamePtrn"))) throw new Exception("파일명패턴을 입력하여 주십시요.");
	}
}
