package com.base.plan.service;

import java.util.Date;

import com.base.api.client.*;
import com.base.api.model.*;
import com.base.api.model.request.FzTaskInfoRpcReqDTO;
import com.base.api.model.request.PayloadRpcReqDTO;
import com.base.api.model.request.PointListRpcReqDTO;
import com.base.api.model.request.TaskStateNotice;
import com.base.orbit.utils.BizUtils;
import com.base.orbit.utils.IdGenerator;
import com.base.plan.mapper.*;
import com.base.plan.model.dto.*;
import com.base.plan.model.dto.request.*;
import com.base.plan.model.entity.*;
import com.base.plan.service.biz.StripBiz;
import com.base.plan.task.handler.SystemIndicatorInfoUpdateHandler;
import com.base.plan.trans.DataTransMissInitialize;
import com.base.plan.trans.DataTransTypeEnums;
import com.base.plan.trans.DataTransmissHandler;
import com.base.plan.trans.SystemDataTransMissHandler;
import com.google.common.collect.Lists;
import com.plan.api.model.StepDtoMapTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
public class PlanService {

    @Resource
    StripMapper stripMapper;

    @Resource
    StripService stripService;

    @Resource
    StripBiz stripBiz;

    @Resource
    TrbutDataRpcService trbutDataRpcService;

    @Resource
    PayloadRpcService payloadRpcService;

    @Resource
    WindowResourceService windowResourceService;

    @Resource
    WindowResourceMapper windowResourceMapper;

    @Resource
    SystemIndicatorInfoUpdateHandler systemIndicatorInfoUpdateHandler;

    @Resource
    FzIndicatorInfoMapper fzIndicatorInfoMapper;

    @Resource
    AreaOptimizationResultInfoMapper areaOptimizationResultInfoMapper;

    @Resource
    SatelliteRpcService satelliteRpcService;

    @Resource
    FzTaskRpcService fzTaskService;

    @Resource
    PointRpcService pointRpcService;

    @Resource
    DataTransMissInitialize dataTransMissInitialize;
    @Resource
    SystemDataTransMissHandler systemDataTransMissHandler;


    /**
     * 获取规划方案
     *
     * @param req
     * @return
     */
    public List<PlanDTO> getPlanDTOList(PlanReqDTO req) {
        List<PlanRelationDTO> planRelationDTOS = formatPlanRelationDTOList(req.getReqIds());
        if (CollectionUtils.isEmpty(planRelationDTOS)) {
            return new ArrayList<>();
        }
        return formatPlanDTOList(planRelationDTOS);
    }

