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.AnswerAlarmDataDTO;
import org.security.trans.dataStatistics.dto.StatisticsDataDTO;
import org.security.trans.dataStatistics.service.AnswerAlarmDataService;
import org.security.trans.dataStatistics.util.CalculationRatioUtil;
import org.security.trans.dataStatistics.vo.AnswerAlarmDataVO;
import org.security.trans.securityCommandCenter.service.ISccAnswerAlarmService;
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/22 15:11
 */
@Service
@Slf4j
@AllArgsConstructor
public class AnswerAlarmDataServiceImpl implements AnswerAlarmDataService {

	private final InspectionDataServiceImpl inspectionDataService;
	private final ISccAnswerAlarmService sccAnswerAlarmService;



	@Override
	public IPage<AnswerAlarmDataVO> getTableData(AnswerAlarmDataDTO answerAlarmDataDTO, IPage<AnswerAlarmDataVO> page) {
		log.info("---------------------联系接警记录数据统计列表数据获取开始---------------------");
		List<AnswerAlarmDataVO> vos = new ArrayList<>();
		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(answerAlarmDataDTO.getDeptId(),answerAlarmDataDTO.getIncludeChildren());
		//获取目标数据
		List<AnswerAlarmDataVO> list = sccAnswerAlarmService.getAimStatisticsData(answerAlarmDataDTO,aimDepts);
		if (Func.isNotEmpty(list)) {
			log.info("联系接警记录统计数据[{}]", list);
			//赋值统计基础数据
			setBaseData(answerAlarmDataDTO, list);
			//根据机构和类型分组获取统计结果值
			setResultData(vos, list);
			//计算出统计值
			setTotalData(answerAlarmDataDTO, vos);
		}
		log.info("---------------------联系接警记录数据统计列表数据获取结束---------------------");
		return page.setRecords(vos);
	}

	/**
	 * 计算出统计值
	 */
	private void setTotalData(AnswerAlarmDataDTO answerAlarmDataDTO, List<AnswerAlarmDataVO> vos) {
		if (Func.isNotEmpty(vos)){
			AnswerAlarmDataVO answerAlarmDataVO = new AnswerAlarmDataVO();
			answerAlarmDataVO.setBeginDate(answerAlarmDataDTO.getBeginDate());
			answerAlarmDataVO.setEndDate(answerAlarmDataDTO.getEndDate());
			answerAlarmDataVO.setType("总计");
			answerAlarmDataVO.setDeptId(null);
			answerAlarmDataVO.setDeptName("总计");
			answerAlarmDataVO.setNumberOfCalls(vos.stream().mapToInt(AnswerAlarmDataVO::getNumberOfCalls).sum());
			answerAlarmDataVO.setAlarmPushNumber(vos.stream().mapToInt(AnswerAlarmDataVO::getAlarmPushNumber).sum());
			answerAlarmDataVO.setSettlementNumber(vos.stream().mapToInt(AnswerAlarmDataVO::getSettlementNumber).sum());
			answerAlarmDataVO.setAlarmPushRatio(CalculationRatioUtil.getAimRatio(answerAlarmDataVO.getAlarmPushNumber(),answerAlarmDataVO.getNumberOfCalls()));
			vos.add(0,answerAlarmDataVO);
		}
	}

	/**
	 * 根据机构和类型分组获取统计结果值
	 */
	private void setResultData(List<AnswerAlarmDataVO> vos, List<AnswerAlarmDataVO> list) {
		Map<String, List<AnswerAlarmDataVO>> collect = list.stream().collect(Collectors.groupingBy(item -> item.getDeptId().concat("-").concat(item.getDeptName()).concat("-").concat(item.getType())));
		if (Func.isNotEmpty(collect)){
			collect.forEach((key,value) ->{
				AnswerAlarmDataVO vo = new AnswerAlarmDataVO();
				vo.setDeptId(key.split("-")[0]);
				vo.setDeptName(key.split("-")[1]);
				vo.setType(key.split("-")[2]);
				vo.setBeginDate(value.get(0).getBeginDate());
				vo.setEndDate(value.get(0).getEndDate());
				vo.setSettlementNumber(value.stream().mapToInt(AnswerAlarmDataVO::getSettlementNumber).sum());
				vo.setAlarmPushNumber(value.stream().mapToInt(AnswerAlarmDataVO::getAlarmPushNumber).sum());
				vo.setNumberOfCalls(value.stream().mapToInt(AnswerAlarmDataVO::getNumberOfCalls).sum());
				vo.setAlarmPushRatio(CalculationRatioUtil.getAimRatio(vo.getAlarmPushNumber(),vo.getNumberOfCalls()));
				vos.add(vo);
			});
		}
	}

	/**
	 * 赋值统计基础数据
	 */
	private void setBaseData(AnswerAlarmDataDTO answerAlarmDataDTO, List<AnswerAlarmDataVO> list) {
		list.forEach(item ->{
			item.setBeginDate(answerAlarmDataDTO.getBeginDate());
			item.setEndDate(answerAlarmDataDTO.getEndDate());
			item.setNumberOfCalls(1);
			item.setAlarmPushNumber(Constant.PUSH_STATE_ONE.equals(item.getStatus()) ? 1 : 0);
			item.setSettlementNumber(Constant.PUSH_STATE_TWO.equals(item.getStatus()) ? 1 : 0);
		});
	}
}
