通用项的新增、删除、查询接口。
This commit is contained in:
parent
594c4d4189
commit
da7ebb3140
22
data/common_items.json
Normal file
22
data/common_items.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"taskLocation": [
|
||||
"浙江横店",
|
||||
"陕西宝鸡",
|
||||
"甘肃兰州"
|
||||
],
|
||||
"aircraftName": [
|
||||
"载机名称一",
|
||||
"载机名称二",
|
||||
"载机名称三"
|
||||
],
|
||||
"sensorDescription": [
|
||||
"电阻式传感器",
|
||||
"电容式传感器"
|
||||
],
|
||||
"taskLabel": [
|
||||
"试验标签一",
|
||||
"试验标签二",
|
||||
"试验标签三",
|
||||
"试验标签四"
|
||||
]
|
||||
}
|
||||
@ -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<String, Object> get() throws IOException {
|
||||
return success(readData());
|
||||
}
|
||||
|
||||
/** 新增 */
|
||||
@PostMapping
|
||||
public Map<String, Object> add(@RequestBody ItemReq req) throws IOException {
|
||||
validate(req);
|
||||
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
Map<String, List<String>> data = readData();
|
||||
List<String> list = data.computeIfAbsent(req.getType(), k -> new ArrayList<>());
|
||||
if (!list.contains(req.getLabel())) {
|
||||
list.add(req.getLabel());
|
||||
}
|
||||
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 {
|
||||
validate(req);
|
||||
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
Map<String, List<String>> data = readData();
|
||||
List<String> list = data.get(req.getType());
|
||||
if (list != null) {
|
||||
list.remove(req.getLabel());
|
||||
}
|
||||
|
||||
writeData(data);
|
||||
|
||||
Map<String, Object> 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<String, List<String>> 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<String, List<String>> 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<Map<String, List<String>>>() {}
|
||||
);
|
||||
} catch (IOException e) {
|
||||
// 如果读取失败,返回默认数据
|
||||
System.err.println("读取文件失败: " + e.getMessage());
|
||||
Map<String, List<String>> 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<String, List<String>> 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<String, Object> success(Object data) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user