package com.base.biz.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.biz.mapper.PayloadInfoMapper;
import com.base.biz.mapper.SatelliteInfoMapper;
import com.base.biz.model.dto.PayloadInfoDTO;
import com.base.biz.model.dto.request.PayloadInfoDTOListReq;
import com.base.biz.model.dto.request.PayloadInfoDTOListReq2;
import com.base.biz.model.entity.PayloadInfo;
import com.base.biz.model.entity.SatelliteInfo;
import com.base.orbit.utils.BizUtils;
import com.base.orbit.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
public class PayloadService extends ServiceImpl<PayloadInfoMapper, PayloadInfo> {

    @Resource
    PayloadInfoMapper payloadInfoMapper;

    @Resource
    SatelliteInfoMapper satelliteInfoMapper;

    public List<PayloadInfoDTO> getPayloadInfoList(PayloadInfoDTOListReq req) {
        PayloadInfoDTOListReq2 req2 = new PayloadInfoDTOListReq2();
        BeanUtils.copyProperties(req, req2);
        String payloadType = req.getPayloadType();
        if (!StringUtils.isEmpty(payloadType)){
            String[] split = payloadType.split(",");
            List<String> strings = Arrays.asList(split);
            req2.setPayloadType(strings);
        }
        List<PayloadInfo> list = payloadInfoMapper.getPayloadInfoList(req2);
        List<PayloadInfoDTO> payloadInfoDTOS = BizUtils.copyEntityToDto(PayloadInfoDTO.class, list);
        payloadInfoDTOS.stream().forEach(payloadInfoDTO -> {
                    List<SatelliteInfo> satelliteInfos = satelliteInfoMapper.listByIds(Arrays.asList(payloadInfoDTO.getSatelliteCode()));
                    Optional<SatelliteInfo> satelliteInfoOpt = satelliteInfos.stream().findFirst();
                    if (satelliteInfoOpt.isPresent()) {
                        SatelliteInfo satelliteInfo = satelliteInfoOpt.get();
                        payloadInfoDTO.setMaxRollAngle(satelliteInfo.getMaxRollAngle());
                        payloadInfoDTO.setMinRollAngle(satelliteInfo.getMinRollAngle());
                        payloadInfoDTO.setSatelliteName(satelliteInfo.getSatelliteName());
                    }
                }
        );
        return payloadInfoDTOS;
    }

    public List<PayloadInfoDTO> getAllList() {
        List<PayloadInfo> list = this.list();
        return BizUtils.copyEntityToDto(PayloadInfoDTO.class, list);
    }

}
