rain
2024-05-06 bc16d0f0fc46ca6cb7d9bfb8496ad8f20ffba677
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.dji.sample.territory.service.impl;
 
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.media.model.MediaFileEntity;
import com.dji.sample.patches.config.pojo.PatchesConfigPojo;
import com.dji.sample.patches.model.entity.LotInfo;
import com.dji.sample.patches.utils.GeoToolsUtil;
import com.dji.sample.patches.utils.MultipartFileTOFileUtil;
import com.dji.sample.patches.xml.mode.XMLTemplateModel;
import com.dji.sample.patches.xml.utils.CreateWaylineFileUtils;
import com.dji.sample.territory.dao.ITbDkjbxxMapper;
import com.dji.sample.territory.dao.ITbFjMapper;
import com.dji.sample.territory.model.entity.TbDkjbxxEntity;
import com.dji.sample.territory.model.entity.TbFjEntity;
import com.dji.sample.territory.model.entity.param.UploadUrlParam;
import com.dji.sample.territory.pojo.TerritoryConfigPojo;
import com.dji.sample.territory.service.ITbDkjbxxService;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
import static com.dji.sample.patches.utils.MultipartFileTOFileUtil.convert;
import static com.dji.sample.patches.utils.ZipUtil.zipFolder;
import static com.dji.sample.territory.utils.CoordinateSystemUtil.*;
 
 
/**
 * @PROJECT_NAME: drone
 * @DESCRIPTION:
 * @USER: aix
 * @DATE: 2024/4/10 11:19
 */
@Service
@DS("sqlite-resource")
public class TbDkjbxxServiceImpl extends ServiceImpl<ITbDkjbxxMapper, TbDkjbxxEntity> implements ITbDkjbxxService {
    @Autowired
    private ITbDkjbxxMapper iTbDkjbxxMapper;
    @Autowired
    private TbFjServiceImpl tbFjService;
    @Autowired
    private PatchesConfigPojo patchesConfigPojo;
    @Autowired
    private TerritoryConfigPojo territoryConfigPojo;
 
    /**
     * 上传DB文件并覆盖之前DB文件,自动读取数据到本地数据库
     *
     * @param file
     * @return
     */
    public ResponseResult uploadFile(MultipartFile file) {
        if (file.isEmpty()) {
            throw new IllegalArgumentException("上传文件为空");
        }
        boolean fileName = Objects.requireNonNull(file.getOriginalFilename()).endsWith("db");
        if (!fileName) {
            throw new IllegalArgumentException("检查文件是否为db文件");
        }
        try {
            // 获取上传的文件输入流
            InputStream inputStream = file.getInputStream();
            // 创建输出流,将文件内容写入资源文件
            OutputStream outputStream = new FileOutputStream(territoryConfigPojo.getPath());
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
            return ResponseResult.success("文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.error(e.getMessage());
        }
    }
 
 
    /**
     * 根据给定的工作空间ID、航线名称、机场纬度和经度,生成并返回一个包含航线文件的MultipartFile对象。
     *
     * @param workspaceId 工作空间ID,用于查询相关数据。
     * @param waylineName 航线名称,用于命名生成的文件。
     * @param airportLat  机场纬度,用于地理坐标转换和航迹点排序。
     * @param airportLon  机场经度,用于地理坐标转换和航迹点排序。
     * @return MultipartFile 对象,包含压缩后的航迹文件。
     * @throws IOException 如果文件操作失败,则抛出IOException。
     */
    @Transactional
    public MultipartFile listFile(String workspaceId, String waylineName, double airportLat, double airportLon) throws IOException {
        List<TbDkjbxxEntity> list = iTbDkjbxxMapper.selectList(null);
        List<LotInfo> info = dbConvertToEntity(list);
        Coordinate[] coordinates = GeoToolsUtil.getRoutePointOrder(info, airportLat, airportLon);
        // 创建XML模板模型
        XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates, info);
        CreateWaylineFileUtils.createWaylineFile(xmlModel, patchesConfigPojo.getTemplate(), patchesConfigPojo.getTargetTemplate(), patchesConfigPojo.getWaylines(), patchesConfigPojo.getTargetWaylines());
        // 压缩文件夹中的内容到KMZ文件
        String destKMZFile = patchesConfigPojo.getDestKMZFile() + waylineName + ".kmz"; // 输出的KMZ文件路径
        zipFolder(patchesConfigPojo.getSourceDir(), destKMZFile);
        // 将压缩文件转换为MultipartFile对象
        return convert(new File(destKMZFile));
    }
 
    @Override
    public ResponseResult uploadUrl(String url) {
        if (url.isEmpty()) {
            throw new IllegalArgumentException("上传文件为空");
        }
        downloadFile(url);
        return ResponseResult.success();
    }
    public File downloadFile(String fileUrl) {
        File downloadedFile = null;
        try {
            URL url = new URL(fileUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            InputStream inputStream = connection.getInputStream();
            downloadedFile = new File(territoryConfigPojo.getPath());
            OutputStream outputStream = new FileOutputStream(downloadedFile);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return downloadedFile;
    }
 
    /**
     * 将TbDkjbxxEntity类型的列表转换为LotInfo类型的列表。
     *
     * @param list TbDkjbxxEntity类型的列表,表示数据库中的实体列表。
     * @return List<LotInfo>类型的列表,表示转换后的业务实体列表。
     */
    private List<LotInfo> dbConvertToEntity(List<TbDkjbxxEntity> list) {
        List<LotInfo> infos = new ArrayList<>();
        for (TbDkjbxxEntity file : list) {
            // 使用Builder模式构建LotInfo对象
            LotInfo.LotInfoBuilder builder = LotInfo.builder();
            if (file != null) {
                // 从TbDkjbxxEntity实体中提取信息,构建LotInfo对象
                builder.bsm(file.getFId())
                        .dkbh(file.getFTbbh())
                        .dkfw(file.getFShape())
                        .xzqdm(file.getFXzqdmsys())
                        .build();
                // 将构建好的LotInfo对象添加到infos列表中
                infos.add(builder.build());
            }
        }
        return infos;
    }
}