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

import java.util.HashMap;
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.data.entity.onl.errorcode.ErrorCode;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.errorcode.ErrorCodeService;

@Service
@Transactional
public class ErrorCodeManService extends BaseService {

	private ErrorCodeMapper errorCodeMapper;

	private ErrorCodeService errorCodeService;

	@Autowired
	public ErrorCodeManService(ErrorCodeMapper errorCodeMapper, ErrorCodeService errorCodeService) {
		this.errorCodeMapper = errorCodeMapper;
		this.errorCodeService = errorCodeService;
	}

	public Page<ErrorCodeUI> selectList(Pageable pageable, String searchMsg, String searchMsgDesc) {
		Page<ErrorCode> page = errorCodeService.findAll(pageable, searchMsg, searchMsgDesc);
		return page.map(errorCodeMapper::toVo);
	}

	public Map<String, Object>[] selectListToExcel() {
		@SuppressWarnings("unchecked")
		Map<String, Object>[] mapArray = errorCodeService.findAll().stream().map(e -> {
			Map<String, Object> map = new HashMap<>();
			map.put("MSG", e.getMsg());
			map.put("MSGCTNT", e.getMsgctnt());
			map.put("TREATMATRCTNT", e.getTreatmatrctnt());
			return map;
		}).toArray(HashMap[]::new);
		return mapArray;
	}

	public ErrorCodeUI selectDetail(String msg) {
		ErrorCode errorCode = errorCodeService.getById(msg);
		return errorCodeMapper.toVo(errorCode);
	}

	public void update(ErrorCodeUI errorCodVo) {
		ErrorCode errorCode = errorCodeService.getById(errorCodVo.getMsg());
		errorCodeMapper.updateToEntity(errorCodVo, errorCode);
		errorCodeService.save(errorCode);
	}

	public void insert(ErrorCodeUI errorCodVo) {
		ErrorCode errorCode = errorCodeMapper.toEntity(errorCodVo);
		errorCodeService.save(errorCode);
	}

	public void delete(String msg) {
		errorCodeService.deleteById(msg);
	}

}
