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

import com.alibaba.druid.sql.visitor.functions.Concat;
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.basic.service.IDeviceService;
import org.security.trans.dailyDuties.entity.DeviceInspectionLine;
import org.security.trans.dailyDuties.service.IDeviceInspectionLineService;
import org.security.trans.dailyDuties.service.IDeviceInspectionService;
import org.security.trans.dataStatistics.dto.DeviceInspectionDataDTO;
import org.security.trans.dataStatistics.service.DeviceInspectionDataService;
import org.security.trans.dataStatistics.util.CalculationRatioUtil;
import org.security.trans.dataStatistics.vo.DeviceInspectionDataVO;
import org.security.trans.settings.service.IDeviceCategorySettingsService;
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 19:43
 */
@Service
@Slf4j
@AllArgsConstructor
public class DeviceInspectionDataServiceImpl implements DeviceInspectionDataService {

	private final InspectionDataServiceImpl inspectionDataService;
	private final IDeviceInspectionService deviceInspectionService;
	private final IDeviceInspectionLineService deviceInspectionLineService;
	private final IDeviceCategorySettingsService deviceCategorySettingsService;
	private final IDeviceService deviceService;

	@Override
	public IPage<DeviceInspectionDataVO> getTableData(DeviceInspectionDataDTO deviceInspectionDataDTO, IPage<DeviceInspectionDataVO> page) {
		log.info("---------------------设备检查整改数据统计列表数据获取开始---------------------");
		List<DeviceInspectionDataVO> vos = new ArrayList<>();
		//获取查询部门的ids
		List<String> aimDepts = inspectionDataService.setDeptIds(deviceInspectionDataDTO.getDeptId(), deviceInspectionDataDTO.getIncludeChildren());
		//获取目标数据
		List<DeviceInspectionDataVO> list = deviceInspectionService.getAimStatisticsData(deviceInspectionDataDTO, aimDepts);
		if (Func.isNotEmpty(list)) {
			//统计基础值赋值
			setBaseData(list);
			//根据机构id、机构名称、设备类型分组计算统计数据
			setResultData(deviceInspectionDataDTO, vos, list);
		}
		//设备类型赋值
		if (Func.isNotEmpty(vos)) {
			vos.forEach(item -> {
				item.setType(deviceCategorySettingsService.getById(deviceService.getById(item.getType()).getTypeId()).getName());
			});
		}
		//计算出一条统计值
		setTotalData(deviceInspectionDataDTO, vos);
		log.info("---------------------设备检查整改数据统计列表数据获取开始---------------------");
		return page.setRecords(vos);
	}

	/**
	 * 统计基础值赋值
	 */
	private void setBaseData(List<DeviceInspectionDataVO> list) {
		list.forEach(item -> {
			List<DeviceInspectionLine> lines = deviceInspectionLineService.list(new QueryWrapper<DeviceInspectionLine>().lambda().eq(Func.isNotEmpty(item.getId()), DeviceInspectionLine::getFormId, item.getId()));
			if (Func.isNotEmpty(Func.isNotEmpty(lines) && lines.size() > 0)) {
				item.setCheckNum(lines.size());
				List<DeviceInspectionLine> collect = lines.stream().filter(line -> Constant.DEVICE_PROBLEM_WARN.equals(line.getProblemLevel())).collect(Collectors.toList());
				item.setProblemNum((Func.isNotEmpty(Func.isNotEmpty(collect) && collect.size() > 0)) ? collect.size() : 0);
			} else {
				item.setCheckNum(0);
				item.setProblemNum(0);
			}
			item.setRectifyNum(Constant.DEVICE_STATE_RECTIFY_OVER.equals(item.getStatus()) ? 1 : 0);
		});
	}

	/**
	 * 根据机构id、机构名称、设备类型分组计算统计数据
	 */
	private void setResultData(DeviceInspectionDataDTO deviceInspectionDataDTO, List<DeviceInspectionDataVO> vos, List<DeviceInspectionDataVO> list) {
		Map<String, List<DeviceInspectionDataVO>> collect = list.stream().collect(Collectors.groupingBy(item -> item.getDeptId().concat("-").concat(item.getDeptName()).concat("-").concat(item.getType())));
		if (Func.isNotEmpty(collect)) {
			collect.forEach((key, vale) -> {
				DeviceInspectionDataVO inspectionDataVO = new DeviceInspectionDataVO();
				inspectionDataVO.setBeginDate(deviceInspectionDataDTO.getBeginDate());
				inspectionDataVO.setEndDate(deviceInspectionDataDTO.getEndDate());
				inspectionDataVO.setDeptId(key.split("-")[0]);
				inspectionDataVO.setDeptName(key.split("-")[1]);
				inspectionDataVO.setType(key.split("-")[2]);
				inspectionDataVO.setRectifyNum(vale.stream().mapToInt(DeviceInspectionDataVO::getRectifyNum).sum());
				inspectionDataVO.setCheckNum(vale.stream().mapToInt(DeviceInspectionDataVO::getCheckNum).sum());
				inspectionDataVO.setProblemNum(vale.stream().mapToInt(DeviceInspectionDataVO::getProblemNum).sum());
				inspectionDataVO.setProblemRatio(CalculationRatioUtil.getAimRatio(inspectionDataVO.getProblemNum(), inspectionDataVO.getCheckNum()));
				inspectionDataVO.setRectifyRatio(CalculationRatioUtil.getAimRatio(inspectionDataVO.getRectifyNum(), list.size()));
				vos.add(inspectionDataVO);
			});
		}
	}

	/**
	 * 计算出一条统计值
	 */
	private void setTotalData(DeviceInspectionDataDTO deviceInspectionDataDTO, List<DeviceInspectionDataVO> vos) {
		if (Func.isNotEmpty(vos)) {
			DeviceInspectionDataVO totalValue = new DeviceInspectionDataVO();
			totalValue.setBeginDate(deviceInspectionDataDTO.getBeginDate());
			totalValue.setDeptName("总计");
			totalValue.setType("总计");
			totalValue.setCheckNum(vos.stream().mapToInt(DeviceInspectionDataVO::getCheckNum).sum());
			totalValue.setProblemNum(vos.stream().mapToInt(DeviceInspectionDataVO::getProblemNum).sum());
			totalValue.setRectifyNum(vos.stream().mapToInt(DeviceInspectionDataVO::getRectifyNum).sum());
			totalValue.setProblemRatio(CalculationRatioUtil.getAimRatio(totalValue.getProblemNum(), totalValue.getCheckNum()));
			totalValue.setRectifyRatio(CalculationRatioUtil.getAimRatio(totalValue.getRectifyNum(), vos.size()));
			vos.add(0, totalValue);
		}
	}
}
