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.service.RectificationNoticeService;
import org.security.trans.dataStatistics.util.CalculationRatioUtil;
import org.security.trans.dataStatistics.vo.RectificationNoticeDataVO;
import org.security.trans.dataStatistics.vo.UnlocaleCheckDataVO;
import org.security.trans.securityCommandCenter.entity.SccUnlocaleCheckLine;
import org.security.trans.securityCommandCenter.service.ISccUnlocaleCheckLineService;
import org.security.trans.securityCommandCenter.service.ISccUnlocaleCheckService;
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
 * @apiNote ：数据统计-整改通知书-实现类
 * @since ：Created in 2022/10/31 14:31
 */
@Service
@Slf4j
@AllArgsConstructor
public class RectificationNoticeServiceImpl implements RectificationNoticeService {

	private final ISccUnlocaleCheckService sccUnlocaleCheckService;
	private final ISccUnlocaleCheckLineService sccUnlocaleCheckLineService;
	private final InspectionDataServiceImpl inspectionDataService;

	@Override
	public IPage<RectificationNoticeDataVO> getTableData(StatisticsDataDTO statisticsDataDTO, IPage<RectificationNoticeDataVO> page) {
		log.info("---------------------非现场整改通知书数据统计列表数据获取开始---------------------");
		List<RectificationNoticeDataVO> vos = new ArrayList<>();
		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(statisticsDataDTO.getDeptId(),statisticsDataDTO.getIncludeChildren());
		//获取目标数据
		List<RectificationNoticeDataVO> list = sccUnlocaleCheckService.getStatisticsData(statisticsDataDTO,aimDepts);
		if (Func.isNotEmpty(list)){
			log.info("非现场整改通知书统计数据[{}]",list);
			//根据id分组获取每条非现场检查整改通知书的问题数和涉及机构数
			Map<String, List<RectificationNoticeDataVO>> collect = list.stream().collect(Collectors.groupingBy(RectificationNoticeDataVO::getId));
			if (Func.isNotEmpty(collect)){
				List<RectificationNoticeDataVO> baseDataList = new ArrayList<>();
				setBaseData(statisticsDataDTO, collect, baseDataList);
				//根据机构和检查类型分组，返回结果赋值
				setResultData(statisticsDataDTO, baseDataList);
			}
		}
		//算出一条统计值
		setTotalData(statisticsDataDTO, vos);
		log.info("---------------------非现场整改通知书数据统计列表数据获取结束---------------------");
		return page.setRecords(vos);
	}

	/**
	 *计算统计值
	 */
	private void setTotalData(StatisticsDataDTO statisticsDataDTO, List<RectificationNoticeDataVO> vos) {
		if (Func.isNotEmpty(vos)){
			RectificationNoticeDataVO noticeDataVO = new RectificationNoticeDataVO();
			noticeDataVO.setDeptName("总计");
			noticeDataVO.setType("总计");
			noticeDataVO.setBeginDate(statisticsDataDTO.getBeginDate());
			noticeDataVO.setEndDate(statisticsDataDTO.getEndDate());
			calculationValue(vos,noticeDataVO);
			vos.add(0,noticeDataVO);
		}
	}

	/**
	 *统计基础数据赋值
	 */
	private void setBaseData(StatisticsDataDTO statisticsDataDTO, Map<String, List<RectificationNoticeDataVO>> collect, List<RectificationNoticeDataVO> baseDataList) {
		collect.forEach((key,value) ->{
			List<SccUnlocaleCheckLine> sccUnlocaleCheckLineList = sccUnlocaleCheckLineService.list(new QueryWrapper<SccUnlocaleCheckLine>().lambda()
				.eq(SccUnlocaleCheckLine::getDelFlag, Constant.DELETE_FLAG_0)
			.eq(Func.isNotEmpty(key),SccUnlocaleCheckLine::getFormId,key)
			.eq(SccUnlocaleCheckLine::getRecFlag,Constant.PROBLEM_STATE_USUUAL));
			//统计开始时间
			value.get(0).setBeginDate(statisticsDataDTO.getBeginDate());
			//统计截止时间
			value.get(0).setEndDate(statisticsDataDTO.getEndDate());
			//问题数
			value.get(0).setProblemNum(sccUnlocaleCheckLineList.size());
			//涉及机构数
			value.get(0).setDeptNum(Func.isNotEmpty(sccUnlocaleCheckLineList) ? sccUnlocaleCheckLineList.stream().collect(Collectors.groupingBy(SccUnlocaleCheckLine::getOutletsCode)).size() : 0);
			//已整改数量
			value.get(0).setRectifyNum((Constant.PROBLEM_STATE_WAIT_TWO.equals(value.get(0).getStatus()) || Constant.PROBLEM_STATE_THREE.equals(value.get(0).getStatus()) ? 1 : 0));
			//按时整改数
			value.get(0).setRectifyOntimeNum(Constant.PROBLEM_STATE_WAIT_TWO.equals(value.get(0).getStatus()) ? 1 : 0);
			baseDataList.addAll(value);
		});
	}

	/**
	 * 返回结果赋值
	 */
	private void setResultData(StatisticsDataDTO statisticsDataDTO, List<RectificationNoticeDataVO> baseDataList) {
		if (Func.isNotEmpty(baseDataList)){
			Map<String, List<RectificationNoticeDataVO>> collect1 = baseDataList.stream().collect(Collectors.groupingBy(item -> item.getDeptId().concat("-").concat(item.getDeptName()).concat("-").concat(item.getType())));
			if (Func.isNotEmpty(collect1)){
				collect1.forEach((key1,value1) ->{
					RectificationNoticeDataVO vo = new RectificationNoticeDataVO();
					vo.setDeptId(key1.split("-")[0]);
					vo.setDeptName(key1.split("-")[1]);
					vo.setType(key1.split("-")[2]);
					vo.setBeginDate(statisticsDataDTO.getBeginDate());
					vo.setEndDate(statisticsDataDTO.getEndDate());
					vo.setTotalNum(value1.size());
					calculationValue(value1, vo);
				});
			}
		}
	}

	/**
	 * 计算
	 */
	private void calculationValue(List<RectificationNoticeDataVO> value1, RectificationNoticeDataVO vo) {
		vo.setProblemNum(value1.stream().mapToInt(RectificationNoticeDataVO::getProblemNum).sum());
		vo.setDeptNum(value1.stream().mapToInt(RectificationNoticeDataVO::getDeptNum).sum());
		vo.setRectifyNum(value1.stream().mapToInt(RectificationNoticeDataVO::getRectifyNum).sum());
		vo.setRectifyOntimeNum(value1.stream().mapToInt(RectificationNoticeDataVO::getRectifyOntimeNum).sum());
		vo.setRectifyRatio(CalculationRatioUtil.getAimRatio(vo.getRectifyNum(),vo.getTotalNum()));
		vo.setRectifyOntimeRatio(CalculationRatioUtil.getAimRatio(vo.getRectifyOntimeNum(),vo.getTotalNum()));
	}
}
