package com.eactive.eai.rms.bap.manage.comm.property;

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

import org.apache.commons.lang.StringEscapeUtils;
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("bapTelePropertyService")
public class BapTelePropertyService extends BapBaseService {

	@Autowired
	@Qualifier("bapTelePropertyDao")
	private BapTelePropertyDao 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 prptyGroupName) throws Exception {

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

		HashMap<String, Object>   detail = dao.propertyGroupDetail(paramMap);
		List<Map<String, Object>> list   = dao.propertyDetail(paramMap);

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

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

		return map;

	}	
	public List<Map<String, Object>> selectPropertyGroupList(String key1, String key2) throws Exception {
		// 프라퍼티 조회 -> key1{key2}
		HashMap paramMap = new HashMap();
		paramMap.put("key1", key1);
		paramMap.put("key2", key2);
		return dao.selectPropertyGroupList(paramMap);
	}

	public void insert(HashMap<String, Object> paramMap, HashMap<String, Object>[] list) throws Exception {
		dao.insert(paramMap);
		HashMap<String, Object> data ;
		for(HashMap<String, Object> map : list){
			data = new HashMap<String, Object>();
			data.putAll(paramMap);
			data.put("prptyName", StringEscapeUtils.unescapeHtml(map.get("PRPTYNAME").toString()));
			data.put("prpty2Val", StringEscapeUtils.unescapeHtml(map.get("PRPTY2VAL").toString()));
			dao.insertProperty(data);
		}

	}

	public void update(HashMap<String, Object> paramMap, HashMap<String, Object>[] list) throws Exception {
		dao.update(paramMap);
		dao.deleteProperty(paramMap);
		HashMap<String, Object> data ;
		for(HashMap<String, Object> map : list){
			data = new HashMap<String, Object>();
			data.putAll(paramMap);
			data.put("prptyName",StringEscapeUtils.unescapeHtml(map.get("PRPTYNAME").toString()));
			data.put("prpty2Val",StringEscapeUtils.unescapeHtml(map.get("PRPTY2VAL").toString()));
			dao.insertProperty(data);
		}
	}	

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