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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.distribution.PwRMpMonitorAlarmCountMMapper;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.device.pms.pojo.param.RMpMonitorAlarmCountMParam;
import com.njcn.device.pms.pojo.po.DistributionMonitor;
import com.njcn.device.pms.pojo.po.RMpMonitorAlarmCountM;
import com.njcn.device.pms.pojo.po.RStatAreaAlarmCountM;
import com.njcn.device.pms.pojo.vo.DistributionMonitorVO;
import com.njcn.device.pms.pojo.vo.PwRMpMonitorAlarmCountMVO;
import com.njcn.device.pms.service.distribution.PwRMpMonitorAlarmCountMService;
import com.njcn.device.pms.service.majornetwork.IDistributionMonitorService;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.IPmsGeneralDeviceService;
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.factory.PageFactory;
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.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

    private final DeptFeignClient deptFeignClient;

    private final DicDataFeignClient dicDataFeignClient;

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

    private final IPwMonitorServiceImpl iPwMonitorService; //【配网监测点】服务类

    private final IPmsGeneralDeviceService iPmsGeneralDeviceService;

    private final IDistributionMonitorService iDistributionMonitorService;

    /***
     * 根据条件查询监测点告警统计(月)(配网)
     * @author jianghaifei
     * @date 2022-11-01 15:21
     * @param rMpMonitorAlarmCountMParam
     * @return java.util.List<com.njcn.device.pms.pojo.vo.PwRMpMonitorAlarmCountMVO>
     */
    @Override
    public Page<PwRMpMonitorAlarmCountMVO> getPwRMpMonitorAlarmCountMList(RMpMonitorAlarmCountMParam rMpMonitorAlarmCountMParam) {

        Page<PwRMpMonitorAlarmCountMVO> pageResult = new Page<>(PageFactory.getPageNum(rMpMonitorAlarmCountMParam),PageFactory.getPageSize(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 monitorName = rMpMonitorAlarmCountMParam.getMeasurementPointName(); //监测点名称

        /*获取下级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes (id, WebUtil.filterDeptType()).getData ();
        if (CollUtil.isEmpty(deptDTOList)) {
            return new Page<>();
        }
        List<String> orgCodeList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));

        //获取配网id
        String dataType = dicDataFeignClient.getDicDataByCode(DicDataEnum.DISTRIBUTION_POINT.getCode()).getData().getId();
        //获取监测点类别字典
        List<DictData> lineSortDict = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, String> lineSortIdMap = lineSortDict.stream().collect(Collectors.toMap(DictData::getCode, DictData::getId));

        //根据条件查询单位下面的所有配网监测点
        List<String> pwMonitorIds = iPmsGeneralDeviceService.getPwPmsMonitorIds(id,rMpMonitorAlarmCountMParam.getMonitorSort());

        //根据监测点idList等条件查询【监测点告警月统计】表数据
        LambdaQueryWrapper<RMpMonitorAlarmCountM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .in(RMpMonitorAlarmCountM::getMeasurementPointId, pwMonitorIds)
                .ge(RMpMonitorAlarmCountM::getDataDate, startTime)
                .le(RMpMonitorAlarmCountM::getDataDate, endTime);
//                .eq(RMpMonitorAlarmCountM::getDataType, dataType);
        Page<RMpMonitorAlarmCountM> page = this.page(new Page<>(PageFactory.getPageNum(rMpMonitorAlarmCountMParam),PageFactory.getPageSize(rMpMonitorAlarmCountMParam)),lambdaQueryWrapper);
        List<RMpMonitorAlarmCountM> alarmCountMList = page.getRecords();
        List<PwRMpMonitorAlarmCountMVO> resultList;
        if (CollUtil.isNotEmpty(alarmCountMList)) {
            //监测点id集合
            List<String> monitorIdList = alarmCountMList.stream().map(RMpMonitorAlarmCountM::getMeasurementPointId).distinct().collect(Collectors.toList());
            List<PwPmsMonitorDTO> pwMonitorList = iDistributionMonitorService.pwMonitorAreaDetailInfo(monitorIdList);
            //监测点map  key:监测点id  value:监测点实体
            Map<String, PwPmsMonitorDTO> monitorMap = pwMonitorList.stream().collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId, Function.identity(),(key1,kye2)->key1));

            //封装前端展示的数据
            resultList = alarmCountMList.stream().map(item -> {
                PwRMpMonitorAlarmCountMVO pwRMpMonitorAlarmCountMVO = new PwRMpMonitorAlarmCountMVO();
                BeanUtils.copyProperties(item, pwRMpMonitorAlarmCountMVO);
                //单位信息
                pwRMpMonitorAlarmCountMVO.setOrgName(deptDTOMap.get(item.getOrgNo()).getName()); //所属单位名称
                //监测点信息
                pwRMpMonitorAlarmCountMVO.setMeasurementPointName(monitorMap.get(item.getMeasurementPointId()).getMonitorName());//监测点名称
//                pwRMpMonitorAlarmCountMVO.setMonitorTypeName(monitorTypeMap.get(monitorMap.get(item.getMeasurementPointId()).getMonitorType()));//监测点类型名称
                pwRMpMonitorAlarmCountMVO.setMonitorSort(monitorMap.get(item.getMeasurementPointId()).getMonitorSort());//监测点类别id
                pwRMpMonitorAlarmCountMVO.setVoltageLevel(monitorMap.get(item.getMeasurementPointId()).getVoltageLevel()); //电压等级id
//                pwRMpMonitorAlarmCountMVO.setVoltageLevelName(voltageLevelMap.get(monitorMap.get(item.getMeasurementPointId()).getVoltageLevel())); //电压等级

                //监测点类别信息(I类监测点)
                if (lineSortIdMap.get(DicDataEnum.ONE_LINE.getCode()).equals(monitorMap.get(item.getMeasurementPointId()).getMonitorSort())) {
                    //变电站信息
                    pwRMpMonitorAlarmCountMVO.setPowerId(monitorMap.get(item.getMeasurementPointId()).getPowerId());
                    pwRMpMonitorAlarmCountMVO.setPowerName(monitorMap.get(item.getMeasurementPointId()).getPowerName());

                    pwRMpMonitorAlarmCountMVO.setPowerDistributionAreaId("/"); //台区
                    pwRMpMonitorAlarmCountMVO.setPowerDistributionAreaName("/");

                    pwRMpMonitorAlarmCountMVO.setPowerClientId("/"); //用户
                    pwRMpMonitorAlarmCountMVO.setPowerClientName("/");
                }
                //监测点类别信息(II类监测点)
                if (lineSortIdMap.get(DicDataEnum.TWO_LINE.getCode()).equals(monitorMap.get(item.getMeasurementPointId()).getMonitorSort())) {
                    //变电站信息
                    pwRMpMonitorAlarmCountMVO.setPowerId(monitorMap.get(item.getMeasurementPointId()).getPowerId());
                    pwRMpMonitorAlarmCountMVO.setPowerName(monitorMap.get(item.getMeasurementPointId()).getPowerName());

                    //台区
                    pwRMpMonitorAlarmCountMVO.setPowerDistributionAreaId(monitorMap.get(item.getMeasurementPointId()).getMonitorId());
                    pwRMpMonitorAlarmCountMVO.setPowerDistributionAreaName(monitorMap.get(item.getMeasurementPointId()).getMonitorName());

                    pwRMpMonitorAlarmCountMVO.setPowerClientId("/"); //用户
                    pwRMpMonitorAlarmCountMVO.setPowerClientName("/");
                }
                //监测点类别信息(III类监测点)
                if (lineSortIdMap.get(DicDataEnum.THREE_LINE.getCode()).equals(monitorMap.get(item.getMeasurementPointId()).getMonitorSort())) {
                    //变电站信息
                    pwRMpMonitorAlarmCountMVO.setPowerId(monitorMap.get(item.getMeasurementPointId()).getPowerId());
                    pwRMpMonitorAlarmCountMVO.setPowerName(monitorMap.get(item.getMeasurementPointId()).getPowerName());
                    //台区
                    pwRMpMonitorAlarmCountMVO.setPowerDistributionAreaId("/");
                    pwRMpMonitorAlarmCountMVO.setPowerDistributionAreaName("/");
                    //用户
                    pwRMpMonitorAlarmCountMVO.setPowerClientId(monitorMap.get(item.getMeasurementPointId()).getMonitorId());
                    pwRMpMonitorAlarmCountMVO.setPowerClientName(monitorMap.get(item.getMeasurementPointId()).getMonitorName());

                }
                return pwRMpMonitorAlarmCountMVO;
            }).collect(Collectors.toList());
            pageResult.setRecords(resultList);
        } else {
            return new Page<>();
        }
        return pageResult;
    }
}
