package com.eactive.eai.rms.onl.manage.serviceinfo.jmsqueue;

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

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitor;
import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitorId;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.QueueMonitorSearch;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.QueueMonitorService;
import com.eactive.eai.rms.onl.common.exception.BizException;

/**
 * JMS Queue 관리 서비스
 */
@Service
@Transactional
public class JmsQueueManService extends BaseService {

	@Autowired
	private QueueMonitorService service;

	@Autowired
	private JmsQueueManMapper mapper;

	public Page<JmsQueueManUI> selectList(Pageable pageable, QueueMonitorSearch uiSearch) throws Exception {
		Page<QueueMonitor> pageEntity = service.findPage(pageable, uiSearch);
		return pageEntity.map(mapper::toVo);
	}

	public JmsQueueManUI selectDetail(String nonSynczTranQueName, String sevrInstncName) throws Exception {
		QueueMonitorId id = new QueueMonitorId();
		id.setNonsyncztranquename(nonSynczTranQueName);
		id.setEaisevrinstncname(sevrInstncName);
		QueueMonitor queueEntity = service.getById(id);
		return mapper.toVo(queueEntity);
	}

	public void insert(JmsQueueManUI ui) throws Exception {
		QueueMonitor entity = mapper.toEntity(ui);
		if (service.findById(entity.getId()).isPresent())
			throw new BizException(entity.getId() + " key already exists");
		
//		if (service.findBizCount(ui.getQueBzwkCtnt(), ui.getBzwkDstcd(), ui.getQueUsagDstcd()).isPresent())
//			throw new BizException(ui.getQueBzwkCtnt() +", "+ ui.getBzwkDstcd() +", "+ ui.getQueUsagDstcd() + " key already exists");
		
		service.save(entity);
	}

	public void update(JmsQueueManUI ui) throws Exception {
		QueueMonitor entity = service.getById(mapper.toEntity(ui).getId());
		mapper.updateToEntity(ui, entity);
		service.save(entity);
	}

	public void delete(String nonSynczTranQueName, String sevrInstncName) throws Exception {
		QueueMonitorId id = new QueueMonitorId();
		id.setEaisevrinstncname(sevrInstncName);
		id.setNonsyncztranquename(nonSynczTranQueName);
		service.deleteById(id);
	}

	public Map<String, List<?>> selectListToExcel(QueueMonitorSearch uiSearch) throws Exception {
		Iterable<QueueMonitor> c2rServices = service.findAll(uiSearch);
		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for (QueueMonitor item : c2rServices) {
			HashMap<String, Object> row = new HashMap<>();
			row.put("NONSYNCZTRANQUENAME", item.getId().getNonsyncztranquename());
			row.put("SEVRINSTNCNAME", item.getId().getEaisevrinstncname());
			row.put("QUEBZWKCTNT", item.getEaiquebzwkctnt());
			row.put("QUEUSEYN", item.getQueuseyn());
			row.put("QUEUSAGDSTCD", item.getQueusagdstcd());
			row.put("BZWKDSTCD", item.getEaibzwkdstcd());
			row.put("QUEBZWKDTALSCTNT", item.getEaiquebzwkdtalsctnt());
			row.put("QUECRTCNMVLVAL", item.getEaiquecrtcnmvlval());
			list.add(row);
		}

		HashMap<String, List<?>> map = new HashMap<>();
		map.put("list", list);

		return map;
	}
}
