package com.njcn.cloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.cloud.mapper.AppEventInfoMapper;
import com.njcn.cloud.mapper.AppEventMsgMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.cloud.pojo.dto.AppEventMsg;
import com.njcn.cloud.pojo.vo.EventInfoDetailVO;
import com.njcn.cloud.pojo.vo.EventMsgDetailVO;
import com.njcn.cloud.service.IAppEventMsgService;
import com.njcn.cloud.utils.PubUtil;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.vo.LineDetailVO;
import com.njcn.event.api.EventDetailFeignClient;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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;

/**
 * <p>
 * app暂态消息表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-10-31
 */
@Service
@RequiredArgsConstructor
public class AppEventMsgServiceImpl extends ServiceImpl<AppEventMsgMapper, AppEventMsg> implements IAppEventMsgService {

    private final AppEventInfoMapper appEventInfoMapper;
    private final LineFeignClient lineFeignClient;
    private final EventDetailFeignClient eventDetailFeignClient;

    @Override
    public EventMsgDetailVO eventMsgPage(BaseParam param) {
        EventMsgDetailVO detailVO=new EventMsgDetailVO();
        String searchValue = param.getSearchValue();
        //获取暂态消息信息
        Page<AppEventMsg> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), new LambdaQueryWrapper<AppEventMsg>()
                .eq(AppEventMsg::getUserIndex, searchValue)
                .orderByDesc(AppEventMsg::getTimeID)
        );
        int count = this.count(new LambdaQueryWrapper<AppEventMsg>()
                .eq(AppEventMsg::getUserIndex, searchValue)
                .eq(AppEventMsg::getUserIndex, searchValue)
                .eq(AppEventMsg::getState, 0)
        );
        detailVO.setUdState(count);
        List<AppEventMsg> records = page.getRecords();
        List<EventMsgDetailVO.udState> info=new ArrayList<>();
        if(CollUtil.isNotEmpty(records)){
            Map<String, AppEventMsg> eventMsg = records.stream().collect(Collectors.toMap(AppEventMsg::getEventdetailIndex, Function.identity()));
            //获取暂态事件id
            List<String> eventIds = records.stream().map(AppEventMsg::getEventdetailIndex).distinct().collect(Collectors.toList());
            List<RmpEventDetailPO> eventDetail = eventDetailFeignClient.getEventDetailByIdsList(eventIds).getData();
            List<String> linedIds = eventDetail.stream().map(RmpEventDetailPO::getLineId).distinct().collect(Collectors.toList());
            List<LineDetailVO.Detail> lineDetail = lineFeignClient.getLineDetailByIds(linedIds).getData();
            //监测点map
            Map<String, LineDetailVO.Detail> lineDetailMap = lineDetail.stream().collect(Collectors.toMap(LineDetailVO.Detail::getLineId, Function.identity()));
            EventMsgDetailVO.udState vo;
            AppEventMsg appEventMsg ;
            for (RmpEventDetailPO detail : eventDetail) {
                if(lineDetailMap.containsKey(detail.getLineId())){
                    LineDetailVO.Detail line = lineDetailMap.get(detail.getLineId());
                    vo=new EventMsgDetailVO.udState();
                    vo.setLineIndex(line.getLineId());
                    vo.setLineName(PubUtil.lineName(detail.getSagsource(),line.getLineName()));
                    vo.setEventDetailIndex(detail.getEventId());
                    vo.setTimeID(detail.getStartTime());
                    vo.setPersistTime(detail.getDuration());
                    vo.setMs(detail.getFirstMs());
                    vo.setEventValue(detail.getFeatureAmplitude());
                    vo.setLineInfo(line.getAreaName()+" "+line.getGdName()+" 电压等级:"+line.getVoltageLevel()+" 网络参数:"+line.getIp());
                    if(eventMsg.containsKey(detail.getEventId())){
                        appEventMsg = eventMsg.get(detail.getEventId());
                        vo.setEventMsgIndex(appEventMsg.getEventmsgIndex());
                        vo.setState(appEventMsg.getState());
                        info.add(vo);
                    }

                }

            }
        }
        Page<EventMsgDetailVO.udState> pageEvent = BeanUtil.copyProperties(page, Page.class);
        pageEvent.setRecords(info);
        detailVO.setPage(pageEvent);
        return detailVO;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public EventInfoDetailVO eventInfoDetail(String id) {
        //查询出
        EventInfoDetailVO eventInfo = appEventInfoMapper.selectEventInfoDetailByID(id);
        //查询暂态事件
        List<RmpEventDetailPO> eventDetail = eventDetailFeignClient.getEventDetailByIdsList(Arrays.asList(eventInfo.getEventDetailIndex())).getData();
        List<String> linedIds = eventDetail.stream().map(RmpEventDetailPO::getLineId).distinct().collect(Collectors.toList());
        if(CollUtil.isNotEmpty(linedIds)){
            List<LineDetailVO.Detail> lineDetail = lineFeignClient.getLineDetailByIds(linedIds).getData();
            LineDetailVO.Detail line = lineDetail.get(0);
            RmpEventDetailPO detail= eventDetail.get(0);
            eventInfo.setLineName(PubUtil.lineName(detail.getSagsource(),line.getLineName()));
            eventInfo.setGdName(line.getGdName());
            eventInfo.setBdzName(line.getSubName());
            eventInfo.setDevName(line.getDevName());
            eventInfo.setScale(line.getVoltageLevel());
            eventInfo.setIp(line.getIp());
            eventInfo.setTimeID(detail.getStartTime());
            eventInfo.setPersistTime(detail.getDuration());
            eventInfo.setMs(detail.getFirstMs());
            eventInfo.setEventValue(detail.getFeatureAmplitude());
            this.update(new LambdaUpdateWrapper<AppEventMsg>()
                    .set(AppEventMsg::getState,1)
                    .eq(AppEventMsg::getEventmsgIndex,id)
            );
        }
        return eventInfo;
    }


}
