package com.eactive.eai.rms.bap.manage.work.bizgbn;

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("bapBizGbnService")
public class BapBizGbnService extends BapBaseService {

	@Autowired
	@Qualifier("bapBizGbnDao")
	private BapBizGbnDao dao;

	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);
		HashMap<String, Object> map = new HashMap<String, Object>();

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

		return map;
	}

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

		HashMap<String, Object>   detail = dao.selectDetail(bjobBzwkDstcd);
		List<Map<String, Object>> list   = dao.jobpropinfo(bjobBzwkDstcd);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("detail", detail);
		map.put("rows", list);

		return map;
	}

	public void update(HashMap<String, Object> paramMap, HashMap<String, Object>[] list) throws Exception {
		dao.update(paramMap);
		dao.deletePropInfo(paramMap);
		HashMap<String, Object> data;
		for(HashMap<String, Object> map : list) {
			data = new HashMap<String, Object>();
			data.putAll(paramMap);
			data.put("prptyTypDstcd",map.get("PRPTYTYPDSTCD"));
			data.put("prptyName",map.get("PRPTYNAME"));
			data.put("prptyDesc",map.get("PRPTYDESC"));
			dao.insertPropInfo(data);
		}
	}

	public void insert(HashMap<String, Object> paramMap, HashMap<String, Object>[] list) throws Exception {
		dao.insert(paramMap);
		dao.deletePropInfo(paramMap);
		HashMap<String, Object> data ;
		for(HashMap<String, Object> map : list) {
			data = new HashMap<String, Object>();
			data.putAll(paramMap);
			data.put("prptyTypDstcd",map.get("PRPTYTYPDSTCD"));
			data.put("prptyName",map.get("PRPTYNAME"));
			data.put("prptyDesc",map.get("PRPTYDESC"));
			dao.insertPropInfo(data);
		}
	}

	public void delete(HashMap<String, Object> paramMap) throws Exception {
		dao.delete(paramMap);
		dao.deletePropInfo(paramMap);
	}
	
	public void insertClone(HashMap<String, Object> paramMap) throws Exception {
		if (dao.checkExists(paramMap) > 0) {
			throw new Exception("중복된 대외기관코드가 존재합니다.");
		}
		
		dao.insertClone(paramMap);
	}
}