package com.eactive.eai.rms.onl.apim.portalstatistics;

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.apim.portalstatistics.PortalStatisticsDailyVO;
import com.eactive.eai.rms.data.entity.onl.apim.portalstatistics.PortalStatisticsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalStatisticsDailyManService extends BaseService {

	private final PortalStatisticsService portalStatisticsService;

	public PortalStatisticsDailyManService(PortalStatisticsService portalStatisticsService) {
		this.portalStatisticsService = portalStatisticsService;
	}

	public Map<String, List<PortalStatisticsDailyUI>> selectStatisticsList(PortalStatisticsUI portalStatisticsUI)  {
		Map<String, List<PortalStatisticsDailyVO>> stats = portalStatisticsService.getDailyStatistics(
				portalStatisticsUI.getStartDate(),
				portalStatisticsUI.getEndDate());

		Map<String, List<PortalStatisticsDailyUI>> result = new HashMap<>();
		result.put("today", convertToUI(stats.get("today")));
		result.put("yesterday", convertToUI(stats.get("yesterday")));

		return result;
	}

	private List<PortalStatisticsDailyUI> convertToUI(List<PortalStatisticsDailyVO> voList) {
		return voList.stream()
				.map(vo -> {
					PortalStatisticsDailyUI ui = new PortalStatisticsDailyUI();
					ui.setStatisticsTime(vo.getStatisticsTime());
					ui.setRequestCount(vo.getRequestCount());
					ui.setFailureCount(vo.getFailureCount());
					return ui;
				})
				.collect(Collectors.toList());
	}
}
