package com.eactive.eai.rms.onl.manage.sna.luinfo;

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

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

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.onl.common.service.AgentUtilService;

@Service("luInfoService")
public class LuInfoService extends BaseService {

	@Autowired
	@Qualifier("luInfoDao")
	private LuInfoDao dao;

	@Autowired
	private ComboService comboService;

	@Autowired
	@Qualifier("onlAgentUtilService")
	public AgentUtilService agentUtilService;

	public HashMap 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<HashMap<String, Object>> list = dao.selectList(paramMap);

		// 송수신 구분 코드값 셋팅
		Map<String, Object> codeMap = comboService
				.getFromCode("SNA_SNDRCV_DSTCD")
				.stream()
				.collect(Collectors.toMap(ComboVo::getCode, ComboVo::getCodename));

		for (int i = 0; i < list.size(); i++) {
			HashMap<String, Object> tmp = (HashMap<String, Object>) list.get(i);
			if (tmp.get("SNDRCVDSTCD") == null || "".equals(tmp.get("SNDRCVDSTCD")))
				continue;
			tmp.put("SNDRCVDSTNM", codeMap.get(tmp.get("SNDRCVDSTCD")));
		}

		HashMap map = new HashMap();

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

		return map;
	}

	public HashMap selectDetail(String adptrGstatSevrName, String adptrGroupName, String luGroupName, String puName,
			String luName) throws Exception {

		HashMap paramMap = new HashMap();

		paramMap.put("adptrGstatSevrName", adptrGstatSevrName);
		paramMap.put("adptrGroupName", adptrGroupName);
		paramMap.put("luGroupName", luGroupName);
		paramMap.put("puName", puName);
		paramMap.put("luName", luName);

		HashMap resultMap = dao.selectDetail(paramMap);

		return resultMap;

	}

	public String insertData(HashMap paramMap) throws Exception {
		String msg = "success";
		int duplChk = 0;
		duplChk = dao.selectDuplChk(paramMap);

		if (duplChk == 0) {
			dao.insert(paramMap);
		} else {
			msg = "이미 등록된 정보입니다. [" + paramMap.get("luName") + "]";
			throw new Exception(msg);
		}
		return msg;
	}

	public void update(HashMap paramMap) throws Exception {
		dao.update(paramMap);

	}

	public void delete(HashMap paramMap) throws Exception {
		dao.delete(paramMap);
	}

	public HashMap selectListToExcel(HashMap<String, Object> paramMap) throws Exception {

		List list = dao.selectListToExcel(paramMap);

		// 송수신 구분 코드값 셋팅
		Map<String, Object> codeMap = comboService
				.getFromCode("SNA_SNDRCV_DSTCD")
				.stream()
				.collect(Collectors.toMap(ComboVo::getCode, ComboVo::getCodename));

		for (int i = 0; i < list.size(); i++) {
			HashMap tmp = (HashMap) list.get(i);
			String rowNo = tmp.get("ROWNO").toString();

			tmp.put("ROWNO", String.valueOf(rowNo));

			if (tmp.get("SNDRCVDSTNM") == null || "".equals(tmp.get("SNDRCVDSTNM")))
				continue;
			tmp.put("SNDRCVDSTNM", codeMap.get(tmp.get("SNDRCVDSTNM")));
		}

		HashMap map = new HashMap();

		map.put("list", list);

		return map;
	}

}
