package com.base.plan.task.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.api.client.*;
import com.base.api.model.FzTaskRpcDTO;
import com.base.api.model.request.FzTaskInfoRpcReqDTO;
import com.base.api.model.request.PayloadRpcReqDTO;
import com.base.plan.model.dto.request.IndicatorReqDTO;
import com.google.common.collect.Lists;

import java.text.DecimalFormat;
import java.util.Date;

import com.base.api.model.CxStationRpcDTO;
import com.base.api.model.TrbutDataRpcDTO;
import com.base.api.model.request.QueryStationAddRpcDTO;
import com.base.plan.model.entity.AreaOptimizationResultInfo;
import com.base.plan.model.entity.WindowResource;
import com.base.plan.service.WindowResourceService;
import com.base.api.model.SatelliteRpcDTO;
import com.base.plan.mapper.AreaOptimizationResultInfoMapper;
import com.base.plan.mapper.StripMapper;
import com.base.plan.model.dto.request.AreaOptimizationResultReqDTO;
import com.base.plan.model.dto.request.StripDTOListReqDTO;
import com.base.plan.model.entity.StripInfo;
import com.base.plan.model.entity.SystemIndicatorInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

@Component
public class SystemIndicatorInfoUpdateHandler {

    public SystemIndicatorInfo systemIndicatorInfo = new SystemIndicatorInfo();

    @Resource
    StationRpcService stationRpcService;
    @Resource
    SatelliteRpcService satelliteRpcService;
    @Resource
    StripMapper stripMapper;
    @Resource
    WindowResourceService windowResourceService;
    @Resource
    AreaOptimizationResultInfoMapper areaOptimizationResultInfoMapper;
    @Resource
    TrbutDataRpcService trbutDataRpcService;
    @Resource
    FzTaskRpcService fzTaskRpcService;

    public void setSystemIndicatorInfo(IndicatorReqDTO req) {
        FzTaskInfoRpcReqDTO fzTaskInfoRpcReqDTO = new FzTaskInfoRpcReqDTO();
        fzTaskInfoRpcReqDTO.setReqIds(req.getReqIds());
        List<FzTaskRpcDTO> fzTaskDTOList = fzTaskRpcService.getFzTaskDTOList(fzTaskInfoRpcReqDTO);
        FzTaskRpcDTO fzTaskRpcDTO = fzTaskDTOList.get(0);

        List<SatelliteRpcDTO> satelliteRpcDTOList = satelliteRpcService.list();
        double sumMem = satelliteRpcDTOList.stream().map(e -> Double.parseDouble(StringUtils.isEmpty(e.getFixedVolume()) ? "0" : e.getFixedVolume())).reduce(Double::sum).get();
        StripDTOListReqDTO stripDTOListReqDTO = new StripDTOListReqDTO();
        Date now = new Date();
        stripDTOListReqDTO.setStartTime(now);
        stripDTOListReqDTO.setEndTime(new Date(now.getTime() - 86400 * 1000));
        List<StripInfo> stripInfoList = stripMapper.getStripInfoList(stripDTOListReqDTO);
        //  根据条带id集合查询优选结果
        AreaOptimizationResultReqDTO areaOptimizationResultReqDTO = new AreaOptimizationResultReqDTO();
        areaOptimizationResultReqDTO.setStripIds(stripInfoList.stream().map(StripInfo::getId).collect(Collectors.toList()));
        List<AreaOptimizationResultInfo> areaOptimizationResultInfos = areaOptimizationResultInfoMapper.selectAreaOptimizationResultInfoList(areaOptimizationResultReqDTO);

        List<String> stripIds = new ArrayList<>();
        for (AreaOptimizationResultInfo areaOptimizationResultInfo : areaOptimizationResultInfos) {
            areaOptimizationResultInfo.setStripIds(Arrays.asList(areaOptimizationResultInfo.getStripId().split(",")));
            stripIds.addAll(areaOptimizationResultInfo.getStripIds());
        }
        Set<String> stripIdSets = new HashSet<>(stripIds);
        //  筛选出优选条带集合
        stripInfoList = stripInfoList.stream().filter(e -> stripIdSets.contains(e.getId())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(stripInfoList)) {
            systemIndicatorInfo.setSatelliteStorageUtilization(String.valueOf(0));
        } else {
            //  累加条带观测时长   星上资源利用率=条带观测时长*450(载荷速率) / 固存容量/1024
//            Long nowMem = stripInfoList.stream().map(e -> ((e.getObservationEndTime().getTime() - e.getObservationStartTime().getTime()) / 1000)).reduce(Long::sum).get();
//            systemIndicatorInfo.setSatelliteStorageUtilization(String.valueOf(nowMem * 450 / sumMem / 1024));

            //  星上资源利用率=(条带观测时长*载荷速率)/总内存
            Long nowMem = stripInfoList.stream().map(e -> ((e.getObservationEndTime().getTime() - e.getObservationStartTime().getTime()) / 1000)).reduce(Long::sum).get();
            systemIndicatorInfo.setSatelliteStorageUtilization(String.valueOf(nowMem * 450 / 400000));

        }

        List<QueryStationAddRpcDTO> queryStationAddRpcDTOS = stationRpcService.getStationListByStationIdList(new ArrayList<>());
        int baseTime = queryStationAddRpcDTOS.size() * 24 * 60 * 60;
//        QueryWrapper<WindowResource> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("window_id", queryWrapper);
//        List<WindowResource> windowResourceList = windowResourceService.list(queryWrapper);

        List<WindowResource> windowResourceList = windowResourceService.list();
        Set<String> trbutIds = windowResourceList.stream().map(WindowResource::getResourceId).collect(Collectors.toSet());
        CxStationRpcDTO cxStationRpcDTO = new CxStationRpcDTO();
        cxStationRpcDTO.setCxStartTime(fzTaskRpcDTO.getStartTime());
        cxStationRpcDTO.setCxEndTime(new Date(fzTaskRpcDTO.getStartTime().getTime() + 86400 * 1000));
        List<TrbutDataRpcDTO> list = trbutDataRpcService.selTrButData(cxStationRpcDTO);
        list = list.stream().filter(e -> trbutIds.contains(e.getId())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            systemIndicatorInfo.setDatePassResourceUtilization(String.valueOf(0));
        } else {
            Long nowSpeed = list.stream().map(e -> ((e.getReceiveEndTime().getTime() - e.getReceiveStartTime().getTime()) / 1000)).reduce(Long::sum).get();
            double reslut = (double) nowSpeed / baseTime;
            DecimalFormat decimalFormat = new DecimalFormat("#0.0000");
            systemIndicatorInfo.setDatePassResourceUtilization(decimalFormat.format(reslut));
        }
        systemIndicatorInfo.setUpdateDate(new Date());
    }

}