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.dto.SimpleDTO;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.mapper.distribution.RMpPwAlarmDetailDMapper;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.device.pms.pojo.param.RStatPwAlarmDetailParam;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.pojo.po.RMpPwAlarmDetailD;
import com.njcn.device.pms.pojo.po.RStatAreaAlarmCountM;
import com.njcn.device.pms.pojo.vo.RStatAlarmDetailVO;
import com.njcn.device.pms.service.distribution.IPwMonitorService;
import com.njcn.device.pms.service.distribution.RMpPwAlarmDetailDService;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.IPmsGeneralDeviceService;
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.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.function.Function;
import java.util.stream.Collectors;

/**
* @author jianghf
* @description 针对表【r_mp_pw_alarm_detail_d】的数据库操作Service实现
* @createDate 2022-10-17 09:00:27
*/
@Service
@RequiredArgsConstructor
public class RMpPwAlarmDetailDServiceImpl extends ServiceImpl<RMpPwAlarmDetailDMapper, RMpPwAlarmDetailD>
implements RMpPwAlarmDetailDService{

    private final IPmsGeneralDeviceService generalPmsDeviceService;

    private final IPwMonitorService iPwMonitorService;  //【配网监测点】

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

    @Override
    public List<RStatAlarmDetailVO> getAllAlarmDetailList(RStatPwAlarmDetailParam rStatPwAlarmDetailParam) {
        //提取参数
        String startTime = rStatPwAlarmDetailParam.getStartTime(); //开始时间
        String endTime = rStatPwAlarmDetailParam.getEndTime(); //结束时间
        //获取登录人当前部门
        String deptIndex = RequestUtil.getDeptIndex();
//        String deptIndex = "5699e5916a18a6381e1ac92da5bd2628";

        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        pmsDeviceInfoParam.setDeptIndex(deptIndex);
        //主网监测点id集合
        ArrayList<String> zwMonitorIds = new ArrayList<>();
        //获取主网台账信息
        List<PmsGeneralDeviceDTO> pmsDeviceInfoWithInOrg = generalPmsDeviceService.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam);
        pmsDeviceInfoWithInOrg.forEach(item -> {
            zwMonitorIds.addAll(item.getMonitorIdList());
        });

        //查询主网监测点信息
        LambdaQueryWrapper<Monitor> zwMonitorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zwMonitorLambdaQueryWrapper.in(CollUtil.isNotEmpty(zwMonitorIds), Monitor::getId, zwMonitorIds);
        //主网监测点集合
        List<Monitor> zwMonitorList = iMonitorService.list(zwMonitorLambdaQueryWrapper);
        Map<String, Monitor> zwMonitorMap = zwMonitorList.stream().collect(Collectors.toMap(Monitor::getId, item -> item));

        //获取配网监测点
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(deptIndex); //单位id
        List<PwPmsMonitorDTO> pwMonitorList = iPwMonitorService.getPwNewMonitorList(pwPmsMonitorParam);
        //配网监测点id集合
        List<String> pwMonitorIds = pwMonitorList.stream().map(PwPmsMonitorDTO::getMonitorId).collect(Collectors.toList());
        //配网监测点Map集合
        Map<String, PwPmsMonitorDTO> pwMonitorMap = pwMonitorList.stream().collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId, Function.identity(),(Key1, kye2) -> Key1));


        //合并主网配网监测点id集合（并去重）
        ArrayList<String> allMonitorIds = new ArrayList<>();
        allMonitorIds.addAll(zwMonitorIds);
        allMonitorIds.addAll(pwMonitorIds);
        if (CollUtil.isEmpty(allMonitorIds)) {
            return new ArrayList<>();
        }
        //查询告警明细
        LambdaQueryWrapper<RMpPwAlarmDetailD> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RMpPwAlarmDetailD::getMeasurementPointId, allMonitorIds)
                .ge(StringUtils.isNotBlank(startTime), RMpPwAlarmDetailD::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpPwAlarmDetailD::getDataDate, endTime);
        List<RMpPwAlarmDetailD> list = this.list(lambdaQueryWrapper);

        List<RStatAlarmDetailVO> resultList = list.stream().map(item -> {
            RStatAlarmDetailVO rStatAlarmDetailVO = new RStatAlarmDetailVO();
            BeanUtils.copyProperties(item, rStatAlarmDetailVO); //监测点id、告警类型、告警描述、告警持续时间

            //监测点名称
            if (zwMonitorMap.containsKey(item.getMeasurementPointId())) {
                rStatAlarmDetailVO.setMeasurementPointName(zwMonitorMap.get(item.getMeasurementPointId()).getName());
            } else if (pwMonitorMap.containsKey(item.getMeasurementPointId())) {
                rStatAlarmDetailVO.setMeasurementPointName(pwMonitorMap.get(item.getMeasurementPointId()).getMonitorName());
            } else {
                rStatAlarmDetailVO.setMeasurementPointName("/");
            }

            return rStatAlarmDetailVO;
        }).collect(Collectors.toList());
        return resultList;
    }
}
