package com.njcn.csharmonic.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
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.constant.LogInfo;
import com.njcn.csdevice.api.CsLedgerFeignClient;
import com.njcn.csdevice.api.NodeFeignClient;
import com.njcn.csdevice.constant.DataParam;
import com.njcn.csdevice.pojo.dto.DevDetailDTO;
import com.njcn.csdevice.pojo.po.Node;
import com.njcn.csdevice.pojo.vo.CsLedgerVO;
import com.njcn.csharmonic.mapper.CsEventPOMapper;
import com.njcn.csharmonic.mapper.CsEventUserPOMapper;
import com.njcn.csharmonic.param.CsEventUserQueryPage;
import com.njcn.csharmonic.param.CsEventUserQueryParam;
import com.njcn.csharmonic.pojo.po.CsEventPO;
import com.njcn.csharmonic.pojo.po.CsEventUserPO;
import com.njcn.csharmonic.pojo.vo.EventDetailVO;
import com.njcn.csharmonic.service.CsEventUserPOService;
import com.njcn.influx.pojo.dto.EventDataSetDTO;
import com.njcn.influx.service.EvtDataService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.api.EleEvtFeignClient;
import com.njcn.system.api.EpdFeignClient;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.system.pojo.po.EleEvtParm;
import com.njcn.user.enums.AppRoleEnum;
import com.njcn.web.pojo.param.BaseParam;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2023/9/4  15:15【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsEventUserPOServiceImpl extends ServiceImpl<CsEventUserPOMapper, CsEventUserPO> implements CsEventUserPOService{

    private final DicDataFeignClient dicDataFeignClient;
    private final CsLedgerFeignClient csLedgerFeignClient;
    private final EvtDataService evtDataService;
    private final EpdFeignClient epdFeignClient;
    private final EleEvtFeignClient eleEvtFeignClient;
    private final CsEventPOMapper csEventPOMapper;
    private final NodeFeignClient nodeFeignClient;

    @Override
    public Integer queryEventCount(CsEventUserQueryParam csEventUserQueryParam) {
        csEventUserQueryParam.setUserId(RequestUtil.getUserIndex());
        Integer integer = this.getBaseMapper().queryEventCount(csEventUserQueryParam);
        return integer;
    }

    @Override
    public List<EventDetailVO> queryUserEventList(CsEventUserQueryParam csEventUserQueryParam) {
        csEventUserQueryParam.setUserId(RequestUtil.getUserIndex());
        String role = RequestUtil.getUserRole();
        Boolean flag =Boolean.TRUE;

        if(Objects.equals(role, LogInfo.UNKNOWN_ROLE)){
            return new ArrayList<>();
        }
        List<String> strings = JSONArray.parseArray(role, String.class);
        if(CollectionUtils.isEmpty(strings)){
            return new ArrayList<>();
        }
        role=strings.get(0);
        if( Objects.equals(role, AppRoleEnum.APP_VIP_USER.getCode())&&Objects.equals(csEventUserQueryParam.getType(),"3")){
            csEventUserQueryParam.setLevel("3");
        }
//        if( Objects.equals(role, AppRoleEnum.OPERATION_MANAGER.getCode())||Objects.equals(role, AppRoleEnum.ROOT.getCode())){
//            csEventUserQueryParam.setUserId(null);
//            flag = Boolean.FALSE;
//
//        }
//        if(CollectionUtil.isEmpty(csEventUserQueryParam.getTarget())&&Objects.equals(csEventUserQueryParam.getType(),"0")){
//            /*获取所有暂态事件类型*/
//            List<DictData> data = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.APP_EVENT.getCode()).getData();
//            List<String> collect = data.stream().map(DictData::getCode).collect(Collectors.toList());
//            csEventUserQueryParam.setTarget(collect);
//        }

        List<String> deviceId = new ArrayList<>();
        List<CsLedgerVO> data = csLedgerFeignClient.getDeviceTree().getData();

        List<String> collect = data.stream().map(CsLedgerVO::getChildren).
                flatMap(Collection::stream).
                map(CsLedgerVO::getChildren).
                flatMap(Collection::stream).
                map(CsLedgerVO::getChildren).
                flatMap(Collection::stream).
                map(CsLedgerVO::getId).
                collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)){
            return  new ArrayList<>();
        }
        List<EventDetailVO>  list = this.getBaseMapper().queryUserEventList(csEventUserQueryParam,collect,flag);
           // 主要用于统计不关心详情，详情重分页查询获取
