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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.mapper.majornetwork.RMpMonitorAlarmCountMMapper;
import com.njcn.device.pms.pojo.param.RMpMonitorAlarmCountMParam;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.pojo.po.RMpMonitorAlarmCountM;
import com.njcn.device.pms.pojo.vo.RMpMonitorAlarmCountMVO;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.RMpMonitorAlarmCountMService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
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.WebUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 监测点告警统计service
* @author jianghf
* @description 针对表【r_mp_monitor_alarm_count_m】的数据库操作Service实现
* @createDate 2022-10-12 20:08:33
*/
@Service
@RequiredArgsConstructor
public class RMpMonitorAlarmCountMServiceImpl extends ServiceImpl<RMpMonitorAlarmCountMMapper, RMpMonitorAlarmCountM>
implements RMpMonitorAlarmCountMService {

    private final DeptFeignClient deptFeignClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final IMonitorService iMonitorService; //【主网监测点】服务类

    /***
     * 根据条件查询监测点告警统计(月)(主网)
     * @author jianghaifei
     * @date 2022-11-01 15:21
     * @param rMpMonitorAlarmCountMParam
     * @return java.util.List<com.njcn.device.pms.pojo.vo.RMpMonitorAlarmCountMVO>
     */
    @Override
    public List<RMpMonitorAlarmCountMVO> getRMpMonitorAlarmCountMList(RMpMonitorAlarmCountMParam rMpMonitorAlarmCountMParam) {
        //提取查询条件
        String id = rMpMonitorAlarmCountMParam.getId(); //单位id
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "单位id不可为空");
        }
        String startTime = rMpMonitorAlarmCountMParam.getStartTime(); //开始时间
        String endTime = rMpMonitorAlarmCountMParam.getEndTime(); //结束时间
        String monitorObjectType = rMpMonitorAlarmCountMParam.getMonitorObjectType(); //监测点对象类型id
        List<String> voltageLevelParamList = StringUtils.isNotBlank(rMpMonitorAlarmCountMParam.getVoltageLevel()) ? Arrays.asList(rMpMonitorAlarmCountMParam.getVoltageLevel().split(",")) : null; //电压等级
        String monitorName = rMpMonitorAlarmCountMParam.getMeasurementPointName(); //监测点名称

        //获取所有子部门信息
//        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, "部门不存在");
        }
        //单位code集合
        List<String> orgCodeList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        //将单位信息转为map集合 key: 单位code  value: 单位实体
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));

        //获取电压等级的字典
        List<DictData> voltageLevelList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        //将电压等级信息转成map，key：id  value：name
        Map<String, String> voltageLevelMap = voltageLevelList.stream().collect(Collectors.toMap(DictData::getId, DictData::getName));
        //获取监测点类型字典
        List<DictData> monitorTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_TYPE.getCode()).getData();
        //将监测点类型字典信息转为map集合 key: 字典id  value: 字典名称
        Map<String, String> monitorTypeMap = monitorTypeList.stream().collect(Collectors.toMap(DictData::getId, DictData::getName));
        //获取主网id
        String dataType = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData().getId();

        //根据条件查询单位下面的所有监测点
        LambdaQueryWrapper<Monitor> monitorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        monitorLambdaQueryWrapper.in(Monitor::getOrgId, orgCodeList) //单位code
                .eq(StringUtils.isNotBlank(monitorObjectType), Monitor::getMonitorTag, monitorObjectType) //监测点对象类型id
                .in(CollUtil.isNotEmpty(voltageLevelParamList), Monitor::getVoltageLevel, voltageLevelParamList) //电压等级id
                .like(StringUtils.isNotBlank(monitorName), Monitor::getName, monitorName); //监测点名称
        //监测点集合
        List<Monitor> monitorList = iMonitorService.list(monitorLambdaQueryWrapper);
        //监测点id集合
        List<String> monitorIdList = monitorList.stream().map(Monitor::getId).collect(Collectors.toList());
        if (CollUtil.isEmpty(monitorIdList)) {
            return new ArrayList<>();
        }
        //监测点map  key:监测点id  value:监测点实体
        Map<String, Monitor> monitorMap = monitorList.stream().collect(Collectors.toMap(Monitor::getId, monitor -> monitor));
        //根据监测点idList等条件查询【监测点告警月统计】表数据
        LambdaQueryWrapper<RMpMonitorAlarmCountM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RMpMonitorAlarmCountM::getMeasurementPointId, monitorIdList)
                .ge(RMpMonitorAlarmCountM::getDataDate, startTime)
                .le(RMpMonitorAlarmCountM::getDataDate, endTime)
                .eq(RMpMonitorAlarmCountM::getDataType, dataType);
        List<RMpMonitorAlarmCountM> alarmCountMList = this.list(lambdaQueryWrapper);
        List<RMpMonitorAlarmCountMVO> resultList;
        if (CollUtil.isNotEmpty(alarmCountMList)) {
            //封装前端展示的数据
            resultList = alarmCountMList.stream().map(item -> {
                RMpMonitorAlarmCountMVO rMpMonitorAlarmCountMVO = new RMpMonitorAlarmCountMVO();
                BeanUtils.copyProperties(item, rMpMonitorAlarmCountMVO);
                //单位信息
                rMpMonitorAlarmCountMVO.setOrgName(deptDTOMap.get(item.getOrgNo()).getName()); //所属单位名称
                //监测点信息
                rMpMonitorAlarmCountMVO.setMeasurementPointName(monitorMap.get(item.getMeasurementPointId()).getName());//监测点名称
                rMpMonitorAlarmCountMVO.setMonitorTypeName(monitorTypeMap.get(monitorMap.get(item.getMeasurementPointId()).getMonitorType()));//监测点类型名称
                rMpMonitorAlarmCountMVO.setMonitorType(monitorMap.get(item.getMeasurementPointId()).getMonitorType());//监测点类型id
                rMpMonitorAlarmCountMVO.setVoltageLevel(monitorMap.get(item.getMeasurementPointId()).getVoltageLevel()); //电压等级id
                rMpMonitorAlarmCountMVO.setVoltageLevelName(voltageLevelMap.get(monitorMap.get(item.getMeasurementPointId()).getVoltageLevel())); //电压等级
                return rMpMonitorAlarmCountMVO;
            }).collect(Collectors.toList());
        } else {
            resultList = new ArrayList<>();
        }
        return resultList;
    }

}
