package com.base.track.utils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.track.model.dto.request.VisualRequestDTO;
import com.base.track.model.dto.response.ElevationAndDateDTO;
import com.base.track.model.entity.TrbutData;
import com.base.track.model.entity.TrbutHrpreData;
import com.base.track.service.TrbutDataService;
import com.base.track.service.TrbutHrpreDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 统计仰角
 */
@Component
public class ElevationCount {

    @Resource
    private TrbutHrpreDataService trbutHrpreDataService;

    @Resource
    private TrbutDataService trbutDataService;

    public ElevationAndDateDTO elevationMargin(VisualRequestDTO visualRequestDTO) {

        ElevationAndDateDTO elevationAndDateDTO = new ElevationAndDateDTO();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.isNotEmpty(visualRequestDTO.getTrbutDataId()), "id", visualRequestDTO.getTrbutDataId());
        List<TrbutData> trbutDatas = trbutDataService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(trbutDatas)) {
            TrbutData trbutData = trbutDatas.get(0);
            QueryWrapper query = new QueryWrapper();
            query.eq(StringUtils.isNotEmpty(trbutData.getSatelliteCode()), "satellite_code", trbutData.getSatelliteCode());
            query.eq(StringUtils.isNotEmpty(trbutData.getStationCode()), "station_code", trbutData.getStationCode());
            List<TrbutHrpreData> trbutHrpreDatas = trbutHrpreDataService.list(query);
            List<Date> dates = trbutHrpreDatas.stream().map(TrbutHrpreData::getPitTime).collect(Collectors.toList());
            List<Double> pitchAngles = trbutHrpreDatas.stream().map(TrbutHrpreData::getPitchAngle).collect(Collectors.toList());
            elevationAndDateDTO.setElevations(pitchAngles);
            elevationAndDateDTO.setDates(dates);
        }
        return elevationAndDateDTO;
    }
}