    private List<PlanRelationDTO> formatPlanRelationDTOList(List<String> reqIds) {
        List<PlanRelationDTO> planRelationDTOS = new ArrayList<>();
        //查询优选条带
        List<AreaOptimizationResultInfo> areaOptimizationResultInfoList = stripBiz.getOptimizationStripIds(reqIds);
        if (CollectionUtils.isEmpty(areaOptimizationResultInfoList)) {
            return planRelationDTOS;
        }
        List<String> stripIds = new ArrayList<>();
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : areaOptimizationResultInfoList) {
            areaOptimizationResultInfo.setStripIds(Arrays.asList(areaOptimizationResultInfo.getStripId().split(",")));
            stripIds.addAll(areaOptimizationResultInfo.getStripIds());
        }
        //查询条带绑定的地面站
        List<WindowResource> windowResourceList = stripBiz.getOptimizationTrbutIds(stripIds);
        //format
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : areaOptimizationResultInfoList) {
            for (String stripId : areaOptimizationResultInfo.getStripIds()) {
                for (WindowResource windowResource : windowResourceList) {
                    if (windowResource.getWindowId().equals(stripId)) {
                        PlanRelationDTO planRelationDTO = new PlanRelationDTO();
                        planRelationDTO.setReqId(areaOptimizationResultInfo.getReqId());
                        planRelationDTO.setStripId(windowResource.getWindowId());
                        planRelationDTO.setTrbutId(windowResource.getResourceId());
                        planRelationDTO.setPayloadId(areaOptimizationResultInfo.getPayloadId());
                        planRelationDTO.setDataTransferDuration(windowResource.getDataTransferDuration());
                        planRelationDTOS.add(planRelationDTO);
                    }
                }
            }
        }
        return planRelationDTOS;
    }


    /**
     * 数传方案优选
     *
     * @param req 规划信息
     * @return 规划结果
     */
    public List<WindowResourceDTO> makePlan(TaskContainDatatransReqDTO req) {
        List<String> stripIds = new ArrayList<>();
        List<String> trbutIds = new ArrayList<>();
        List<String> reqIds = new ArrayList<>();
        req.getTaskPlanningReqDTOS().forEach(e -> {
            stripIds.addAll(e.getStripIds());
            reqIds.addAll(e.getReqIds());
            trbutIds.addAll(e.getTrbutIds());       //  用户不勾选进出站时，界面将传全部进出站
        });

        for (String reqId : reqIds) {
            stripBiz.taskStepStateNotice(reqId, BizTaskStepEnum.TASK_PLANNING, 0);
        }
        try {
            //查询所有的条带
            StripDTOListReqDTO stripDTOListReqDTO = new StripDTOListReqDTO();
            stripDTOListReqDTO.setIds(stripIds);
            List<StripDTO> stripDTOList = stripService.getStripDTOList(stripDTOListReqDTO);
            CxStationRpcDTO cxStationRpcDTO = new CxStationRpcDTO();
            cxStationRpcDTO.setIds(trbutIds);
            List<TrbutDataRpcDTO> trbutDataRpcDTOList = trbutDataRpcService.selTrButData(cxStationRpcDTO);
            List<WindowResourceDTO> windowResourceDTOS = systemDataTransMissHandler.trbutMake(stripDTOList, trbutDataRpcDTOList);
            if (Objects.nonNull(windowResourceDTOS)) {
                for (String reqId : reqIds) {
                    stripBiz.taskStepStateNotice(reqId, BizTaskStepEnum.TASK_PLANNING, 1);
                }
                return windowResourceDTOS;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            for (String reqId : reqIds) {
                stripBiz.taskStepStateNotice(reqId, BizTaskStepEnum.TASK_PLANNING, 2);
            }
        }
        return null;
    }


    /**
     * 重置数传站
     *
     * @param stripIdList 条带id
     */
    public void resetTrbutPlan(List<String> stripIdList, List<String> reqIds) {
        windowResourceMapper.deleteByStripIds(stripIdList);
        //重置任务阶段
        for (String reqId : reqIds) {
            stripBiz.taskStepStateNotice(reqId, BizTaskStepEnum.AREA_OPTIMIZATION, 1);
        }
    }

    /**
     * 重置数传站
     *
     * @param reqIdList 条带计划
     */
    @Transactional(rollbackFor = Exception.class)
    public void resetPlan(List<String> reqIdList) {
        //查询任务列表
        FzTaskInfoRpcReqDTO fzTaskInfoRpcReqDTO = new FzTaskInfoRpcReqDTO();
        fzTaskInfoRpcReqDTO.setReqIds(reqIdList);
        List<FzTaskRpcDTO> fzTaskRpcDTOS = fzTaskService.getFzTaskDTOList(fzTaskInfoRpcReqDTO);

        AreaOptimizationResultReqDTO areaOptimizationResultReqDTO = new AreaOptimizationResultReqDTO();
        areaOptimizationResultReqDTO.setReqIds(reqIdList);
        List<AreaOptimizationResultInfo> areaOptimizationResultInfoList = areaOptimizationResultInfoMapper.selectAreaOptimizationResultInfoList(areaOptimizationResultReqDTO);
        if (CollectionUtils.isEmpty(areaOptimizationResultInfoList)) {
            return;
        }
        //删除优选方案
        areaOptimizationResultInfoMapper.deleteByReqIds(reqIdList);
        //删除数传方案
        List<String> stripIds = new ArrayList<>();
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : areaOptimizationResultInfoList) {
            areaOptimizationResultInfo.setStripIds(Arrays.asList(areaOptimizationResultInfo.getStripId().split(",")));
            stripIds.addAll(areaOptimizationResultInfo.getStripIds());
        }
        windowResourceMapper.deleteByStripIds(stripIds);
        //删除目标指标
        fzIndicatorInfoMapper.deleteByReqIds(reqIdList);
        //删除条带
        stripMapper.delStripByReqId(reqIdList);

        //重置任务阶段 现在修改成直接删除任务
        for (FzTaskRpcDTO f : fzTaskRpcDTOS) {
            /* stripBiz.taskStepStateNotice(id, BizTaskStepEnum.TASK_GET, 0);*/
            //先删除目标点
            if (!CollectionUtils.isEmpty(f.getPointDTOList())) {
                List<String> ids = f.getPointDTOList().stream().map(PointRpcDTO::getId).collect(Collectors.toList());
                pointRpcService.deleteByIds(ids);
            }
            //删除缓存中仿真任务步骤信息
            StepDtoMapTool.remove(f.getTaskId());
            //删除任务
            TaskStateNotice taskStateNotice = new TaskStateNotice();
            taskStateNotice.setBizTaskId(f.getReqId());
            fzTaskService.taskStepStateDelete(taskStateNotice);
        }
    }


    /**
     * 指标计算
     *
     * @param req
     * @return
     */
    public FzSystemIndicatorDTO getFzSystemIndicatorDTO(IndicatorReqDTO req) {
        //查询之前先set系统指标
        systemIndicatorInfoUpdateHandler.setSystemIndicatorInfo(req);
        FzSystemIndicatorDTO fzSystemIndicatorDTO = new FzSystemIndicatorDTO();
        fzSystemIndicatorDTO.setSatelliteStorageUtilization(systemIndicatorInfoUpdateHandler.systemIndicatorInfo.getSatelliteStorageUtilization());
        fzSystemIndicatorDTO.setDataPassResourceUtilization(systemIndicatorInfoUpdateHandler.systemIndicatorInfo.getDatePassResourceUtilization());
        fzSystemIndicatorDTO.setSystemIndicatorTime(systemIndicatorInfoUpdateHandler.systemIndicatorInfo.getUpdateDate());
        List<FzTargetIndicatorDTO> fzIndicatorInfos = BizUtils.copyEntityToDto(FzTargetIndicatorDTO.class, fzIndicatorInfoMapper.getFzIndicatorInfoList(req));
        List<String> satelliteCodes = new ArrayList<>();
        if (!CollectionUtils.isEmpty(fzIndicatorInfos)) {
            for (FzTargetIndicatorDTO fzTargetIndicatorDTO : fzIndicatorInfos) {
                fzTargetIndicatorDTO.setSatelliteCodes(Arrays.asList(fzTargetIndicatorDTO.getSatelliteCode().split(",")));
                satelliteCodes.addAll(fzTargetIndicatorDTO.getSatelliteCodes());
            }
        }
        fzSystemIndicatorDTO.setFzTargetIndicatorDTOList(fzIndicatorInfos);
        List<SatelliteEarthLinkAllowanceDTO> satelliteEarthLinkAllowanceDTOS = new ArrayList<>();
        /*   List<String> satelliteCodes = fzIndicatorInfos.stream().map(FzTargetIndicatorDTO::getSatelliteCode).collect(Collectors.toList());
         */

        List<SatelliteRpcDTO> satelliteRpcDTOS = satelliteRpcService.listByIds(satelliteCodes);
        satelliteRpcDTOS.stream().forEach(satelliteRpcDTO -> {
                    Random random = new Random();
                    String totalMemory = satelliteRpcDTO.getTotalMemory();
                    SatelliteEarthLinkAllowanceDTO satelliteEarthLinkAllowanceDTO = new SatelliteEarthLinkAllowanceDTO();
                    satelliteEarthLinkAllowanceDTO.setSatelliteId(satelliteRpcDTO.getId());
                    satelliteEarthLinkAllowanceDTO.setSatelliteCode(satelliteRpcDTO.getSatelliteCode());
                    satelliteEarthLinkAllowanceDTO.setSatelliteEarthSimulationValue(String.valueOf(Integer.valueOf(totalMemory) - random.nextInt(10 + 1) + 1));
                    satelliteEarthLinkAllowanceDTO.setSatelliteEarthDesignValue(totalMemory);
                    satelliteEarthLinkAllowanceDTOS.add(satelliteEarthLinkAllowanceDTO);
                }
        );
        fzSystemIndicatorDTO.setSatelliteEarthLinkAllowanceDTOS(satelliteEarthLinkAllowanceDTOS);
        return fzSystemIndicatorDTO;
    }


    public PlanMakeDTO getPlanMakeDTO(PlanMakeReqDTO req) {
        PlanMakeDTO planMakeDTO = new PlanMakeDTO();
        List<SatellitePlanDTO> satellitePlanDTOList = new ArrayList<>();
        List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOList = new ArrayList<>();
        //这里根据优选表查条带
        List<AreaOptimizationResultInfo> list = stripBiz.getOptimizationStripIds(req.getReqIds().stream().distinct().collect(Collectors.toList()));
        StripDTOListReqDTO stripDTOListReqDTO = new StripDTOListReqDTO();
        List<String> stripIdes = new ArrayList<>();
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : list) {
            areaOptimizationResultInfo.setStripIds(Arrays.asList(areaOptimizationResultInfo.getStripId().split(",")));
            stripIdes.addAll(areaOptimizationResultInfo.getStripIds());
        }
        stripDTOListReqDTO.setIds(stripIdes);
        //查询到所有优选条带信息
        List<StripInfo> stripInfoList = stripMapper.getStripInfoList(stripDTOListReqDTO);
        //载荷信息
        PayloadRpcReqDTO payloadRpcReqDTO = new PayloadRpcReqDTO();
        payloadRpcReqDTO.setPayloadIds(Lists.newArrayList(stripInfoList.stream().map(StripInfo::getPayloadId).collect(Collectors.toSet())));
        List<PayloadRpcDTO> payloadRpcDTOList = payloadRpcService.getPayloadRpcDTOList(payloadRpcReqDTO);

        //  根据条带id查询数传资源
