package com.base.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.api.model.InstanceRpcDTO;
import com.base.biz.mapper.InstanceInfoMapper;
import com.base.biz.mapper.SatelliteInfoMapper;
import com.base.biz.mapper.StationInfoMapper;
import com.base.biz.model.dto.InstanceInfoDTO;
import com.base.biz.model.dto.request.InstanceInfoDTOListReq;
import com.base.biz.model.entity.AntennaInfo;
import com.base.biz.model.entity.InstanceInfo;
import com.base.biz.model.entity.SatelliteInfo;
import com.base.biz.model.entity.StationInfo;
import com.base.orbit.utils.BizUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InstanceService extends ServiceImpl<InstanceInfoMapper, InstanceInfo> {

    @Resource
    InstanceInfoMapper instanceInfoMapper;

    @Resource
    SatelliteInfoMapper yxInfoBasisSatelliteMapper;

    public List<InstanceInfoDTO> getInstanceListBySatellite(List<String> satelliteCode) {
        List<InstanceInfo> instanceInfos = instanceInfoMapper.selectBySatellite(satelliteCode);
        return BizUtils.copyEntityToDto(InstanceInfoDTO.class,instanceInfos);
    }

    public void deleteBySatelliteCode(String satelliteCode) {
        QueryWrapper<InstanceInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("satellite_code",satelliteCode);
        this.remove(wrapper);
    }
    public void save(InstanceRpcDTO instanceRpcDTO) {
        InstanceInfo instanceInfo = new InstanceInfo();
        BeanUtils.copyProperties(instanceRpcDTO, instanceInfo);
        this.saveOrUpdate(instanceInfo);
    }

    public List<InstanceInfoDTO> getInstanceInfoDTOList(InstanceInfoDTOListReq req){
        List<InstanceInfo> instanceInfoList = instanceInfoMapper.getInstanceInfoDTOList(req);
        return BizUtils.copyEntityToDto(InstanceInfoDTO.class,instanceInfoList);
    }
    public List<InstanceInfoDTO> queryNewestInstance() {
        List<InstanceInfo> instanceDOS = instanceInfoMapper.selectList(new QueryWrapper<>());
        List<InstanceInfoDTO> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(instanceDOS)){
            return new ArrayList<>();
        }
        List<SatelliteInfo> yxInfoBasisSatellites = yxInfoBasisSatelliteMapper.selectBatchIds(instanceDOS.stream().map(InstanceInfo::getSatelliteCode).collect(Collectors.toSet()));
        Map<String, List<InstanceInfo>> instanceGroupBySatelliteId = instanceDOS.stream().collect(Collectors.groupingBy(InstanceInfo::getSatelliteCode));
        instanceGroupBySatelliteId.values().forEach(x -> {
            if (!x.isEmpty()) {
                InstanceInfo yxInfoBasisInstance = x.stream().max(Comparator.comparing(InstanceInfo::getEpochTime)).get();
                InstanceInfoDTO yxInfoBasisInstanceDTO = new InstanceInfoDTO();
                BeanUtils.copyProperties(yxInfoBasisInstance,yxInfoBasisInstanceDTO);
                Optional<SatelliteInfo> optionalYxInfoBasisSatellite = yxInfoBasisSatellites.stream().filter(e->e.getId().equals(yxInfoBasisInstance.getSatelliteCode())).findFirst();
                optionalYxInfoBasisSatellite.ifPresent(yxInfoBasisSatellite -> yxInfoBasisInstanceDTO.setSatelliteCode(yxInfoBasisSatellite.getSatelliteCode()));
                list.add(yxInfoBasisInstanceDTO);
            }
        });
        return list;
    }

}
