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

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;
import com.eactive.eai.rms.bap.manage.comm.property.BapTelePropertyDao;

@Service("bapMessageProcService")
public class BapMessageProcService extends BapBaseService {

	@Autowired
	@Qualifier("bapMessageProcDao")
	private BapMessageProcDao dao;

	@Autowired
	@Qualifier("bapTelePropertyDao")
	private BapTelePropertyDao telePropDao;

	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(HashMap<String, Object> param) throws Exception {

		HashMap<String, Object>   detail        = dao.selectDetail(param);
		String bjobBzwkDstcd = (String)detail.get("BJOBBZWKDSTCD");
		String bjobTranDstcdName = (String)detail.get("BJOBTRANDSTCDNAME");
		param.put("bjobBzwkDstcd", bjobBzwkDstcd );
		param.put("propGroupName", "TelegramInfo{" + bjobBzwkDstcd + "_" + bjobTranDstcdName + "}");
		
		List<Map<String, Object>> list          = dao.getProps(param);
		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, HashMap<String, Object> connList) throws Exception {
		dao.update(paramMap);
		telePropDao.delete(paramMap);
		telePropDao.deleteProperty(paramMap);
		telePropDao.insert(paramMap);
		HashMap<String, Object> prop ;
		for(HashMap<String, Object> map : list){
			String prptyName = (String)map.get("PRPTYNAME");
			String prpty2Val = (String)map.get("PRPTY2VAL");
			if ( !prptyName.equals("") && !prpty2Val.equals("")){
				prop = new HashMap<String, Object>();
				prop.putAll(paramMap);
				prop.put("prptyGroupName",paramMap.get("prptyGroupName"));
				prop.put("prptyName", prptyName);
				prop.put("prpty2Val", prpty2Val);
				telePropDao.insertProperty(prop);
			}
		}
	}

	public void insert(HashMap<String, Object> paramMap, HashMap<String, Object>[] list, HashMap<String, Object> connList) throws Exception {		
		paramMap.put( "bjobMsgDstcd", dao.generatePK(paramMap));
		dao.insert(paramMap);
		telePropDao.delete(paramMap);
		telePropDao.deleteProperty(paramMap);
		telePropDao.insert(paramMap);
		HashMap<String, Object> prop ;
		for(HashMap<String, Object> map : list){
			String prptyName = (String)map.get("PRPTYNAME");
			String prpty2Val = (String)map.get("PRPTY2VAL");
			if ( !prptyName.equals("") && !prpty2Val.equals("")){
				prop = new HashMap<String, Object>();
				prop.putAll(paramMap);
				prop.put("prptyGroupName",paramMap.get("prptyGroupName"));
				prop.put("prptyName", prptyName);
				prop.put("prpty2Val", prpty2Val);
				telePropDao.insertProperty(prop);
			}
		}
	}

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

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

		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("bjobBzwkDstcd", bjobBzwkDstcd);
		param.put("propGroupName", "TelegramInfo{" + bjobMsgDstcd + "}");
		List<Map<String, Object>> list = dao.getProps(param);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		return map;
	}
	
	public HashMap<String, Object> getProps(String bjobBzwkDstcd) throws Exception {

		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("bjobBzwkDstcd", bjobBzwkDstcd);
		List<Map<String, Object>> list = dao.getProps(param);
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("rows", list);

		return map;
	}

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

		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("bjobBzwkDstcd", bjobBzwkDstcd);

		List<Map<String, Object>> list = dao.selectOsidInstiDstcd(param);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		return map;
	}

	public String checkPK(HashMap<String, Object> param) throws Exception {
		return dao.checkPK(param);
	}
	
	public HashMap<String, Object> selectVerifyInfo(HashMap<String, Object> param) throws Exception {

		HashMap<String, Object> map = new HashMap<String, Object>();
		HashMap<String, Object> detail = dao.selectVerifyInfo(param);
		map.put("detail", detail);

		return map;
	}
	
	public void updateVerifyInfo(HashMap<String, Object> param) throws Exception {
		dao.updateVerifyInfo(param);
	}
}