aix
2024-08-06 7f1e5a861d9944cc28285cd36ee47b33dee04446
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.dji.sample.wayline.controller;
 
import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.common.model.PaginationData;
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.common.util.MinioUrlUtils;
import com.dji.sample.log.aspect.SysLogAnnotation;
import com.dji.sample.wayline.model.dto.WaylineFileDTO;
import com.dji.sample.wayline.model.dto.WaylineFileUploadDTO;
import com.dji.sample.wayline.model.dto.WaylineListDTO;
import com.dji.sample.wayline.model.entity.WaylineFileEntity;
import com.dji.sample.wayline.model.param.WaylineQueryParam;
import com.dji.sample.wayline.service.IWaylineFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
 
import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;
 
/**
 * @author sean
 * @version 0.3
 * @date 2021/12/22
 */
@RestController
@RequestMapping("${url.wayline.prefix}${url.wayline.version}/workspaces")
public class WaylineFileController {
 
    @Autowired
    private IWaylineFileService waylineFileService;
 
    /**
     * 根据查询条件查询航路线文件的基本数据。
     * pilot中的查询条件字段是固定的。
     *
     * @param orderBy      排序的字段。在sql语句的末尾拼接。
     * @param favorited    路径线文件是否为收藏夹。
     * @param page
     * @param pageSize
     * @param templateType
     * @param workspaceId
     * @return
     */
    @GetMapping("/{workspace_id}/waylines")
    @SysLogAnnotation(operModul = "航线库", operType = "查询", operDesc = "根据查询条件查询航路线文件的基本数据")
    public ResponseResult<PaginationData<WaylineFileDTO>> getWaylinesPagination(@RequestParam(name = "order_by") String orderBy,
                                                                                @RequestParam(required = false) boolean favorited, @RequestParam Integer page,
                                                                                @RequestParam(name = "page_size", defaultValue = "10") Integer pageSize,
                                                                                @RequestParam(name = "template_type", required = false) Integer[] templateType,
                                                                                @PathVariable(name = "workspace_id") String workspaceId) {
        WaylineQueryParam param = WaylineQueryParam.builder()
                .favorited(favorited)
                .page(page)
                .pageSize(pageSize)
                .orderBy(orderBy)
                .templateType(templateType)
                .build();
        PaginationData<WaylineFileDTO> data = waylineFileService.getWaylinesByParam(workspaceId, param);
        return ResponseResult.success(data);
    }
 
    @GetMapping("/{workspace_id}/formatWayline")
    @SysLogAnnotation(operModul = "航线库", operType = "查询", operDesc = "根据查询条件查询临时航路线文件的基本数据")
    public ResponseResult<PaginationData<WaylineFileDTO>> getShowWaylinesPagination(@RequestParam(name = "order_by") String orderBy,
                                                                                @RequestParam(required = false) boolean favorited, @RequestParam Integer page,
                                                                                @RequestParam(name = "page_size", defaultValue = "10") Integer pageSize,
                                                                                @RequestParam(name = "template_type", required = false) Integer[] templateType,
                                                                                @PathVariable(name = "workspace_id") String workspaceId) {
        WaylineQueryParam param = WaylineQueryParam.builder()
                .favorited(favorited)
                .page(page)
                .pageSize(pageSize)
                .orderBy(orderBy)
                .templateType(templateType)
                .build();
        PaginationData<WaylineFileDTO> data = waylineFileService.getShowWaylinesByParam(workspaceId, param);
        return ResponseResult.success(data);
    }
 
