package com.djhk.uav.airspace.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.djhk.common.core.domain.entity.SysDept;
import com.djhk.common.core.domain.entity.SysDictData;
import com.djhk.common.core.domain.entity.SysUser;
import com.djhk.common.exception.UtilException;
import com.djhk.common.utils.DictUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.StringUtils;
import com.djhk.system.service.ISysDeptService;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.airspace.domain.*;
import com.djhk.uav.airspace.mapper.AirplaneAirspaceMapper;
import com.djhk.uav.airspace.mapper.AirplaneFlyMapper;
import com.djhk.uav.airspace.mapper.AirplaneMapper;
import com.djhk.uav.airspace.mapper.AirplaneOperatorMapper;
import com.djhk.uav.airspace.service.AirplaneService;
import com.djhk.uav.airspace.service.AirspaceService;
import com.djhk.uav.airspace.service.AirspaceWaypointService;
import com.djhk.uav.equ.domain.EquInfoEntity;
import com.djhk.uav.equ.service.EquInfoService;
import com.djhk.uav.label.domain.Plotting;
import com.djhk.uav.label.mapper.PlottingMappers;
import com.djhk.uav.label.service.LabelService;
import com.djhk.uav.uav.service.IUavOperatorService;
import com.djhk.uav.utils.ExportWord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class AirplaneImpl implements AirplaneService {

    @Autowired
    private AirplaneMapper airplaneMapper;
    @Autowired
    private AirplaneFlyMapper airplaneFlyMapper;
    @Autowired
    private AirplaneAirspaceMapper airspaceMapper;
    @Autowired
    private AirplaneOperatorMapper airplaneOperatorMapper;
    @Resource
    private IUavOperatorService uavOperatorService;
//    @Autowired
//    private RemoteDeptService remoteDeptService;

    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private EquInfoService equInfoService;
//    @Autowired
//    private RemoteUserService userService;
    @Resource
    private AirspaceService airspaceService;
    @Resource
    private AirspaceWaypointService airspaceWaypointService;
    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private LabelService labelService;
    @Autowired
    private PlottingMappers plottingMapper;






    /**
     * 跟根据id查询申请详情
     * @param id 空域申请主键
     * @return
     */
    @Override
    public AirplaneApply selectAirplaneApplyById(Long id) {
        AirplaneApply airplaneApply = airplaneMapper.selectAirplaneApplyById(id);
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        Boolean S= "admin".equals(sysUser.getUserName());
        if ("admin".equals(SecurityUtils.getUsername()) || "CQSGAJ".equals(SecurityUtils.getUsername())){
            airplaneApply.setIsShowStatus(true);
        }else {
            airplaneApply.setIsShowStatus(false);
        }
        List<String> equCodes = new ArrayList<>();
        // 根据飞行申请查询该飞行申请的关联飞行器主键
        List<AirplaneApplyAirplane> airplaneApplyAirplanes = airplaneFlyMapper.selectAirplaneFlyApplyList(id);
        for (int i = 0; i < airplaneApplyAirplanes.size(); i++) {
            EquInfoEntity equInfoEntity = equInfoService.selectEquInfoById(airplaneApplyAirplanes.get(i).getAirplaneId().toString());

            if (equInfoEntity != null) {
                try {
                    List<SysDictData> sysDictDataList = DictUtils.getDictDataByType("equ_model_info");

                        // modelId转
                        if (sysDictDataList != null) {
                            for (SysDictData sysDictData : sysDictDataList) {
                                if (StringUtils.equals(equInfoEntity.getModelId(), String.valueOf(sysDictData.getDictValue()))) {
                                    equInfoEntity.setModelId(sysDictData.getDictLabel());
                                    break;
                                }
                            }
                        }

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

            }
            equCodes.add(equInfoEntity.getModelId());
        }
        airplaneApply.setUavInfoName(equCodes);
        // 根据飞行申请查询该飞行申请的关联飞手主键
        List<String> uavOperators = new ArrayList<>();
        List<AirplaneApplyOperator> airplaneApplyOperators = airplaneOperatorMapper.selectAirplaneOperatorApplyList(id);
        for (int i = 0; i < airplaneApplyOperators.size(); i++) {
            UavOperator uavOperator = uavOperatorService.selectUavOperatorById(airplaneApplyOperators.get(i).getOperatorId());
            if (uavOperator!=null && uavOperator.getUserName()!=null){
                uavOperators.add(uavOperator.getUserName());
            }
        }
        airplaneApply.setUavOperatorName(uavOperators);
        // 根据飞行申请查询该飞行申请的关联空域
        List<String> airspaces = new ArrayList<>();
        List<AirplaneApplyAirspace> airplaneApplyAirspaces = airspaceMapper.selectAirplaneAirspaceApplyList(id);
        for (int i = 0; i < airplaneApplyAirspaces.size(); i++) {
            AirspaceApply airspaceApply = airspaceService.selectAirspaceApplyById(airplaneApplyAirspaces.get(i).getAirspaceId());
            if (airspaceApply!=null && airspaceApply.getName()!=null){
                airspaces.add(airspaceApply.getName());
            }
        }
        airplaneApply.setAirspaceName(airspaces);
        return airplaneApply;
    }


    /**
     * 查询才新增申请的id
     * @return
     */
//    @Override
//    public Long selectAirplaneApplyId() {
//        return airplaneMapper.selectAirplaneApplyId();
//    }


    /**
     * 查询列表
     * @param airplaneApply
     * @return
     */
    @Override
    public List<AirplaneApply> selectAirplaneApplyList(AirplaneApply airplaneApply) {
        List<AirplaneApply> list = airplaneMapper.selectAirplaneApplyList(airplaneApply);
        if (list.size()>0){
            for (int i = 0; i < list.size(); i++) {
//                SysDept dept = remoteDeptService.getDeptInfo(list.get(i).getDeptId() , "inner").getData();
//                SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
                SysDept dept = deptService.selectDeptById(list.get(i).getDeptId());
                list.get(i).setDept(dept);
                if (list.get(i).getCreateBy() ==  SecurityUtils.getUsername() && list.get(i).getSts().equals("4")){
                    list.get(i).setHandle("1");
                }
                if (SecurityUtils.getDeptId() == 1L){
                    list.get(i).setHandle("1");
                }
            }
        }
        return list;
    }

    @Override
    public void exportAirplaneApply(HttpServletResponse response, Long id) throws Exception {

        AirplaneApply apply = airplaneMapper.selectAirplaneApplyById(id);
        Map<String, Object> dataMap = new HashMap<>();
        // 部门名称
//        SysDept dept = remoteDeptService.getDeptInfo(apply.getDeptId(), "inner").getData();
//        dataMap.put("deptName",dept.getDeptName());
        SysDept dept = deptService.selectDeptById(apply.getDeptId());
        dataMap.put("deptName",dept.getDeptName());
        // 任务性质 0=飞行任务,1=飞行训练
        if(apply.getTaskProp()=="0"){
            dataMap.put("taskProp","飞行任务");
        }else {
            dataMap.put("taskProp","飞行训练");
        }
        // 申请飞手
        List<String> uavOperators = new ArrayList<>();
        List<AirplaneApplyOperator> airplaneApplyOperators = airplaneOperatorMapper.selectAirplaneOperatorApplyList(apply.getId());
        String operator = null;
        if (airplaneApplyOperators.size()>0){
            for (int i = 0; i < airplaneApplyOperators.size(); i++) {
                UavOperator uavOperator = uavOperatorService.selectUavOperatorById(airplaneApplyOperators.get(i).getOperatorId());
                uavOperators.add(uavOperator.getUserName()+":"+uavOperator.getTel());
                if (operator==null){
                    operator = uavOperator.getUserName()+":"+uavOperator.getTel()+",";
                }else {
                    operator = operator+uavOperator.getUserName()+":"+uavOperator.getTel()+",";
                }
            }
        }
//        dataMap.put("operator",uavOperators.toString());
        dataMap.put("operator",operator);
        // 申请无人机
        // 根据飞行申请查询该飞行申请的关联飞行器主键
        List<String> equCode = new ArrayList<>();
        List<AirplaneApplyAirplane> airplaneApplyAirplanes = airplaneFlyMapper.selectAirplaneFlyApplyList(apply.getId());
        String equ =null;
        if (airplaneApplyAirplanes.size()>0){
            for (int i = 0; i < airplaneApplyAirplanes.size(); i++) {
                EquInfoEntity equInfoEntity = equInfoService.selectEquInfoById(airplaneApplyAirplanes.get(i).getAirplaneId().toString());
                if (equInfoEntity != null) {
                    try {
                        List<SysDictData> sysDictDataList = DictUtils.getDictDataByType("equ_model_info");

                        // modelId转
                        if (sysDictDataList != null) {
                            for (SysDictData sysDictData : sysDictDataList) {
                                if (StringUtils.equals(equInfoEntity.getModelId(), String.valueOf(sysDictData.getDictValue()))) {
                                    equInfoEntity.setModelId(sysDictData.getDictLabel());
                                    break;
                                }
                            }
                        }

                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }

                }
                equCode.add(equInfoEntity.getModelId()+":"+equInfoEntity.getEquSn());
                if (equ==null){
                    equ=equInfoEntity.getModelId()+":"+equInfoEntity.getEquSn()+",";
                }else {
                    equ=equInfoEntity.getModelId()+":"+equInfoEntity.getEquSn()+","+equ;
                }

            }
        }

//        dataMap.put("uav",equCode.toString());
        dataMap.put("uav",equ);


        // 申请空域
        List<String> location = new ArrayList<>();
        List<String> planBeg = new ArrayList<>();
        List<String> planEnd = new ArrayList<>();
        List<String> heightMin = new ArrayList<>();
        List<String> heightMax = new ArrayList<>();
        String rad = null;
        List<AirplaneApplyAirspace> airplaneApplyAirspaces = airspaceMapper.selectAirplaneAirspaceApplyList(apply.getId());
        if (airplaneApplyAirspaces.size()>0){
            for (int i = 0; i < airplaneApplyAirspaces.size(); i++) {
                AirspaceApply airspaceApply = airspaceService.selectAirspaceApplyById(airplaneApplyAirspaces.get(i).getAirspaceId());
                planBeg.add(airspaceApply.getPlanBeg());
                planEnd.add(airspaceApply.getPlanEnd());
                heightMin.add(airspaceApply.getHightMin());
                heightMax.add(airspaceApply.getHightMax());
                Plotting plotting = plottingMapper.selectPlottingByUUId(airspaceApply.getUuid());
                JSONObject json = JSONObject.parseObject(plotting.getData());
                JSONArray jsonArray = json.getJSONObject("geometry").getJSONArray("coordinates");
                if(("1").equals(airspaceApply.getAirspaceType())){
                    Object jsonRad = json.getJSONObject("properties").getJSONObject("style").get("radius");
                    if (rad ==null){
                        rad="以("+jsonArray.get(0)+","+jsonArray.get(1)+")为圆心,半径为"+jsonRad+"米的圆形区域,";
                    }else {
                        rad=rad+"以("+jsonArray.get(0)+","+jsonArray.get(1)+")为圆心,半径为"+jsonRad+"米的圆形区域,";
                    }
//                    if (rad ==null){
//                        rad="以("+airspaceApply.getLng()+","+airspaceApply.getLat()+")为圆心,半径为"+airspaceApply.getRadLength()+"米的圆形区域,";
//                    }else {
//                        rad=rad+"以("+airspaceApply.getLng()+","+airspaceApply.getLat()+")为圆心,半径为"+airspaceApply.getRadLength()+"米的圆形区域,";
//                    }
                }else {
                    String loc ="";
                    for (int i1 = 0; i1 < jsonArray.getJSONArray(0).size(); i1++) {
                        Object Lng = jsonArray.getJSONArray(0).getJSONArray(i1).get(0);
                        Object Lat = jsonArray.getJSONArray(0).getJSONArray(i1).get(1);
                        loc =loc +"("+Lng+","+Lat+"),";
                    }
                    if (rad == null) {
                        rad =loc+"前述点连线范围内部区域,";
                    }else {
                        rad =loc+"前述点连线范围内部区域,"+rad;
                    }



//                    List<AirspaceWaypoint> airspaceWaypoints = airspaceWaypointService.selectAirspaceWaypointList(airspaceApply.getId());
//                    String loc = "("+airspaceApply.getLng()+","+airspaceApply.getLat()+"),";
//                    if (airspaceWaypoints.size()>0){
//                        for (AirspaceWaypoint airspaceWaypoint : airspaceWaypoints) {
//                            loc =loc +"("+airspaceWaypoint.getLng()+","+airspaceWaypoint.getLat()+"),";
//                        }
//                        if (rad == null) {
//                            rad =loc+"前述点连线范围内部区域,";
//                        }else {
//                            rad =loc+"前述点连线范围内部区域,"+rad;
//                        }
//                    }
                }

            }
            dataMap.put("airspace",rad);

            // 计划时间
            // 计划起飞时间
            apply.getFlyTime();
            dataMap.put("time","开始时间:"+planBeg.get(0).toString()+",结束时间:"+planEnd.get(0).toString());
            dataMap.put("height","0~~~"+heightMax.get(0)+"米");
        }



        new ExportWord("UTF-8").exportDoc(response, "申请空域", "airspaceApply.ftl", dataMap, freeMarkerConfigurer);
    }


    /**
     * 导出列表
     * @param airplaneApply
     * @return
     */
    @Override
    public List<AirplaneApplyExport> exportAirplaneApplyList(AirplaneApply airplaneApply) {
        List<AirplaneApplyExport> listAirplaneApply=new ArrayList<>();
        List<AirplaneApply> list = airplaneMapper.selectAirplaneApplyList(airplaneApply);


        if (list.size()>0){
            for (AirplaneApply apply : list) {
                AirplaneApplyExport airplaneApplyExport = new AirplaneApplyExport();
                // 主键
                airplaneApplyExport.setId(apply.getId());
                // 部门名称
//                SysDept dept = remoteDeptService.getDeptInfo(apply.getDeptId() , "inner").getData();
                SysDept dept = deptService.selectDeptById(apply.getDeptId());
                airplaneApplyExport.setDeptName(dept.getDeptName());
                // 飞行申请名称
                airplaneApplyExport.setName(apply.getName());
                // 申请状态
                airplaneApplyExport.setSts(apply.getSts());
                // 任务性质
                airplaneApplyExport.setTaskProp(apply.getTaskProp());
                // 申请无人机
                // 根据飞行申请查询该飞行申请的关联飞行器主键
                List<String> equCode = new ArrayList<>();
                List<AirplaneApplyAirplane> airplaneApplyAirplanes = airplaneFlyMapper.selectAirplaneFlyApplyList(apply.getId());
                for (int i = 0; i < airplaneApplyAirplanes.size(); i++) {
                    EquInfoEntity equInfoEntity = equInfoService.selectEquInfoById(airplaneApplyAirplanes.get(i).getAirplaneId().toString());
                    equCode.add(equInfoEntity.getEquCode()+":"+equInfoEntity.getEquSn()+":"+equInfoEntity.getModelId());
                }
                airplaneApplyExport.setUavInfoNames(equCode.toString());
                // 申请空域
                List<String> airspaceName = new ArrayList<>();
                List<String> location = new ArrayList<>();
                List<String> planBeg = new ArrayList<>();
                List<String> planEnd = new ArrayList<>();
                List<String> heightMin = new ArrayList<>();
                List<String> heightMax = new ArrayList<>();
                List<AirplaneApplyAirspace> airplaneApplyAirspaces = airspaceMapper.selectAirplaneAirspaceApplyList(apply.getId());
                for (int i = 0; i < airplaneApplyAirspaces.size(); i++) {
                    AirspaceApply airspaceApply = airspaceService.selectAirspaceApplyById(airplaneApplyAirspaces.get(i).getAirspaceId());
                    airspaceName.add(airspaceApply.getName());
                    planBeg.add(airspaceApply.getPlanBeg());
                    planEnd.add(airspaceApply.getPlanEnd());
                    heightMin.add(airspaceApply.getHightMin());
                    heightMax.add(airspaceApply.getHightMax());
                    List<AirspaceWaypoint> airspaceWaypoints = airspaceWaypointService.selectAirspaceWaypointList(airspaceApply.getId());
                    for (AirspaceWaypoint airspaceWaypoint : airspaceWaypoints) {
                        String loc ="("+airspaceWaypoint.getLng()+","+airspaceWaypoint.getLat()+")";
                        location.add(loc);
                    }
                }
                // 空域名称
                airplaneApplyExport.setAirspaceNames(airspaceName.toString());
                // 空域经纬度
                airplaneApplyExport.setLocation(location.toString());
                // 空域开始时间
                airplaneApplyExport.setPlanBeg(planBeg.toString());
                // 空域结束时间
                airplaneApplyExport.setPlanEnd(planEnd.toString());
                if (heightMin.size()>0){
                    // 空域最高高度
                    Double maxH = 0.0;
                    for (int i = 0; i < heightMax.size(); i++) {
                    }
                    airplaneApplyExport.setHeightMax(heightMax.get(0));
                    // 空域最低高度
                    airplaneApplyExport.setHeightMin(heightMin.get(0));
                }
                // 申请飞手
                List<String> uavOperators = new ArrayList<>();
                List<AirplaneApplyOperator> airplaneApplyOperators = airplaneOperatorMapper.selectAirplaneOperatorApplyList(apply.getId());
                for (int i = 0; i < airplaneApplyOperators.size(); i++) {
                    UavOperator uavOperator = uavOperatorService.selectUavOperatorById(airplaneApplyOperators.get(i).getOperatorId());
                    uavOperators.add(uavOperator.getUserName()+"-"+uavOperator.getPoliceNo());
                }
                airplaneApplyExport.setUavOperatorNames(uavOperators.toString());

                listAirplaneApply.add(airplaneApplyExport);
            }
        }
        return listAirplaneApply;
    }


    /**
     * 新增
     * @param airplaneApply 空域申请
     * @return
     */
    @Override
    public int insertAirplaneApply(AirplaneApply airplaneApply) {
        int i= airplaneMapper.insertAirplaneApply(airplaneApply);
        Long maxId = airplaneMapper.selectAirplaneApplyId().getId();
        // 根据飞行申请新增该飞行申请的关联飞行器主键
//        for (UavInfo uavInfo : airplaneApply.getUavInfos()) {
//            AirplaneApplyAirplane airplaneApplyAirplane = null;
//            airplaneApplyAirplane.setAirplaneApplyId(applyId);
//            airplaneApplyAirplane.setAirplaneId(uavInfo.getId());
//            airplaneFlyMapper.insertAirplaneFlyApply(airplaneApplyAirplane);
//        }
//        for (String uavName : airplaneApply.getUavInfoName()) {
//            EquInfoEntity equInfoEntity = new EquInfoEntity();
//            equInfoEntity.setModelId(uavName);
//            log.info("uavName的值==============:{}",uavName);
////            List<EquInfoEntity> list = equInfoService.getEquInfoLists(equInfoEntity);
//            List<EquInfoEntity> list = equInfoService.getEquInfos(equInfoEntity);
//            log.info("list返回的值==============:{}",list);
//            Long euqId = Long.valueOf(list.get(0).getEquId());
//            AirplaneApplyAirplane airplaneApplyAirplane = new AirplaneApplyAirplane();
//            airplaneApplyAirplane.setAirplaneApplyId(maxId);
//            airplaneApplyAirplane.setAirplaneId(euqId);
//            airplaneFlyMapper.insertAirplaneFlyApply(airplaneApplyAirplane);
//        }
        for (String equCode : airplaneApply.getAirUAV()) {
            EquInfoEntity equInfoEntity = new EquInfoEntity();
            equInfoEntity.setEquCode(equCode);
            log.info("equCode的值==============:{}",equCode);
//            List<EquInfoEntity> list = equInfoService.getEquInfoLists(equInfoEntity);
            List<EquInfoEntity> list = equInfoService.getEquInfos(equInfoEntity);
            log.info("list返回的值==============:{}",list);
            if (list.size()>0){
                Long euqId = Long.valueOf(list.get(0).getEquId());
                AirplaneApplyAirplane airplaneApplyAirplane = new AirplaneApplyAirplane();
                airplaneApplyAirplane.setAirplaneApplyId(maxId);
                airplaneApplyAirplane.setAirplaneId(euqId);
                airplaneFlyMapper.insertAirplaneFlyApply(airplaneApplyAirplane);
            }

        }
        // 根据飞行申请新增该飞行申请的关联飞手主键
//        for (UavOperator uavOperator : airplaneApply.getUavOperators()) {
//            AirplaneApplyOperator airplaneApplyOperator = null;
//            airplaneApplyOperator.setAirplaneApplyId(applyId);
//            airplaneApplyOperator.setOperatorId(uavOperator.getId());
//            airplaneOperatorMapper.insertAirplaneOperatorApply(airplaneApplyOperator);
//        }
        for (String uavOperatorName : airplaneApply.getUavOperatorName()) {
            UavOperator uavOperator = new UavOperator();
//            SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//            Long deptId = sysUser.getDeptId();
//            Long deptId = SecurityUtils.getDeptId();
//            uavOperator.setDeptId(deptId);
            uavOperator.setUserName(uavOperatorName);
            List<UavOperator> list = uavOperatorService.selectUavOperatorList(uavOperator);
            AirplaneApplyOperator airplaneApplyOperator = new AirplaneApplyOperator();
            airplaneApplyOperator.setAirplaneApplyId(maxId);
            airplaneApplyOperator.setOperatorId(list.get(0).getUserId());
            airplaneOperatorMapper.insertAirplaneOperatorApply(airplaneApplyOperator);
        }
        // 根据飞行申请新增该飞行申请的关联空域主键
//        for (AirspaceApply airspaceApply : airplaneApply.getAirspaceApplies()) {
//            AirplaneApplyAirspace airplaneApplyAirspace = null;
//            airplaneApplyAirspace.setAirplaneApplyId(applyId);
//            airplaneApplyAirspace.setAirspaceId(airspaceApply.getId());
//            airspaceMapper.insertAirplaneAirspaceApply(airplaneApplyAirspace);
//        }
        for (String airspaceName : airplaneApply.getAirspaceName()) {
            AirspaceApply airspaceApply = new AirspaceApply();
//            SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//            airspaceApply.setCreateBy(sysUser.getUserName());
            airspaceApply.setName(airspaceName);
            List<AirspaceApply> list = airspaceService.selectAirspaceApplyList(airspaceApply);
            AirplaneApplyAirspace airplaneApplyAirspace = new AirplaneApplyAirspace();
            airplaneApplyAirspace.setAirplaneApplyId(maxId);
            airplaneApplyAirspace.setAirspaceId(list.get(0).getId());
            airspaceMapper.insertAirplaneAirspaceApply(airplaneApplyAirspace);
        }
        return i;
    }

    /**
     * 根据id更细数据
     * @param airplaneApply 空域申请
     * @return
     */
    @Override
    public int updateAirplaneApply(AirplaneApply airplaneApply) {
        // 先删除原有的关联数据
        airplaneFlyMapper.deleteAirplaneFlyApplyByFlyId(airplaneApply.getId());
        airplaneOperatorMapper.deleteAirplaneApplyByOperatorId(airplaneApply.getId());
        airspaceMapper.deleteAirplaneAirspaceApplyByAirspaceId(airplaneApply.getId());
        // 根据飞行申请新增该飞行申请的关联飞行器主键
//        for (UavInfo uavInfo : airplaneApply.getUavInfos()) {
//            AirplaneApplyAirplane airplaneApplyAirplane = null;
//            airplaneApplyAirplane.setAirplaneApplyId(airplaneApply.getId());
//            airplaneApplyAirplane.setAirplaneId(uavInfo.getId());
//            airplaneFlyMapper.insertAirplaneFlyApply(airplaneApplyAirplane);
//        }
//        for (String uavName : airplaneApply.getUavInfoName()) {
//            EquInfoEntity equInfoEntity = new EquInfoEntity();
//            equInfoEntity.setModelId(uavName);
//            log.info("uavName的值==============:{}",uavName);
////            List<EquInfoEntity> list = equInfoService.getEquInfoLists(equInfoEntity);
//            List<EquInfoEntity> list = equInfoService.getEquInfos(equInfoEntity);
//            log.info("list返回的值==============:{}",list);
//            Long euqId = Long.valueOf(list.get(0).getEquId());
//            AirplaneApplyAirplane airplaneApplyAirplane = new AirplaneApplyAirplane();
//            airplaneApplyAirplane.setAirplaneApplyId(airplaneApply.getId());
//            airplaneApplyAirplane.setAirplaneId(euqId);
//            airplaneFlyMapper.insertAirplaneFlyApply(airplaneApplyAirplane);
//        }
//        if (airplaneApply.getAirUAV()==null || airplaneApply.getUavOperatorName()==null ||  airplaneApply.getAirspaceName()==null){
//            throw new UtilException("重要参数缺失，请检查参数");
//        }
        for (String equCode : airplaneApply.getAirUAV()) {
            EquInfoEntity equInfoEntity = new EquInfoEntity();
            equInfoEntity.setEquCode(equCode);
            log.info("equCode的值==============:{}",equCode);
//            List<EquInfoEntity> list = equInfoService.getEquInfoLists(equInfoEntity);
            List<EquInfoEntity> list = equInfoService.getEquInfos(equInfoEntity);
            log.info("list返回的值==============:{}",list);
            if (list.size()>0){
                Long euqId = Long.valueOf(list.get(0).getEquId());
                AirplaneApplyAirplane airplaneApplyAirplane = new AirplaneApplyAirplane();
                airplaneApplyAirplane.setAirplaneApplyId(airplaneApply.getId());
                airplaneApplyAirplane.setAirplaneId(euqId);
                airplaneFlyMapper.insertAirplaneFlyApply(airplaneApplyAirplane);
            }

        }

        // 根据飞行申请新增该飞行申请的关联飞手主键
//        for (UavOperator uavOperator : airplaneApply.getUavOperators()) {
//            AirplaneApplyOperator airplaneApplyOperator = null;
//            airplaneApplyOperator.setAirplaneApplyId(airplaneApply.getId());
//            airplaneApplyOperator.setOperatorId(uavOperator.getId());
//            airplaneOperatorMapper.insertAirplaneOperatorApply(airplaneApplyOperator);
//        }
        for (String uavOperatorName : airplaneApply.getUavOperatorName()) {
            UavOperator uavOperator = new UavOperator();
//            SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//            Long deptId = sysUser.getDeptId();
            Long deptId = SecurityUtils.getDeptId();
            uavOperator.setDeptId(deptId);
            uavOperator.setUserName(uavOperatorName);
            List<UavOperator> list = uavOperatorService.selectUavOperatorList(uavOperator);
            AirplaneApplyOperator airplaneApplyOperator = new AirplaneApplyOperator();
            airplaneApplyOperator.setAirplaneApplyId(airplaneApply.getId());
            airplaneApplyOperator.setOperatorId(list.get(0).getUserId());
            airplaneOperatorMapper.insertAirplaneOperatorApply(airplaneApplyOperator);
        }
        // 根据飞行申请新增该飞行申请的关联空域主键
//        for (AirspaceApply airspaceApply : airplaneApply.getAirspaceApplies()) {
//            AirplaneApplyAirspace airplaneApplyAirspace = null;
//            airplaneApplyAirspace.setAirplaneApplyId(airplaneApply.getId());
//            airplaneApplyAirspace.setAirspaceId(airspaceApply.getId());
//            airspaceMapper.insertAirplaneAirspaceApply(airplaneApplyAirspace);
//        }
        for (String airspaceName : airplaneApply.getAirspaceName()) {
            AirspaceApply airspaceApply = new AirspaceApply();
//            SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//            airspaceApply.setCreateBy(sysUser.getUserName());
            airspaceApply.setName(airspaceName);
            List<AirspaceApply> list = airspaceService.selectAirspaceApplyList(airspaceApply);
            AirplaneApplyAirspace airplaneApplyAirspace = new AirplaneApplyAirspace();
            airplaneApplyAirspace.setAirplaneApplyId(airplaneApply.getId());
            airplaneApplyAirspace.setAirspaceId(list.get(0).getId());
            airspaceMapper.insertAirplaneAirspaceApply(airplaneApplyAirspace);
        }
        return airplaneMapper.updateAirplaneApply(airplaneApply);
    }

    /**
     * 根据id删除
     * @param id 空域申请主键
     * @return
     */
    @Override
    public int deleteAirplaneApplyById(Long id) {
        return airplaneMapper.deleteAirplaneApplyById(id);
    }

    /**
     * 根据id批量删除
     * @param ids 需要删除的数据主键集合
     * @return
     */
    @Override
    public int deleteAirplaneApplyByIds(Long[] ids) {
        for (Long id : ids) {
            // 先删除原有的关联数据
            airplaneFlyMapper.deleteAirplaneFlyApplyByFlyId(id);
            airplaneOperatorMapper.deleteAirplaneApplyByOperatorId(id);
            airspaceMapper.deleteAirplaneAirspaceApplyByAirspaceId(id);
        }
        return airplaneMapper.deleteAirplaneApplyByIds(ids);
    }
}
