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.MaterialDataDTO;
import org.security.trans.dataStatistics.dto.StatisticsDataDTO;
import org.security.trans.dataStatistics.service.MaterialDataService;
import org.security.trans.dataStatistics.vo.MaterialDataVO;
import org.security.trans.prevent.service.IPreventMaterialService;
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:00
 */
@Service
@Slf4j
@AllArgsConstructor
public class MaterialDataServiceImpl implements MaterialDataService {

	private final InspectionDataServiceImpl inspectionDataService;
	private final IPreventMaterialService preventMaterialService;

	@Override
	public IPage<MaterialDataVO> getTableData(MaterialDataDTO materialDataDTO, IPage<MaterialDataVO> page) {
		log.info("---------------------案件材料报送数据统计列表数据获取开始---------------------");
		List<MaterialDataVO> vos = new ArrayList<>();

		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(materialDataDTO.getDeptId(),materialDataDTO.getIncludeChildren());
		//获取目标数据
		List<MaterialDataVO> list = preventMaterialService.getAimStatisticsData(materialDataDTO,aimDepts);
		if (Func.isNotEmpty(list)){
			log.info("案件材料报送统计数据[{}]",list);
			//统计基础数据赋值
			setBaseData(list);
			//根据机构id、机构名称和类型分组得出统计值
			setResultData(materialDataDTO, vos, list);
			//算出一条统计值
			setTotalValue(materialDataDTO, vos);
		}
		log.info("---------------------案件材料报送数据统计列表数据获取结束---------------------");
		return page.setRecords(vos);
	}

	private void setBaseData(List<MaterialDataVO> list) {
		list.forEach(item ->{
			item.setTotalNum(1);
		});
	}

	/**
	 * 根据机构id、机构名称和类型分组得出统计值
	 */
	private void setResultData(MaterialDataDTO materialDataDTO, List<MaterialDataVO> vos, List<MaterialDataVO> list) {
		Map<String, List<MaterialDataVO>> 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) ->{
				MaterialDataVO materialDataVO = new MaterialDataVO();
				materialDataVO.setBeginDate(materialDataDTO.getBeginDate());
				materialDataVO.setEndDate(materialDataDTO.getEndDate());
				materialDataVO.setDeptId(key.split("-")[0]);
				materialDataVO.setDeptName(key.split("-")[1]);
				materialDataVO.setBusinessType(key.split("-")[2]);
				materialDataVO.setTotalNum(value.stream().mapToInt(MaterialDataVO::getTotalNum).sum());
				vos.add(materialDataVO);
			});
		}
	}

	/**
	 * 计算出总计值
	 */
	private void setTotalValue(MaterialDataDTO materialDataDTO, List<MaterialDataVO> vos) {
		if (Func.isNotEmpty(vos)){
			MaterialDataVO totalValue = new MaterialDataVO();
			totalValue.setBeginDate(materialDataDTO.getBeginDate());
			totalValue.setEndDate(materialDataDTO.getEndDate());
			totalValue.setDeptId(null);
			totalValue.setDeptName("总计");
			totalValue.setBusinessType("总计");
			totalValue.setTotalNum(vos.stream().mapToInt(MaterialDataVO::getTotalNum).sum());
			vos.add(0,totalValue);
		}
	}
}