//        list.forEach(temp->{
//            DevDetailDTO devDetail = csLedgerFeignClient.queryDevDetail(temp.getDeviceId()).getData();
//            temp.setEquipmentName(devDetail.getEquipmentName());
//            temp.setProjectId(devDetail.getProjectId());
//            temp.setProjectName(devDetail.getProjectName());
//            temp.setEngineeringid(devDetail.getEngineeringid());
//            temp.setEngineeringName(devDetail.getEngineeringName());
//            EleEpdPqd data = epdFeignClient.findByName(temp.getTag()).getData();
//            temp.setShowName(data.getShowName());
//
//            if(Objects.equals(csEventUserQueryParam.getType(),"0")){
//                List<EleEvtParm> data1 = eleEvtFeignClient.queryByPid(data.getId()).getData();
//                List<EventDataSetDTO> eventDataSetDTOS = new ArrayList<>();
//                for (EleEvtParm eleEvtParm : data1) {
//                    EventDataSetDTO eventDataSetDTO = new EventDataSetDTO();
//                    BeanUtils.copyProperties(eleEvtParm,eventDataSetDTO);
//                    EventDataSetDTO evtData = evtDataService.getEventDataSet("evt_data", temp.getId(), eleEvtParm.getName());
//                    if (evtData == null) {
//                        eventDataSetDTO.setValue("-");
//                    }else {
//                        eventDataSetDTO.setValue(Optional.ofNullable(evtData.getValue()).orElse("-"));
//
//                    }
//
//                    eventDataSetDTOS.add(eventDataSetDTO);
//                }
//                temp.setDataSet(eventDataSetDTOS);
//
//                List<EventDataSetDTO> evtParamVVaDepth = eventDataSetDTOS.stream().
//                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_VVaDepth")).
//                        collect(Collectors.toList());
//                if(CollectionUtil.isEmpty(evtParamVVaDepth)){
//                    temp.setEvtParamVVaDepth("-");
//
//                }else {
//                    temp.setEvtParamVVaDepth(evtParamVVaDepth.get(0).getValue()+(Objects.isNull(evtParamVVaDepth.get(0).getUnit())?"":evtParamVVaDepth.get(0).getUnit()));
//
//                }
//
//                List<EventDataSetDTO> evtParamPosition = eventDataSetDTOS.stream().
//                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Position")).
//                        collect(Collectors.toList());
//                if(CollectionUtil.isEmpty(evtParamPosition)){
//                    temp.setEvtParamPosition("-");
//
//                }else {
//                    temp.setEvtParamPosition(evtParamPosition.get(0).getValue()+(Objects.isNull(evtParamPosition.get(0).getUnit())?"":evtParamPosition.get(0).getUnit()));
//
//                }
//
//                List<EventDataSetDTO> evtParamTm = eventDataSetDTOS.stream().
//                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Tm")).
//                        collect(Collectors.toList());
//                if(CollectionUtil.isEmpty(evtParamTm)){
//                    temp.setEvtParamTm("-");
//
//                }else {
//                    temp.setEvtParamTm(evtParamTm.get(0).getValue()+(Objects.isNull(evtParamTm.get(0).getUnit())?"":evtParamTm.get(0).getUnit()));
//
//                }
//
//                List<EventDataSetDTO> evtParamPhase = eventDataSetDTOS.stream().
//                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Phase")).
//                        collect(Collectors.toList());
//                if(CollectionUtil.isEmpty(evtParamPhase)){
//                    temp.setEvtParamPhase("-");
//
//                }else {
//                    temp.setEvtParamPhase(evtParamPhase.get(0).getValue()+(Objects.isNull(evtParamPhase.get(0).getUnit())?"":evtParamPhase.get(0).getUnit()));
//
//                }
//
//            }
//
//        });


        return list;
    }

    @Override
    public void updateStatus(CsEventUserQueryParam csEventUserQueryParam) {
        csEventUserQueryParam.setUserId(RequestUtil.getUserIndex());
        //getEventIds传空一键已读
        if(CollectionUtil.isEmpty(csEventUserQueryParam.getEventIds())){
            List<EventDetailVO> list = this.queryUserEventList(csEventUserQueryParam);
            if(!CollectionUtils.isEmpty(list)){
                List<String> collect = list.stream().map(EventDetailVO::getId).collect(Collectors.toList());

                this.lambdaUpdate().in(CsEventUserPO::getEventId,collect).
                        eq(CsEventUserPO::getUserId,csEventUserQueryParam.getUserId()).
                        set(CsEventUserPO::getStatus,1).update();
            }

        }else {
            this.lambdaUpdate().in(CsEventUserPO::getEventId,csEventUserQueryParam.getEventIds()).
                    eq(CsEventUserPO::getUserId,csEventUserQueryParam.getUserId()).
                    set(CsEventUserPO::getStatus,1).update();
        }

    }

    @Override
    public Page<EventDetailVO> queryEventpage(CsEventUserQueryPage csEventUserQueryPage) {
        Page<EventDetailVO> returnpage = new Page<> (csEventUserQueryPage.getPageNum ( ), csEventUserQueryPage.getPageSize ( ));
        Boolean flag =Boolean.TRUE;

        csEventUserQueryPage.setUserId(RequestUtil.getUserIndex());
        String role = RequestUtil.getUserRole();
        if(Objects.equals(role, LogInfo.UNKNOWN_ROLE)){
            return returnpage;
        }
        List<String> strings = JSONArray.parseArray(role, String.class);
        if(CollectionUtils.isEmpty(strings)){
            return returnpage;
        }
        role=strings.get(0);
        if( Objects.equals(role, AppRoleEnum.APP_VIP_USER.getCode())&&Objects.equals(csEventUserQueryPage.getType(),"3")){
            csEventUserQueryPage.setLevel("3");
        }
//        if( Objects.equals(role, AppRoleEnum.OPERATION_MANAGER.getCode())||Objects.equals(role, AppRoleEnum.ROOT.getCode())){
//            csEventUserQueryPage.setUserId(null);
//            flag = Boolean.FALSE;
//        }
        List<String> deviceId = new ArrayList<>();
        List<CsLedgerVO> data = csLedgerFeignClient.getDeviceTree().getData();

//        List<String> collect = data.stream().filter(temp->StringUtils.isEmpty(csEventUserQueryPage.getEngineeringid())||
//                        Objects.equals(temp.getId(), csEventUserQueryPage.getEngineeringid())).map(CsLedgerVO::getChildren).
//                flatMap(Collection::stream).filter(
//                        temp->StringUtils.isEmpty(csEventUserQueryPage.getProjectId())||
//                                Objects.equals(temp.getId(), csEventUserQueryPage.getProjectId())
//                ).
//                map(CsLedgerVO::getChildren).
//                flatMap(Collection::stream).filter(
//                        temp->StringUtils.isEmpty(csEventUserQueryPage.getDeviceId())||
//                                Objects.equals(temp.getId(), csEventUserQueryPage.getDeviceId())
//                ).
//                map(CsLedgerVO::getId).
//                collect(Collectors.toList());
        List<String> collect = data.stream().filter(temp->StringUtils.isEmpty(csEventUserQueryPage.getEngineeringid())||
                        Objects.equals(temp.getId(), csEventUserQueryPage.getEngineeringid()))
                .map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getProjectId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getProjectId())
                ).
                map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getDeviceId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getDeviceId())
                ).
                map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getDeviceId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getDeviceId())
                ).
                map(CsLedgerVO::getId).
                collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)){
            return returnpage;
        }

        returnpage  = this.getBaseMapper().queryEventpage(returnpage,csEventUserQueryPage,collect,flag);

        returnpage.getRecords().forEach(temp->{
            DevDetailDTO devDetail = csLedgerFeignClient.queryDevDetail(temp.getDeviceId()).getData();
            temp.setEquipmentName(devDetail.getEquipmentName());
            temp.setProjectId(devDetail.getProjectId());
            temp.setProjectName(devDetail.getProjectName());
            temp.setEngineeringid(devDetail.getEngineeringid());
            temp.setEngineeringName(devDetail.getEngineeringName());
            EleEpdPqd ele = epdFeignClient.findByName(temp.getTag()).getData();
            temp.setShowName(ele.getShowName());
            temp.setCode(ele.getDefaultValue());
            if(Objects.equals(csEventUserQueryPage.getType(),"0")){

                List<EleEvtParm> data1 = eleEvtFeignClient.queryByPid(ele.getId()).getData();
                List<EventDataSetDTO> eventDataSetDTOS = new ArrayList<>();
                for (EleEvtParm eleEvtParm : data1) {
                    EventDataSetDTO eventDataSetDTO = new EventDataSetDTO();
                    BeanUtils.copyProperties(eleEvtParm,eventDataSetDTO);
                    if (Objects.equals(eventDataSetDTO.getName(),"Evt_Param_Position")) {
                        continue;
//                        eventDataSetDTO.setValue(Objects.equals(temp.getLocation(),"grid")?"电网侧":"负载侧");
                    }
                    EventDataSetDTO evtData = evtDataService.getEventDataSet("evt_data", temp.getId(), eleEvtParm.getName());
                    if (evtData == null) {
                        eventDataSetDTO.setValue("-");
                    }else {
                        eventDataSetDTO.setValue(Optional.ofNullable(evtData.getValue()).orElse("-"));

                    }
//                    if (Objects.equals(eventDataSetDTO.getName(),"Evt_Param_Position")) {
//                        eventDataSetDTO.setValue(Objects.equals(temp.getLocation(),"grid")?"电网侧":"负载侧");
//                    }
                    eventDataSetDTOS.add(eventDataSetDTO);
                }
                temp.setDataSet(eventDataSetDTOS);

                List<EventDataSetDTO> evtParamVVaDepth = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_VVaDepth")).
                        collect(Collectors.toList());

                if(CollectionUtil.isEmpty(evtParamVVaDepth)){
                    temp.setEvtParamVVaDepth("-");

                }else {
                    temp.setEvtParamVVaDepth(evtParamVVaDepth.get(0).getValue()+(Objects.isNull(evtParamVVaDepth.get(0).getUnit())?"":evtParamVVaDepth.get(0).getUnit()));

                }

                List<EventDataSetDTO> evtParamPosition = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Position")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamPosition)){
                    temp.setEvtParamPosition("-");

                }else {
                    //temp.setEvtParamPosition(evtParamPosition.get(0).getValue()+(Objects.isNull(evtParamPosition.get(0).getUnit())?"":evtParamPosition.get(0).getUnit()));
                    temp.setEvtParamPosition(evtParamPosition.get(0).getValue());
                }
                if (Objects.equals(temp.getEvtParamPosition(),"-")) {
                    if (!Objects.isNull(temp.getLocation())) {
                        temp.setEvtParamPosition(Objects.equals(temp.getLocation(),"grid")?"电网侧":"负载侧");
                    }
                }

                List<EventDataSetDTO> evtParamTm = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Tm")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamTm)){
                    temp.setEvtParamTm("-");

                }else {
                    temp.setEvtParamTm(evtParamTm.get(0).getValue()+(Objects.isNull(evtParamTm.get(0).getUnit())?"":evtParamTm.get(0).getUnit()));

                }

                List<EventDataSetDTO> evtParamPhase = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Phase")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamPhase)){
                    temp.setEvtParamPhase("-");

                }else {
                    temp.setEvtParamPhase(evtParamPhase.get(0).getValue()+(Objects.isNull(evtParamPhase.get(0).getUnit())?"":evtParamPhase.get(0).getUnit()));

                }

            }

        });



        return returnpage;
    }

    @Override
    public Page<EventDetailVO> queryEventPageWeb(CsEventUserQueryPage csEventUserQueryPage) {
        Page<EventDetailVO> returnpage = new Page<> (csEventUserQueryPage.getPageNum ( ), csEventUserQueryPage.getPageSize ( ));
        List<CsLedgerVO> data = csLedgerFeignClient.getDeviceTree().getData();
        List<String> devIds = new ArrayList<>();
        data = data.stream().filter(temp -> StringUtils.isEmpty(csEventUserQueryPage.getDeviceTypeName() )||
                Objects.equals(temp.getName(), csEventUserQueryPage.getDeviceTypeName())).collect(Collectors.toList());
        //便携式设备和治理设备拆分
        List<CsLedgerVO> portableDevTree = data.stream().filter(temp -> Objects.equals(temp.getName(), DataParam.portableDev)).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(portableDevTree)){
            portableDevTree = portableDevTree.get(0).getChildren();
        }
        List<CsLedgerVO> governmentDevTree = data.stream().filter(temp -> Objects.equals(temp.getName(), DataParam.governmentDev)).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(governmentDevTree)){
            governmentDevTree = governmentDevTree.get(0).getChildren();
        }
        List<CsLedgerVO> cldDevTree = data.stream().filter(temp -> Objects.equals(temp.getName(), DataParam.cldDev)).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(cldDevTree)){
            cldDevTree = cldDevTree.get(0).getChildren();
        }

        //便携式就1层下边就是设备
        List<String> portableDevIds = portableDevTree.stream().filter(
                temp -> StringUtils.isEmpty(csEventUserQueryPage.getDeviceId()) ||
                        Objects.equals(temp.getId(), csEventUserQueryPage.getDeviceId())
        ).map(CsLedgerVO::getId).collect(Collectors.toList());
        List<String> governmentDevIds = governmentDevTree.stream().filter(temp->StringUtils.isEmpty(csEventUserQueryPage.getEngineeringid())||
                        Objects.equals(temp.getId(), csEventUserQueryPage.getEngineeringid()))
                .map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getProjectId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getProjectId())
                ).
                map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getDeviceId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getDeviceId())
                ).
                map(CsLedgerVO::getId).
                collect(Collectors.toList());
        List<String> cldDevIds = cldDevTree.stream().filter(temp->StringUtils.isEmpty(csEventUserQueryPage.getEngineeringid())||
                        Objects.equals(temp.getId(), csEventUserQueryPage.getEngineeringid()))
                .map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getProjectId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getProjectId())
                ).
                map(CsLedgerVO::getChildren).flatMap(Collection::stream).filter(
                        temp->StringUtils.isEmpty(csEventUserQueryPage.getDeviceId())||
                                Objects.equals(temp.getId(), csEventUserQueryPage.getDeviceId())
                ).
                map(CsLedgerVO::getId).
                collect(Collectors.toList());

        devIds.addAll(portableDevIds);
        devIds.addAll(governmentDevIds);
        devIds.addAll(cldDevIds);

        if (CollectionUtils.isEmpty(devIds)){
            return returnpage;
        }
        returnpage  = this.getBaseMapper().queryEventPageWeb(returnpage,csEventUserQueryPage,devIds);
        returnpage.getRecords().forEach(temp->{
            DevDetailDTO devDetail = csLedgerFeignClient.queryDevDetail(temp.getDeviceId()).getData();
            temp.setEquipmentName(devDetail.getEquipmentName());
            temp.setProjectId(devDetail.getProjectId());
            temp.setProjectName(devDetail.getProjectName());
            temp.setEngineeringid(devDetail.getEngineeringid());
            temp.setEngineeringName(devDetail.getEngineeringName());
            EleEpdPqd ele = epdFeignClient.findByName(temp.getTag()).getData();
            if (Objects.isNull(ele)) {
                temp.setShowName(temp.getTag());
                temp.setCode(temp.getCode());
            } else {
                temp.setShowName(ele.getShowName());
                temp.setCode(ele.getDefaultValue());
            }
            if (ObjectUtil.isNotNull(temp.getLineId())) {
                if (temp.getLineId().endsWith("0")) {
                    temp.setModuleNo("模块" + temp.getClDid());
                }
            }
            if(Objects.equals(csEventUserQueryPage.getType(),"0")){
                List<EleEvtParm> data1 = eleEvtFeignClient.queryByPid(ele.getId()).getData();
                List<EventDataSetDTO> eventDataSetDTOS = new ArrayList<>();
                for (EleEvtParm eleEvtParm : data1) {
                    EventDataSetDTO eventDataSetDTO = new EventDataSetDTO();
                    BeanUtils.copyProperties(eleEvtParm,eventDataSetDTO);
                    if (Objects.equals(eventDataSetDTO.getName(),"Evt_Param_Position")) {
                        continue;
                    }
                    EventDataSetDTO evtData = evtDataService.getEventDataSet("evt_data", temp.getId(), eleEvtParm.getName());
                    if (evtData == null) {
                        eventDataSetDTO.setValue("-");
                    }else {
                        eventDataSetDTO.setValue(Optional.ofNullable(evtData.getValue()).orElse("-"));
                    }
                    eventDataSetDTOS.add(eventDataSetDTO);
                }
                temp.setDataSet(eventDataSetDTOS);
                List<EventDataSetDTO> evtParamVVaDepth = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_VVaDepth")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamVVaDepth)){
                    temp.setEvtParamVVaDepth("-");
                }else {
                    temp.setEvtParamVVaDepth(evtParamVVaDepth.get(0).getValue()+(Objects.isNull(evtParamVVaDepth.get(0).getUnit())?"":evtParamVVaDepth.get(0).getUnit()));
                }

                List<EventDataSetDTO> evtParamPosition = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Position")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamPosition)){
                    temp.setEvtParamPosition("-");
                }else {
                    temp.setEvtParamPosition(evtParamPosition.get(0).getValue());
                }
                if (Objects.equals(temp.getEvtParamPosition(),"-")) {
                    if (!Objects.isNull(temp.getLocation())) {
                        temp.setEvtParamPosition(Objects.equals(temp.getLocation(),"grid")?"电网侧":"负载侧");
                    }
                }

                List<EventDataSetDTO> evtParamTm = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Tm")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamTm)){
                    temp.setEvtParamTm("-");
                }else {
                    temp.setEvtParamTm(evtParamTm.get(0).getValue()+(Objects.isNull(evtParamTm.get(0).getUnit())?"":evtParamTm.get(0).getUnit()));
                }

                List<EventDataSetDTO> evtParamPhase = eventDataSetDTOS.stream().
                        filter(dataSetDTO -> Objects.equals(dataSetDTO.getName(), "Evt_Param_Phase")).
                        collect(Collectors.toList());
                if(CollectionUtil.isEmpty(evtParamPhase)){
                    temp.setEvtParamPhase("-");
                }else {
                    temp.setEvtParamPhase(evtParamPhase.get(0).getValue()+(Objects.isNull(evtParamPhase.get(0).getUnit())?"":evtParamPhase.get(0).getUnit()));
                }
            }
        });
        return returnpage;
    }

    @Override
    public Page<CsEventPO> getFrontWarnInfo(BaseParam baseParam) {
        Page<CsEventPO> page = new Page<>(baseParam.getPageNum(), baseParam.getPageSize());
        List<Node> nodeList = nodeFeignClient.nodeAllList().getData();
        Map<String, Node> nodeMap = nodeList.stream().collect(Collectors.toMap(Node::getId, Function.identity()));

        if (CollectionUtil.isNotEmpty(nodeList)) {
            if (ObjectUtil.isNotNull(baseParam.getSearchValue()) || StringUtil.isNotBlank(baseParam.getSearchValue())) {
                nodeList = nodeList.stream().filter(item-> item.getName().contains(baseParam.getSearchValue()) || item.getIp().contains(baseParam.getSearchValue())).collect(Collectors.toList());
            }
            if (CollectionUtil.isNotEmpty(nodeList)) {
                List<String> nodeIds = nodeList.stream().map(Node::getId).collect(Collectors.toList());
                LambdaQueryWrapper<CsEventPO> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(CsEventPO::getDeviceId, nodeIds)
                        .between(CsEventPO::getStartTime,
                                DateUtil.beginOfDay(DateUtil.parse(baseParam.getSearchBeginTime())).toString(),
                                DateUtil.endOfDay(DateUtil.parse(baseParam.getSearchEndTime())).toString())
                        .eq(CsEventPO::getType, 4)
                        .orderByDesc(CsEventPO::getStartTime);
                page = csEventPOMapper.selectPage(page, queryWrapper);
            }
        }
        List<CsEventPO> records = page.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            page.getRecords().forEach(item->{
                //这边将前置名称放进lineId字段；将前置IP放进wavePath字段；进程号使用clDid
                item.setLineId(nodeMap.get(item.getDeviceId()).getName());
                item.setWavePath(nodeMap.get(item.getDeviceId()).getIp());
            });
        }
        return page;
    }
}
