1.新增解析方法

This commit is contained in:
chenweilong 2022-12-08 15:40:55 +08:00
parent 04cdddede9
commit ed9b7789b8
7 changed files with 433 additions and 0 deletions

View File

@ -31,6 +31,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>

View File

@ -0,0 +1,31 @@
package com.example.liteflow.mysql.controller;
import cn.hutool.json.JSONUtil;
import com.example.liteflow.mysql.model.role.Role;
import com.example.liteflow.mysql.model.role.RoleForm;
import com.example.liteflow.mysql.util.RoleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 10:36
*/
@Slf4j
@RestController
@RequestMapping("/submitRole")
public class SubmitRoleController {
@PostMapping
public void orderTicket(@RequestBody RoleForm roleForm) {
List<Role> roles = roleForm.getRoles();
List<Map<String, Object>> collect = roles.stream().map(RoleUtil::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
}

View File

@ -0,0 +1,17 @@
package com.example.liteflow.mysql.model.role;
import lombok.Data;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 17:55
*/
@Data
public class Match {
// 条件运算符:"$eq"
private String conditionalOperators;
// 匹配值
private String matchingValue;
// 匹配内容
private String matchingContent;
}

View File

@ -0,0 +1,23 @@
package com.example.liteflow.mysql.model.role;
import lombok.Data;
import java.util.List;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 16:04
*/
@Data
public class Role {
// 逻辑运算符"$or"
private String logicOperators;
// 条件运算符:"$eq"
private String conditionalOperators;
// 匹配值
private String matchingValue;
// 匹配内容
private String matchingContent;
private List<Role> children;
private List<Match> matches;
}

View File

@ -0,0 +1,14 @@
package com.example.liteflow.mysql.model.role;
import lombok.Data;
import java.util.List;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 10:41
*/
@Data
public class RoleForm {
private List<Role> roles;
}

View File

@ -0,0 +1,77 @@
package com.example.liteflow.mysql.util;
import cn.hutool.core.collection.CollUtil;
import com.example.liteflow.mysql.model.role.Match;
import com.example.liteflow.mysql.model.role.Role;
import org.assertj.core.util.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: 陈韦龙
* @date: 2022年12月08日 15:37
*/
public class RoleUtil {
public static Map<String, Object> parseRole(Role role) {
List<Match> matchList = role.getMatches();
List<Role> roleChildren = role.getChildren();
String logicOperators = role.getLogicOperators();
if (CollUtil.isNotEmpty(roleChildren)) {
List<Map<String,Object>> collect = roleChildren.stream().map(child -> {
List<Role> sunList = child.getChildren();
if (CollUtil.isNotEmpty(sunList)) {
List<Map<String, Object>> sunCollect = sunList.stream().map(RoleUtil::parseRole).collect(Collectors.toList());
String childLogicOperators = child.getLogicOperators();
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(childLogicOperators, sunCollect);
return itemMap;
} else {
List<Match> childMatches = child.getMatches();
String childLogicOperators = child.getLogicOperators();
List<Map<String, List<String>>> list = childMatches.stream().map(item -> {
String conditionalOperators = item.getConditionalOperators();
String matchingValue = item.getMatchingValue();
String matchingContent = item.getMatchingContent();
List<String> itemList = Lists.newArrayList(matchingValue, matchingContent);
Map<String, List<String>> itemMap = new HashMap<>();
itemMap.put(conditionalOperators, itemList);
return itemMap;
}).collect(Collectors.toList());
Map<String, Object> childMap = new HashMap<>();
childMap.put(childLogicOperators, list);
return childMap;
}
}).collect(Collectors.toList());
Map<String, Object> childMap = new HashMap<>();
childMap.put(logicOperators, collect);
return childMap;
} else if (CollUtil.isNotEmpty(matchList)){
List<Map<String, Object>> list = matchList.stream().map(matche -> {
String conditionalOperators = matche.getConditionalOperators();
String matchingValue = matche.getMatchingValue();
String matchingContent = matche.getMatchingContent();
List<String> itemList = Lists.newArrayList(matchingValue, matchingContent);
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(conditionalOperators, itemList);
return itemMap;
}).collect(Collectors.toList());
Map<String, Object> childMap = new HashMap<>();
childMap.put(logicOperators, list);
return childMap;
} else {
String matchingValue = role.getMatchingValue();
String matchingContent = role.getMatchingContent();
String conditionalOperators = role.getConditionalOperators();
List<String> itemList = Lists.newArrayList(matchingValue, matchingContent);
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(conditionalOperators, itemList);
List<Map<String, Object>> list = Lists.newArrayList(itemMap);
Map<String, Object> map = new HashMap<>();
map.put(logicOperators, list);
return map;
}
}
}

267
src/test/java/MyTest.java Normal file
View File

@ -0,0 +1,267 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.example.liteflow.mysql.enums.MathOperatorEnum;
import com.example.liteflow.mysql.model.role.Match;
import com.example.liteflow.mysql.model.role.Role;
import com.example.liteflow.mysql.model.role.RoleForm;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 16:26
*/
public class MyTest {
@Test
public void test01() {
List<Role> roles = new ArrayList<>();
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
List<Role> children = new ArrayList<>();
Role childRole = new Role();
childRole.setLogicOperators(MathOperatorEnum.and.getOperator());
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("1");
match.setMatchingContent("1");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("2");
match.setMatchingContent("1");
matches.add(match);
childRole.setMatches(matches);
children.add(childRole);
role.setChildren(children);
roles.add(role);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test02() {
List<Role> roles = new ArrayList<>();
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
Role child = new Role();
child.setLogicOperators(MathOperatorEnum.or.getOperator());
Role sun = new Role();
sun.setLogicOperators(MathOperatorEnum.or.getOperator());
List<Role> sunList = new ArrayList<>();
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("1");
match.setMatchingContent("1");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("2");
match.setMatchingContent("1");
matches.add(match);
sun.setMatches(matches);
sunList.add(sun);
child.setChildren(sunList);
List<Role> children = Lists.newArrayList(child);
role.setChildren(children);
roles.add(role);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test3() {
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("1");
match.setMatchingContent("1");
matches.add(match);
role.setMatches(matches);
List<Role> roles = Lists.newArrayList(role);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test4() {
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("1");
match.setMatchingContent("1");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("2");
match.setMatchingContent("2");
matches.add(match);
role.setMatches(matches);
List<Role> roles = Lists.newArrayList(role);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test5() {
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
role.setConditionalOperators(MathOperatorEnum.eq.getOperator());
role.setMatchingValue("1");
role.setMatchingContent("1");
List<Role> roles = Lists.newArrayList(role);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test6() {
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
Role child = new Role();
child.setLogicOperators(MathOperatorEnum.and.getOperator());
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("1");
match.setMatchingContent("1");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match.setMatchingValue("2");
match.setMatchingContent("2");
matches.add(match);
child.setMatches(matches);
List<Role> children = Lists.newArrayList(child);
role.setChildren(children);
Role role2 = new Role();
role2.setLogicOperators(MathOperatorEnum.or.getOperator());
Role child2 = new Role();
child2.setLogicOperators(MathOperatorEnum.and.getOperator());
List<Match> matches2 = new ArrayList<>();
Match match2 = new Match();
match2.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match2.setMatchingValue("3");
match2.setMatchingContent("3");
matches2.add(match2);
match2 = new Match();
match2.setConditionalOperators(MathOperatorEnum.eq.getOperator());
match2.setMatchingValue("4");
match2.setMatchingContent("4");
matches2.add(match2);
child2.setMatches(matches2);
List<Role> children2 = Lists.newArrayList(child2);
role2.setChildren(children2);
List<Role> roles = Lists.newArrayList(role, role2);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
private Map<String, Object> parseRole(Role role) {
List<Match> matchList = role.getMatches();
List<Role> roleChildren = role.getChildren();
String logicOperators = role.getLogicOperators();
if (CollUtil.isNotEmpty(roleChildren)) {
List<Map<String,Object>> collect = roleChildren.stream().map(child -> {
List<Role> sunList = child.getChildren();
if (CollUtil.isNotEmpty(sunList)) {
List<Map<String, Object>> sunCollect = sunList.stream().map(this::parseRole).collect(Collectors.toList());
String childLogicOperators = child.getLogicOperators();
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(childLogicOperators, sunCollect);
return itemMap;
} else {
List<Match> childMatches = child.getMatches();
String childLogicOperators = child.getLogicOperators();
List<Map<String, List<String>>> list = childMatches.stream().map(item -> {
String conditionalOperators = item.getConditionalOperators();
String matchingValue = item.getMatchingValue();
String matchingContent = item.getMatchingContent();
List<String> itemList = Lists.newArrayList(matchingValue, matchingContent);
Map<String, List<String>> itemMap = new HashMap<>();
itemMap.put(conditionalOperators, itemList);
return itemMap;
}).collect(Collectors.toList());
Map<String, Object> childMap = new HashMap<>();
childMap.put(childLogicOperators, list);
return childMap;
}
}).collect(Collectors.toList());
Map<String, Object> childMap = new HashMap<>();
childMap.put(logicOperators, collect);
return childMap;
} else if (CollUtil.isNotEmpty(matchList)){
List<Map<String, Object>> list = matchList.stream().map(matche -> {
String conditionalOperators = matche.getConditionalOperators();
String matchingValue = matche.getMatchingValue();
String matchingContent = matche.getMatchingContent();
List<String> itemList = Lists.newArrayList(matchingValue, matchingContent);
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(conditionalOperators, itemList);
return itemMap;
}).collect(Collectors.toList());
Map<String, Object> childMap = new HashMap<>();
childMap.put(logicOperators, list);
return childMap;
} else {
String matchingValue = role.getMatchingValue();
String matchingContent = role.getMatchingContent();
String conditionalOperators = role.getConditionalOperators();
List<String> itemList = Lists.newArrayList(matchingValue, matchingContent);
Map<String, Object> itemMap = new HashMap<>();
itemMap.put(conditionalOperators, itemList);
List<Map<String, Object>> list = Lists.newArrayList(itemMap);
Map<String, Object> map = new HashMap<>();
map.put(logicOperators, list);
return map;
}
}
}