package org.security.trans.dataStatistics.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.trans.dataStatistics.dto.StatisticsDataDTO;
import org.security.trans.dataStatistics.service.RiskDataService;
import org.security.trans.dataStatistics.vo.RiskDataVO;
import org.security.trans.prevent.service.IPreventRiskService;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

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

/**
 * @author ：wzf
 * @since ：Created in 2022/11/24 10:30
 */
@Service
@Slf4j
@AllArgsConstructor
public class RiskDataServiceImpl implements RiskDataService {

	private final InspectionDataServiceImpl inspectionDataService;
	private final IPreventRiskService preventRiskService;

	@Override
	public IPage<RiskDataVO> getTableData(StatisticsDataDTO statisticsDataDTO, IPage<RiskDataVO> page) {
		log.info("---------------------风险提示数据统计列表数据获取开始---------------------");
		List<RiskDataVO> vos = new ArrayList<>();
		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(statisticsDataDTO.getDeptId(),statisticsDataDTO.getIncludeChildren());
		//获取目标数据
		List<RiskDataVO> list = preventRiskService.getAimStatisticsData(statisticsDataDTO, aimDepts);
		if (Func.isNotEmpty(list)) {
			log.info("风险提示统计数据[{}]",list);
			//赋值统计基础值
			setBaseData(list);
			//根据机构id、机构名称分组计算统计数据
			setResultData(statisticsDataDTO, vos, list);
			//计算出一条总计值
			setTotalData(statisticsDataDTO, vos);
		}
		log.info("---------------------风险提示数据统计列表数据获取结束---------------------");
		return page.setRecords(vos);
	}

	/**
	 * 赋值统计基础值
	 */
	private void setBaseData(List<RiskDataVO> list) {
		list.forEach(item -> {
			item.setTotalNum(1);
		});
	}

	/**
	 * 根据机构id、机构名称分组计算统计数据
	 */
	private void setResultData(StatisticsDataDTO statisticsDataDTO, List<RiskDataVO> vos, List<RiskDataVO> list) {
		Map<String, List<RiskDataVO>> collect = list.stream().collect(Collectors.groupingBy(item -> item.getDeptId().concat("-").concat(item.getDeptName())));
		if (Func.isNotEmpty(collect)) {
			collect.forEach((key, value) -> {
				RiskDataVO riskDataVO = new RiskDataVO();
				riskDataVO.setBeginDate(statisticsDataDTO.getBeginDate());
				riskDataVO.setEndDate(statisticsDataDTO.getEndDate());
				riskDataVO.setDeptId(key.split("-")[0]);
				riskDataVO.setDeptName(key.split("-")[1]);
				riskDataVO.setTotalNum(value.stream().mapToInt(RiskDataVO::getTotalNum).sum());
				vos.add(riskDataVO);
			});
		}
	}

	/**
	 * 计算出一条总计值
	 */
	private void setTotalData(StatisticsDataDTO statisticsDataDTO, List<RiskDataVO> vos) {
		if (Func.isNotEmpty(vos)) {
			RiskDataVO vo = new RiskDataVO();
			vo.setBeginDate(statisticsDataDTO.getBeginDate());
			vo.setEndDate(statisticsDataDTO.getEndDate());
			vo.setDeptId(null);
			vo.setDeptName("总计");
			vo.setTotalNum(vos.stream().mapToInt(RiskDataVO::getTotalNum).sum());
			vos.add(0, vo);
		}
	}
}
