package com.base.plan.service;

import com.base.api.client.FzTaskRpcService;
import com.base.api.client.PointRpcService;
import com.base.api.client.SatelliteRpcService;
import com.base.api.client.StationRpcService;
import com.base.api.model.*;
import com.base.api.model.request.FzTaskInfoRpcReqDTO;
import com.base.api.model.request.PointListRpcReqDTO;
import com.base.api.model.request.QueryStationAddRpcDTO;
import com.base.orbit.utils.BizUtils;
import com.base.orbit.utils.DateUtils;
import com.base.plan.model.dto.*;
import com.base.plan.model.dto.request.FzTaskRunningStateReqDTO;
import com.base.plan.model.dto.request.FzTaskStepStateReqDTO;
import com.base.plan.model.dto.request.PlanMakeReqDTO;
import com.base.plan.model.dto.request.StripDTOListReqDTO;
import com.base.plan.model.entity.AreaOptimizationResultInfo;
import com.base.plan.model.entity.WindowResource;
import com.base.plan.model.enums.TaskStateEnums;
import com.base.plan.service.biz.StripBiz;
import com.google.common.collect.Lists;
import com.plan.api.model.WindowResourceRpcDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FzViewService {

    @Resource
    FzTaskRpcService fzTaskRpcService;
    @Resource
    PlanService planService;
    @Resource
    SatelliteRpcService satelliteRpcService;
    @Resource
    StationRpcService stationRpcService;
    @Resource
    PointRpcService pointRpcService;
    @Resource
    StripService stripService;
    @Resource
    WindowResourceService windowResourceService;

    @Resource
    StripBiz stripBiz;

    public List<TaskStateDTO> getFzTaskStepStateReqDTOList(FzTaskStepStateReqDTO req) {
        FzTaskInfoRpcReqDTO fzTaskInfoReqDTO = new FzTaskInfoRpcReqDTO();
        BeanUtils.copyProperties(req, fzTaskInfoReqDTO);
        List<FzTaskRpcDTO> fzTaskInfoList = fzTaskRpcService.getFzTaskDTOList(fzTaskInfoReqDTO);
        return formatTaskStateDTOList(fzTaskInfoList);
    }

    public FzTaskRunningStateDTO getFzTaskRunningState(FzTaskRunningStateReqDTO req) {
        PlanMakeDTO planMakeDTO = getPlanMakeDTO(req);
        return formatFzTaskRunningStateDTO(planMakeDTO);
    }

    public FzSatelliteRunningStateDTO getFzSatelliteRunningState(FzTaskRunningStateReqDTO req) {
        //设置卫星任务信息
        PlanMakeDTO planMakeDTO = getPlanMakeDTO(req);
        FzSatelliteRunningStateDTO fzSatelliteRunningStateDTO = new FzSatelliteRunningStateDTO();
        List<SatellitePlanDTO> satellitePlanDTOList = planMakeDTO.getSatellitePlanDTOList();
        fzSatelliteRunningStateDTO.setSatellitePlanDTOList(satellitePlanDTOList);
        //卫星按照载荷分类
        Map<String, List<SatellitePlanDTO>> map = satellitePlanDTOList.stream().collect(Collectors.groupingBy(SatellitePlanDTO::getSatelliteCode));
        List<String> satellitecodes = Lists.newArrayList(map.keySet());
        List<SatelliteRpcDTO> satelliteRpcDTOList = satelliteRpcService.listByIds(satellitecodes);
        Map<String, List<SatelliteRpcDTO>> listMap = satelliteRpcDTOList.stream().collect(Collectors.groupingBy(SatelliteRpcDTO::getLoadType));
        List<SatelliteResourceDTO> satelliteResourceDTOList = new ArrayList<>();
        for (Map.Entry<String, List<SatelliteRpcDTO>> entry : listMap.entrySet()) {
            SatelliteResourceDTO satelliteResourceDTO = new SatelliteResourceDTO();
            satelliteResourceDTO.setPayloadType(entry.getKey());
            List<SatellitePayloadSimpleDTO> satellitePayloadSimpleDTOS = new ArrayList<>();
            for (SatelliteRpcDTO satelliteRpcDTO : entry.getValue()) {
                SatellitePayloadSimpleDTO satellitePayloadSimpleDTO = new SatellitePayloadSimpleDTO();
                satellitePayloadSimpleDTO.setSatelliteId(satelliteRpcDTO.getId());
                satellitePayloadSimpleDTO.setPayloadType(satelliteRpcDTO.getLoadType());
                satellitePayloadSimpleDTO.setSatelliteCode(satelliteRpcDTO.getSatelliteCode());
                satellitePayloadSimpleDTOS.add(satellitePayloadSimpleDTO);
            }
            satelliteResourceDTO.setSatellitePayloadSimpleDTOList(satellitePayloadSimpleDTOS);
            satelliteResourceDTOList.add(satelliteResourceDTO);
        }
        fzSatelliteRunningStateDTO.setSatelliteResourceDTOList(satelliteResourceDTOList);
        return fzSatelliteRunningStateDTO;
    }

    public StationDataPassRunningDTO getFzStationRunningState(FzTaskRunningStateReqDTO req) {
        PlanMakeDTO planMakeDTO = makePlanMakeDTO(req);
        //设置地面站参数
        return formatStationDataPassRunningDTO(planMakeDTO);
    }

    public FzResultDetailInfoDTO getFzResultDetailInfoDTO(FzTaskRunningStateReqDTO req) {
        FzResultDetailInfoDTO fzResultDetailInfoDTO = new FzResultDetailInfoDTO();
        //查询所有地面站
        List<QueryStationAddRpcDTO> queryStationAddRpcDTOS = stationRpcService.getStationListByStationIdList(new ArrayList<>());
        List<StationRpcDTO> stationRpcDTOS = BizUtils.copyEntityToDto(StationRpcDTO.class, queryStationAddRpcDTOS);
        //查询任务
        FzTaskInfoRpcReqDTO fzTaskInfoReqDTO = new FzTaskInfoRpcReqDTO();
        fzTaskInfoReqDTO.setStartTime(req.getTaskReqStartTime());
        fzTaskInfoReqDTO.setEndTime(req.getTaskReqEndTime());
        fzTaskInfoReqDTO.setSteps(req.getTaskState());
        List<FzTaskRpcDTO> fzTaskInfoList = fzTaskRpcService.getFzTaskDTOList(fzTaskInfoReqDTO);
        Set<String> reqIds = fzTaskInfoList.stream().map(FzTaskRpcDTO::getReqId).collect(Collectors.toSet());
        //查询条带信息
        List<AreaOptimizationResultInfo> areaOptimizationResultInfoList = stripBiz.getOptimizationStripIds(Lists.newArrayList(reqIds));
        StripDTOListReqDTO stripDTOListReqDTO = new StripDTOListReqDTO();
        List<String> stripIds = new ArrayList<>();
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : areaOptimizationResultInfoList) {
            areaOptimizationResultInfo.setStripIds(Arrays.asList(areaOptimizationResultInfo.getStripId().split(",")));
            stripIds.addAll(areaOptimizationResultInfo.getStripIds());
        }
        stripDTOListReqDTO.setIds(stripIds);
        List<StripDTO> strips = stripService.getStripDTOList(stripDTOListReqDTO);
        //查询目标信息
        List<TargetRpcDTO> targetRpcDTOS = getTargetRpcDTOs(fzTaskInfoList);
        fzResultDetailInfoDTO.setStationRpcDTOList(stationRpcDTOS);
        fzResultDetailInfoDTO.setStripDTOList(strips);
        fzResultDetailInfoDTO.setTargetRpcDTOList(targetRpcDTOS);
        return fzResultDetailInfoDTO;
    }

    private List<TargetRpcDTO> getTargetRpcDTOs(List<FzTaskRpcDTO> fzTaskInfoList) {
        List<TargetRpcDTO> targetRpcDTOS = new ArrayList<>();
        for (FzTaskRpcDTO fzTaskInfo : fzTaskInfoList) {
            TargetRpcDTO targetRpcDTO = new TargetRpcDTO();
            targetRpcDTO.setTargetName(fzTaskInfo.getTargetName());
            targetRpcDTO.setTargetCode(fzTaskInfo.getTargetCode());
            targetRpcDTO.setReqId(fzTaskInfo.getReqId());
            targetRpcDTO.setTaskStartTime(fzTaskInfo.getReqStartTime());
            targetRpcDTO.setTaskEndTime(fzTaskInfo.getReqEndTime());

            PointListRpcReqDTO pointListReqDTO = new PointListRpcReqDTO();
            pointListReqDTO.setParentIds(Lists.newArrayList(targetRpcDTO.getTargetCode()));
            pointListReqDTO.setPointType(PointTypeEnums.TARGET.toString());
            List<PointRpcDTO> pointInfos = pointRpcService.getPointDTOList(pointListReqDTO);
            targetRpcDTO.setPointRpcDTOList(pointInfos);
            targetRpcDTOS.add(targetRpcDTO);
        }
        return targetRpcDTOS;
    }


    private FzTaskRunningStateDTO formatFzTaskRunningStateDTO(PlanMakeDTO planMakeDTO) {
        FzTaskRunningStateDTO fzTaskRunningStateDTO = new FzTaskRunningStateDTO();
        fzTaskRunningStateDTO.setFzTaskRunningInfoDTOList(new ArrayList<>());
        fzTaskRunningStateDTO.setSatellitePlanDTOList(planMakeDTO.getSatellitePlanDTOList());
        fzTaskRunningStateDTO.setSatellitePlanDTOList(planMakeDTO.getSatellitePlanDTOList());
        for (SatellitePlanDTO satellitePlanDTO : planMakeDTO.getSatellitePlanDTOList()) {
            FzTaskRunningInfoDTO fzTaskRunningInfoDTO = new FzTaskRunningInfoDTO();
            fzTaskRunningInfoDTO.setReqId(satellitePlanDTO.getReqId());
            fzTaskRunningInfoDTO.setTaskType("0");
            fzTaskRunningInfoDTO.setTaskStation(satellitePlanDTO.getSatelliteCode());
            fzTaskRunningInfoDTO.setStartTime(satellitePlanDTO.getStartTime());
            fzTaskRunningInfoDTO.setDuration(satellitePlanDTO.getDuration());
            fzTaskRunningInfoDTO.setSatelliteId(satellitePlanDTO.getSatelliteId());
            fzTaskRunningInfoDTO.setRollAngle(satellitePlanDTO.getRollAngle());
            fzTaskRunningStateDTO.getFzTaskRunningInfoDTOList().add(fzTaskRunningInfoDTO);

        }
        for (DatePassTrbutPlanDTO datePassTrbutPlanDTO : planMakeDTO.getDatePassTrbutPlanDTOList()) {
            FzTaskRunningInfoDTO fzTaskRunningInfoDTO = new FzTaskRunningInfoDTO();
            fzTaskRunningInfoDTO.setReqId(datePassTrbutPlanDTO.getReqId());
            fzTaskRunningInfoDTO.setTaskType("1");
            fzTaskRunningInfoDTO.setTaskStation(datePassTrbutPlanDTO.getStationName());
            fzTaskRunningInfoDTO.setStartTime(datePassTrbutPlanDTO.getDatePassTrbutStartTime());
            if (datePassTrbutPlanDTO.getDatePassTrbutEndTime() != null && datePassTrbutPlanDTO.getDatePassTrbutStartTime() != null) {
                fzTaskRunningInfoDTO.setDuration((int) ((datePassTrbutPlanDTO.getDatePassTrbutEndTime().getTime() - datePassTrbutPlanDTO.getDatePassTrbutStartTime().getTime()) / 1000) + "");
            }
            fzTaskRunningStateDTO.getFzTaskRunningInfoDTOList().add(fzTaskRunningInfoDTO);
        }
        return fzTaskRunningStateDTO;
    }

    private void setPointInfo(FzPointViewListDTO fzPointViewListDTO, List<SatellitePlanDTO> satellitePlanDTOList) {
        fzPointViewListDTO.setFzPointViewDTOList(new ArrayList<>());
        List<String> stripIds = satellitePlanDTOList.stream().map(SatellitePlanDTO::getStripId).distinct().collect(Collectors.toList());
        List<String> targetIds = satellitePlanDTOList.stream().map(SatellitePlanDTO::getTargetCode).distinct().collect(Collectors.toList());
        //条带
        FzPointViewDTO fzStripPointViewDTO = formatFzPointViewDTO(PointTypeEnums.STRIP, stripIds);
        fzPointViewListDTO.getFzPointViewDTOList().add(fzStripPointViewDTO);
        //目标
        FzPointViewDTO fzTargetPointViewDTO = formatFzPointViewDTO(PointTypeEnums.TARGET, targetIds);
        fzPointViewListDTO.getFzPointViewDTOList().add(fzTargetPointViewDTO);
    }

    private FzPointViewDTO formatFzPointViewDTO(PointTypeEnums pointTypeEnums, List<String> ids) {
        FzPointViewDTO fzPointViewDTO = new FzPointViewDTO();
        fzPointViewDTO.setPointType(pointTypeEnums.toString());
        switch (pointTypeEnums) {
            case STRIP:
                setFzPointViewValue(PointTypeEnums.STRIP, fzPointViewDTO, ids);
            case TARGET:
                setFzPointViewValue(PointTypeEnums.TARGET, fzPointViewDTO, ids);
            default:
        }
        return fzPointViewDTO;
    }

    private void setFzPointViewValue(PointTypeEnums pointTypeEnums, FzPointViewDTO fzPointViewDTO, List<String> ids) {
        PointListRpcReqDTO pointListRpcReqDTO = new PointListRpcReqDTO();
        pointListRpcReqDTO.setPointType(pointTypeEnums.toString());
        pointListRpcReqDTO.setParentIds(ids);
        List<PointRpcDTO> pointRpcDTOS = pointRpcService.getPointDTOList(pointListRpcReqDTO);
        fzPointViewDTO.setPointDTOList(pointRpcDTOS);
    }

    private StationDataPassRunningDTO formatStationDataPassRunningDTO(PlanMakeDTO planMakeDTO) {
        StationDataPassRunningDTO stationDataPassRunningDTO = new StationDataPassRunningDTO();
        List<String> stationIds = planMakeDTO.getDatePassTrbutPlanDTOList().stream().map(DatePassTrbutPlanDTO::getStationId).distinct().collect(Collectors.toList());
        List<QueryStationAddRpcDTO> queryStationAddRpcDTOS = stationRpcService.getStationListByStationIdList(stationIds);
        //设置基础站信息
        setBaseStationInfo(queryStationAddRpcDTOS, planMakeDTO.getDatePassTrbutPlanDTOList());
        //分组设置站数据
        groupSetStationInfo(stationDataPassRunningDTO, planMakeDTO.getDatePassTrbutPlanDTOList());
        //设置数传站资源数据 TODO
//        List<DataPassProcessListDTO> dataPassProcessListDTOList = getDataPassProcessListDTOList(planMakeDTO.getDatePassTrbutPlanDTOList());
        List<DataPassProcessListDTO> dataPassProcessListDTOList = makeDataPassProcessListDTOList(planMakeDTO.getSatellitePlanDTOList());
        stationDataPassRunningDTO.setDataPassProcessListDTOList(dataPassProcessListDTOList);
        return stationDataPassRunningDTO;
    }

    private List<DataPassProcessListDTO> makeDataPassProcessListDTOList(List<SatellitePlanDTO> satellitePlanDTOList) {
        List<DataPassProcessListDTO> dataPassProcessListDTOList = new ArrayList<>();
        List<String> satelliteCodes = satellitePlanDTOList.stream().map(SatellitePlanDTO::getSatelliteCode).distinct().collect(Collectors.toList());
        List<SatelliteRpcDTO> satelliteRpcDTOList = satelliteRpcService.listByIds(satelliteCodes);
        Map<String, List<SatellitePlanDTO>> listMap = satellitePlanDTOList.stream().collect(Collectors.groupingBy(SatellitePlanDTO::getSatelliteId));
        for (Map.Entry<String, List<SatellitePlanDTO>> entry : listMap.entrySet()) {
            Optional<SatelliteRpcDTO> satelliteRpcDTOOptional = satelliteRpcDTOList.stream().filter(e -> e.getId().equals(entry.getKey())).findFirst();
            if (satelliteRpcDTOOptional.isPresent()) {
                DataPassProcessListDTO dto = new DataPassProcessListDTO();
                //设置卫星信息
                SatelliteRpcDTO satelliteRpcDTO = satelliteRpcDTOOptional.get();
                dto.setSatelliteId(satelliteRpcDTO.getId());
                dto.setSatelliteCode(satelliteRpcDTO.getSatelliteCode());
                dto.setTotalMemory(satelliteRpcDTO.getTotalMemory());
                //设置数传信息
                List<SatellitePlanDTO> satellitePlanDTOS = entry.getValue();
                satellitePlanDTOS.sort(Comparator.comparing(SatellitePlanDTO::getStartTime));
                double totalSize = 0;
                List<DataPassProcessInfoDTO> dataPassProcessInfoDTOS = new ArrayList<>();
                for (SatellitePlanDTO satellitePlanDTO : satellitePlanDTOS) {
                    int count = (int) (satellitePlanDTO.getEndTime().getTime() - satellitePlanDTO.getStartTime().getTime()) / 1000;
                    totalSize += count * Integer.parseInt(satelliteRpcDTO.getDataPassSpeed());
                    List<DatePassTrbutPlanDTO> stripIdCountTrbuts = satellitePlanDTO.getStripIdCountTrbuts();
                    if (!CollectionUtils.isEmpty(stripIdCountTrbuts)) {
                        for (DatePassTrbutPlanDTO datePassTrbutPlanDTO : stripIdCountTrbuts) {
                            DataPassProcessInfoDTO dataPassProcessInfoDTO = new DataPassProcessInfoDTO();
                            dataPassProcessInfoDTO.setDataPassStartTime(datePassTrbutPlanDTO.getDatePassTrbutStartTime());
                            dataPassProcessInfoDTO.setDataPassEndTime(getDataPassTrbutEndTime(datePassTrbutPlanDTO));
//                            dataPassProcessInfoDTO.setDataPassEndTime(datePassTrbutPlanDTO.getDatePassTrbutEndTime());
                            DecimalFormat decimalFormat = new DecimalFormat("#.00");
                            double total = Double.valueOf(count * Integer.parseInt(satelliteRpcDTO.getDataPassSpeed()));
                            String dataSize = decimalFormat.format(total / 1024);
                            dataPassProcessInfoDTO.setDataSize(String.valueOf(dataSize));
                            dataPassProcessInfoDTO.setStationId(datePassTrbutPlanDTO.getStationId());
                            dataPassProcessInfoDTO.setStationName(datePassTrbutPlanDTO.getStationName());
                            dataPassProcessInfoDTOS.add(dataPassProcessInfoDTO);
                        }
                    }
                }
                DecimalFormat decimalFormat = new DecimalFormat("#.00");
                String baseMemory = decimalFormat.format(totalSize / 1024);
                dto.setBaseMemory(baseMemory);
                dto.setDataPassProcessInfoDTOList(dataPassProcessInfoDTOS);
                dataPassProcessListDTOList.add(dto);
            }
        }
        return dataPassProcessListDTOList;
    }

    /**
     * 根据条带id和任务id和地面站id确认唯一数传配置信息
     * 取条带所需数传时长+地面站接收开始时间(默认开始接收即开始数传)得地面站结束时间
     *
     * @param datePassTrbutPlanDTO
     * @return
     */
    public Date getDataPassTrbutEndTime(DatePassTrbutPlanDTO datePassTrbutPlanDTO) {
        List<WindowResourceRpcDTO> windowResourceInfos = windowResourceService.getWindowResourceInfo(Arrays.asList(datePassTrbutPlanDTO.getReqId()));
        WindowResourceRpcDTO windowResourceRpcDTO = windowResourceInfos.stream().filter(windowResourceInfo ->
                windowResourceInfo.getResourceId().equals(datePassTrbutPlanDTO.getResourceId()) && windowResourceInfo.getWindowId().equals(datePassTrbutPlanDTO.getStripId())
        ).collect(Collectors.toList()).get(0);
        Long duration = Long.valueOf(windowResourceRpcDTO.getDataTransferDuration());

        LocalDateTime trbutEndTimeLocalDateTime = DateUtils.DateCaseToLocalDateTime(datePassTrbutPlanDTO.getDatePassTrbutStartTime()).plusSeconds(duration);
        return DateUtils.toDate(trbutEndTimeLocalDateTime);
    }


    private List<DataPassProcessListDTO> getDataPassProcessListDTOList(List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOList) {
        List<DataPassProcessListDTO> dataPassProcessListDTOList = new ArrayList<>();
        List<String> satelliteCodes = datePassTrbutPlanDTOList.stream().map(DatePassTrbutPlanDTO::getSatelliteCode).distinct().collect(Collectors.toList());
        List<SatelliteRpcDTO> satelliteRpcDTOList = satelliteRpcService.listByIds(satelliteCodes);
        Map<String, List<DatePassTrbutPlanDTO>> listMap = datePassTrbutPlanDTOList.stream().collect(Collectors.groupingBy(DatePassTrbutPlanDTO::getSatelliteId));
        for (Map.Entry<String, List<DatePassTrbutPlanDTO>> entry : listMap.entrySet()) {
            Optional<SatelliteRpcDTO> satelliteRpcDTOOptional = satelliteRpcDTOList.stream().filter(e -> e.getId().equals(entry.getKey())).findFirst();
            if (satelliteRpcDTOOptional.isPresent()) {
                DataPassProcessListDTO dto = new DataPassProcessListDTO();
                //设置卫星信息
                SatelliteRpcDTO satelliteRpcDTO = satelliteRpcDTOOptional.get();
                dto.setSatelliteId(satelliteRpcDTO.getId());
                dto.setSatelliteCode(satelliteRpcDTO.getSatelliteCode());
                dto.setTotalMemory(satelliteRpcDTO.getTotalMemory());
                //设置数传信息
                List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOS = entry.getValue();
                datePassTrbutPlanDTOS.sort(Comparator.comparing(DatePassTrbutPlanDTO::getDatePassTrbutStartTime));
                int totalSize = 0;
                List<DataPassProcessInfoDTO> dataPassProcessInfoDTOS = new ArrayList<>();
                for (DatePassTrbutPlanDTO datePassTrbutPlanDTO : datePassTrbutPlanDTOS) {
                    int count = (int) ((datePassTrbutPlanDTO.getDatePassTrbutEndTime().getTime() - datePassTrbutPlanDTO.getDatePassTrbutStartTime().getTime()) / 1000);
                    totalSize += count * Integer.parseInt(satelliteRpcDTO.getDataPassSpeed());
                    DataPassProcessInfoDTO dataPassProcessInfoDTO = new DataPassProcessInfoDTO();
                    dataPassProcessInfoDTO.setDataPassStartTime(datePassTrbutPlanDTO.getDatePassTrbutStartTime());
                    dataPassProcessInfoDTO.setDataPassEndTime(datePassTrbutPlanDTO.getDatePassTrbutEndTime());
                    dataPassProcessInfoDTO.setDataSize(String.valueOf(count * Integer.parseInt(satelliteRpcDTO.getDataPassSpeed())));
                    dataPassProcessInfoDTO.setStationId(datePassTrbutPlanDTO.getStationId());
                    dataPassProcessInfoDTOS.add(dataPassProcessInfoDTO);
                }
                dto.setBaseMemory(Double.toString((double) totalSize / 1024));
                dto.setDataPassProcessInfoDTOList(dataPassProcessInfoDTOS);
                dataPassProcessListDTOList.add(dto);
            }
        }
        return dataPassProcessListDTOList;
    }

    private void groupSetStationInfo(StationDataPassRunningDTO stationDataPassRunningDTO, List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOList) {
        List<StationRunningStateDTO> dmzStationRunningStateDTOList = new ArrayList<>();
        RelayRunningStateDTO relayRunningStateDTO = new RelayRunningStateDTO();
        //记录stationId 时间复杂度O(1)
        Set<String> markStation = new HashSet<>();
        for (DatePassTrbutPlanDTO datePassTrbutPlanDTO : datePassTrbutPlanDTOList) {
            if (!markStation.contains(datePassTrbutPlanDTO.getStationId()) && datePassTrbutPlanDTO.getRelayType() != null) {
                //根据中继类型分组
                switch (datePassTrbutPlanDTO.getRelayType()) {
                    case "0":
                        getStationRunningStateDTO(dmzStationRunningStateDTOList, datePassTrbutPlanDTO);
                        break;
                    case "1":
                        relayRunningStateDTO.setGenerationRelayList(new ArrayList<>());
                        getStationRunningStateDTO(relayRunningStateDTO.getGenerationRelayList(), datePassTrbutPlanDTO);
                        break;
                    case "2":
                        relayRunningStateDTO.setSecondGenerationRelayList(new ArrayList<>());
                        getStationRunningStateDTO(relayRunningStateDTO.getSecondGenerationRelayList(), datePassTrbutPlanDTO);
                        break;
                }
                markStation.add(datePassTrbutPlanDTO.getStationId());
            }
        }
        stationDataPassRunningDTO.setDmzStationRunningStateDTOList(dmzStationRunningStateDTOList);
        stationDataPassRunningDTO.setRelayRunningStateDTO(relayRunningStateDTO);
    }

    private void getStationRunningStateDTO(List<StationRunningStateDTO> list, DatePassTrbutPlanDTO datePassTrbutPlanDTO) {
        StationRunningStateDTO stationRunningStateDTO = new StationRunningStateDTO();
        stationRunningStateDTO.setStationId(datePassTrbutPlanDTO.getStationId());
        stationRunningStateDTO.setStationCode(datePassTrbutPlanDTO.getStationCode());
        stationRunningStateDTO.setStationName(datePassTrbutPlanDTO.getStationName());
        stationRunningStateDTO.setStationType(datePassTrbutPlanDTO.getStationType());
        stationRunningStateDTO.setRelayType(datePassTrbutPlanDTO.getRelayType());
        list.add(stationRunningStateDTO);
    }

    private void setBaseStationInfo(List<QueryStationAddRpcDTO> queryStationAddRpcDTOS, List<DatePassTrbutPlanDTO> datePassTrbutPlanDTOList) {
        datePassTrbutPlanDTOList.forEach(e -> {
            Optional<QueryStationAddRpcDTO> stationRpcDTOOp = queryStationAddRpcDTOS.stream().filter(k -> k.getId().equals(e.getStationId())).findFirst();
            if (stationRpcDTOOp.isPresent()) {
                QueryStationAddRpcDTO stationRpcDTO = stationRpcDTOOp.get();
                e.setStationType(String.valueOf(stationRpcDTO.getResourceType()));
                e.setRelayType(stationRpcDTO.getRelayType().toString());
            }
        });
    }

    public PlanMakeDTO getPlanMakeDTO(FzTaskRunningStateReqDTO reqDTO) {
        PlanMakeReqDTO planMakeReqDTO = new PlanMakeReqDTO();
        planMakeReqDTO.setReqIds(reqDTO.getReqIds());
        return planService.getPlanMakeDTO(planMakeReqDTO);
    }

    public PlanMakeDTO makePlanMakeDTO(FzTaskRunningStateReqDTO reqDTO) {
        PlanMakeReqDTO planMakeReqDTO = new PlanMakeReqDTO();
        planMakeReqDTO.setReqIds(reqDTO.getReqIds());
        return planService.makePlanMakeDTO(planMakeReqDTO);
    }


    private List<TaskStateDTO> formatTaskStateDTOList(List<FzTaskRpcDTO> fzTaskInfoList) {
        List<TaskStateDTO> taskStateDTOList = new ArrayList<>();
        for (FzTaskRpcDTO fzTaskInfo : fzTaskInfoList) {
            TaskStateDTO taskStateDTO = new TaskStateDTO();
            taskStateDTO.setReqId(fzTaskInfo.getReqId());
            taskStateDTO.setTargetId(fzTaskInfo.getTargetCode());
            taskStateDTO.setTargetName(fzTaskInfo.getTargetName());
            taskStateDTO.setTaskStep(fzTaskInfo.getFzTaskStep());
            setTaskStateDTOState(taskStateDTO, fzTaskInfo);
            taskStateDTOList.add(taskStateDTO);
        }
        return taskStateDTOList;
    }

    private void setTaskStateDTOState(TaskStateDTO taskStateDTO, FzTaskRpcDTO fzTaskInfo) {
        //待筹划和执行中都是  执行中
        if (Arrays.asList("0", "1").contains(fzTaskInfo.getFzTaskState())) {
            taskStateDTO.setTaskState("0");
        } else if (fzTaskInfo.getFzTaskState().equals("3")) {
            //如果已经筹划完成那么就以任务的需求时间判断
            Date date = new Date();
            if (date.getTime() >= fzTaskInfo.getReqEndTime().getTime()) {
                taskStateDTO.setTaskState("1");
                taskStateDTO.setTaskStep(TaskStateEnums.TASK_EXECUTE.toString());
            } else {
                taskStateDTO.setTaskState("0");
            }
        } else {
            taskStateDTO.setTaskState("2");
        }

    }

}