PropertyMetric.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package org.jetlinks.community;
  2. import io.swagger.v3.oas.annotations.media.Schema;
  3. import lombok.*;
  4. import org.hswebframework.web.bean.FastBeanCopier;
  5. import org.jetlinks.community.utils.ConverterUtils;
  6. import org.springframework.util.StringUtils;
  7. import javax.validation.constraints.NotBlank;
  8. import java.util.Map;
  9. import java.util.function.Function;
  10. @Getter
  11. @Setter
  12. @Builder
  13. @NoArgsConstructor
  14. @AllArgsConstructor
  15. public class PropertyMetric {
  16. @Schema(description = "指标ID")
  17. @NotBlank
  18. private String id;
  19. @Schema(description = "名称")
  20. @NotBlank
  21. private String name;
  22. @Schema(description = "值,范围值使用逗号分隔")
  23. private Object value;
  24. @Schema(description = "是否为范围值")
  25. private boolean range;
  26. @Schema(description = "其他拓展配置")
  27. private Map<String, Object> expands;
  28. public Object castValue() {
  29. if (value == null) {
  30. return null;
  31. }
  32. if (range) {
  33. return ConverterUtils.tryConvertToList(value, Function.identity());
  34. }
  35. return value;
  36. }
  37. public PropertyMetric merge(PropertyMetric another) {
  38. if (!StringUtils.hasText(this.name)) {
  39. this.setValue(another.value);
  40. }
  41. return this;
  42. }
  43. public static PropertyMetric of(String id, String name, Object value) {
  44. PropertyMetric metric = new PropertyMetric();
  45. metric.setId(id);
  46. metric.setName(name);
  47. metric.setValue(value);
  48. return metric;
  49. }
  50. public static PropertyMetric of(Object mapMetric) {
  51. return FastBeanCopier.copy(mapMetric, new PropertyMetric());
  52. }
  53. }