From da7ebb3140f67adf15634ab2d039003b713e601c Mon Sep 17 00:00:00 2001 From: wanxiaoli Date: Tue, 6 Jan 2026 11:17:56 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8=E9=A1=B9=E7=9A=84=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E3=80=81=E5=88=A0=E9=99=A4=E3=80=81=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/common_items.json | 22 +++ .../controller/CommonItemController.java | 146 ++++++++++++++++++ .../modules/experimentalData/dto/ItemReq.java | 25 +++ 3 files changed, 193 insertions(+) create mode 100644 data/common_items.json create mode 100644 java/src/main/java/com/yfd/platform/modules/experimentalData/controller/CommonItemController.java create mode 100644 java/src/main/java/com/yfd/platform/modules/experimentalData/dto/ItemReq.java diff --git a/data/common_items.json b/data/common_items.json new file mode 100644 index 0000000..81b1a83 --- /dev/null +++ b/data/common_items.json @@ -0,0 +1,22 @@ +{ + "taskLocation": [ + "浙江横店", + "陕西宝鸡", + "甘肃兰州" + ], + "aircraftName": [ + "载机名称一", + "载机名称二", + "载机名称三" + ], +"sensorDescription": [ + "电阻式传感器", + "电容式‌传感器" + ], +"taskLabel": [ + "试验标签一", + "试验标签二", + "试验标签三", + "试验标签四" + ] +} \ No newline at end of file diff --git a/java/src/main/java/com/yfd/platform/modules/experimentalData/controller/CommonItemController.java b/java/src/main/java/com/yfd/platform/modules/experimentalData/controller/CommonItemController.java new file mode 100644 index 0000000..238a8bd --- /dev/null +++ b/java/src/main/java/com/yfd/platform/modules/experimentalData/controller/CommonItemController.java @@ -0,0 +1,146 @@ +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 java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +@RestController +@RequestMapping("/api/common-items") +public class CommonItemController { + + private static final String FILE_NAME = "common_items.json"; + private static final String DATA_DIR = "data"; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + + /** 查询 */ + @GetMapping + public Map get() throws IOException { + return success(readData()); + } + + /** 新增 */ + @PostMapping + public Map add(@RequestBody ItemReq req) throws IOException { + validate(req); + + lock.writeLock().lock(); + try { + Map> data = readData(); + List list = data.computeIfAbsent(req.getType(), k -> new ArrayList<>()); + if (!list.contains(req.getLabel())) { + list.add(req.getLabel()); + } + writeData(data); + + Map result = new HashMap<>(); + result.put(req.getType(), list); + return success(result); + } finally { + lock.writeLock().unlock(); + } + } + + /** 删除 */ + @DeleteMapping + public Map delete(@RequestBody ItemReq req) throws IOException { + validate(req); + + lock.writeLock().lock(); + try { + Map> data = readData(); + List list = data.get(req.getType()); + if (list != null) { + list.remove(req.getLabel()); + } + + writeData(data); + + Map result = new HashMap<>(); + result.put(req.getType(), list != null ? list : Collections.emptyList()); + return success(result); + } finally { + lock.writeLock().unlock(); + } + } + + /* ================== 工具方法 ================== */ + + private void validate(ItemReq req) { + if (req == null + || (!"taskLocation".equals(req.getType()) && !"aircraftName".equals(req.getType()) + && !"sensorDescription".equals(req.getType()) && !"taskLabel".equals(req.getType())) + || req.getLabel() == null || req.getLabel().trim().isEmpty()) { + throw new IllegalArgumentException("参数无效"); + } + } + + private Map> readData() throws IOException { + // 获取项目根目录下的data文件夹路径 + String userDir = System.getProperty("user.dir"); + Path dataDir = Paths.get(userDir, DATA_DIR); + Path filePath = dataDir.resolve(FILE_NAME); + + if (!Files.exists(filePath)) { + // 如果项目根目录下没有文件,尝试从resources目录获取默认数据 + Files.createDirectories(dataDir); + Map> init = new HashMap<>(); + init.put("taskLocation", new ArrayList<>()); + init.put("aircraftName", new ArrayList<>()); + init.put("sensorDescription", new ArrayList<>()); + init.put("taskLabel", new ArrayList<>()); + writeData(init); + return init; + } + + // 文件存在,读取数据 + try { + return objectMapper.readValue( + filePath.toFile(), + new TypeReference>>() {} + ); + } catch (IOException e) { + // 如果读取失败,返回默认数据 + System.err.println("读取文件失败: " + e.getMessage()); + Map> init = new HashMap<>(); + init.put("taskLocation", new ArrayList<>()); + init.put("aircraftName", new ArrayList<>()); + init.put("sensorDescription", new ArrayList<>()); + init.put("taskLabel", new ArrayList<>()); + return init; + } + } + + private void writeData(Map> data) throws IOException { + String userDir = System.getProperty("user.dir"); + Path dataDir = Paths.get(userDir, DATA_DIR); + Path filePath = dataDir.resolve(FILE_NAME); + + Files.createDirectories(dataDir); + objectMapper.writerWithDefaultPrettyPrinter() + .writeValue(filePath.toFile(), data); + } + + private Map success(Object data) { + Map result = new HashMap<>(); + result.put("success", true); + result.put("data", data); + return result; + } +} \ No newline at end of file diff --git a/java/src/main/java/com/yfd/platform/modules/experimentalData/dto/ItemReq.java b/java/src/main/java/com/yfd/platform/modules/experimentalData/dto/ItemReq.java new file mode 100644 index 0000000..129adf4 --- /dev/null +++ b/java/src/main/java/com/yfd/platform/modules/experimentalData/dto/ItemReq.java @@ -0,0 +1,25 @@ +package com.yfd.platform.modules.experimentalData.dto; + + +public class ItemReq { + + private String type; // treatment | suggestion + private String label; // 内容 + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } +} +