package com.eactive.eai.rms.onl.manage.serviceinfo.stddefault;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.data.entity.onl.stdmessage.ExtendedColumnDefinition;
import com.eactive.eai.rms.data.entity.onl.stdmessage.ExtendedColumnDefinitionService;
import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.eactive.eai.rms.onl.manage.serviceinfo.stdmessage.ExtendedColumnDefinitionUI;
import com.eactive.eai.rms.onl.manage.serviceinfo.stdmessage.ExtendedColumnDefinitionUIMapper;

@Service
@Transactional
public class StdDefaultManService {

	@Autowired
	private ExtendedColumnDefinitionService extendedColumnDefinitionService;

	@Autowired
	private ExtendedColumnDefinitionUIMapper extendedColumnDefinitionUIMapper;

	public Page<ExtendedColumnDefinitionUI> selectList(Pageable pageable, String searchColumnName,
			String searchColumnDesc) {
		return extendedColumnDefinitionService
				.findAll(pageable, searchColumnName, searchColumnDesc)
				.map(extendedColumnDefinitionUIMapper::toVo);
	}

	public List<Map<String, Object>> selectListToExcel(String searchColumnName, String searchColumnDesc) {
		return extendedColumnDefinitionService
				.findAll(Pageable.unpaged(), searchColumnName, searchColumnDesc)
				.map(e -> {
					Map<String, Object> map = new HashMap<>();

					map.put("COLUMNNAME", e.getColumnName());
					map.put("COLUMNDESC", e.getColumnDesc());
					map.put("COLUMNLENGTH", e.getColumnLength());
					map.put("ISKEY", e.getIsKey());
					map.put("ISINTERFACE", e.getIsInterface());

					return map;
				})
				.getContent();
	}

	public ExtendedColumnDefinitionUI selectDetail(String columnName) {
		ExtendedColumnDefinition extendedColumnDefinition = extendedColumnDefinitionService.getById(columnName);
		return extendedColumnDefinitionUIMapper.toVo(extendedColumnDefinition);
	}

	public void insert(ExtendedColumnDefinitionUI extendedColumnDefinitionUI) {

		if (extendedColumnDefinitionService.existsById(extendedColumnDefinitionUI.getColumnName())) {
			throw new EMSRuntimeException("이미 존재하는 컬럼명입니다.");
		}

		ExtendedColumnDefinition extendedColumnDefinition = extendedColumnDefinitionUIMapper
				.toEntity(extendedColumnDefinitionUI);
		extendedColumnDefinitionService.save(extendedColumnDefinition);
	}

	public void update(ExtendedColumnDefinitionUI extendedColumnDefinitionUI) {

		ExtendedColumnDefinition extendedColumnDefinition = extendedColumnDefinitionService
				.getById(extendedColumnDefinitionUI.getColumnName());

		extendedColumnDefinitionUIMapper.updateEntity(extendedColumnDefinitionUI, extendedColumnDefinition);
		extendedColumnDefinitionService.save(extendedColumnDefinition);
	}

	public void delete(String columnName) {
		extendedColumnDefinitionService.deleteById(columnName);
	}

}
