package com.eactive.eai.rms.onl.apim.template;

import com.eactive.apim.portal.template.entity.MessageTemplate;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.apim.template.MessageTemplateSearch;
import com.eactive.eai.rms.data.entity.onl.apim.template.MessageTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(transactionManager = "transactionManagerForEMS")
@RequiredArgsConstructor
public class MessageTemplateManService extends BaseService {

	private final MessageTemplateService messageTemplateService;

	private final MessageTemplateUIMapper messageTemplateUIMapper;

	public Page<MessageTemplateUI> selectList(Pageable pageable, MessageTemplateSearch messageTemplateSearch) {
		Page<MessageTemplate> page = messageTemplateService.findAll(pageable, messageTemplateSearch);
		return page.map(messageTemplateUIMapper::toVo);
	}

	public MessageTemplateUI selectDetail(String id) {
		MessageTemplate messageTemplate = messageTemplateService.getById(id);
		return messageTemplateUIMapper.toVo(messageTemplate);
	}

	public void insert(MessageTemplateUI messageTemplateUI) {
		MessageTemplate messageTemplate = messageTemplateUIMapper.toEntity(messageTemplateUI);
		messageTemplateService.save(messageTemplate);
	}

	public void update(MessageTemplateUI messageTemplateUI) {
		MessageTemplate messageTemplate = messageTemplateService.getById(messageTemplateUI.getMessageCode());
		messageTemplateUIMapper.updateToEntity(messageTemplateUI, messageTemplate);
		messageTemplateService.save(messageTemplate);
	}

	public void delete(String id) {
		messageTemplateService.deleteById(id);
	}

}
