package com.eactive.eai.rms.onl.manage.rule.transformfunc;

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.data.entity.onl.transformer.function.Function;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.data.entity.onl.transform.FunctionService;
import com.eactive.eai.rms.onl.manage.rule.transformfunc.map.TransformFuncManMapper;
import com.eactive.eai.rms.onl.manage.rule.transformfunc.map.TransformFuncManMapperWithoutChild;
import com.eactive.eai.rms.onl.manage.rule.transformfunc.ui.TransformFuncUI;

/**
 * Rule 관련 Service 클래스입니다.
 */
@Service
@Transactional
public class TransformFuncManService extends OnlBaseService {

	@Autowired
	private TransformFuncManMapperWithoutChild mapperWithoutChild;

	@Autowired
	private TransformFuncManMapper mapper;

	@Autowired
	private FunctionService functionService;

	public Page<TransformFuncUI> selectList(Pageable pageable, String searchCnvsnFuntnName) {
		Page<Function> entityPage = functionService.findAll(pageable, searchCnvsnFuntnName);
		return entityPage.map(f -> mapperWithoutChild.toVo(f));
	}

	public TransformFuncUI selectDetail(String cnvsnFuntnName) {
		Function function = functionService.getById(cnvsnFuntnName);
		function.sortChildren();
		return mapper.toVo(function);
	}

	public void update(TransformFuncUI ui) {
		Function fuction = functionService.getById(ui.getCnvsnFuntnName());
		mapper.updateToEntity(ui, fuction);
		functionService.save(fuction);
	}

	public void insert(TransformFuncUI ui) {
		Function fuction = mapper.toEntity(ui);
		functionService.save(fuction);
	}

	public void delete(String cnvsnFuntnName) {
		functionService.deleteById(cnvsnFuntnName);
	}
}
