diff --git a/pom.xml b/pom.xml index 425cd77..ee1e783 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-web + com.baomidou diff --git a/src/main/java/com/example/liteflow/mysql/controller/SubmitRoleController.java b/src/main/java/com/example/liteflow/mysql/controller/SubmitRoleController.java new file mode 100644 index 0000000..39a6875 --- /dev/null +++ b/src/main/java/com/example/liteflow/mysql/controller/SubmitRoleController.java @@ -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 roles = roleForm.getRoles(); + List> collect = roles.stream().map(RoleUtil::parseRole).collect(Collectors.toList()); + System.out.println(JSONUtil.toJsonStr(collect)); + } +} diff --git a/src/main/java/com/example/liteflow/mysql/model/role/Match.java b/src/main/java/com/example/liteflow/mysql/model/role/Match.java new file mode 100644 index 0000000..9565d65 --- /dev/null +++ b/src/main/java/com/example/liteflow/mysql/model/role/Match.java @@ -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; +} diff --git a/src/main/java/com/example/liteflow/mysql/model/role/Role.java b/src/main/java/com/example/liteflow/mysql/model/role/Role.java new file mode 100644 index 0000000..108a4b7 --- /dev/null +++ b/src/main/java/com/example/liteflow/mysql/model/role/Role.java @@ -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 children; + private List matches; +} diff --git a/src/main/java/com/example/liteflow/mysql/model/role/RoleForm.java b/src/main/java/com/example/liteflow/mysql/model/role/RoleForm.java new file mode 100644 index 0000000..f32c3c2 --- /dev/null +++ b/src/main/java/com/example/liteflow/mysql/model/role/RoleForm.java @@ -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 roles; +} diff --git a/src/main/java/com/example/liteflow/mysql/util/RoleUtil.java b/src/main/java/com/example/liteflow/mysql/util/RoleUtil.java new file mode 100644 index 0000000..420b5e0 --- /dev/null +++ b/src/main/java/com/example/liteflow/mysql/util/RoleUtil.java @@ -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 parseRole(Role role) { + List matchList = role.getMatches(); + List roleChildren = role.getChildren(); + String logicOperators = role.getLogicOperators(); + if (CollUtil.isNotEmpty(roleChildren)) { + List> collect = roleChildren.stream().map(child -> { + List sunList = child.getChildren(); + if (CollUtil.isNotEmpty(sunList)) { + List> sunCollect = sunList.stream().map(RoleUtil::parseRole).collect(Collectors.toList()); + String childLogicOperators = child.getLogicOperators(); + Map itemMap = new HashMap<>(); + itemMap.put(childLogicOperators, sunCollect); + return itemMap; + } else { + List childMatches = child.getMatches(); + String childLogicOperators = child.getLogicOperators(); + List>> list = childMatches.stream().map(item -> { + String conditionalOperators = item.getConditionalOperators(); + String matchingValue = item.getMatchingValue(); + String matchingContent = item.getMatchingContent(); + List itemList = Lists.newArrayList(matchingValue, matchingContent); + Map> itemMap = new HashMap<>(); + itemMap.put(conditionalOperators, itemList); + return itemMap; + }).collect(Collectors.toList()); + Map childMap = new HashMap<>(); + childMap.put(childLogicOperators, list); + return childMap; + } + }).collect(Collectors.toList()); + Map childMap = new HashMap<>(); + childMap.put(logicOperators, collect); + return childMap; + } else if (CollUtil.isNotEmpty(matchList)){ + List> list = matchList.stream().map(matche -> { + String conditionalOperators = matche.getConditionalOperators(); + String matchingValue = matche.getMatchingValue(); + String matchingContent = matche.getMatchingContent(); + List itemList = Lists.newArrayList(matchingValue, matchingContent); + Map itemMap = new HashMap<>(); + itemMap.put(conditionalOperators, itemList); + return itemMap; + }).collect(Collectors.toList()); + Map childMap = new HashMap<>(); + childMap.put(logicOperators, list); + return childMap; + } else { + String matchingValue = role.getMatchingValue(); + String matchingContent = role.getMatchingContent(); + String conditionalOperators = role.getConditionalOperators(); + List itemList = Lists.newArrayList(matchingValue, matchingContent); + Map itemMap = new HashMap<>(); + itemMap.put(conditionalOperators, itemList); + List> list = Lists.newArrayList(itemMap); + Map map = new HashMap<>(); + map.put(logicOperators, list); + return map; + } + } +} diff --git a/src/test/java/MyTest.java b/src/test/java/MyTest.java new file mode 100644 index 0000000..fe8449e --- /dev/null +++ b/src/test/java/MyTest.java @@ -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 roles = new ArrayList<>(); + Role role = new Role(); + role.setLogicOperators(MathOperatorEnum.or.getOperator()); + + List children = new ArrayList<>(); + Role childRole = new Role(); + childRole.setLogicOperators(MathOperatorEnum.and.getOperator()); + + List 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> collect = roles.stream().map(this::parseRole).collect(Collectors.toList()); + System.out.println(JSONUtil.toJsonStr(collect)); + + } + + @Test + public void test02() { + List 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 sunList = new ArrayList<>(); + + List 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 children = Lists.newArrayList(child); + role.setChildren(children); + roles.add(role); + System.out.println(JSONUtil.toJsonStr(roles)); + + List> 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 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 roles = Lists.newArrayList(role); + System.out.println(JSONUtil.toJsonStr(roles)); + List> 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 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 roles = Lists.newArrayList(role); + System.out.println(JSONUtil.toJsonStr(roles)); + List> 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 roles = Lists.newArrayList(role); + System.out.println(JSONUtil.toJsonStr(roles)); + List> 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 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 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 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 children2 = Lists.newArrayList(child2); + role2.setChildren(children2); + + List roles = Lists.newArrayList(role, role2); + System.out.println(JSONUtil.toJsonStr(roles)); + List> collect = roles.stream().map(this::parseRole).collect(Collectors.toList()); + System.out.println(JSONUtil.toJsonStr(collect)); + } + private Map parseRole(Role role) { + List matchList = role.getMatches(); + List roleChildren = role.getChildren(); + String logicOperators = role.getLogicOperators(); + if (CollUtil.isNotEmpty(roleChildren)) { + List> collect = roleChildren.stream().map(child -> { + List sunList = child.getChildren(); + if (CollUtil.isNotEmpty(sunList)) { + List> sunCollect = sunList.stream().map(this::parseRole).collect(Collectors.toList()); + String childLogicOperators = child.getLogicOperators(); + Map itemMap = new HashMap<>(); + itemMap.put(childLogicOperators, sunCollect); + return itemMap; + } else { + List childMatches = child.getMatches(); + String childLogicOperators = child.getLogicOperators(); + List>> list = childMatches.stream().map(item -> { + String conditionalOperators = item.getConditionalOperators(); + String matchingValue = item.getMatchingValue(); + String matchingContent = item.getMatchingContent(); + List itemList = Lists.newArrayList(matchingValue, matchingContent); + Map> itemMap = new HashMap<>(); + itemMap.put(conditionalOperators, itemList); + return itemMap; + }).collect(Collectors.toList()); + Map childMap = new HashMap<>(); + childMap.put(childLogicOperators, list); + return childMap; + } + }).collect(Collectors.toList()); + Map childMap = new HashMap<>(); + childMap.put(logicOperators, collect); + return childMap; + } else if (CollUtil.isNotEmpty(matchList)){ + List> list = matchList.stream().map(matche -> { + String conditionalOperators = matche.getConditionalOperators(); + String matchingValue = matche.getMatchingValue(); + String matchingContent = matche.getMatchingContent(); + List itemList = Lists.newArrayList(matchingValue, matchingContent); + Map itemMap = new HashMap<>(); + itemMap.put(conditionalOperators, itemList); + return itemMap; + }).collect(Collectors.toList()); + Map childMap = new HashMap<>(); + childMap.put(logicOperators, list); + return childMap; + } else { + String matchingValue = role.getMatchingValue(); + String matchingContent = role.getMatchingContent(); + String conditionalOperators = role.getConditionalOperators(); + List itemList = Lists.newArrayList(matchingValue, matchingContent); + Map itemMap = new HashMap<>(); + itemMap.put(conditionalOperators, itemList); + List> list = Lists.newArrayList(itemMap); + Map map = new HashMap<>(); + map.put(logicOperators, list); + return map; + } + } +}