| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package com.zswl.dataservice.service.iot;
- import cn.hutool.json.JSONArray;
- import cn.hutool.json.JSONObject;
- import com.zswl.dataservice.domain.iot.IotMain;
- import com.zswl.dataservice.model.iot.IotAttribute;
- import com.zswl.dataservice.type.DataType;
- import com.zswl.dataservice.type.FunctionType;
- import com.zswl.dataservice.utils.CommonUtil;
- import com.zswl.dataservice.utils.bean.BeanUtils;
- import com.zswl.dataservice.utils.result.ResultContent;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.stereotype.Service;
- import java.math.BigDecimal;
- /**
- * @author TRX
- * @date 2024/7/17
- */
- @Slf4j
- @Service
- public class IotDataVerifyService {
- /**
- * 验证
- *
- * @param iotMain
- * @param data
- * @return
- */
- public ResultContent<Boolean> verifyIotParam(IotMain iotMain, JSONObject data) {
- if (iotMain != null && data != null) {
- FunctionType functionType = iotMain.getFunctionType();
- if (functionType == FunctionType.Attribute) {
- IotAttribute attribute = new IotAttribute();
- BeanUtils.copyProperties(iotMain, attribute);
- attribute.setChilds(iotMain.getChilds());
- String identifier = attribute.getIdentifier();
- String name = attribute.getName();
- // 属性
- if (!data.containsKey(identifier)) {
- return ResultContent.buildFail(String.format("数据中不包含【%s】", identifier));
- }
- 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.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.Boolean) {
- // 布尔型
- Boolean value = data.get(identifier, Boolean.class);
- data.set(identifier, value);
- }
- 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.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 (elementType == DataType.Boolean) {
- if (!(object instanceof Boolean)) {
- return ResultContent.buildFail(
- String.format("【%s】属性第【%d】项内容不属于【%s】类型",
- identifier, (i + 1), DataType.Boolean.getRemark())
- );
- }
- }
- }
- }
- } catch (Exception e) {
- String msg = String.format("【%s】无法转换为JSONArray", identifier);
- log.error(msg);
- return ResultContent.buildFail(msg);
- }
- }
- }
- }
- 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();
- }
- return null;
- }
- }
|