首页 > 编程语言 >动态条件实现java

动态条件实现java

时间:2024-07-12 20:10:00浏览次数:17  
标签:java String getConditionListExpressionValue List 条件 import 动态 Data public

提交页面设计

 

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可拓展为其他动作

 

标签:java,String,getConditionListExpressionValue,List,条件,import,动态,Data,public
From: https://www.cnblogs.com/timseng/p/18282112

相关文章

  • Java三剑客:封装、继承、多态的魔法世界
    第一章:封装的艺术——保护你的宝藏案例分析:银行账户系统想象一下,你正在构建一个银行账户系统。每个账户都有一个余额,这个余额需要受到严格的保护,不能被随意修改。我们可以通过封装来实现这一目标。示例代码:publicclassBankAccount{privatedoublebalance;//......
  • JAVA利用HttpClient进行POST请求(HTTPS)
    finalHttpClientUtilhttpClientUtil=newHttpClientUtil();finalMap<String,String>map=Maps.newHashMap();map.put("username",workNo);map.put("password",passwd);finalStringresultStr......
  • java将小写金额转换为大写的工具类
    摘自:http://www.cnblogs.com/niuyaoBolg/p/5654113.htmlpublicclassTool{privatestaticfinalStringUNIT="万千佰拾亿千佰拾万千佰拾元角分";privatestaticfinalStringDIGIT="零壹贰叁肆伍陆柒捌玖";privatestaticfinaldoub......
  • mybatis动态Sql(where)和sql片段
    sql片段的定义;1<sqlid="condition">2<iftest="entity.dicttype!=null">and`dicttype`=#{entity.dicttype}</if>3<iftest="entity.dictname!=nullandentity.dictname!=''......
  • 【JavaWeb程序设计】EL和JSTL
    目录一、在数据库Book中建立表Tbook,包含图书ID,图书名称,图书价格。实现模糊查询图书,如果图书的价格在50元以上,则以黄色字体显示书名。相应的模糊查询放在Servlet中实现,在图书的显示代码中使用JSTL。1.运行截图2.建表(tbook)并插入数据3.search.jsp(1)查找表单(2)CSS4.mybat......
  • JavaScript调试技巧总结
    debug javascript最全面的JavaScript调试技巧总结本文将一一讲解各种前端JS调试技巧,也许你已经熟练掌握,那让我们一起来温习,也许有你没见过的方法,不妨一起来学习,也许你尚不知如何调试,赶紧趁此机会填补空白。Thisentrywaspostedin Review andtagged debug, javasc......
  • 动态添加HTML时onclick函数参数传递
    onclick函数动态传参1.参数为数值类型时:var tmp=123;var strHTML="<divonclick=func(" +tmp+")>点击弹出数据及其类型</div>";info.append(strHTML); function func(tmp){    alert(typeof tmp+"" +tmp);}string12......
  • 同时用到,网页,java程序,数据库的web小应用
    具体实现功能:通过网页传输添加用户的请求,需要通过JDBC来向MySql添加一个用户数据第一步,部署所有需要用到的工具IDEA(2021.1),Tomcat(9),谷歌浏览器,MySql,jdk(17)第二步,创建java项目,提前部署数据库在idea中新建一个空的java项目:我创建的数据库名为learnbase,执行use......
  • 数据结构(Java):队列&集合Queue&力扣面试OJ题
    1、队列1.1队列的概念队列是一个特殊的线性表,只允许在一端(队尾)进行插入数据操作,在另一端(对头)进行删除数据。队列具有先进先出FIFO(FirstInFirstOut)的特性。入队:数据只能从队尾进队列    出队:数据只能从对头出队列即:队尾进队头出我们可以把队列想象为一个排队......
  • 深入理解 JavaScript 闭包:前端开发中的重要概念
    闭包是JavaScript中一个非常重要的概念,对于理解和编写高效、灵活的代码至关重要。尽管它看似复杂,但一旦掌握了闭包,你将能够更好地理解JavaScript的函数作用域和变量生命周期。本文将深入探讨JavaScript闭包,帮助你在前端开发中更好地运用这一强大工具。什么是闭包?闭包是指......