/*
package com.base.track.schedule;




import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.api.client.PayloadRpcService;
import com.base.api.model.PayloadRpcDTO;
import com.base.api.model.request.PayloadRpcReqDTO;
import com.base.orbit.model.aop.ServiceExceptionCatch;
import com.base.orbit.utils.DateUtils;
import com.base.orbit.utils.StringUtil;
import com.base.track.mapper.SubPointDataMapper;
import com.base.track.model.dto.request.QueryEphListDTO;
import com.base.track.model.dto.request.SubPointRequestDTO;
import com.base.track.model.dto.response.SubPointDataDTO;
import com.base.track.model.dto.response.SubPointSatelliteListByDTO;
import com.base.track.model.entity.EphData;
import com.base.track.model.entity.SubPointData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubPointDataService extends ServiceImpl<SubPointDataMapper,SubPointData> {

    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://192.168.1.188:3306/track_data?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true";
    private static String userName = "root";
    private static String password = "root";

    @Resource
    SubPointDataMapper subPointDataMapper;

    @Resource
    PayloadRpcService payloadRpcService;

    public List<SubPointSatelliteListByDTO> listsub(SubPointRequestDTO subPointRequestDTO){
        List<SubPointSatelliteListByDTO> subPointSatelliteListByDTOS=new ArrayList<>();
        List<String> satelliteCodes=subPointRequestDTO.getSatelliteCodes();
        PayloadRpcReqDTO payloadRpcReqDTO = new PayloadRpcReqDTO();
        payloadRpcReqDTO.setSatelliteCodes(satelliteCodes);
        List<PayloadRpcDTO> payloadRpcDTOList = payloadRpcService.getPayloadRpcDTOList(payloadRpcReqDTO);
        Map<String, List<PayloadRpcDTO>> codePayloadMap = payloadRpcDTOList.stream().collect(Collectors.groupingBy(PayloadRpcDTO::getSatelliteCode));
        for (String satelliteCode: satelliteCodes) {
            SubPointSatelliteListByDTO subPointSatelliteListByDTO=new SubPointSatelliteListByDTO();
            String code=satelliteCode;
            satelliteCode= StringUtil.change(satelliteCode);
            subPointSatelliteListByDTO.setMaxSwathWidth(codePayloadMap.get(code).stream().max(Comparator.comparing(PayloadRpcDTO::getSwathWidth)).get().getSwathWidth().toString());
            List<SubPointData> subPointDataList =subPointDataMapper.getSingleSatelliteSubPointDataList(satelliteCode,DateUtils.StringToDate(subPointRequestDTO.getStartTime()),DateUtils.StringToDate(subPointRequestDTO.getEndTime()));
            if (!CollectionUtils.isEmpty(subPointDataList)){
                List<SubPointDataDTO> subPointDataDTOS= changeToSubPointDataDTO(subPointDataList);
                subPointSatelliteListByDTO.setSatelliteCode(code);
                subPointSatelliteListByDTO.setStartTime(subPointDataList.get(0).getDate());
                subPointSatelliteListByDTO.setEndTime(subPointDataList.get(subPointDataList.size()-1).getDate());
                subPointSatelliteListByDTO.setSubPointDataDTOS(subPointDataDTOS);
                subPointSatelliteListByDTOS.add(subPointSatelliteListByDTO);
            }
        }
        return subPointSatelliteListByDTOS;
    }


    public List<SubPointDataDTO> getSingleSatelliteSubPointDataList(String satelliteCode, Date startTime, Date endTime){
        satelliteCode= StringUtil.change(satelliteCode);
        List<SubPointData> list = subPointDataMapper.getSingleSatelliteSubPointDataList(satelliteCode, startTime, endTime);
        return changeToSubPointDataDTO(list);
    }
    @ServiceExceptionCatch
    public List<SubPointDataDTO> getSubPointDataList(QueryEphListDTO queryEphListDTO){
        queryEphListDTO.setCode(StringUtil.change(queryEphListDTO.getCode()));
        List<SubPointData> list = null;
        if (queryEphListDTO.getPayloadType().equals("ELECTRON")||queryEphListDTO.getPayloadType().equals("AIS")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss");
            try {
                list =  queryEphEle("subpoint_"+queryEphListDTO.getCode(),simpleDateFormat.format(queryEphListDTO.getStartTime()),simpleDateFormat.format(queryEphListDTO.getEndTime()),2,queryEphListDTO.getNorthLat(),queryEphListDTO.getSouthLat(),queryEphListDTO.getEastLon(),queryEphListDTO.getWestLon());

            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }else {
            list =  subPointDataMapper.selectCommonSubPointDataList(queryEphListDTO);
        }
        return changeToSubPointDataDTO(list);
    }


    public static synchronized List<SubPointData> queryEphEle(String tablename, String starttime,
                                                                     String endtime, int updown, double northlat, double southlat, double eastlon, double westlon) throws Exception {

        if ((updown != 0)&& (updown != 1)&&(updown != 2)){

            return null;
        }
// 连接数据库
        Class.forName(driver);
        // url中是否包含useSSL字段，没有则添加设该字段且禁用
        if (!url.contains("?")) {
            url = url + "?useSSL=false";
        } else if (!url.contains("useSSL=false") || !url.contains("useSSL=true")) {
            url = url + "&useSSL=false";
        }
        // 获取连接
        Connection conn = DriverManager.getConnection(url, userName, password);
        // 设置事务为非自动提交
        conn.setAutoCommit(false);
        String sql,sql2;

        List<SubPointData> result = new ArrayList<>();
        List<SubPointData> result0 = new ArrayList<>();
        List<SubPointData> result1 = new ArrayList<>();
        List<SubPointData> result2 = new ArrayList<>();

        if ((updown == 0)||(updown == 2)) {
            double westlonQuery=westlon;
            double eastlonQuery=eastlon;
            int updownint=0;
            sql = "select a.* from " + tablename + " a where a.UPDOWN = '" + updownint + "' and a.date >= str_to_date('"+starttime
                    + "','%Y,%m,%d,%H,%i,%s') and a.date <=  str_to_date('"+endtime+"','%Y,%m,%d,%H,%i,%s') and (a.lr_lat >= " + southlat + " and a.rr_lat <= " + northlat
                    + ") and (a.lr_lon <a.rr_lon and a.rr_lon >= " + westlonQuery + " and a.lr_lon <= " + eastlonQuery
                    + ") order by date asc";

            PreparedStatement pst = conn.prepareStatement(" ");
            ResultSet resultSet1= pst.executeQuery(sql);
            while (resultSet1.next()){
                SubPointData subPointData = new SubPointData();
                Timestamp res =  resultSet1.getTimestamp("date");
                Date res1 = new Date(res.getTime());
                subPointData.setDate(res1);
                subPointData.setLat(resultSet1.getDouble("lat"));
                subPointData.setLon(resultSet1.getDouble("lon"));
                subPointData.setAlt(resultSet1.getDouble("alt"));

                result.add(subPointData);
            }
            conn.commit();
            pst.close();
            result1.addAll(result);
            result.clear();

            //跨子午线
            if (westlon < 0) {
                westlonQuery += 360.0;
            }
            if (eastlon < 0) {
                eastlonQuery += 360.0;
            }
            // 异常情况 lr_lon > rr_lon 跨子午线

            sql2 = "select a.* from " + tablename + " a where a.UPDOWN = '" + updownint + "' and a.date >=  str_to_date('"
                    +  starttime+"','%Y,%m,%d,%H,%i,%s') and a.date <=  str_to_date('"+endtime+"','%Y,%m,%d,%H,%i,%s') and (a.lr_lat >= " + southlat
                    + " and a.rr_lat <= " + northlat + ") and ((a.lr_lon >a.rr_lon )and ((a.rr_lon+360.0) >= " + westlonQuery
                    + ") and (a.lr_lon <= " + eastlonQuery + ")) order by date asc";

            PreparedStatement pst1 = conn.prepareStatement(" ");
            ResultSet resultSet2= pst1.executeQuery(sql2);
            while (resultSet2.next()){
                SubPointData subPointData = new SubPointData();

                Timestamp res =  resultSet2.getTimestamp("date");
                Date res1 = new Date(res.getTime());
                subPointData.setDate(res1);
                subPointData.setLat(resultSet2.getDouble("lat"));
                subPointData.setLon(resultSet2.getDouble("lon"));
                subPointData.setAlt(resultSet2.getDouble("alt"));

                result.add(subPointData);
            }
            result1.addAll(result);
            result.clear();
        }


        if ((updown == 1)||(updown == 2)) {
            double westlonQuery = westlon;
            double eastlonQuery = eastlon;
            int updownint = 1;
            sql = "select a.* from " + tablename + " a where a.UPDOWN = '" + updownint + "' and a.date >=  str_to_date('"+starttime
                    + "','%Y,%m,%d,%H,%i,%s') and a.date <=  str_to_date('"+endtime+"','%Y,%m,%d,%H,%i,%s') and (a.rr_lat >= " + southlat + " and a.lr_lat <= " + northlat
                    + ") and (a.lr_lon <a.rr_lon and a.rr_lon >= " + westlonQuery + " and a.lr_lon <= " + eastlonQuery
                    + ") order by date asc";
            PreparedStatement pst2 = conn.prepareStatement(" ");
            ResultSet resultSet3 = pst2.executeQuery(sql);
            while (resultSet3.next()) {
                SubPointData subPointData = new SubPointData();
                Timestamp res =  resultSet3.getTimestamp("date");
                Date res1 = new Date(res.getTime());
                subPointData.setDate(res1);
                subPointData.setLat(resultSet3.getDouble("lat"));
                subPointData.setLon(resultSet3.getDouble("lon"));
                subPointData.setAlt(resultSet3.getDouble("alt"));

                result.add(subPointData);
            }
            result0.addAll(result);
            result.clear();

            //跨子午线
            if (westlon < 0) {
                westlonQuery += 360.0;
            }
            if (eastlon < 0) {
                eastlonQuery += 360.0;
            }
            // 异常情况 lr_lon > rr_lon 跨子午线

            sql2 = "select a.* from " + tablename + " a where a.UPDOWN = '" + updownint + "' and a.date >=  str_to_date('"
                    + starttime+"','%Y,%m,%d,%H,%i,%s') and a.date <=  str_to_date('"+endtime+"','%Y,%m,%d,%H,%i,%s') and (a.rr_lat >= " + southlat
                    + " and a.lr_lat <= " + northlat + ") and ((a.lr_lon >a.rr_lon )and ((a.rr_lon+360.0) >= " + westlonQuery
                    + ") and (a.lr_lon <= " + eastlonQuery + ")) order by date asc";

            PreparedStatement pst3 = conn.prepareStatement(" ");
            ResultSet resultSet4 = pst3.executeQuery(sql2);
            while (resultSet4.next()) {
                SubPointData subPointData = new SubPointData();
                Timestamp res =  resultSet4.getTimestamp("date");
                Date res1 = new Date(res.getTime());
                subPointData.setDate(res1);
                subPointData.setLat(resultSet4.getDouble("lat"));
                subPointData.setAlt(resultSet4.getDouble("alt"));
                subPointData.setLon(resultSet4.getDouble("lon"));
                result.add(subPointData);
            }
            result0.addAll(result);
            result.clear();
        }

        result2.addAll(result1);
        result2.addAll(result0);

        conn.close();


        result2.sort(Comparator.comparing(SubPointData::getDate));

        return result2;

    }


    List<SubPointDataDTO> changeToSubPointDataDTO(List<SubPointData> list){
        List<SubPointDataDTO> res = new ArrayList<>();
        for (SubPointData subPointData:list){
            SubPointDataDTO subPointDataDTO = new SubPointDataDTO();
            BeanUtils.copyProperties(subPointData,subPointDataDTO);
            res.add(subPointDataDTO);
        }
        return res;
    }

    public List<SubPointDataDTO> getSubPointListForStripExcute(QueryEphListDTO queryEphListDTO) throws Exception{
        queryEphListDTO.setCode(StringUtil.change(queryEphListDTO.getCode()));
        List<SubPointData> list = null;
        if (queryEphListDTO.getPayloadType().equals("ELECTRON")||queryEphListDTO.getPayloadType().equals("AIS")){
            //电子星 暂时不用写
        }else {
            list = queryEphVrss2(queryEphListDTO);
        }
        return changeToSubPointDataDTO(list);
    }


    public  List<SubPointData> queryEphVrss2(QueryEphListDTO queryEphListDTO) throws Exception {
        List<SubPointData> subPointDataListall=new ArrayList<>();
        List<SubPointData> subPointDataListall1=new ArrayList<>();
        List<SubPointData> subPointDataListall2=new ArrayList<>();
        String starttime=DateUtils.DateToString(queryEphListDTO.getStartTime());
        String endtime=DateUtils.DateToString(queryEphListDTO.getEndTime());
        int updown=queryEphListDTO.getUpDown();
        double northlat=queryEphListDTO.getNorthLat();
        double southlat=queryEphListDTO.getSouthLat();
        double eastlon=queryEphListDTO.getEastLon();
        double westlon=queryEphListDTO.getWestLon();
        if ((updown != 0)&& (updown != 1)&&(updown != 2)){
            return null;
        }
        if ((updown == 1)||(updown == 2)) {
            double westlonQuery=westlon;
            double eastlonQuery=eastlon;
            int updownint=1;
            List<SubPointData> subPointDataList1=subPointDataMapper.selectSubPointDataListforStrip1(
                    queryEphListDTO.getCode()
                    ,updownint
                    ,starttime
                    ,endtime
                    ,southlat
                    ,northlat
                    ,westlonQuery
                    ,eastlonQuery
                    );
            subPointDataListall1.addAll(subPointDataList1);



            //跨子午线
            if (westlon < 0) {
                westlonQuery += 360.0;
            }
            if (eastlon < 0) {
                eastlonQuery += 360.0;
            }
            // 异常情况 lr_lon > rr_lon 跨子午线
            List<SubPointData> subPointDataList2=subPointDataMapper.selectSubPointDataListforStrip2(
                    queryEphListDTO.getCode()
                    ,updownint
                    ,starttime
                    ,endtime
                    ,southlat
                    ,northlat
                    ,westlonQuery
                    ,eastlonQuery
            );
            subPointDataListall1.addAll(subPointDataList2);

            if (updown == 1) {
                //按eph_time先后排序
                listSort(subPointDataListall1);
                return subPointDataListall1;
            }
        }

        if ((updown == 0)||(updown == 2)) {
            double westlonQuery=westlon;
            double eastlonQuery=eastlon;
            int updownint=0;

            List<SubPointData> subPointDataList1=subPointDataMapper.selectSubPointDataListforStrip3(
                    queryEphListDTO.getCode()
                    ,updownint
                    ,starttime
                    ,endtime
                    ,southlat
                    ,northlat
                    ,westlonQuery
                    ,eastlonQuery
            );
            subPointDataListall2.addAll(subPointDataList1);

            //跨子午线
            if (westlon < 0) {
                westlonQuery += 360.0;
            }
            if (eastlon < 0) {
                eastlonQuery += 360.0;
            }
            // 异常情况 lr_lon > rr_lon 跨子午线

            List<SubPointData> subPointDataList2=subPointDataMapper.selectSubPointDataListforStrip4(
                    queryEphListDTO.getCode()
                    ,updownint
                    ,starttime
                    ,endtime
                    ,southlat
                    ,northlat
                    ,westlonQuery
                    ,eastlonQuery
            );
            subPointDataListall2.addAll(subPointDataList2);

            if (updown == 0) {
                //按eph_time先后排序
                listSort(subPointDataListall2);
                return subPointDataListall2;
            }
        }
        subPointDataListall.addAll(subPointDataListall1);
        subPointDataListall.addAll(subPointDataListall2);
        //按eph_time先后排序
        listSort(subPointDataListall);
        return subPointDataListall;
    }

    public  void listSort(List<SubPointData> subPointDatas) throws Exception{

        Collections.sort(subPointDatas,new Comparator<SubPointData>(){

            public int compare(SubPointData o1,SubPointData o2){

                Date d1=o1.getDate();
                Date d2=o2.getDate();
                long l1=d1.getTime();
                long l2=d2.getTime();

                if (l1<l2) {
                    return -1;
                } else {
                    return 1;
                }

            }
        });
    }

    public List<SubPointDataDTO> getSubPointDataLists(String satelliteCode, Date startTime, Date endTime){
        satelliteCode= StringUtil.change(satelliteCode);
        List<SubPointData> list = subPointDataMapper.getSubPointDataLists(satelliteCode,startTime,endTime);
        return changeToSubPointDataDTO(list);
    }

}

*/
