package com.base.biz.service;

import com.base.biz.mapper.SatelliteAppendixMapper;
import com.base.biz.model.dto.LinkMarginParamDTO;
import com.base.biz.model.entity.SatelliteAppendix;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 *
 *
 * @author yc
 * @date 2023-02-24
 */
@Service
public class SatelliteAppendixService {

    private final Double re=6378.0;


    @Resource
    private SatelliteAppendixMapper satelliteAppendixMapper;

    public List<SatelliteAppendix> getList(List<String> satelliteCode){
        return satelliteAppendixMapper.getList(satelliteCode);
    }
    public void removeById(String id){
         satelliteAppendixMapper.removeById(id);
    }

    public void updateAddById(SatelliteAppendix satelliteAppendix){
        List<SatelliteAppendix> satelliteAppendices = getList(Lists.newArrayList(satelliteAppendix.getSatelliteCode()));
        LinkMarginParamDTO linkMarginParamDTO = new LinkMarginParamDTO();
        BeanUtils.copyProperties(satelliteAppendix, linkMarginParamDTO);
        Double linkValue = getLinkMarginResult(linkMarginParamDTO);
        satelliteAppendix.setLinkMargin(linkValue);
        if (!CollectionUtils.isEmpty(satelliteAppendices)){
            satelliteAppendixMapper.update(satelliteAppendix);
        }else {
            saveSatelliteAppendix(satelliteAppendix);
        }
    }

    public void saveSatelliteAppendix(SatelliteAppendix satelliteAppendix){
        satelliteAppendixMapper.save(satelliteAppendix);
    }


    public Double getLinkMarginResult(LinkMarginParamDTO linkMarginParam){
        Double eirp = linkMarginParam.getEirp();
        Double f = linkMarginParam.getF();
        Double h = linkMarginParam.getH();
        Double elevation = linkMarginParam.getElevation();


        Double l1 = linkMarginParam.getL1();
        Double l2 = linkMarginParam.getL2();
        Double l3 = linkMarginParam.getL3();
        Double l4 = linkMarginParam.getL4();
        Double gt = linkMarginParam.getGt();
        Double k = linkMarginParam.getK();

        Double fb = linkMarginParam.getFb();
        Double gCodfing = linkMarginParam.getGCodfing();
        Double snr = linkMarginParam.getSnr();

        //星地距离
        double d=Math.sqrt(Math.pow(re,2)*Math.pow(Math.sin(elevation),2)+2*h*re+Math.pow(h,2))-re*Math.sin(elevation);
        //自由空间损失
        double lPath=20*Math.log10(d)*Math.pow(10,3)+20*Math.log10(f)*Math.pow(10,6)+20*Math.log10(4*Math.PI/3*Math.pow(10,8));
        //C/NO
        double cNO=eirp+gt-lPath-l1-l2-l3-l4-k;
        //Eb/No
        double ebNo=cNO-20*Math.log10(fb);

        return ebNo+gCodfing-snr;

    }
}

