package com.base.track.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.api.client.AntennaRpcService;
import com.base.api.client.StationRpcService;
import com.base.api.model.AntennaRpcDTO;
import com.base.api.model.request.QueryStationAddRpcDTO;
import com.base.orbit.utils.IdGenerator;
import com.base.track.mapper.TrbutAntennaDataMapper;
import com.base.track.model.entity.TrbutAntennaData;
import com.base.track.model.entity.TrbutData;
import com.base.track.model.entity.VisualData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TrbutAntennaDataService extends ServiceImpl<TrbutAntennaDataMapper, TrbutAntennaData> {

    @Resource
    private TrbutAntennaDataMapper trbutAntennaDataMapper;
    @Resource
    private StationRpcService stationRpcService;
    @Resource
    private AntennaRpcService antennaRpcService;

    public void asyncSaveTrbutAntennaData(TrbutData trbutData) {
        List<AntennaRpcDTO> antennaRpcDTOS = antennaRpcService.selAllAntenaInfos(); //  所有天线信息
        List<QueryStationAddRpcDTO> stationListByStationIdList = stationRpcService.getStationListByStationIdList(Arrays.asList(trbutData.getStationId()));
        Optional<QueryStationAddRpcDTO> stationAddRpcDTO = stationListByStationIdList.stream().findFirst();
        if (stationAddRpcDTO.isPresent()) {
            QueryStationAddRpcDTO queryStationAddRpcDTO = stationAddRpcDTO.get();
            List<AntennaRpcDTO> antennaRpcDTOList = antennaRpcDTOS.stream().filter(antennaRpcDTO -> antennaRpcDTO.getDmzdh().equals(queryStationAddRpcDTO.getStationCode()))
                    .collect(Collectors.toList());

            //  入库前先清理
            Set<String> antennaIds = antennaRpcDTOList.stream().map(AntennaRpcDTO::getId).collect(Collectors.toSet());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq(!StringUtils.isEmpty(trbutData.getId()),"trbut_id", trbutData.getId());
            queryWrapper.eq(!StringUtils.isEmpty(queryStationAddRpcDTO.getStationCode()),"station_id", queryStationAddRpcDTO.getStationCode());
            queryWrapper.in(!CollectionUtils.isEmpty(antennaIds),"antenna_id", antennaIds);
            trbutAntennaDataMapper.delete(queryWrapper);

            antennaRpcDTOList.stream().forEach(antennaRpcDTO -> {
                TrbutAntennaData trbutAntennaData = new TrbutAntennaData();
                trbutAntennaData.setId(IdGenerator.getStrId());
                trbutAntennaData.setTrbutId(trbutData.getId());
                trbutAntennaData.setAntennaId(antennaRpcDTO.getId());
                trbutAntennaData.setStationId(queryStationAddRpcDTO.getStationCode());
                trbutAntennaData.setIdent(1);
                trbutAntennaData.setCreateTime(LocalDateTime.now());
                trbutAntennaDataMapper.insert(trbutAntennaData);
            });
        }
    }

    @Async
    public void asyncUpTrbutAntennaData(VisualData visualData) {
        trbutAntennaDataMapper.updateDataByVo(visualData);
    }


    public List<TrbutAntennaData> selTrbutAntennasByIds(List<String> trbutIds) {
        return trbutAntennaDataMapper.selTrbutAntennasByIds(trbutIds);
    }
}
