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

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.trans.basic.service.IDeviceService;
import org.security.trans.dataStatistics.dto.DeviceDataDTO;
import org.security.trans.dataStatistics.entity.DeviceData;
import org.security.trans.dataStatistics.service.DeviceDataService;
import org.security.trans.dataStatistics.vo.PieChartDataVO;
import org.security.trans.dataStatistics.vo.RisingSunChartDeviceVo;
import org.security.trans.dataStatistics.vo.RisingSunChartVO;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：wzf
 * @apiNote ：设备统计实现层
 * @since ：Created in 2022/11/28 15:14
 */
@Service
@Slf4j
@AllArgsConstructor
public class DeviceDataServiceImpl implements DeviceDataService {

	private final IDeviceService deviceService;
	private final InspectionDataServiceImpl inspectionDataService;

	@Override
	public RisingSunChartDeviceVo getRisSunChartData(DeviceDataDTO deviceDataDTO) {
		ArrayList<RisingSunChartVO> vos = new ArrayList<>();
		RisingSunChartDeviceVo risingSunChartDeviceVo = new RisingSunChartDeviceVo();
		log.info("---------------------设备统计旭日数据获取开始---------------------");
		//获取统计数据
		//获取查询部门的ids
		String IncludeParam ;//设置‘是否包含子级数据’参数
		if(Func.isNotEmpty(deviceDataDTO.getIncludeChildren())){
			IncludeParam=deviceDataDTO.getIncludeChildren();
		}else{//若前端没传，默认值为 1-是
			IncludeParam = Constant.INCLUDE_CHILDREN_TYPE_ONE;
		}
		List<String> aimDepts = inspectionDataService.setDeptIds(deviceDataDTO.getDeptId(),IncludeParam);
		List<DeviceData> list = deviceService.getRisSunChartData(aimDepts);
		if (Func.isNotEmpty(list)) {
			risingSunChartDeviceVo.setTotal(list.stream().mapToInt(DeviceData::getNum).sum());
			//找出顶级节点
			List<DeviceData> collect = list.stream().filter(item -> "0".equals(item.getParentId())).collect(Collectors.toList());
			if (Func.isNotEmpty(collect)) {
				collect.forEach(item -> {
					RisingSunChartVO chartVO = new RisingSunChartVO();
					chartVO.setName(item.getName());
					chartVO.setChildren(setChildrenData(item, list));
					chartVO.setValue(Func.isNotEmpty(chartVO.getChildren()) ? chartVO.getValue() + chartVO.getChildren().stream().mapToInt(RisingSunChartVO::getValue).sum() : chartVO.getValue());
					vos.add(chartVO);
				});
			}
		}
		risingSunChartDeviceVo.setChartData(vos);

		log.info("---------------------设备统计旭日数据获取结束---------------------");
		return risingSunChartDeviceVo;
	}

	/**
	 * 给自己数据赋值
	 *
	 * @param item
	 * @param list
	 * @return
	 */
	private List<RisingSunChartVO> setChildrenData(DeviceData item, List<DeviceData> list) {
		List<DeviceData> collect1 = list.stream().filter(childItem -> item.getId().equals(childItem.getParentId())).collect(Collectors.toList());
		ArrayList<RisingSunChartVO> child = new ArrayList<>();
		if (Func.isNotEmpty(collect1)) {
			collect1.forEach(device -> {
				RisingSunChartVO vo = new RisingSunChartVO();
				vo.setName(device.getName());
				vo.setChildren(setChildrenData(device, list));
				vo.setValue(Func.isNotEmpty(vo.getChildren()) ? device.getNum() + vo.getChildren().stream().mapToInt(RisingSunChartVO::getValue).sum() : device.getNum());
				child.add(vo);
			});
		}
		return child;
	}

	@Override
	public List<PieChartDataVO> getPieDeptData(DeviceDataDTO deviceDataDTO) {
		List<PieChartDataVO> vos = new ArrayList<>();
		log.info("---------------------设备统计 按机构分布饼图数据获取开始---------------------");
		//获取统计数据
		List<DeviceData> list =deviceService.getPieDeptData(deviceDataDTO.getDeptId());
		if (Func.isNotEmpty(list)) {
			//找出设备数不为0的机构
			List<DeviceData> collect = list.stream().filter(item -> (item.getNum()>0)).collect(Collectors.toList());
			collect.forEach(item -> {
					PieChartDataVO chartVO = new PieChartDataVO();
					chartVO.setName(item.getDeptName());
					chartVO.setValue(item.getNum());
					vos.add(chartVO);
				});
			}
		log.info("---------------------设备统计 按机构分布饼图数据获取结束---------------------");
		return vos;
	}

}
