zhaowenyu
2026-03-07 bc8f980e912825eaaadaf9fdd2367f443a56de02
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
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.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@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);
            result.put("success", success);
            result.put("msg", success ? "目录新增成功" : "目录新增失败");
            result.put("data", entity.getId()); // 返回新增ID
        } catch (Exception e) {
            log.error("新增目录异常", e);
            result.put("success", false);
            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);
            result.put("success", success);
            result.put("msg", success ? "目录修改成功" : "目录修改失败");
        } catch (Exception e) {
            log.error("修改目录异常", e);
            result.put("success", false);
            result.put("msg", "修改目录失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("删除目录及子项")
    @DeleteMapping("/directory/{id}")
    public ResponseEntity<Map<String, Object>> deleteDirectory(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = directoryService.deleteDirectory(id);
            result.put("success", success);
            result.put("msg", success ? "目录删除成功" : "目录删除失败");
        } catch (Exception e) {
            log.error("删除目录异常", e);
            result.put("success", false);
            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);
            result.put("success", success);
            result.put("msg", success ? "标记新增成功" : "标记新增失败");
            result.put("data", entity.getId());
        } catch (Exception e) {
            log.error("新增标记异常", e);
            result.put("success", false);
            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);
            result.put("success", success);
            result.put("msg", success ? "标记修改成功" : "标记修改失败");
        } catch (Exception e) {
            log.error("修改标记异常", e);
            result.put("success", false);
            result.put("msg", "修改标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("删除标记")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteAnnotation(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = annotationService.deleteAnnotation(id);
            result.put("success", success);
            result.put("msg", success ? "标记删除成功" : "标记删除失败");
        } catch (Exception e) {
            log.error("删除标记异常", e);
            result.put("success", false);
            result.put("msg", "删除标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    @ApiOperation("批量删除标记")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDeleteAnnotation(@RequestBody List<Integer> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = annotationService.batchDeleteAnnotation(ids);
            result.put("success", success);
            result.put("msg", success ? "标记批量删除成功" : "标记批量删除失败");
        } catch (Exception e) {
            log.error("批量删除标记异常", e);
            result.put("success", false);
            result.put("msg", "批量删除标记失败:" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
 
    // 层级查询:目录→标记→坐标
    @ApiOperation("按目标id获取所有目录及子标记信息")
    @GetMapping("/directory/{directoryId}")
    public ResponseEntity<Map<String, Object>> getDirectoryWithAnnotations(@PathVariable Integer directoryId) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 1. 查询目录
            AnnotationDirectoryEntity dirEntity = directoryService.getById(directoryId);
            if (dirEntity == null) {
                result.put("success", false);
                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("success", true);
            result.put("data", dirDto);
        } catch (Exception e) {
            log.error("查询目录及标记异常", e);
            result.put("success", false);
            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("success", true);
        result.put("msg", "查询全部目录及标注成功");
        result.put("data", directoryList);
 
        return ResponseEntity.ok(result);
    }
}