package com.njcn.event.service.majornetwork.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.event.mapper.majornetwork.RStatSubstationVoltageMMapper;
import com.njcn.event.pojo.param.StatSubstationBizBaseParam;
import com.njcn.event.pojo.po.RStatSubstationVoltageM;
import com.njcn.event.pojo.vo.RVoltageIconVO;
import com.njcn.event.service.majornetwork.RStatSubstationVoltageMService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
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 org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rui.wu
 * @since 2022-10-18
 */
@Service
@RequiredArgsConstructor
public class RStatSubstationVoltageMServiceImpl extends ServiceImpl<RStatSubstationVoltageMMapper, RStatSubstationVoltageM> implements RStatSubstationVoltageMService {

    private final RStatSubstationVoltageMMapper rStatSubstationVoltageMMapper;
    private final DeptFeignClient deptFeignClient;
    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public List<RVoltageIconVO> getStatSubstationIcon(StatisticsBizBaseParam param) {
        //获取当前部门下所有部门信息
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId(param.getId()).getData();
        List<String> deptIds = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
        baseParam.setIds(deptIds);
        //获取电压信息
        List<DictData> devVoltageData = dicDataFeignClient.getDicDataByTypeCode(
                DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();

        //查询数据库
        List<RVoltageIconVO> statSubstationIcon = rStatSubstationVoltageMMapper.getStatSubstationIcon(baseParam);
        Map<String, RVoltageIconVO> devVoltageMap = statSubstationIcon.stream()
                .collect(Collectors.toMap(RVoltageIconVO::getSubstationVoltage, Function.identity()));

        //处理主节点不存在的集合
        List<DictData> notMeasurementList = devVoltageData.stream().filter(r -> !devVoltageMap.containsKey(r.getId()))
                .collect(Collectors.toList());
        for (DictData notData : notMeasurementList) {
            RVoltageIconVO iconVO = new RVoltageIconVO();
            iconVO.setSubstationVoltage(notData.getId());
            iconVO.setSubstationCount(0);
            iconVO.setHarmonicCount(0);
            statSubstationIcon.add(iconVO);
        }
        return statSubstationIcon;
    }
}
