package com.eactive.eai.rms.onl.httpouttlsinfo;

import com.eactive.eai.data.entity.onl.httpouttlsinfo.HttpOutTlsInfo;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.httpouttlsinfo.HttpOutTlsInfoService;
import com.eactive.eai.rms.onl.common.exception.BizException;
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 java.io.IOException;

@Service
@Transactional
public class HttpOutTlsInfoManService extends BaseService {

	private final HttpOutTlsInfoManMapper httpOutTlsInfoManMapper;

	private final HttpOutTlsInfoService httpOutTlsInfoService;


	@Autowired
	public HttpOutTlsInfoManService(HttpOutTlsInfoManMapper httpOutTlsInfoManMapper,
									HttpOutTlsInfoService httpOutTlsInfoService) {
		this.httpOutTlsInfoManMapper = httpOutTlsInfoManMapper;
		this.httpOutTlsInfoService = httpOutTlsInfoService;
	}

	public Page<HttpOutTlsInfoUI> selectList(Pageable pageable) {
		Page<HttpOutTlsInfo> page = httpOutTlsInfoService.findAll(pageable);
		return page.map(httpOutTlsInfoManMapper::toVo);
	}


	public HttpOutTlsInfoUI selectDetail(String clientId) {
		HttpOutTlsInfo httpOutTlsInfo = httpOutTlsInfoService.getById(clientId);
		return httpOutTlsInfoManMapper.toVo(httpOutTlsInfo);
	}

	public void update(HttpOutTlsInfoUI httpOutTlsInfoUI) {
		HttpOutTlsInfo httpOutTlsInfo = httpOutTlsInfoService.getById(httpOutTlsInfoUI.getClientId());
		httpOutTlsInfoManMapper.updateToEntity(httpOutTlsInfoUI, httpOutTlsInfo);
		httpOutTlsInfoService.save(httpOutTlsInfo);
	}

	public void insert(HttpOutTlsInfoUI httpOutTlsInfoUI) throws IOException {

		if(httpOutTlsInfoService.existsById(httpOutTlsInfoUI.getClientId())) {
			throw new BizException("ClientId가 이미 존재합니다.");
		}

		HttpOutTlsInfo httpOutTlsInfo = httpOutTlsInfoManMapper.toEntity(httpOutTlsInfoUI);

		httpOutTlsInfoService.save(httpOutTlsInfo);
	}


	public void delete(String clientId) {
		httpOutTlsInfoService.deleteById(clientId);
	}

    public boolean existsById(String clientId) {
		return httpOutTlsInfoService.existsById(clientId);
    }
}
