动态条件实现java

提交页面设计

 

json数据格式

[
    {
        "name": "规则1",
        "action": {
            "with": [
                {
                    "type": "SHOW",
                    "targets": [
                        "xd_hh_158444776217"
                    ]
                },
                {
                    "type": "HIDE",
                    "targets": [
                        "xd_hh_158772314112"
                    ]
                }
            ],
            "other": [
                {
                    "type": "HIDE",
                    "targets": [
                        "xd_hh_158444776217"
                    ]
                },
                {
                    "type": "SHOW",
                    "targets": [
                        "xd_hh_158772314112"
                    ]
                }
            ]
        },
        "condition": {
            "groupMode": true,
            "conditionList": [
                {
                    "mode": true,
                    "conditionList": [
                        {
                            "key": "xd_hh_162172344044",
                            "value": [
                                {
                                    "key": "16",
                                    "value": "16"
                                }
                            ],
                            "expression": "in"
                        }
                    ]
                }
            ]
        }
    }
]

java解析

  public Set<String> getRuleHideShowList(String rule,Map<String, Object> variables,Boolean returnHide)
      throws ErrorResponseException {
    Set<String> showIds = new HashSet<>();
    Set<String> hideIds = new HashSet<>();

    if(StrUtil.isNotBlank(rule)){

      List<FormRuleDto> formRuleDtoList = CommonUtil.toArray(rule, FormRuleDto.class);
      // 遍历每个规则
      for (FormRuleDto formRuleDto : formRuleDtoList) {
        Boolean with=null;
        FormRuleConditionDto formRuleConditionDto=formRuleDto.getCondition();
        for (FormRuleConditionItemDto formRuleConditionItemDto : formRuleConditionDto.getConditionList()) {

          Boolean getConditionListExpressionValue=null;
          for (FormRuleConditionItemItemDto formRuleConditionItemItemDto : formRuleConditionItemDto.getConditionList()) {
            boolean expressionValue=false;
            String key = formRuleConditionItemItemDto.getKey();
            Object value = formRuleConditionItemItemDto.getValue();
            String expression = formRuleConditionItemItemDto.getExpression();
            if(Objects.equals(expression, "in")){
              Object keyValue =variables.get(key);
              expressionValue=Objects.equals(value,keyValue);
            }else if(Objects.equals(expression, "notin"))
{ Object keyValue =variables.get(key);
expressionValue=!Objects.equals(value,keyValue);
}else
{ log.error("该动态表单表达式暂未支持,请联系管理员:{}", new Gson().toJson(rule)); ErrorResponseException exception = new ErrorResponseException(HttpStatus.NOT_FOUND); exception.setDetail("该动态表单表达式暂未支持,请联系管理员:" + expression); throw exception; } // if(formRuleConditionItemDto.getMode()){ if(getConditionListExpressionValue==null){ getConditionListExpressionValue=expressionValue; }else{ getConditionListExpressionValue=getConditionListExpressionValue && expressionValue; } }else{ // if(getConditionListExpressionValue==null){ getConditionListExpressionValue=expressionValue; }else{ getConditionListExpressionValue=getConditionListExpressionValue || expressionValue; } } } // if(formRuleConditionDto.getGroupMode()){ if(with==null){ with=getConditionListExpressionValue; }else{ with=with && getConditionListExpressionValue; } }else{ // if(with==null){ with=getConditionListExpressionValue; }else{ with=with || getConditionListExpressionValue; } } } // 获取所有action子节点 FormRuleActionDto action=formRuleDto.getAction(); if(with!=null && action!=null){ if(with){ List<FormRuleActionItemDto> actionWith= action.getWith(); for (FormRuleActionItemDto actionItemDto : actionWith) { if(Objects.equals(actionItemDto.getType(), "SHOW")){ showIds.addAll(actionItemDto.getTargets()); } if(Objects.equals(actionItemDto.getType(), "HIDE")){ hideIds.addAll(actionItemDto.getTargets()); } } }else{ List<FormRuleActionItemDto> actionOther= action.getOther(); for (FormRuleActionItemDto actionItemDto : actionOther) { if(Objects.equals(actionItemDto.getType(), "SHOW")){ showIds.addAll(actionItemDto.getTargets()); } if(Objects.equals(actionItemDto.getType(), "HIDE")){ hideIds.addAll(actionItemDto.getTargets()); } } } } } } if(returnHide){ return hideIds; }else{ return showIds; } }

支撑pdo

import lombok.Data;

@Data
public class FormRuleDto {
  String name;
  FormRuleActionDto action;
  FormRuleConditionDto condition;
}

import lombok.Data;

@Data
public class FormRuleConditionItemItemDto {
  String key;
  Object value;
  String expression;

}
import java.util.List;
import lombok.Data;

@Data
public class FormRuleConditionItemDto {
  Boolean mode;
  List<FormRuleConditionItemItemDto> conditionList;

}
import java.util.List;
import lombok.Data;

@Data
public class FormRuleConditionDto {
  Boolean groupMode;
  List<FormRuleConditionItemDto> conditionList;

}
import java.util.List;
import lombok.Data;

@Data
public class FormRuleActionItemDto {
  //  SHOW HIDE
  String type;
  List<String> targets;

}
import java.util.List;
import lombok.Data;

@Data
public class FormRuleActionDto {
  List<FormRuleActionItemDto> with;
  List<FormRuleActionItemDto> other;

}

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xd.flow.engine.bo.flow.NodeUser;
import lombok.SneakyThrows;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class CommonUtil {

  /**
   * 1-26
   * 数字转大写英文字符
   *
   * @param num
   * @return
   */
  public static String numberToLetter(int num) {
    if (num <= 0) {
      return null;
    }
    String letter = "";
    num--;
    do {
      if (letter.length() > 0) {
        num--;
      }
      letter = ((char) (num % 26 + (int) 'A')) + letter;
      num = (int) ((num - num % 26) / 26);
    } while (num > 0);

    return letter;
  }

  @SneakyThrows
  public static <T> List<T> toArray(String json, Class<T> tClass) {
    Type type = TypeToken.getParameterized(ArrayList.class, tClass).getType();
    return new Gson().fromJson(json, type);
//    return JSON.parseArray(json, tClass);
  }

  @SneakyThrows
  public static <T> T toObj(String json, Class<T> tClass) {
    if (StrUtil.isBlank(json)) {
      return null;
    }
    return new Gson().fromJson(json, tClass);
  }

  @SneakyThrows
  public static String toJson(Object obj) {
    return new Gson().toJson(obj);
  }
}

该版本只支持in表达式可拓展startWith endWith contains等,动作支持hide show可拓展为其他动作

 

posted @ 2024-07-12 20:02  timseng  阅读(14)  评论(0编辑  收藏  举报