添加批量增加。

This commit is contained in:
wanxiaoli 2026-01-06 17:36:33 +08:00
parent 4477000298
commit 51a2a2676b

View File

@ -3,7 +3,12 @@ package com.yfd.platform.modules.experimentalData.controller;
import com.yfd.platform.modules.experimentalData.dto.ItemReq;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.nio.file.Files;
@ -55,6 +60,33 @@ public class CommonItemController {
}
}
/** 新增多个标签 */
@PostMapping("/batch")
public Map<String, Object> addBatch(@RequestBody ItemReq req) throws IOException {
validateForBatch(req);
lock.writeLock().lock();
try {
Map<String, List<String>> data = readData();
List<String> list = data.computeIfAbsent(req.getType(), k -> new ArrayList<>());
// 批量添加不重复的标签
for (String label : req.getLabels()) {
if (!list.contains(label)) {
list.add(label);
}
}
writeData(data);
Map<String, Object> result = new HashMap<>();
result.put(req.getType(), list);
return success(result);
} finally {
lock.writeLock().unlock();
}
}
/** 删除 */
@DeleteMapping
public Map<String, Object> delete(@RequestBody ItemReq req) throws IOException {
@ -89,6 +121,15 @@ public class CommonItemController {
}
}
private void validateForBatch(ItemReq req) {
if (req == null
|| (!"taskLocation".equals(req.getType()) && !"aircraftName".equals(req.getType())
&& !"sensorDescription".equals(req.getType()) && !"taskLabel".equals(req.getType()))
|| req.getLabels() == null || req.getLabels().isEmpty()) {
throw new IllegalArgumentException("参数无效");
}
}
private Map<String, List<String>> readData() throws IOException {
// 获取项目根目录下的data文件夹路径
String userDir = System.getProperty("user.dir");