    /**
     * 根据wayline文件id查询文件的下载地址;
     * 和重定向到此地址直接下载。
     *
     * @param workspaceId
     * @param waylineId
     * @param response
     */
    @GetMapping("/{workspace_id}/waylines/{wayline_id}/url")
    @SysLogAnnotation(operModul = "航线库", operType = "查询", operDesc = "根据wayline文件id查询文件的下载地址1")
    public void getFileUrl(@PathVariable(name = "workspace_id") String workspaceId,
                           @PathVariable(name = "wayline_id") String waylineId, HttpServletResponse response) {
 
        try {
            URL url = waylineFileService.getObjectUrl(workspaceId, waylineId);
            response.sendRedirect(MinioUrlUtils.getUrl(url));
 
        } catch (IOException | SQLException e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/{workspace_id}/getWayline")
    public ResponseResult getWaylineByJobId(
            @PathVariable(name = "workspace_id") String workspaceId,
            @RequestParam String jobId) {
        return ResponseResult.success(waylineFileService.getWaylineByWaylineId(workspaceId,jobId));
    }
 
    @GetMapping("/{workspace_id}/waylines/{wayline_id}/urlData")
    @SysLogAnnotation(operModul = "航线库", operType = "查询", operDesc = "根据wayline文件id查询文件的下载地址2")
    public ResponseResult getFileUrlData(@PathVariable(name = "workspace_id") String workspaceId,
                                         @PathVariable(name = "wayline_id") String waylineId, HttpServletResponse response) {
 
        URL url = null;
        try {
            url = waylineFileService.getObjectUrl(workspaceId, waylineId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return ResponseResult.success(MinioUrlUtils.getUrl(url));
    }
 
    /**
     * 当航路线文件被飞行员上传到存储服务器时,
     * 通过该接口报告文件的基本信息。
     *
     * @param request
     * @param workspaceId
     * @param uploadFile
     * @return
     */
    @PostMapping("/{workspace_id}/upload-callback")
    @SysLogAnnotation(operModul = "航线库", operType = "新增", operDesc = "当航路线文件被飞行员上传到存储服务器时,通过该接口报告文件的基本信息")
    public ResponseResult uploadCallBack(HttpServletRequest request,
                                         @PathVariable(name = "workspace_id") String workspaceId,
                                         @RequestBody WaylineFileUploadDTO uploadFile) {
 
        CustomClaim customClaim = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
 
        WaylineFileDTO metadata = uploadFile.getMetadata();
        metadata.setUsername(customClaim.getUsername());
        metadata.setObjectKey(uploadFile.getObjectKey());
        metadata.setName(uploadFile.getName());
 
        int id = waylineFileService.saveWaylineFile(workspaceId, metadata);
 
        return id <= 0 ? ResponseResult.error() : ResponseResult.success();
    }
 
    /**
     * 根据路径线文件id收藏路径线文件。
     *
     * @param workspaceId
     * @param ids         wayline file id
     * @return
     */
    @PostMapping("/{workspace_id}/favorites")
    @SysLogAnnotation(operModul = "航线库", operType = "修改", operDesc = "根据路径线文件id收藏路径线文件")
    public ResponseResult markFavorite(@PathVariable(name = "workspace_id") String workspaceId,
                                       @RequestParam(name = "id") List<String> ids) {
        boolean isMark = waylineFileService.markFavorite(workspaceId, ids, true);
 
        return isMark ? ResponseResult.success() : ResponseResult.error();
    }
 
    /**
     * 根据航路线文件id删除此航路线文件的收藏夹。
     *
     * @param workspaceId
     * @param ids         wayline file id
     * @return
     */
    @DeleteMapping("/{workspace_id}/favorites")
    @SysLogAnnotation(operModul = "航线库", operType = "删除", operDesc = "根据航路线文件id删除此航路线文件的收藏夹")
    public ResponseResult unmarkFavorite(@PathVariable(name = "workspace_id") String workspaceId,
                                         @RequestParam(name = "id") List<String> ids) {
        boolean isMark = waylineFileService.markFavorite(workspaceId, ids, false);
 
        return isMark ? ResponseResult.success() : ResponseResult.error();
    }
 
    /**
     * 根据航路线名称检查名称是否已经存在,必须保证航路线名称的唯一性。
     * 此接口将在上传航路线时被调用,并且必须可用。
     *
     * @param workspaceId
     * @param names
     * @return
     */
    @GetMapping("/{workspace_id}/waylines/duplicate-names")
    @SysLogAnnotation(operModul = "航线库", operType = "查询", operDesc = "根据航路线名称检查名称是否已经存在")
    public ResponseResult checkDuplicateNames(@PathVariable(name = "workspace_id") String workspaceId,
                                              @RequestParam(name = "name") List<String> names) {
        List<String> existNamesList = waylineFileService.getDuplicateNames(workspaceId, names);
 
        return ResponseResult.success(existNamesList);
    }
 
    /**
     * 根据航路线id删除工作区中的航路线文件。
     *
     * @param workspaceId
     * @param waylineId
     * @return
     */
    @DeleteMapping("/{workspace_id}/waylines/{wayline_id}")
    @SysLogAnnotation(operModul = "航线库", operType = "删除", operDesc = "根据航路线id删除工作区中的航路线文件")
    public ResponseResult deleteWayline(@PathVariable(name = "workspace_id") String workspaceId,
                                        @PathVariable(name = "wayline_id") String waylineId) {
        boolean isDel = waylineFileService.deleteByWaylineId(workspaceId, waylineId);
        return isDel ? ResponseResult.success() : ResponseResult.error("航线删除失败");
    }
 
    /**
     * 上传kmz航线文件
     *
     * @param file
     * @return
     */
    @PostMapping("/{workspace_id}/waylines/file/upload")
    @SysLogAnnotation(operModul = "航线库", operType = "上传", operDesc = "上传kmz航线文件")
    public ResponseResult importKmzFile(@PathVariable(name = "workspace_id") String workspaceId,
                                        HttpServletRequest request, MultipartFile file,
                                        @RequestParam(required = false) String patchesId,
                                        @RequestParam(defaultValue = "1",required = false) String isTemp
    ) {
        if (Objects.isNull(file)) {
            return ResponseResult.error("未上传文件");
        }
        CustomClaim customClaim = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
        String creator = customClaim.getUsername();
        String back = waylineFileService.importKmzFile(file, workspaceId, creator, patchesId,isTemp);
        if (back != null) {
            return ResponseResult.error(back);
        }
        return ResponseResult.success(back);
    }
 
    @GetMapping("/{workspace_id}/waylines_list")
    @SysLogAnnotation(operModul = "航线库", operType = "查询", operDesc = "查询当前工作区航线库列表")
    public ResponseResult<List<WaylineListDTO>> waylineList(@PathVariable(name = "workspace_id") String workspaceId, String droneName) {
        return ResponseResult.success(waylineFileService.waylineList(workspaceId, droneName));
    }
 
    @PutMapping("/{workspace_id}/wayline_update")
    public ResponseResult upWayline(@RequestParam String waylineId, @RequestParam String name) {
        WaylineFileEntity entity = WaylineFileEntity
                .builder()
                .waylineId(waylineId)
                .name(name)
                .build();
        if (waylineFileService.updateWayline(entity) == 0) {
            return ResponseResult.error("更新失败");
        } else return ResponseResult.success();
    }
}