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.common.Constant;
import org.security.trans.dataStatistics.dto.EduAndDrillDataDTO;
import org.security.trans.dataStatistics.service.EduAndDrillDataService;
import org.security.trans.dataStatistics.vo.EduAndDrillDataVO;
import org.security.trans.task.service.ITaskHandinService;
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/23 9:59
 */
@Service
@Slf4j
@AllArgsConstructor
public class EduAndDrillDataServiceImpl implements EduAndDrillDataService {

	private final ITaskHandinService taskHandinService;
	private final InspectionDataServiceImpl inspectionDataService;

	@Override
	public IPage<EduAndDrillDataVO> getTableData(EduAndDrillDataDTO eduAndDrillDataDTO, IPage<EduAndDrillDataVO> page) {
		log.info("---------------------教育培训/应急演练数据统计列表数据获取开始---------------------");
		List<EduAndDrillDataVO> vos = new ArrayList<>();
		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(eduAndDrillDataDTO.getDeptId(),eduAndDrillDataDTO.getIncludeChildren());
		String aimModule = null;
		//获取教育培训统计数据
		if (Constant.EDU_DATA_TYPE.equals(eduAndDrillDataDTO.getDataType())) {
			aimModule = "edu";
		}
		//获取应急演练统计数据
		if (Constant.DRILL_DATA_TYPE.equals(eduAndDrillDataDTO.getDataType())) {
			aimModule = "drill";
		}
		List<EduAndDrillDataVO> list = taskHandinService.getAimStatisticsData(eduAndDrillDataDTO, aimDepts, aimModule);
		//统计基础值赋值
		setBaseData(list);
		//根据机构和业务类型进行分组统计
		setResultData(eduAndDrillDataDTO, vos, list);
		//计算出总计记录
		setTotalData(eduAndDrillDataDTO, vos);
		log.info("---------------------教育培训/应急演练数据统计列表数据获取结束---------------------");
		return page.setRecords(vos);
	}

	/**
	 * 统计基础值赋值
	 */
	private void setBaseData(List<EduAndDrillDataVO> list) {
		if (Func.isNotEmpty(list)) {
			log.info("教育培训/应急演练统计数据[{}]", list);
			list.forEach(item -> {
				item.setTotalNum(1);
				item.setTotalPeopleNum(Func.isNotEmpty(item.getTotalPeopleNum()) ? item.getTotalPeopleNum() : 0);
			});
		}
	}

	/**
	 * 根据机构和业务类型进行分组统计
	 */
	private void setResultData(EduAndDrillDataDTO eduAndDrillDataDTO, List<EduAndDrillDataVO> vos, List<EduAndDrillDataVO> list) {
		Map<String, List<EduAndDrillDataVO>> collect = list.stream().collect(Collectors.groupingBy(item -> item.getDeptId().concat("-").concat(item.getDeptName()).concat("-").concat(item.getBusinessType())));
		if (Func.isNotEmpty(collect)) {
			collect.forEach((key, value) -> {
				EduAndDrillDataVO dataVO = new EduAndDrillDataVO();
				dataVO.setBeginDate(eduAndDrillDataDTO.getBeginDate());
				dataVO.setEndDate(eduAndDrillDataDTO.getEndDate());
				dataVO.setDeptId(key.split("-")[0]);
				dataVO.setDeptName(key.split("-")[1]);
				dataVO.setBusinessType(key.split("-")[2]);
				dataVO.setTotalNum(value.stream().mapToInt(EduAndDrillDataVO::getTotalNum).sum());
				dataVO.setTotalPeopleNum(value.stream().mapToInt(EduAndDrillDataVO::getTotalPeopleNum).sum());
				vos.add(dataVO);
			});
		}
	}

	/**
	 * 计算出总计记录
	 */
	private void setTotalData(EduAndDrillDataDTO eduAndDrillDataDTO, List<EduAndDrillDataVO> vos) {
		if (Func.isNotEmpty(vos)) {
			EduAndDrillDataVO vo = new EduAndDrillDataVO();
			vo.setBeginDate(eduAndDrillDataDTO.getBeginDate());
			vo.setEndDate(eduAndDrillDataDTO.getEndDate());
			vo.setBusinessType("总计");
			vo.setDeptName("总计");
			vo.setDeptId(null);
			vo.setTotalNum(vos.stream().mapToInt(EduAndDrillDataVO::getTotalNum).sum());
			vo.setTotalPeopleNum(vos.stream().mapToInt(EduAndDrillDataVO::getTotalPeopleNum).sum());
			vos.add(0, vo);
		}
	}
}
