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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
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.data.entity.onl.lifecycle.LifeCycle;
import com.eactive.eai.rms.common.base.BaseService;

@Transactional
@Service
public class LifecycleManService extends BaseService {

	@Autowired
	private LifecycleUIMapper lifeCycleUIMapper;

	@Autowired
	private LifecycleService lifecycleService;

	public Page<LifecycleUI> selectList(Pageable pageable) {
		Page<LifeCycle> page = lifecycleService.findAll(pageable);
		return page.map(lifeCycleUIMapper::toVo);
	}

	public LifecycleUI selectDetail(String lifeCyclClsName) {
		LifeCycle lifeCycle = lifecycleService.getById(lifeCyclClsName);
		return lifeCycleUIMapper.toVo(lifeCycle);
	}

	public void insert(LifecycleUI lifecycleUI) {
		LifeCycle lifeCycle = lifeCycleUIMapper.toEntity(lifecycleUI);
		if (lifecycleService.existsById(lifeCycle.getLifecyclclsname())) {
			throw new DataIntegrityViolationException("중복된 데이터입니다.");
		}
		lifecycleService.save(lifeCycle);
	}

	public void update(LifecycleUI lifecycleUI) {
		LifeCycle lifecycle = lifecycleService.getById(lifecycleUI.getLifeCyclClsName());
		lifeCycleUIMapper.updateToEntity(lifecycleUI, lifecycle);
		lifecycleService.save(lifecycle);
	}

	public void delete(String lifeCyclClsName) {
		lifecycleService.deleteById(lifeCyclClsName);
	}

}
