zhaowenyu
2025-11-06 ef497a4547843e1ade23d80f40bc682dca952ed0
ard-route/src/main/java/com/ard/route/service/impl/UavRouteServiceImpl.java
@@ -13,9 +13,11 @@
import com.ard.route.domain.kml.*;
import com.ard.route.domain.route.ArdRouteInformation;
import com.ard.route.domain.vo.KmzInfoVO;
import com.ard.route.domain.vo.KmzResultVo;
import com.ard.route.enums.kml.ExitOnRCLostEnums;
import com.ard.route.service.IArdRouteInformationService;
import com.ard.route.util.FileUtils;
import com.ard.route.util.InitializationUtils;
import com.ard.route.util.RouteFileUtils;
import com.ard.route.service.UavRouteService;
import com.thoughtworks.xstream.XStream;
@@ -38,21 +40,14 @@
import java.util.zip.ZipOutputStream;
import static com.ard.route.util.RouteFileUtils.*;
import static com.ard.route.util.ToolUtils.extractFilePath;
import static com.ard.route.util.ToolUtils.getFileNameWithoutExtension;
import static com.ard.route.util.ToolUtils.*;
/**
 * Author:songjian
 * Author:xia
 * Date: 2024/12/22 10:36
 **/
@Service
public class UavRouteServiceImpl implements UavRouteService {
    private Boolean isLocal = false;
    private String localFilePath = "KmzFile/kmz/";
    private String kmzPath = "kmz";
    @Resource
    private OssServiceContext ossService;
@@ -60,16 +55,22 @@
    @Resource
    private IArdRouteInformationService ardRouteInformationService;
    /**
     * XML 头部
     */
    private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    InitializationUtils init = new InitializationUtils();
    /**
     * 编辑kmz文件
     */
    @Override
    public String updateKmz(UavRouteReq uavRouteReq) {
        // TODO 替换本地文件路径!!!
        File file = FileUtil.file("/Users/songjian/Project/IdeaProjects/dj-uav/file/kmz/航线kmz文件.kmz");
        try (ArchiveInputStream archiveInputStream = new ZipArchiveInputStream(FileUtil.getInputStream(file))) {
    public String updateKmz(UavRouteReq uavRouteReq,String fileName){
        // 校验修改的文件是否存在
        ArdRouteInformation routeInformation = ardRouteInformationService.selectArdRouteInformationByName(fileName);
        if ( routeInformation == null ){
            return "修改航线文件失败,请检查文件是否存在";
        }
        // 获取文件
        String minioUrl = routeInformation.getMinioUpPath();
        try (InputStream inputStream = ossService.getObject(kmzPath,minioUrl);
             ArchiveInputStream archiveInputStream = new ZipArchiveInputStream(inputStream)){
            ArchiveEntry entry;
            KmlInfo kmlInfo = new KmlInfo();
            KmlInfo wpmlInfo = new KmlInfo();
@@ -85,9 +86,12 @@
                    this.handleRouteUpdate(wpmlInfo, uavRouteReq, FileTypeConstants.WPML, kmlParams);
                }
            }
            return RouteFileUtils.buildKmz("更新航线kmz文件", kmlInfo, wpmlInfo, kmzPath);
            // 删除minio的文件
            // 插入修改好的minio文件
            return RouteFileUtils.buildKmz("更新航线kmz文件111111", kmlInfo, wpmlInfo, kmzPath);
        } catch (Exception e) {
            e.printStackTrace();
            return "更新航线kmz文件失败";
        }
    }
@@ -133,8 +137,13 @@
        }
    }
    /**
     * 生成kmz文件(带航点)
     */
    @Override
    public String buildKmz(UavRouteReq uavRouteReq, String fileName) {
    public KmzResultVo buildKmz(UavRouteReq uavRouteReq, String fileName) {
        // 赋值默认参数
        init.initialization(uavRouteReq);
        KmlParams kmlParams = new KmlParams();
        BeanUtils.copyProperties(uavRouteReq, kmlParams);
        kmlParams.setRoutePointList(BeanUtil.copyToList(uavRouteReq.getRoutePointList(), RoutePointInfo.class));
@@ -148,7 +157,7 @@
     * @param kmlParams 参数对象
     * @return 本地文件路径
     */
    public String buildKmz(String fileName, KmlParams kmlParams) {
    public KmzResultVo buildKmz(String fileName, KmlParams kmlParams) {
        KmlInfo kmlInfo = buildKml(kmlParams);
        KmlInfo wpmlInfo = buildWpml(kmlParams);
        return buildKmz(fileName, kmlInfo, wpmlInfo);
@@ -162,13 +171,16 @@
     * @param wpmlInfo wpml 文件信息
     * @return 本地文件路径
     */
    public String buildKmz(String fileName, KmlInfo kmlInfo, KmlInfo wpmlInfo) {
    public KmzResultVo buildKmz(String fileName, KmlInfo kmlInfo, KmlInfo wpmlInfo) {
        XStream xStream = new XStream(new DomDriver());
        xStream.processAnnotations(KmlInfo.class);
        xStream.addImplicitCollection(KmlActionGroup.class, "action");
        xStream.addImplicitCollection(KmlPlacemark.class, "actionGroup");
        String kml = XML_HEADER + xStream.toXML(kmlInfo);
        String wpml = XML_HEADER + xStream.toXML(wpmlInfo);
        KmzResultVo result = new KmzResultVo();
        // 判断本地生成kmz文件
        if (isLocal) {
@@ -183,7 +195,8 @@
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return localFilePath + fileName + ".kmz";
            result.setUpPath(localFilePath + fileName + ".kmz");
            return result;
        }else {
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 ZipOutputStream zipOutputStream = new ZipOutputStream(baos)) {
@@ -207,27 +220,25 @@
                        new ByteArrayInputStream(baos.toByteArray())
                );
                String kmzFilePath = ossService.upload(kmzPath,multipartFile);
                // KMZ信息管理
                ArdRouteInformation ardRouteInformation = new ArdRouteInformation();
                ardRouteInformation.setFileName(fileName);
                ardRouteInformation.setFileNameNew(getFileNameWithoutExtension(kmzFilePath));
                ardRouteInformation.setMinioUpPath(extractFilePath(kmzFilePath));
                ardRouteInformation.setUpPath(kmzFilePath);
                ardRouteInformation.setUpFileSize(fileSize);
                ardRouteInformation.setCreateBy(SecurityUtils.getUsername());
                ardRouteInformationService.insertArdRouteInformation(ardRouteInformation);
                return kmzFilePath;
                result.setFileNameNew(getFileNameWithoutExtension(kmzFilePath));
                result.setMinioUpPath(extractFilePath(kmzFilePath));
                result.setUpPath(kmzFilePath);
                result.setUpFileSize(fileSize);
                return result;
            } catch (Exception e) {
                throw new RuntimeException("KMZ文件生成或上传失败", e);
            }
        }
    }
    /**
     * 解析kmz文件
     */
    @Override
    public KmzInfoVO parseKmz(String fileUrl) throws IOException {
    public KmzInfoVO parseKmz(String fileUrl){
        KmzInfoVO kmzInfoVO = new KmzInfoVO();
        File file = FileUtils.downloadUrlToTempFile(fileUrl);
        try (ArchiveInputStream archiveInputStream = new ZipArchiveInputStream(FileUtil.getInputStream(file))) {
        try (InputStream inputStream = ossService.getObject(kmzPath,fileUrl);
             ArchiveInputStream archiveInputStream = new ZipArchiveInputStream(inputStream)){
            ArchiveEntry entry;
            while (!Objects.isNull(entry = archiveInputStream.getNextEntry())) {
                String name = entry.getName();
@@ -238,7 +249,7 @@
                }
            }
            return kmzInfoVO;
        } catch (Exception e) {
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;