//        List<String> stripIds = stripInfoList.stream().map(StripInfo::getId).collect(Collectors.toList());
//        List<WindowResource> windowResourceList = stripBiz.getOptimizationTrbutIds(stripIds);
        List<WindowResource> windowResourceList = stripBiz.getOptimizationTrbutIds(stripIdes);
        //数传弧段信息
        CxStationRpcDTO cxStationRpcDTO = new CxStationRpcDTO();
        cxStationRpcDTO.setIds(Lists.newArrayList(windowResourceList.stream().map(WindowResource::getResourceId).distinct().collect(Collectors.toList())));
        List<TrbutDataRpcDTO> trbutDataRpcDTOList = trbutDataRpcService.selTrButData(cxStationRpcDTO);

        //  条带对应数传资源
        for (StripInfo stripInfo : stripInfoList) {
            List<DatePassTrbutPlanDTO> datePassTrbutPlanDTO = new ArrayList<>();
            SatellitePlanDTO satellitePlanDTO = formatSatellitePlanDTO(stripInfo, payloadRpcDTOList);
            satellitePlanDTOList.add(satellitePlanDTO);
            //  找到该条带对应的数传站id
            Set<String> resourceIds = windowResourceList.stream().filter(e -> e.getWindowId().equals(stripInfo.getId())).collect(Collectors.toList()).stream().map(WindowResource::getResourceId).collect(Collectors.toSet());
            List<TrbutDataRpcDTO> dataRpcDTOList = trbutDataRpcDTOList.stream().filter(e -> resourceIds.contains(e.getId())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(dataRpcDTOList)) {
                formatDatePassTrbutPlanDTO(datePassTrbutPlanDTO, stripInfo, dataRpcDTOList);
                satellitePlanDTO.setStripIdCountTrbuts(datePassTrbutPlanDTO);
                datePassTrbutPlanDTOList.addAll(datePassTrbutPlanDTO);
            }
        }
        planMakeDTO.setDatePassTrbutPlanDTOList(datePassTrbutPlanDTOList);
        planMakeDTO.setSatellitePlanDTOList(satellitePlanDTOList);
        return planMakeDTO;
    }

    public PlanMakeDTO makePlanMakeDTO(PlanMakeReqDTO req) {
        List<TrbutDataRpcDTO> trbutDataRpcDTOList = trbutDataRpcService.selTrButData(new CxStationRpcDTO());
        PlanMakeDTO planMakeDTO = new PlanMakeDTO();
        List<SatellitePlanDTO> satellitePlanDTOList = new ArrayList<>();
        List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOList = new ArrayList<>();
        List<WindowResource> windowResources = windowResourceService.list();
        List<WindowResource> windowResourcesByReqIds = windowResources.stream().filter(windowResource -> req.getReqIds().contains(windowResource.getReqId())).collect(Collectors.toList());
        List<String> stripIds = windowResourcesByReqIds.stream().map(WindowResource::getWindowId).distinct().collect(Collectors.toList());
        StripDTOListReqDTO stripDTOListReqDTO = new StripDTOListReqDTO();
        stripDTOListReqDTO.setIds(stripIds);
        //查询到所有优选条带信息
        List<StripInfo> stripInfoList = stripMapper.getStripInfoList(stripDTOListReqDTO);

        //载荷信息
        PayloadRpcReqDTO payloadRpcReqDTO = new PayloadRpcReqDTO();
        payloadRpcReqDTO.setPayloadIds(Lists.newArrayList(stripInfoList.stream().map(StripInfo::getPayloadId).collect(Collectors.toSet())));
        List<PayloadRpcDTO> payloadRpcDTOList = payloadRpcService.getPayloadRpcDTOList(payloadRpcReqDTO);

        //  条带对应数传资源
        for (StripInfo stripInfo : stripInfoList) {
            List<DatePassTrbutPlanDTO> datePassTrbutPlanDTO = new ArrayList<>();
            SatellitePlanDTO satellitePlanDTO = formatSatellitePlanDTO(stripInfo, payloadRpcDTOList);
            satellitePlanDTOList.add(satellitePlanDTO);
            //  找到该条带对应的数传站id
            Set<String> resourceIds = windowResourcesByReqIds.stream().filter(e -> e.getWindowId().equals(stripInfo.getId())).collect(Collectors.toList()).stream().map(WindowResource::getResourceId).collect(Collectors.toSet());
            List<TrbutDataRpcDTO> dataRpcDTOList = trbutDataRpcDTOList.stream().filter(e -> resourceIds.contains(e.getId())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(dataRpcDTOList)) {
                formatDatePassTrbutPlanDTO(datePassTrbutPlanDTO, stripInfo, dataRpcDTOList);
                satellitePlanDTO.setStripIdCountTrbuts(datePassTrbutPlanDTO);
                datePassTrbutPlanDTOList.addAll(datePassTrbutPlanDTO);
            }
        }
        planMakeDTO.setDatePassTrbutPlanDTOList(datePassTrbutPlanDTOList);
        planMakeDTO.setSatellitePlanDTOList(satellitePlanDTOList);
        return planMakeDTO;
    }

    private void formatDatePassTrbutPlanDTO(List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOList, StripInfo stripInfo, List<TrbutDataRpcDTO> trbutDataRpcDTOList) {
        for (TrbutDataRpcDTO trbutDataRpcDTO : trbutDataRpcDTOList) {
            DatePassTrbutPlanDTO datePassTrbutPlanDTO = new DatePassTrbutPlanDTO();
            datePassTrbutPlanDTO.setReqId(stripInfo.getReqId());
            datePassTrbutPlanDTO.setSatelliteId(stripInfo.getSatelliteId());
            datePassTrbutPlanDTO.setSatelliteCode(stripInfo.getSatelliteCode());
            datePassTrbutPlanDTO.setResourceId(trbutDataRpcDTO.getId());
            datePassTrbutPlanDTO.setStationCode(trbutDataRpcDTO.getStationId());
            datePassTrbutPlanDTO.setStationName(trbutDataRpcDTO.getStationName());
            datePassTrbutPlanDTO.setDatePassTrbutStartTime(trbutDataRpcDTO.getReceiveStartTime());
            datePassTrbutPlanDTO.setStripId(stripInfo.getId());
            datePassTrbutPlanDTO.setDatePassTrbutEndTime(trbutDataRpcDTO.getReceiveEndTime());
            datePassTrbutPlanDTO.setStationId(trbutDataRpcDTO.getStationId());
            datePassTrbutPlanDTO.setCircleNumber(stripInfo.getCircle());
            datePassTrbutPlanDTOList.add(datePassTrbutPlanDTO);
        }
    }


    private SatellitePlanDTO formatSatellitePlanDTO(StripInfo stripInfo, List<PayloadRpcDTO> payloadRpcDTOList) {
        SatellitePlanDTO satellitePlanDTO = new SatellitePlanDTO();
        satellitePlanDTO.setReqId(stripInfo.getReqId());
        satellitePlanDTO.setStripId(stripInfo.getId());
        satellitePlanDTO.setSatelliteId(stripInfo.getSatelliteId());
        satellitePlanDTO.setSatelliteCode(stripInfo.getSatelliteCode());
        satellitePlanDTO.setRollAngle(stripInfo.getRollAngle());
        satellitePlanDTO.setStartTime(stripInfo.getObservationStartTime());
        satellitePlanDTO.setTargetName(stripInfo.getTargetName());
        satellitePlanDTO.setTargetCode(stripInfo.getTargetId());
        satellitePlanDTO.setPayloadId(stripInfo.getPayloadId());
        satellitePlanDTO.setVisitTime(new Date(stripInfo.getObservationStartTime().getTime() + ((stripInfo.getObservationEndTime().getTime() - stripInfo.getObservationStartTime().getTime()) / 2)));
        satellitePlanDTO.setEndTime(stripInfo.getObservationEndTime());
        satellitePlanDTO.setDuration(getDuration(stripInfo.getObservationEndTime(), stripInfo.getObservationStartTime()).toString());
        Optional<PayloadRpcDTO> payloadRpcDTOOptional = payloadRpcDTOList.stream().filter(e -> e.getId().equals(stripInfo.getPayloadId())).findFirst();
        payloadRpcDTOOptional.ifPresent(payloadRpcDTO -> {
            satellitePlanDTO.setGroundResolution(String.valueOf(payloadRpcDTO.getGroundResolution()));
            satellitePlanDTO.setPayloadType(payloadRpcDTO.getPayloadType());
        });
        return satellitePlanDTO;
    }


    private List<PlanDTO> formatPlanDTOList(List<PlanRelationDTO> planRelationDTOS) {
        List<PlanDTO> list = new ArrayList<>();
        //查询优选条带
        List<AreaOptimizationResultInfo> areaOptimizationResultInfoList = stripBiz.getOptimizationStripIds(planRelationDTOS.stream().map(PlanRelationDTO::getReqId).distinct().collect(Collectors.toList()));
        List<String> stripIds = new ArrayList<>();
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : areaOptimizationResultInfoList) {
            areaOptimizationResultInfo.setStripIds(Arrays.asList(areaOptimizationResultInfo.getStripId().split(",")));
            stripIds.addAll(areaOptimizationResultInfo.getStripIds());
        }
        StripDTOListReqDTO stripDTOListReqDTO = new StripDTOListReqDTO();
        stripDTOListReqDTO.setIds(stripIds);
        List<StripInfo> stripInfoList = stripMapper.getStripInfoList(stripDTOListReqDTO);
        //数传弧段信息
        CxStationRpcDTO cxStationRpcDTO = new CxStationRpcDTO();
        List<WindowResource> windowResourceList = stripBiz.getOptimizationTrbutIds(stripIds);
        cxStationRpcDTO.setIds(windowResourceList.stream().map(WindowResource::getResourceId).distinct().collect(Collectors.toList()));
        List<TrbutDataRpcDTO> trbutDataRpcDTOList = trbutDataRpcService.selTrButData(cxStationRpcDTO);
        //载荷信息
        PayloadRpcReqDTO payloadRpcReqDTO = new PayloadRpcReqDTO();
        payloadRpcReqDTO.setPayloadIds(Lists.newArrayList(planRelationDTOS.stream().map(PlanRelationDTO::getPayloadId).collect(Collectors.toSet())));
        List<PayloadRpcDTO> payloadRpcDTOList = payloadRpcService.getPayloadRpcDTOList(payloadRpcReqDTO);
        for (StripInfo stripInfo : stripInfoList) {
            String resourceId = "";
            Optional<WindowResource> windowResource1 = windowResourceList.stream().filter(windowResource -> windowResource.getWindowId().equals(stripInfo.getId())).findFirst();
            if (windowResource1.isPresent()) {
                resourceId = windowResource1.get().getResourceId();
            }
            for (PlanRelationDTO planRelationDTO : planRelationDTOS) {
                if (planRelationDTO.getReqId().equals(stripInfo.getReqId()) && planRelationDTO.getStripId().equals(stripInfo.getId())) {
                    PlanDTO planDTO = new PlanDTO();
                    planDTO.setTaskId(planRelationDTO.getTaskId());
                    planDTO.setReqId(planRelationDTO.getReqId());
                    planDTO.setTargetCode(stripInfo.getTargetId());
                    planDTO.setTargetName(stripInfo.getTargetName());
                    planDTO.setSatelliteCode(stripInfo.getSatelliteCode());
                    planDTO.setSatelliteId(stripInfo.getSatelliteId());
                    planDTO.setObservationStartTime(stripInfo.getObservationStartTime());
                    planDTO.setObservationDuration(getDuration(stripInfo.getObservationEndTime(), stripInfo.getObservationStartTime()).toString());
                    //  todo  新增条带观测结束时间
                    planDTO.setObservationEndTime(stripInfo.getObservationEndTime());

                    //获取数传信息
                    for (TrbutDataRpcDTO trbutDataRpcDTO : trbutDataRpcDTOList) {
                        if (trbutDataRpcDTO.getId().equals(resourceId)) {
                            planDTO.setDataPassStationCode(trbutDataRpcDTO.getStationId());
                            planDTO.setDataPassStationName(trbutDataRpcDTO.getStationName());
                            planDTO.setDataPassStartTime(trbutDataRpcDTO.getReceiveStartTime());
                            planDTO.setDataPassEndTime(trbutDataRpcDTO.getReceiveEndTime());
                            planDTO.setOrbitNum(String.valueOf(trbutDataRpcDTO.getOrbitNum()));
                            planDTO.setDataPassDuration(planRelationDTO.getDataTransferDuration().toString());
                            break;
                        }
                    }

//                    Optional<TrbutDataRpcDTO> trbutDataRpcDTOOptional = trbutDataRpcDTOList.stream().filter(e -> e.getId().equals(stripInfo.getTrbutId())).findFirst();
//                    if (trbutDataRpcDTOOptional.isPresent()) {
//                        TrbutDataRpcDTO trbutDataRpcDTO = trbutDataRpcDTOOptional.get();
//                        planDTO.setDataPassStationCode(trbutDataRpcDTO.getStationCode());
//                        planDTO.setDataPassStationName(trbutDataRpcDTO.getName());
//                        planDTO.setDataPassStartTime(trbutDataRpcDTO.getReceiveStartTime());
//                        planDTO.setDataPassDuration(getDuration(stripInfo.getObservationEndTime(), stripInfo.getObservationStartTime()) .toString());
//                    }

                    Optional<PayloadRpcDTO> payloadRpcDTOOptional = payloadRpcDTOList.stream().filter(e -> e.getId().equals(stripInfo.getPayloadId())).findFirst();
                    if (payloadRpcDTOOptional.isPresent()) {
                        PayloadRpcDTO payloadRpcDTO = payloadRpcDTOOptional.get();
                        planDTO.setGroundResolution(String.valueOf(payloadRpcDTO.getGroundResolution()));
                        planDTO.setSwathWidth(payloadRpcDTO.getSwathWidth().toString());
                        //  todo  新增查询wx轨道高度
                        List<SatelliteRpcDTO> satelliteRpcDTOS = satelliteRpcService.listByIds(Arrays.asList(payloadRpcDTO.getSatelliteCode()));
                        Optional<SatelliteRpcDTO> satelliteRpcDTO = satelliteRpcDTOS.stream().findFirst();
                        if (satelliteRpcDTO.isPresent()) {
                            planDTO.setAltitude(satelliteRpcDTO.get().getAltitude());
                        }
/*                        List<String> satelliteCodes=new ArrayList<>();
                        satelliteCodes.add(payloadRpcDTO.getSatelliteCode());
                        List<SatelliteRpcDTO> satelliteRpcDTOS = satelliteRpcService.listByIds(satelliteCodes);
                        planDTO.setAltitude(satelliteRpcDTOS.get(0).getAltitude());*/
                    }
                    planDTO.setRollAngle(stripInfo.getRollAngle());
                    list.add(planDTO);
                }
            }
        }
        return list;
    }

    private Integer getDuration(Date startTime, Date endTime) {
        return (int) ((startTime.getTime() - endTime.getTime()) / 1000);
    }
}