package com.eactive.eai.rms.onl.manage.control.log.bzwkgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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 com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogManService;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogManUI;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogManUISearch;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogServiceDto;

@Service("bzwkGroupControlLogManService")
public class BzwkGroupControlLogManService extends ControlLogManService {
	
	@Autowired
	private BzwkGroupControlLogService service;
	
	public Page<ControlLogManUI> selectListGroupBy(Pageable pageable, ControlLogManUISearch uiSearch) {
		Page<ControlLogServiceDto> pageDto = service.findALLGroupBy(pageable, uiSearch);
		List<ComboVo> bizTypeComboList = comboService.getFromBusinessType();
		Map<String, String> controlTypeMap = bizTypeComboList.stream()
				.collect(Collectors.toMap(ComboVo::getCode, ComboVo::getCodename));
		return pageDto.map(mapper::toVo).map(ui -> {
			ui.setBzwkdsticname(controlTypeMap.get(ui.getEaibzwkdstcd()));
			return ui;
		});
	}
	
	public Map<String, Object> selectListToExcelGroupBy(ControlLogManUISearch uiSearch) {
		List<ControlLogServiceDto> dtoList = service.findALLGroupBy(uiSearch);
		List<ComboVo> bizTypeComboList = comboService.getFromBusinessType();
		Map<String, String> controlTypeMap = bizTypeComboList.stream()
				.collect(Collectors.toMap(ComboVo::getCode, ComboVo::getCodename));
		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for (ControlLogServiceDto item : dtoList) {
			HashMap<String, Object> row = new HashMap<>();
			row.put("eaibzwkdstcd", item.getEaibzwkdstcd());
			row.put("bzwkdsticname", controlTypeMap.get(item.getEaibzwkdstcd()));
			row.put("cnt", item.getCnt());
			list.add(row);
		}
		HashMap<String, Object> map = new HashMap<>();
		map.put("list", list);
		return map;
	}
}
