diff --git a/muyu-quest-server/src/main/java/com/muyu/quest/service/impl/TaskServiceImpl.java b/muyu-quest-server/src/main/java/com/muyu/quest/service/impl/TaskServiceImpl.java index 069e1b7..8545e05 100644 --- a/muyu-quest-server/src/main/java/com/muyu/quest/service/impl/TaskServiceImpl.java +++ b/muyu-quest-server/src/main/java/com/muyu/quest/service/impl/TaskServiceImpl.java @@ -156,7 +156,7 @@ public class TaskServiceImpl extends ServiceImpl // 开始节点处理 while (true){ List nextNode = NodeUtils.getNextNode(thisNode, nodeListAll); - Set nextNodeTypes = nextNode.stream().map(Node::getNodeCode).collect(Collectors.toSet()); + Set nextNodeTypes = nextNode.stream().map(Node::getNodeType).collect(Collectors.toSet()); if (nextNode.isEmpty()){ throw new TaskException("任务执行失败,节点 "+thisNode+" 无后续节点"); }else if (nextNodeTypes.size()>1){ @@ -173,20 +173,22 @@ public class TaskServiceImpl extends ServiceImpl .selectNodeDispositionList(new NodeDisposition().buildNodeCode(thisNode.getNodeCode())); // 根据情况拼接查询sql与新增sql if(StringUtils.equals(thisNode.getNodeType(), "table")) { - // 如果表节点下一级为数据输出节点,即表示为单表查询,将 当前表结构处理 + // 如果表节点下一级为数据输出节点,即表示为单表查询,将 当前表结构处理为查询语句 Node newNode = NodeUtils.getNextNode(thisNode, nodeListAll).get(0); if (StringUtils.equals(newNode.getNodeType(), "exportation")){ - findSql = DispUtils.tableNode(dispList); + findSql = NodeUtils.tableNode(dispList); } }else if (StringUtils.equals(thisNode.getNodeType(), "unite")){ - findSql = DispUtils.nodeDispUnite(dispList); + findSql = NodeUtils.nodeDispUnite(dispList); }else if (StringUtils.equals(thisNode.getNodeType(), "exportation")){ -// addSql = exportationNode(thisNode); + addSql = NodeUtils.nodeDispExportation(dispList); } } - log.info("任务执行完毕,查询sql为: {},新增sql为: {}",findSql, addSql); - return ""; + log.info("任务执行结束,最终sql为: [{}]",addSql+" "+findSql); + return addSql+" "+findSql; } + + } diff --git a/muyu-quest-server/src/main/java/com/muyu/quest/utils/DispUtils.java b/muyu-quest-server/src/main/java/com/muyu/quest/utils/DispUtils.java index cade7f2..2ad2115 100644 --- a/muyu-quest-server/src/main/java/com/muyu/quest/utils/DispUtils.java +++ b/muyu-quest-server/src/main/java/com/muyu/quest/utils/DispUtils.java @@ -28,8 +28,9 @@ public class DispUtils { /** * 筛选节点配置信息 - * @param nodeType 节点类型 - * @param nodeTypeList 节点类型集合 + * + * @param nodeType 节点类型 + * @param nodeTypeList 节点类型集合 * @return 配置key数组 */ public static String[] getDispKeys(String nodeType, List nodeTypeList) { @@ -44,9 +45,10 @@ public class DispUtils { /** * 整理节点配置信息 - * @param nodeType 节点类型 - * @param nodeTypeList 节点类型集合 - * @param dispList 节点配置集合 + * + * @param nodeType 节点类型 + * @param nodeTypeList 节点类型集合 + * @param dispList 节点配置集合 * @return map集合 */ public static Map> getDispMap( @@ -66,7 +68,7 @@ public class DispUtils { public static Map> getDispMap(String[] dispKeys, List dispList) { HashMap> dispMap = new HashMap<>(); for (String nodeTypeName : dispKeys) { - dispMap.put(nodeTypeName,dispList.stream() + dispMap.put(nodeTypeName, dispList.stream() .filter(disp -> StringUtils.equals(disp.getDispKey(), nodeTypeName)) .toList()); } @@ -75,14 +77,15 @@ public class DispUtils { /** * 整理节点配置信息 - * @param dispList 节点配置集合 + * + * @param dispList 节点配置集合 * @return map集合 */ public static Map> getDispMap(List dispList) { HashMap> map = new HashMap<>(); dispList.forEach(disp -> { List dispositions = map.get(disp.getDispKey()); - if (dispositions == null || dispositions.isEmpty()){ + if (dispositions == null || dispositions.isEmpty()) { dispositions = new ArrayList<>(); } dispositions.add(disp); @@ -91,83 +94,4 @@ public class DispUtils { return map; } - /** - * 联合查询节点处理 - * @param dispList 节点全部配置信息 - * @return 查询sql - */ - public static String nodeDispUnite(List dispList) { - return nodeDispUnite(getDispMap(dispList)); - } - - - /** - * 联合查询节点处理 - * @param dispMap 节点整理后的Map配置信息 - * @return 查询sql - */ - public static String nodeDispUnite(Map> dispMap) { - List dbList = dispMap.get("db"); - List tableList = dispMap.get("table"); - List fieldList = dispMap.get("fields"); - NodeDisposition join = dispMap.get("join").get(0); - NodeDisposition joinDataForm = dispMap.get("joinDataForm").get(0); - NodeDisposition joinDataTo = dispMap.get("joinDataTo").get(0); - // 查询表 - Object[] array = tableList.stream().map(NodeDisposition::getDispDesc).toArray(); - String table = array[0].toString() + - " " + - join.getDispValue() + - " " + - array[1].toString() + - " ON " + - joinDataForm.getDispDesc() + - "." + - joinDataForm.getDispValue() + - " = " + - joinDataTo.getDispDesc() + - "." + - joinDataTo.getDispValue(); - // 查询列 - String field = StringUtils.join(fieldList - .stream() - .map(fields -> fields.getDispDesc()+"."+fields.getDispValue()) - .toArray(), - ","); - Set dbNameSet = dbList.stream().map(NodeDisposition::getDispValue).collect(Collectors.toSet()); - for (Object o : dbNameSet) { - table = StringUtils.replace(table, o.toString()+".", "`" + o.toString() + "`."); - field = StringUtils.replace(field, o.toString()+".", "`" + o.toString() + "`."); - } - - return "SELECT " + field + " FROM " + table; - } - - - /** - * 表结构节点处理 - * @param dispList 节点全部配置信息 - * @return 查询sql - */ - public static String tableNode(List dispList) { - return tableNode(getDispMap(dispList)); - } - - - /** - * 表结构节点处理 - * @param dispMap 节点整理后的Map配置信息 - * @return 查询sql - */ - private static String tableNode(Map> dispMap) { - NodeDisposition db = dispMap.get("db").get(0); - NodeDisposition table = dispMap.get("table").get(0); - List fieldList = dispMap.get("fields"); - String dbTable = "`"+db.getDispValue()+"`."+table.getDispValue(); - String fields = StringUtils.join(fieldList.stream(). - map(field -> dbTable + "." + field.getDispValue()). - toArray(), - ","); - return "SELECT " + fields + " FROM " + dbTable; - } } diff --git a/muyu-quest-server/src/main/java/com/muyu/quest/utils/NodeUtils.java b/muyu-quest-server/src/main/java/com/muyu/quest/utils/NodeUtils.java index c30aac0..f39a25f 100644 --- a/muyu-quest-server/src/main/java/com/muyu/quest/utils/NodeUtils.java +++ b/muyu-quest-server/src/main/java/com/muyu/quest/utils/NodeUtils.java @@ -15,10 +15,7 @@ import com.muyu.quest.domain.NodeDisposition; import com.muyu.quest.domain.NodeType; import com.muyu.quest.exception.TaskException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; /** @@ -30,6 +27,7 @@ import java.util.stream.Collectors; */ public class NodeUtils { + private static HashMap sqlMap = new HashMap<>(); /** * 节点初始化 * 将所有节点按节点类型分类 @@ -77,43 +75,19 @@ public class NodeUtils { * @param nodes 所有节点 * @return 下级节点列表 */ - public static List getNextNode(Node node, List nodes){ - if (nodes == null || nodes.isEmpty()){ + public static List getNextNode(Node node, List nodes) { + if (nodes == null || nodes.isEmpty()) { throw new TaskException("节点列表为空"); - }else if (node == null){ + } else if (node == null) { throw new TaskException("当前节点为空"); } return nodes.stream() .filter(nodeIndex -> - StringUtils.equals(node.getNodeCode() ,nodeIndex.getNodePreCode()) || - StringUtils.equals(node.getNodeNextCode() ,nodeIndex.getNodeCode())) + StringUtils.equals(node.getNodeCode(), nodeIndex.getNodePreCode()) || + StringUtils.equals(node.getNodeNextCode(), nodeIndex.getNodeCode())) .toList(); } - /** - * 节点处理 - 数据输出 - * @param dispList 节点配置信息 - * @return map - 数据库+sql - */ - public static Map nodeDispOutput(List dispList){ - // 3. 获取节点配置信息 - Map> dispMap = DispUtils.getDispMap(dispList); - NodeDisposition toDbList = dispMap.get("toDb").get(0); - List toFieldList = dispMap.get("toFields"); - // 数据库与表 - String dbName = toDbList.getDispValue().toString()+"."+toDbList.getDispDesc(); - // 拼接sql - String sql = "INSERT INTO "+dbName+"("+ - toFieldList.stream().map(NodeDisposition::getDispDesc).collect(Collectors.joining(","))+ - ") VALUES("+ - toFieldList.stream().map(NodeDisposition::getDispValue).map(Object::toString).collect(Collectors.joining(","))+ - ")"; - HashMap hashMap = new HashMap<>(); - hashMap.put("dbName",dbName); - hashMap.put("sql",sql); - return hashMap; - } - /** * 校验任务流程是否符合节点规范 @@ -157,4 +131,137 @@ public class NodeUtils { }); } + /** + * 联合查询节点处理 + * @param dispList 节点全部配置信息 + * @return 查询sql + */ + public static String nodeDispUnite(List dispList) { + return nodeDispUnite(DispUtils.getDispMap(dispList)); + } + + + /** + * 联合查询节点处理 + * @param dispMap 节点整理后的Map配置信息 + * @return 查询sql + */ + public static String nodeDispUnite(Map> dispMap) { + List dbList = dispMap.get("db"); + List tableList = dispMap.get("table"); + List fieldList = dispMap.get("fields"); + NodeDisposition join = dispMap.get("join").get(0); + NodeDisposition joinDataForm = dispMap.get("joinDataForm").get(0); + NodeDisposition joinDataTo = dispMap.get("joinDataTo").get(0); + // 查询表 + Object[] array = tableList.stream().map(NodeDisposition::getDispDesc).toArray(); + String table = array[0].toString() + + " " + + join.getDispValue() + + " " + + array[1].toString() + + " ON " + + joinDataForm.getDispDesc() + + "." + + joinDataForm.getDispValue() + + " = " + + joinDataTo.getDispDesc() + + "." + + joinDataTo.getDispValue(); + // 查询列 + String field = StringUtils.join(fieldList + .stream() + .map(fields -> fields.getDispDesc()+"."+fields.getDispValue()) + .toArray(), + ","); + Set dbNameSet = dbList.stream().map(NodeDisposition::getDispValue).collect(Collectors.toSet()); + for (Object o : dbNameSet) { + table = StringUtils.replace(table, o.toString()+".", "`" + o.toString() + "`."); + field = StringUtils.replace(field, o.toString()+".", "`" + o.toString() + "`."); + } + sqlMap = new HashMap<>(); + sqlMap.put("dbTable",table); + sqlMap.put("fields",field); + return findSqlSplice(table, field); + } + + + /** + * 表结构节点处理 + * @param dispList 节点全部配置信息 + * @return 查询sql + */ + public static String tableNode(List dispList) { + return tableNode(DispUtils.getDispMap(dispList)); + } + + + /** + * 表结构节点处理 + * @param dispMap 节点整理后的Map配置信息 + * @return 查询sql + */ + private static String tableNode(Map> dispMap) { + NodeDisposition db = dispMap.get("db").get(0); + NodeDisposition table = dispMap.get("table").get(0); + List fieldList = dispMap.get("fields"); + String dbTable = "`"+db.getDispValue()+"`."+table.getDispValue(); + String fields = StringUtils.join(fieldList.stream(). + map(field -> dbTable + "." + field.getDispValue()). + toArray(), + ","); + + sqlMap = new HashMap<>(); + sqlMap.put("dbTable",dbTable); + sqlMap.put("fields",fields); + return findSqlSplice(dbTable, fields); + } + + /** + * 查询sql拼接 + * @param table 表名 + * @param fields 字段 + * @return sql语句 + */ + private static String findSqlSplice(String table, String fields) { + return "SELECT " + fields + " FROM " + table; + } + + /** + * 输出节点 节点处理 + * + * @param dispList 节点全部配置信息 + * @return 新增sql + */ + public static String nodeDispExportation(List dispList) { + return nodeDispExportation(DispUtils.getDispMap(dispList)); + } + + + /** + * 输出节点 节点处理 + * + * @param dispMap 节点整理后的Map配置信息 + * @return 新增sql + */ + private static String nodeDispExportation(Map> dispMap) { + // 拼接新增表 + NodeDisposition db = dispMap.get("toDb").get(0); + String dbTable = "`" + db.getDispDesc() + "`." + db.getDispValue(); + // 根据表结构拼接新增字段 + List fieldList = dispMap.get("toFields"); + HashMap map = new HashMap<>(); + fieldList.forEach(field -> map.put(field.getDispDesc(),field.getDispValue().toString())); + List findFieldList = Arrays. + stream(sqlMap.get("fields").split(",")). + map(field -> field.split("\\.")[2]). + toList(); + String insSql = "INSERT INTO " + + dbTable + + "(" + + StringUtils.join(findFieldList.stream().map(map::get).toArray(), ",") + + ")"; + return insSql; + } + }