package com.njcn.device.pms.service.distribution.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.pojo.param.PwRDnOperatingParam;
import com.njcn.device.pms.pojo.po.RDnOperatingIndexY;
import com.njcn.device.pms.pojo.vo.PwRDnOperatingIndexCommonVO;
import com.njcn.device.pms.service.distribution.RDnOperatingYService;
import com.njcn.device.pms.mapper.distribution.RDnOperatingYMapper;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author jianghf
* @description 针对表【r_dn_operating_index_y(配网运行指标统计-年表,配网监测指标数据质量统计-年表)】的数据库操作Service实现
* @createDate 2022-11-10 10:21:46
*/
@Service
@RequiredArgsConstructor
public class RDnOperatingYServiceImpl extends ServiceImpl<RDnOperatingYMapper, RDnOperatingIndexY>
    implements RDnOperatingYService{

    private final DicDataFeignClient dicDataFeignClient;

    private final DeptFeignClient deptFeignClient;

    @Override
    public List<PwRDnOperatingIndexCommonVO> getOperatingList(PwRDnOperatingParam pwRDnOperatingParam) {
        //提取参数
        String id = pwRDnOperatingParam.getId(); //单位id
        String startTime = pwRDnOperatingParam.getStartTime(); //开始时间
        String endTime = pwRDnOperatingParam.getEndTime(); //结束时间

        //获取所有子部门信息
//        List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes(id, WebUtil.filterDeptType()).getData();
        /*获取下一级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (id).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "部门不存在");
        }
        //单位id集合
        List<String> orgCodeList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        //将单位信息转为map集合 key: 单位id  value: 单位实体
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));

        //获取监测点类别字典id
        String monitorSortOne = dicDataFeignClient.getDicDataByCode(DicDataEnum.ONE_LINE.getCode()).getData().getId(); // I类
        String monitorSortTwo = dicDataFeignClient.getDicDataByCode(DicDataEnum.TWO_LINE.getCode()).getData().getId(); // II类
        String monitorSortThree = dicDataFeignClient.getDicDataByCode(DicDataEnum.THREE_LINE.getCode()).getData().getId(); // III类

        HashMap<String, Object> condMap = new HashMap<>();
        condMap.put("orgIdList", orgCodeList);
        condMap.put("startTime", startTime);
        condMap.put("endTime", endTime);
        condMap.put("monitorSortOne", monitorSortOne);
        condMap.put("monitorSortTwo", monitorSortTwo);
        condMap.put("monitorSortThree", monitorSortThree);
        List<PwRDnOperatingIndexCommonVO> list =  this.baseMapper.getOperatingList(condMap);
        list = list.stream().peek(item -> {
            item.setOrgName(deptDTOMap.get(item.getOrgNo()).getName()); //单位名称
        }).collect(Collectors.toList());
        return list;
    }
}




