package com.njcn.device.pq.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.njcn.device.common.service.GeneralDeviceService;
import com.njcn.device.common.service.TerminalBaseService;
import com.njcn.device.pq.pojo.bo.DeviceType;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.vo.CommunicateVO;
import com.njcn.device.pq.pojo.vo.TerminalBaseVO;
import com.njcn.device.pq.service.ISubstationExpendService;
import com.njcn.influx.imapper.PqsCommunicateMapper;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import com.njcn.influx.pojo.po.PqsCommunicate;
import com.njcn.influx.query.InfluxQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-05-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SubstationExpendServiceImpl implements ISubstationExpendService {

    private final PqsCommunicateMapper pqsCommunicateMapper;

    private final GeneralDeviceService generalDeviceService;

    private final TerminalBaseService terminalBaseService;

    @Override
    public List<CommunicateVO.PVCommunicateVO> getCommunicationStatus(DeviceInfoParam.CompareLimitParam param) {
        List<GeneralDeviceDTO> generalDeviceDTOS = generalDeviceService.getDeviceInfo(param, Stream.of(0).collect(Collectors.toList()), Stream.of(1).collect(Collectors.toList()));
        //获取所有终端id
        List<String> terminalIds = generalDeviceDTOS.stream().flatMap(dto -> dto.getDeviceIndexes().stream()).collect(Collectors.toList());
        List<CommunicateVO.PVCommunicateVO> info = new ArrayList<>();
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(PqsCommunicate.class);
        influxQueryWrapper.between(InfluxDBTableConstant.TIME,
                DateUtil.beginOfDay(DateUtil.parse(param.getSearchBeginTime())).toString(),
                DateUtil.beginOfDay(DateUtil.parse(param.getSearchEndTime()))).toString();
        if (CollUtil.isNotEmpty(terminalIds)) {
            influxQueryWrapper.or(PqsCommunicate::getDevId, terminalIds);
        }
        influxQueryWrapper.limit(param.getLimit());
        List<PqsCommunicate> communicates = pqsCommunicateMapper.selectByQueryWrapper(influxQueryWrapper);
        if (CollUtil.isEmpty(communicates)) {
            return info;
        }
        List<String> ids = communicates.stream().map(PqsCommunicate::getDevId).distinct().collect(Collectors.toList());
        if (CollUtil.isNotEmpty(ids)) {
            List<TerminalBaseVO> deviceByIds = terminalBaseService.getDeviceByIdOnOrOff(ids, new DeviceType(), 0);
            Map<String, String> terminalNameIp = deviceByIds.stream().collect(Collectors.toMap(TerminalBaseVO::getDevId, x -> x.getDevName() + "_" + x.getIp()));
            CommunicateVO.PVCommunicateVO communicate;
            for (PqsCommunicate cate : communicates) {
                communicate = new CommunicateVO.PVCommunicateVO();
                BeanUtil.copyProperties(cate, communicate);
                communicate.setTime(cate.getTime());
                if (terminalNameIp.containsKey(cate.getDevId())) {
                    communicate.setDevIp(terminalNameIp.get(cate.getDevId()));
                }
                info.add(communicate);
            }
        }
        return info;
    }

}
