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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.StatisticsDataDTO;
import org.security.trans.dataStatistics.dto.UnlocaleCheckDataDTO;
import org.security.trans.dataStatistics.service.UnlocaleCheckDataService;
import org.security.trans.dataStatistics.util.CalculationRatioUtil;
import org.security.trans.dataStatistics.vo.UnlocaleCheckDataVO;
import org.security.trans.securityCommandCenter.entity.SccCheckLines;
import org.security.trans.securityCommandCenter.service.ISccCheckLinesService;
import org.security.trans.securityCommandCenter.service.ISccCheckService;
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/10/28 17:35
 */
@Service
@Slf4j
@AllArgsConstructor
public class UnlocaleCheckDataServiceImpl implements UnlocaleCheckDataService {
	private final ISccCheckService sccCheckService;
	private final ISccCheckLinesService sccCheckLinesService;
	private final InspectionDataServiceImpl inspectionDataService;


	@Override
	public IPage<UnlocaleCheckDataVO> getTableData(UnlocaleCheckDataDTO unlocaleCheckDataDTO, IPage<UnlocaleCheckDataVO> page) {
		log.info("---------------------非现场检查数据统计列表数据获取开始---------------------");
		List<UnlocaleCheckDataVO> vos = new ArrayList<>();
		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(unlocaleCheckDataDTO.getDeptId(),unlocaleCheckDataDTO.getIncludeChildren());
		//获取目标数据
		List<UnlocaleCheckDataVO> list = sccCheckService.getAimStatisticsData(unlocaleCheckDataDTO,aimDepts);
		if (Func.isNotEmpty(list)){
			log.info("非现场检查统计数据[{}]",list);
			//根据id分组获取每条非现场检查记录的检查项数和异常数
			Map<String, List<UnlocaleCheckDataVO>> collect = list.stream().collect(Collectors.groupingBy(UnlocaleCheckDataVO::getId));
			List<UnlocaleCheckDataVO> baseDataList = new ArrayList<>();
			if (Func.isNotEmpty(collect)){
				setStatisticsBaseData(unlocaleCheckDataDTO, collect, baseDataList);
				//根据机构和检查类型分组，返回结果赋值
				setResultData(vos, baseDataList,unlocaleCheckDataDTO);
			}
		}
		log.info("---------------------非现场检查数据统计列表数据获取结束---------------------");
		//算出一条统计值
		setTotalValue(unlocaleCheckDataDTO, vos);
		return page.setRecords(vos);
	}

	/**
	 * 算出一条统计值
	 */
	private void setTotalValue(UnlocaleCheckDataDTO unlocaleCheckDataDTO, List<UnlocaleCheckDataVO> vos) {
		if (Func.isNotEmpty(vos)){
			UnlocaleCheckDataVO checkDataVO = new UnlocaleCheckDataVO();
			checkDataVO.setDeptName("总计");
			checkDataVO.setCheckType("总计");
			checkDataVO.setBeginDate(unlocaleCheckDataDTO.getBeginDate());
			checkDataVO.setEndDate(unlocaleCheckDataDTO.getEndDate());
			calculationValue(vos,checkDataVO);
			vos.add(0,checkDataVO);
		}
	}

	/**
	 *统计基础数据赋值
	 */
	private void setStatisticsBaseData(UnlocaleCheckDataDTO unlocaleCheckDataDTO, Map<String, List<UnlocaleCheckDataVO>> collect, List<UnlocaleCheckDataVO> baseDataList) {
		collect.forEach((key,value) ->{
			List<SccCheckLines> sccCheckLinesList = sccCheckLinesService.list(new QueryWrapper<SccCheckLines>().lambda()
				.eq(SccCheckLines::getDelFlag, Constant.DELETE_FLAG_0)
				.eq(Func.isNotEmpty(key), SccCheckLines::getFormId, key));
			//统计开始时间
			value.get(0).setBeginDate(unlocaleCheckDataDTO.getBeginDate());
			//统计截止时间
			value.get(0).setEndDate(unlocaleCheckDataDTO.getEndDate());
			//非现场检查检查项
			value.get(0).setCheckNum(sccCheckLinesList.size());
			//非现场检查异常项
			value.get(0).setProblemNum((int) sccCheckLinesList.stream().filter(item -> Constant.CHECK_STATUS_ZERO.equals(item.getCheckStatus())).count());
			baseDataList.addAll(value);
		});
	}

	/**
	 *统计结果赋值
	 */
	private void setResultData(List<UnlocaleCheckDataVO> vos, List<UnlocaleCheckDataVO> baseDataList,UnlocaleCheckDataDTO unlocaleCheckDataDTO) {
		Map<String, List<UnlocaleCheckDataVO>> collect1 = baseDataList.stream().collect(Collectors.groupingBy(item -> item.getDeptId().concat("-").concat(item.getDeptName()).concat("-").concat(item.getCheckType())));
		if (Func.isNotEmpty(collect1)){
			collect1.forEach((key1,value1) ->{
				UnlocaleCheckDataVO unlocaleCheckDataVO = new UnlocaleCheckDataVO();
				unlocaleCheckDataVO.setDeptId(key1.split("-")[0]);
				unlocaleCheckDataVO.setDeptName(key1.split("-")[1]);
				unlocaleCheckDataVO.setCheckType(key1.split("-")[2]);
				unlocaleCheckDataVO.setBeginDate(unlocaleCheckDataDTO.getBeginDate());
				unlocaleCheckDataVO.setEndDate(unlocaleCheckDataDTO.getEndDate());
				calculationValue(value1, unlocaleCheckDataVO);
				vos.add(unlocaleCheckDataVO);
			});
		}
	}

	/**
	 *封装计算的数值
	 */
	private void calculationValue(List<UnlocaleCheckDataVO> value1, UnlocaleCheckDataVO vo) {
		vo.setCheckNum(value1.stream().mapToInt(UnlocaleCheckDataVO::getCheckNum).sum());
		vo.setProblemNum(value1.stream().mapToInt(UnlocaleCheckDataVO::getProblemNum).sum());
		vo.setProblemRatio(CalculationRatioUtil.getAimRatio(vo.getProblemNum(),vo.getCheckNum()));
	}
}
