package com.eactive.eai.rms.onl.manage.comm.error;

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 org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterGroupService;
import com.eactive.eai.rms.data.entity.onl.inbound.error.InboundErrorInfoSearch;
import com.eactive.eai.rms.data.entity.onl.inbound.error.InboundErrorInfoService;

@Service
@Transactional
public class InboundErrorInfoManService extends BaseService {

	private InboundErrorInfoService inboundErrorInfoService;

	private InboundErrorInfoUIMapper inboundErrorInfoUIMapper;

	private AdapterGroupService adapterGroupService;

	@Autowired
	public InboundErrorInfoManService(InboundErrorInfoService inboundErrorInfoService,
			InboundErrorInfoUIMapper inboundErrorInfoUIMapper, AdapterGroupService adapterGroupService) {
		this.inboundErrorInfoService = inboundErrorInfoService;
		this.inboundErrorInfoUIMapper = inboundErrorInfoUIMapper;
		this.adapterGroupService = adapterGroupService;
	}

	public Page<InboundErrorInfoUI> selectList(Pageable pageable, InboundErrorInfoSearch inboundErrorInfoSearch) {
		return inboundErrorInfoService.findAll(pageable, inboundErrorInfoSearch).map(inboundErrorInfoUIMapper::toVo);
	}

	public InboundErrorInfoUI selectDetail(String eaiSvcSerno) {

		InboundErrorInfoUI ui = inboundErrorInfoUIMapper.toVo(inboundErrorInfoService.getById(eaiSvcSerno));

		if (ui.getAdptrBzwkGroupName() != null) {
			adapterGroupService.findById(ui.getAdptrBzwkGroupName()).ifPresent(a -> {
				ui.setAdptrMsgDstcd(a.getAdptrmsgdstcd());
				ui.setAdptrMsgPtrncd(a.getAdptrmsgptrncd());
			});
		}

		return ui;
	}

}
