TRX 1 年間 前
コミット
78c9f4da71

+ 3 - 0
src/main/java/com/zswl/dataservice/domain/iot/IotMain.java

@@ -55,6 +55,9 @@ public class IotMain extends SuperEntity {
     @Schema(description = "数据类型,number、Boolean、String 等")
     private DataType dataType;
 
+    @Schema(description = "是否必须")
+    private Boolean required = Boolean.TRUE;
+
     @Schema(description = "元素类型,只有 dataType=Array才使用")
     private DataType elementType;
 

+ 2 - 2
src/main/java/com/zswl/dataservice/model/iot/IotAttribute.java

@@ -30,8 +30,8 @@ public class IotAttribute {
     @Schema(description = "参数名称,如:name")
     private String identifier;
 
-    @Schema(description = "是否必须")
-    private Boolean required = Boolean.FALSE;
+    @Schema(description = "是否必须", required = true)
+    private Boolean required = Boolean.TRUE;
 
     @Schema(description = "单位名称,如:千克、流明、立方米等...")
     private String unitName;

+ 1 - 1
src/main/java/com/zswl/dataservice/model/iot/IotMainParam.java

@@ -56,7 +56,7 @@ public class IotMainParam extends SuperParam {
     private BigDecimal end;
 
     @Schema(description = "最大的长度,如:类型为字符串时使用")
-    private String maxLength;
+    private Integer maxLength;
 
     @Schema(description = "属性,参数列表,输入参数")
     private List<IotAttribute> childs;

+ 158 - 75
src/main/java/com/zswl/dataservice/service/iot/IotDataVerifyService.java

@@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
+import java.util.List;
 
 /**
  * @author TRX
@@ -37,92 +38,132 @@ public class IotDataVerifyService {
                 IotAttribute attribute = new IotAttribute();
                 BeanUtils.copyProperties(iotMain, attribute);
                 attribute.setChilds(iotMain.getChilds());
+                return loopCheck(attribute, data);
+            }
+        }
+        return ResultContent.buildSuccess();
+    }
 
-                String identifier = attribute.getIdentifier();
-                String name = attribute.getName();
-                // 属性
-                if (!data.containsKey(identifier)) {
-                    return ResultContent.buildFail(String.format("数据中不包含【%s】", identifier));
+    public ResultContent loopCheck(IotAttribute attribute, JSONObject data) {
+        if (attribute != null && data != null) {
+            String identifier = attribute.getIdentifier();
+            // 属性
+            if (!data.containsKey(identifier)) {
+                // 没有该属性
+                if (attribute.getRequired() != null && attribute.getRequired()) {
+                    // 必须包含该属性
+                    return ResultContent.buildFail(String.format("数据中不包含参数【%s】", identifier));
                 }
+                return ResultContent.buildSuccess();
+            }
+            // 值
+            Object objectValue = data.get(identifier);
+            // 参数 数据类型
+            DataType dataType = attribute.getDataType();
+            if (objectValue == null) {
+                return ResultContent.buildSuccess();
+            }
+            // 检查内容类型
+            ResultContent resultContent = checkDataType(objectValue, identifier, dataType, null);
+            if (resultContent.isFailed()) {
+                return resultContent;
+            }
 
-                DataType dataType = attribute.getDataType();
-                if (dataType == DataType.String) {
-                    // 字符串
-                    String value = data.get(identifier, String.class);
-                    Integer maxLength = attribute.getMaxLength();
-                    if (!CommonUtil.longIsEmpty(maxLength) && StringUtils.isNotEmpty(value) && value.length() > maxLength) {
-                        return ResultContent.buildFail(String.format("【%s】值超过最大长度【%d】", identifier, maxLength));
-                    }
+            if (dataType == DataType.String) {
+                // 字符串
+                String value = data.get(identifier, String.class);
+                Integer maxLength = attribute.getMaxLength();
+                if (!CommonUtil.longIsEmpty(maxLength) && StringUtils.isNotEmpty(value) && value.length() > maxLength) {
+                    return ResultContent.buildFail(String.format("【%s】值超过最大长度【%d】", identifier, maxLength));
                 }
+            }
 
-                if (dataType == DataType.Number) {
-                    BigDecimal value = data.get(identifier, BigDecimal.class);
-                    BigDecimal start = attribute.getStart();
-                    BigDecimal end = attribute.getEnd();
-                    if (start != null && start.compareTo(value) > 0) {
-                        return ResultContent.buildFail(String.format("【%s】值小于规定最小值【%s】", identifier, start.toString()));
-                    }
-                    if (end != null && end.compareTo(value) < 0) {
-                        return ResultContent.buildFail(String.format("【%s】值大于规定最大值【%s】", identifier, end.toString()));
-                    }
-                    data.set(identifier, value);
+            if (dataType == DataType.Number) {
+                // 数字
+                BigDecimal value = data.get(identifier, BigDecimal.class);
+                BigDecimal start = attribute.getStart();
+                BigDecimal end = attribute.getEnd();
+                if (start != null && start.compareTo(value) > 0) {
+                    return ResultContent.buildFail(String.format("【%s】值小于规定最小值【%s】", identifier, start.toString()));
                 }
-
-                if (dataType == DataType.Boolean) {
-                    // 布尔型
-                    Boolean value = data.get(identifier, Boolean.class);
-                    data.set(identifier, value);
+                if (end != null && end.compareTo(value) < 0) {
+                    return ResultContent.buildFail(String.format("【%s】值大于规定最大值【%s】", identifier, end.toString()));
                 }
+            }
 
-                if (dataType == DataType.Object) {
-                    // 对象
-                    try {
-                        JSONObject object = data.get(identifier, JSONObject.class);
-                    } catch (Exception e) {
-                        String msg = String.format("【%s】无法转换为JSONObject", identifier);
-                        log.error(msg);
-                        return ResultContent.buildFail(msg);
+            // 对象
+            if (dataType == DataType.Object) {
+                JSONObject object = data.get(identifier, JSONObject.class);
+                List<IotAttribute> childs = attribute.getChilds();
+                if (childs != null) {
+                    for (IotAttribute iotAttribute : childs) {
+                        ResultContent resultChildContent = loopCheck(iotAttribute, object);
+                        if (resultChildContent.isFailed()) {
+                            return resultChildContent;
+                        }
                     }
                 }
-                if (dataType == DataType.Array) {
-                    // 数组
-                    try {
-                        JSONArray array = data.get(identifier, JSONArray.class);
-                        if (array != null && array.size() > 0) {
-                            DataType elementType = attribute.getElementType();
-                            for (int i = 0; i < array.size(); i++) {
-                                Object object = array.get(i);
-                                if (elementType == DataType.String) {
-                                    if (!(object instanceof String)) {
-                                        return ResultContent.buildFail(
-                                                String.format("【%s】属性第【%d】项内容不属于【%s】类型", identifier, (i + 1), DataType.String.getRemark())
-                                        );
-                                    }
-                                }
+            }
 
-                                if (elementType == DataType.Number) {
-                                    if (!(object instanceof Number)) {
-                                        return ResultContent.buildFail(
-                                                String.format("【%s】属性第【%d】项内容不属于【%s】类型",
-                                                        identifier, (i + 1), DataType.Number.getRemark())
-                                        );
+            if (dataType == DataType.Array) {
+                // 数组
+                JSONArray array = data.get(identifier, JSONArray.class);
+                if (array != null && array.size() > 0) {
+                    // 数组里包含的元素类型
+                    DataType elementType = attribute.getElementType();
+                    for (int i = 0; i < array.size(); i++) {
+                        Object object = array.get(i);
+
+                        // 检查数组里的内容类型
+                        ResultContent childContent = checkDataType(object, identifier, elementType, i + 1);
+                        if (childContent.isFailed()) {
+                            return resultContent;
+                        }
+                        // 常规数据检查
+                        if (List.of(DataType.String, DataType.Boolean, DataType.Number).contains(elementType)) {
+                            JSONObject jsonObject = new JSONObject();
+                            jsonObject.put("value", jsonObject);
+                            IotAttribute tempAttribute = new IotAttribute();
+                            BeanUtils.copyProperties(attribute, tempAttribute);
+                            tempAttribute.setDataType(elementType);
+                            tempAttribute.setIdentifier("value");
+
+                            ResultContent resultContent1 = loopCheck(tempAttribute, jsonObject);
+                            if (resultContent1.isFailed()) {
+                                return resultContent1;
+                            }
+                        }
+
+                        // 对象
+                        if (elementType == DataType.Object) {
+                            List<IotAttribute> childs = attribute.getChilds();
+                            if (childs != null) {
+                                for (IotAttribute iotAttribute : childs) {
+                                    ResultContent resultChildContent = loopCheck(iotAttribute, array.getJSONObject(i));
+                                    if (resultChildContent.isFailed()) {
+                                        return resultChildContent;
                                     }
                                 }
+                            }
+                        }
 
-                                if (elementType == DataType.Boolean) {
-                                    if (!(object instanceof Boolean)) {
-                                        return ResultContent.buildFail(
-                                                String.format("【%s】属性第【%d】项内容不属于【%s】类型",
-                                                        identifier, (i + 1), DataType.Boolean.getRemark())
-                                        );
+                        if (elementType == DataType.Array) {
+                            List<IotAttribute> childs = attribute.getChilds();
+                            JSONArray temp = array.getJSONArray(i);
+                            if (childs != null && temp != null) {
+                                for (IotAttribute iotAttribute : childs) {
+                                    for (Object object1 : temp) {
+                                        String childIdentifier = iotAttribute.getIdentifier();
+                                        JSONObject childObject = new JSONObject();
+                                        childObject.put(childIdentifier, object1);
+                                        ResultContent resultChildContent = loopCheck(iotAttribute, childObject);
+                                        if (resultChildContent.isFailed()) {
+                                            return resultChildContent;
+                                        }
                                     }
                                 }
                             }
                         }
-                    } catch (Exception e) {
-                        String msg = String.format("【%s】无法转换为JSONArray", identifier);
-                        log.error(msg);
-                        return ResultContent.buildFail(msg);
                     }
                 }
             }
@@ -130,13 +171,55 @@ public class IotDataVerifyService {
         return ResultContent.buildSuccess();
     }
 
-    public <T> T getValue(JSONObject data, String identifier, Class<T> t) {
-        try {
-            return data.get(identifier, t);
-        } catch (Exception e) {
-            e.printStackTrace();
+    /**
+     * 检查内容类型
+     *
+     * @param object      内容
+     * @param identifier  参数名称,如:name
+     * @param elementType 数据类型
+     * @param index       第几项
+     * @return
+     */
+    public ResultContent checkDataType(Object object, String identifier, DataType elementType, Integer index) {
+        boolean b = true;
+        if (object != null && elementType != null) {
+            if (elementType == DataType.String) {
+                if (!(object instanceof String)) {
+                    b = false;
+                }
+            }
+            if (elementType == DataType.Number) {
+                if (!(object instanceof Number)) {
+                    b = false;
+                }
+            }
+            if (elementType == DataType.Boolean) {
+                if (!(object instanceof Boolean)) {
+                    b = false;
+                }
+            }
+            if (elementType == DataType.Object) {
+                if (!(object instanceof JSONObject)) {
+                    b = false;
+                }
+            }
+            if (elementType == DataType.Array) {
+                if (!(object instanceof JSONArray)) {
+                    b = false;
+                }
+            }
+        }
+        if (!b) {
+            if (index != null) {
+                return ResultContent.buildFail(
+                        String.format("【%s】属性第【%d】项内容不属于【%s】类型", identifier, index, elementType.getRemark())
+                );
+            } else {
+                return ResultContent.buildFail(
+                        String.format("【%s】属性内容不属于【%s】类型", identifier, elementType.getRemark())
+                );
+            }
         }
-        return null;
+        return ResultContent.buildSuccess();
     }
-
 }

+ 5 - 0
src/main/java/com/zswl/dataservice/utils/test/TestReplace.java

@@ -1,8 +1,10 @@
 package com.zswl.dataservice.utils.test;
 
 import cn.hutool.json.JSONObject;
+import com.zswl.dataservice.type.DataType;
 
 import java.math.BigDecimal;
+import java.util.List;
 import java.util.regex.Pattern;
 
 /**
@@ -24,5 +26,8 @@ public class TestReplace {
         object.put("isRe", 458.21);
         Object ob = object.get("isRe");
         System.out.println(ob instanceof String);
+
+        boolean b = List.of(DataType.String, DataType.Boolean).contains(DataType.Object);
+        System.out.println(b);
     }
 }