package com.njcn.harmonic.service.majornetwork.impl;

import cn.hutool.core.bean.BeanUtil;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RStatSubstationVoltageM;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationVoltageMMapper;
import com.njcn.harmonic.pojo.vo.RIconVO;
import com.njcn.harmonic.pojo.vo.RVoltageIconVO;
import com.njcn.harmonic.service.majornetwork.RStatSubstationVoltageMService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.harmonic.utils.DeptUtil;
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.Comparator;
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> deptList = deptFeignClient.getDepSonDetailByDeptId(param.getId()).getData();
        List<String> deptIds = deptList.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<String> ids = devVoltageData.stream().map(DictData::getId).collect(Collectors.toList());
        Map<String, Integer> iconVOMap = devVoltageData.stream()
                .collect(Collectors.toMap(DictData::getId,DictData::getSort ));

        //查询数据库
        List<RVoltageIconVO> statSubstationIcon = rStatSubstationVoltageMMapper.getStatSubstationIcon(baseParam,ids);
        statSubstationIcon.stream().forEach(r->{
            if(iconVOMap.containsKey(r.getSubstationVoltage())){
                r.setSort(iconVOMap.get(r.getSubstationVoltage()));
            }
        });
        statSubstationIcon.sort(Comparator.comparing(s -> s.getSort()));
        return statSubstationIcon;
    }
}
