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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package com.ard.web.controller.sample;
 
import com.ard.sample.annotation.model.dto.AnnotationDTO;
import com.ard.sample.annotation.model.dto.AnnotationDirectoryDTO;
import com.ard.sample.annotation.model.dto.AnnotationPointsDTO;
import com.ard.sample.annotation.model.entity.AnnotationDirectoryEntity;
import com.ard.sample.annotation.model.entity.AnnotationEntity;
import com.ard.sample.annotation.service.IAnnotationDirectoryService;
import com.ard.sample.annotation.service.IAnnotationService;
import com.ard.sample.annotation.util.ConvertUtil;
import com.ard.sample.annotation.util.GeoCalculator;
import com.ard.sample.annotation.util.JsonSerializeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Api(tags = "标注管理")
@Slf4j
@RestController
@RequestMapping("/annotation")
@RequiredArgsConstructor
public class AnnotationController {
 
    private final IAnnotationDirectoryService directoryService;
    private final IAnnotationService annotationService;
 
    // ====================== 目录接口 ======================
    @ApiOperation("新增目录")
    @PostMapping("/directory")
    public ResponseEntity<Map<String, Object>> addDirectory(@RequestBody AnnotationDirectoryEntity entity) {
        Map<String, Object> result = new HashMap<>();
        try {
            //AnnotationDirectoryEntity entity = ConvertUtil.directoryDtoToEntity(dto);
            boolean success = directoryService.addDirectory(entity);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "目录新增成功" : "目录新增失败");
            result.put("data", entity.getId()); // 返回新增ID
        } catch (Exception e) {
            log.error("新增目录异常", e);
            result.put("code", 400);
            result.put("msg", "新增目录失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("修改目录")
    @PutMapping("/directory")
    public ResponseEntity<Map<String, Object>> editDirectory(@RequestBody AnnotationDirectoryEntity entity) {
        Map<String, Object> result = new HashMap<>();
        try {
//            AnnotationDirectoryEntity entity = ConvertUtil.directoryDtoToEntity(dto);
            boolean success = directoryService.editDirectory(entity);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "目录修改成功" : "目录修改失败");
        } catch (Exception e) {
            log.error("修改目录异常", e);
            result.put("code", 400);
            result.put("msg", "修改目录失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    // 新增:修改目录显示状态
    @ApiOperation("修改目录是否可见")
    @PutMapping("/dirvisible/{id}/{isTrue}")
    public ResponseEntity<Map<String, Object>> updateDirectoryVisible(@PathVariable String id, @PathVariable Boolean isTrue) {
        Map<String, Object> result = new HashMap<>();
        try{
            boolean success = directoryService.updateDirectoryVisible(id, isTrue);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "修改目录是否可见成功" : "修改目录是否可见失败");
        }
        catch (Exception e) {
            log.error("修改目录是否可见异常", e);
            result.put("code", 400);
            result.put("msg", "修改目录是否可见失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("删除目录及子项")
    @DeleteMapping("/directory/{id}")
    public ResponseEntity<Map<String, Object>> deleteDirectory(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = directoryService.deleteDirectory(id);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "目录删除成功" : "目录删除失败");
        } catch (Exception e) {
            log.error("删除目录异常", e);
            result.put("code", 400);
            result.put("msg", "删除目录失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
 
    // ====================== 标记接口 ======================
    @ApiOperation("新增标记")
    @PostMapping
    public ResponseEntity<Map<String, Object>> addAnnotation(@RequestBody AnnotationDTO dto) {
        Map<String, Object> result = new HashMap<>();
        try {
            AnnotationEntity entity = ConvertUtil.annotationDtoToEntity(dto);
            boolean success = annotationService.addAnnotation(entity);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "标记新增成功" : "标记新增失败");
            result.put("data", entity.getId());
        } catch (Exception e) {
            log.error("新增标记异常", e);
            result.put("code", 400);
            result.put("msg", "新增标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("修改标记")
    @PutMapping
    public ResponseEntity<Map<String, Object>> editAnnotation(@RequestBody AnnotationDTO dto) {
        Map<String, Object> result = new HashMap<>();
        try {
            AnnotationEntity entity = ConvertUtil.annotationDtoToEntity(dto);
            boolean success = annotationService.editAnnotation(entity);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "标记修改成功" : "标记修改失败");
        } catch (Exception e) {
            log.error("修改标记异常", e);
            result.put("code", 400);
            result.put("msg", "修改标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    // 新增:修改标注显示状态
    @ApiOperation("修改标记是否可见")
    @PutMapping("/annvisible/{id}/{isTrue}")
    public ResponseEntity<Map<String, Object>> updateAnnotationVisible(@PathVariable String id, @PathVariable Boolean isTrue) {
        Map<String, Object> result = new HashMap<>();
        try{
            boolean success = annotationService.updateAnnotationVisible(id, isTrue);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "修改标记是否可见成功" : "修改标记是否可见失败");
        }
        catch (Exception e) {
            log.error("修改标记是否可见异常", e);
            result.put("code", 400);
            result.put("msg", "修改标记是否可见失败:" + e.getMessage());
        }
        //boolean result = annotationService.updateAnnotationVisible(id, isTrue);
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("删除标记")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteAnnotation(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = annotationService.deleteAnnotation(id);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "标记删除成功" : "标记删除失败");
        } catch (Exception e) {
            log.error("删除标记异常", e);
            result.put("code", 400);
            result.put("msg", "删除标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("批量删除标记")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDeleteAnnotation(@RequestBody List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = annotationService.batchDeleteAnnotation(ids);
            int re=0;
            if(success) {
                re = 200;
            }
            else {
                re = 400;
            }
            result.put("code", re);
            result.put("msg", success ? "标记批量删除成功" : "标记批量删除失败");
        } catch (Exception e) {
            log.error("批量删除标记异常", e);
            result.put("code", 400);
            result.put("msg", "批量删除标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    // 层级查询:目录→标记→坐标
    @ApiOperation("按目标id获取所有目录及子标记信息")
    @GetMapping("/directory/{directoryId}")
    public ResponseEntity<Map<String, Object>> getDirectoryWithAnnotations(@PathVariable String directoryId) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 1. 查询目录
            AnnotationDirectoryEntity dirEntity = directoryService.getById(directoryId);
            if (dirEntity == null) {
                result.put("code", 400);
                result.put("msg", "目录不存在");
                return ResponseEntity.ok(result);
            }
            AnnotationDirectoryDTO dirDto = ConvertUtil.directoryEntityToDto(dirEntity);
 
            // 2. 查询目录下标记
            List<AnnotationEntity> annoEntities = annotationService.listByDirectoryId(directoryId);
            List<AnnotationDTO> annoDtos = ConvertUtil.annotationEntityListToDtoList(annoEntities);
            dirDto.setAnnotations(annoDtos);
 
            result.put("code", 200);
            result.put("data", dirDto);
        } catch (Exception e) {
            log.error("查询目录及标记异常", e);
            result.put("code", 400);
            result.put("msg", "查询失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    /**
     * 新增:获取全部目录及下属标注数据
     */
    @ApiOperation("获取全部目录及下属标记数据")
    @GetMapping("/directory/all")
    public ResponseEntity<Map<String, Object>> listAllDirectoryWithAnnotations() {
        List<AnnotationDirectoryDTO> directoryList = directoryService.listAllDirectoryWithAnnotations();
 
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "查询全部目录及标记成功");
        result.put("data", directoryList);
 
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("按标注ID获取标记详细信息,包含线的长度,面、圆的面积")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getAnnotationDetailById(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            // 1. 参数校验
            if (id == null || id.isEmpty()) {
                resultMap.put("code", 400);
                resultMap.put("msg", "标记ID不能为空");
                resultMap.put("data", null);
                return new ResponseEntity<>(resultMap, HttpStatus.BAD_REQUEST);
            }
 
            // 2. 查询标记数据
            AnnotationEntity annotationEntity = annotationService.getAnnotationById(id);
            if (annotationEntity == null) {
                resultMap.put("code", 404);
                resultMap.put("msg", "标记不存在或已删除");
                resultMap.put("data", null);
                return new ResponseEntity<>(resultMap, HttpStatus.NOT_FOUND);
            }
 
            // 3. 转换为DTO并反序列化坐标
            AnnotationDTO annotationDTO = AnnotationDTO.builder().build();
            org.springframework.beans.BeanUtils.copyProperties(annotationEntity, annotationDTO);
            List<AnnotationPointsDTO> coordinateList = null;
            List<GeoCalculator.Coordinate> geoPoints = new java.util.ArrayList<>();
            try {
                coordinateList = JsonSerializeUtil.deserialize(annotationEntity.getCoordinateInfo());
                annotationDTO.setCoordinateInfo(coordinateList);
 
                // 转换为工具类坐标
                if (coordinateList != null && !coordinateList.isEmpty()) {
                    geoPoints = coordinateList.stream()
                            .map(p -> new GeoCalculator.Coordinate(p.getLongitude(), p.getLatitude()))
                            .collect(Collectors.toList());
                }
            } catch (Exception e) {
                log.error("反序列化坐标失败,标注ID:{}", id, e);
                annotationDTO.setCoordinateInfo(null);
            }
 
            // 4. 按标注类型计算所有参数
            String annotationType = annotationEntity.getAnnotationType();
            switch (annotationType) {
                case "2": // line 线:水平距离、直线距离
                    annotationDTO.setLineHorizontalDistance(GeoCalculator.calculatePolylineHorizontalDistance(geoPoints));
                    annotationDTO.setLineStraightDistance(GeoCalculator.calculatePolylineStraightDistance(geoPoints));
                    break;
 
                case "3": // polygon 面:水平面积、水平周长
                    annotationDTO.setPolygonHorizontalArea(GeoCalculator.calculatePolygonHorizontalArea(geoPoints));
                    annotationDTO.setPolygonHorizontalPerimeter(GeoCalculator.calculatePolygonHorizontalPerimeter(geoPoints));
                    break;
 
                case "4": // circle 圆:水平面积、水平周长(仅需半径)
                    Double radius = annotationDTO.getCoordinateInfo().get(0).getRadius(); //annotationEntity.getRadius();
                    if (radius == null) radius = 0.0;
                    annotationDTO.setCircleHorizontalArea(GeoCalculator.calculateCircleHorizontalArea(radius));
                    annotationDTO.setCircleHorizontalPerimeter(GeoCalculator.calculateCircleHorizontalPerimeter(radius));
                    break;
 
                case "1": // point 点:无计算项,赋值0
                    annotationDTO.setLineHorizontalDistance(0.00);
                    annotationDTO.setLineStraightDistance(0.00);
                    annotationDTO.setPolygonHorizontalArea(0.00);
                    annotationDTO.setPolygonHorizontalPerimeter(0.00);
                    annotationDTO.setCircleHorizontalArea(0.00);
                    annotationDTO.setCircleHorizontalPerimeter(0.00);
                    break;
 
                default: // 未知类型
                    log.warn("未知标记类型:{},不计算地理参数", annotationType);
                    break;
            }
 
            // 5. 返回结果
            resultMap.put("code", 200);
            resultMap.put("msg", "查询成功");
            resultMap.put("data", annotationDTO);
            return new ResponseEntity<>(resultMap, HttpStatus.OK);
 
        } catch (Exception e) {
            log.error("查询标记详情异常,ID:{}", id, e);
            resultMap.put("code", 500);
            resultMap.put("msg", "查询失败:" + e.getMessage());
            resultMap.put("data", null);
            return new ResponseEntity<>(resultMap, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
 
//    // 新增:按标注ID查询详细信息(核心补充接口,返回格式统一为ResponseEntity<Map>)
//    @ApiOperation("按标注ID获取标注详细信息")
//    @GetMapping("/{id}")
//    public ResponseEntity<Map<String, Object>> getAnnotationDetailById(@PathVariable String id) {
//        Map<String, Object> resultMap = new HashMap<>();
//        try {
//            // 1. 参数校验
//            if (id == null || id.isEmpty()) {
//                resultMap.put("code", 400);
//                resultMap.put("msg", "标注ID不能为空");
//                resultMap.put("data", null);
//                return new ResponseEntity<>(resultMap, HttpStatus.BAD_REQUEST);
//            }
//
//            // 2. 查询标注实体
//            AnnotationEntity annotationEntity = annotationService.getAnnotationById(id);
//            if (annotationEntity == null) {
//                resultMap.put("code", 404);
//                resultMap.put("msg", "标注不存在或已删除");
//                resultMap.put("data", null);
//                return new ResponseEntity<>(resultMap, HttpStatus.NOT_FOUND);
//            }
//
//            // 3. 实体转DTO(反序列化坐标数据)
//            AnnotationDTO annotationDTO = AnnotationDTO.builder().build();
//            org.springframework.beans.BeanUtils.copyProperties(annotationEntity, annotationDTO);
//            try {
//                annotationDTO.setCoordinateInfo(JsonSerializeUtil.deserialize(annotationEntity.getCoordinateInfo()));
//            } catch (Exception e) {
//                log.error("反序列化标注坐标失败,标注ID:{}", id, e);
//                annotationDTO.setCoordinateInfo(null);
//            }
//
//            // 4. 统一返回格式
//            resultMap.put("code", 200);
//            resultMap.put("msg", "查询成功");
//            resultMap.put("data", annotationDTO);
//            return new ResponseEntity<>(resultMap, HttpStatus.OK);
//
//        } catch (Exception e) {
//            // 全局异常捕获
//            log.error("按ID查询标注详情异常,标注ID:{}", id, e);
//            resultMap.put("code", 500);
//            resultMap.put("msg", "查询失败:" + e.getMessage());
//            resultMap.put("data", null);
//            return new ResponseEntity<>(resultMap, HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }
}