83 lines
2.9 KiB
Java
83 lines
2.9 KiB
Java
package com.fizz.business.controller;
|
||
|
||
import com.fizz.business.domain.msg.AppMeasureCoatMessage;
|
||
import com.fizz.business.domain.msg.AppMeasureEntryMessage;
|
||
import com.fizz.business.domain.msg.AppMeasureExitMessage;
|
||
import com.fizz.business.domain.msg.AppMeasureFurnaceMessage;
|
||
import com.ruoyi.common.annotation.Anonymous;
|
||
import com.ruoyi.common.core.domain.R;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.media.Schema;
|
||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
import lombok.Data;
|
||
import org.springframework.web.bind.annotation.GetMapping;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import java.lang.reflect.Field;
|
||
import java.util.*;
|
||
|
||
@RestController
|
||
@RequestMapping("/api/deviceFieldMeta")
|
||
@Tag(name = "设备字段元数据")
|
||
@Anonymous
|
||
public class DeviceFieldMetaController {
|
||
|
||
@GetMapping("/all")
|
||
@Operation(description = "获取测量字段的友好文案(来自 @Schema(description))")
|
||
public R<Map<String, FieldMetaVO>> all() {
|
||
Map<String, FieldMetaVO> map = new HashMap<>();
|
||
// 4个消息类全扫一遍
|
||
collect(map, AppMeasureEntryMessage.class);
|
||
collect(map, AppMeasureFurnaceMessage.class);
|
||
collect(map, AppMeasureCoatMessage.class);
|
||
collect(map, AppMeasureExitMessage.class);
|
||
return R.ok(map);
|
||
}
|
||
|
||
private void collect(Map<String, FieldMetaVO> map, Class<?> clazz) {
|
||
for (Field f : clazz.getDeclaredFields()) {
|
||
String fieldName = f.getName();
|
||
Schema schema = f.getAnnotation(Schema.class);
|
||
if (schema == null) continue;
|
||
|
||
String desc = schema.description();
|
||
if (desc == null || desc.trim().isEmpty()) continue;
|
||
|
||
FieldMetaVO vo = new FieldMetaVO();
|
||
vo.setLabel(parseLabel(desc));
|
||
vo.setUnit(parseUnit(desc));
|
||
vo.setDescription(desc);
|
||
map.putIfAbsent(fieldName, vo);
|
||
}
|
||
}
|
||
|
||
// 例:"上层平均涂层重量 (g/m²)" -> label=上层平均涂层重量
|
||
private String parseLabel(String desc) {
|
||
int idx = desc.indexOf('(');
|
||
if (idx > 0) return desc.substring(0, idx).trim();
|
||
idx = desc.indexOf('(');
|
||
if (idx > 0) return desc.substring(0, idx).trim();
|
||
return desc.trim();
|
||
}
|
||
|
||
// 例:"上层平均涂层重量 (g/m²)" -> unit=g/m²
|
||
private String parseUnit(String desc) {
|
||
int l = desc.indexOf('(');
|
||
int r = desc.indexOf(')');
|
||
if (l >= 0 && r > l) return desc.substring(l + 1, r).trim();
|
||
l = desc.indexOf('(');
|
||
r = desc.indexOf(')');
|
||
if (l >= 0 && r > l) return desc.substring(l + 1, r).trim();
|
||
return "";
|
||
}
|
||
|
||
@Data
|
||
public static class FieldMetaVO {
|
||
private String label;
|
||
private String unit;
|
||
private String description;
|
||
}
|
||
}
|
||
|