package com.eactive.eai.rms.onl.manage.control.history;

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.base.BaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;

@Service
public class ControlHistoryManService extends BaseService {

	@Autowired
	private ControlHistoryService service;
	
	@Autowired
	private ControlHistoryManMapper mapper;
	
	@Autowired
	private ComboService comboService;

	public Page<ControlHistoryManUI> selectList(Pageable pageable, ControlHistoryManUISearch uiSearch) {
		Page<ControlHistoryServiceDto> dtoPage = service.findAll(pageable, uiSearch);
		Page<ControlHistoryManUI> uiPage = dtoPage.map(mapper::toVo);
		
		List<ComboVo> controlCombo = comboService.getFromCode("CONTROL_TYPE");
		Map<String, String> controlTypeMap = controlCombo.stream()
				.collect(Collectors.toMap(ComboVo::getCode, ComboVo::getCodename));

		return uiPage.map(ui -> {
			ui.setEaictrlnamedstcd("1".equals(ui.getEaictrlnamedstcd()) ? controlTypeMap.get("S") : controlTypeMap.get("I"));
			return ui;
		});
	}
	
	public Map<String, List<?>> selectListToExcel(ControlHistoryManUISearch uiSearch) {

		List<ControlHistoryServiceDto> dtoList = service.findAll(uiSearch);

		List<ComboVo> controlCombo = comboService.getFromCode("CONTROL_TYPE");
		Map<String, String> controlTypeMap = controlCombo.stream()
				.collect(Collectors.toMap(ComboVo::getCode, ComboVo::getCodename));

		List<ControlHistoryManUI> uiList = dtoList.stream().map(mapper::toVo).map(ui -> {
			ui.setEaictrlnamedstcd("1".equals(ui.getEaictrlnamedstcd()) ? controlTypeMap.get("S") : controlTypeMap.get("I"));
			return ui;
		}).collect(Collectors.toList());
		
		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for (ControlHistoryManUI item : uiList) {
			HashMap<String, Object> row = new HashMap<>();
			row.put("ctrlprcssname", item.getCtrlprcssname());
			row.put("ctrlprcssyms", item.getCtrlprcssyms());
			row.put("getCtrlrevocyms", item.getCtrlrevocyms());
			row.put("getEaictrldesc", item.getEaictrldesc());
			row.put("getEaictrldsticctnt", item.getEaictrldsticctnt());
			row.put("getEaictrlname", item.getEaictrlname());
			row.put("getEaictrlnamedstcd", item.getEaictrlnamedstcd());
			row.put("getRevocprcssname", item.getRevocprcssname());
			list.add(row);
		}
		HashMap<String, List<?>> map = new HashMap<>();
		map.put("list", uiList);
		
		return map;
	}

}
