package com.njcn.device.pq.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.vo.LineDistributionVO;
import com.njcn.device.pq.service.OperationService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.RequestUtil;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * pqs
 *
 * @author cdf
 * @date 2023/2/9
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OperationServiceImpl implements OperationService {

    private final DeptFeignClient deptFeignClient;


    /**
     * 获取监测点分布
     * @author cdf
     * @date 2023/2/9
     */
    @Override
    public List<LineDistributionVO> getLineDistribution(String lineGrade) {
        String deptIndex = RequestUtil.getDeptIndex();
        //初始化部门筛选条件
        List<Integer> deptType = WebUtil.filterDeptType();
        //获取包括当前部门的后代所有部门信息
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(deptIndex, deptType).getData();
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(deptIndex)).sorted(Comparator.comparing(DeptDTO::getSort))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(deptIndex))
                    .collect(Collectors.toList());

        } else {
            for (DeptDTO deptDTO : directDeptInfos) {
                //筛选上级部门pids包含该id的所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> indexes = descendantDeptDTO.stream()
                        .map(DeptDTO::getId)
                        .distinct()
                        .collect(Collectors.toList());
                indexes.add(deptDTO.getId());
            }
        }

        return null;

    }
}
