完成执行任务接口,执行任务后生成对应的新增查询sql
parent
62ab73a6fb
commit
7c92817489
|
@ -156,7 +156,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
|
|||
// 开始节点处理
|
||||
while (true){
|
||||
List<Node> nextNode = NodeUtils.getNextNode(thisNode, nodeListAll);
|
||||
Set<String> nextNodeTypes = nextNode.stream().map(Node::getNodeCode).collect(Collectors.toSet());
|
||||
Set<String> 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<TaskMapper, Task>
|
|||
.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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -28,8 +28,9 @@ public class DispUtils {
|
|||
|
||||
/**
|
||||
* 筛选节点配置信息
|
||||
* @param nodeType 节点类型
|
||||
* @param nodeTypeList 节点类型集合
|
||||
*
|
||||
* @param nodeType 节点类型
|
||||
* @param nodeTypeList 节点类型集合
|
||||
* @return 配置key数组
|
||||
*/
|
||||
public static String[] getDispKeys(String nodeType, List<NodeType> 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<String, List<NodeDisposition>> getDispMap(
|
||||
|
@ -66,7 +68,7 @@ public class DispUtils {
|
|||
public static Map<String, List<NodeDisposition>> getDispMap(String[] dispKeys, List<NodeDisposition> dispList) {
|
||||
HashMap<String, List<NodeDisposition>> 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<String, List<NodeDisposition>> getDispMap(List<NodeDisposition> dispList) {
|
||||
HashMap<String, List<NodeDisposition>> map = new HashMap<>();
|
||||
dispList.forEach(disp -> {
|
||||
List<NodeDisposition> 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<NodeDisposition> dispList) {
|
||||
return nodeDispUnite(getDispMap(dispList));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 联合查询节点处理
|
||||
* @param dispMap 节点整理后的Map配置信息
|
||||
* @return 查询sql
|
||||
*/
|
||||
public static String nodeDispUnite(Map<String,List<NodeDisposition>> dispMap) {
|
||||
List<NodeDisposition> dbList = dispMap.get("db");
|
||||
List<NodeDisposition> tableList = dispMap.get("table");
|
||||
List<NodeDisposition> 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<Object> 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<NodeDisposition> dispList) {
|
||||
return tableNode(getDispMap(dispList));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 表结构节点处理
|
||||
* @param dispMap 节点整理后的Map配置信息
|
||||
* @return 查询sql
|
||||
*/
|
||||
private static String tableNode(Map<String, List<NodeDisposition>> dispMap) {
|
||||
NodeDisposition db = dispMap.get("db").get(0);
|
||||
NodeDisposition table = dispMap.get("table").get(0);
|
||||
List<NodeDisposition> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, String> sqlMap = new HashMap<>();
|
||||
/**
|
||||
* 节点初始化
|
||||
* 将所有节点按节点类型分类
|
||||
|
@ -77,43 +75,19 @@ public class NodeUtils {
|
|||
* @param nodes 所有节点
|
||||
* @return 下级节点列表
|
||||
*/
|
||||
public static List<Node> getNextNode(Node node, List<Node> nodes){
|
||||
if (nodes == null || nodes.isEmpty()){
|
||||
public static List<Node> getNextNode(Node node, List<Node> 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<String, String> nodeDispOutput(List<NodeDisposition> dispList){
|
||||
// 3. 获取节点配置信息
|
||||
Map<String, List<NodeDisposition>> dispMap = DispUtils.getDispMap(dispList);
|
||||
NodeDisposition toDbList = dispMap.get("toDb").get(0);
|
||||
List<NodeDisposition> 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<String, String> 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<NodeDisposition> dispList) {
|
||||
return nodeDispUnite(DispUtils.getDispMap(dispList));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 联合查询节点处理
|
||||
* @param dispMap 节点整理后的Map配置信息
|
||||
* @return 查询sql
|
||||
*/
|
||||
public static String nodeDispUnite(Map<String,List<NodeDisposition>> dispMap) {
|
||||
List<NodeDisposition> dbList = dispMap.get("db");
|
||||
List<NodeDisposition> tableList = dispMap.get("table");
|
||||
List<NodeDisposition> 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<Object> 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<NodeDisposition> dispList) {
|
||||
return tableNode(DispUtils.getDispMap(dispList));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 表结构节点处理
|
||||
* @param dispMap 节点整理后的Map配置信息
|
||||
* @return 查询sql
|
||||
*/
|
||||
private static String tableNode(Map<String, List<NodeDisposition>> dispMap) {
|
||||
NodeDisposition db = dispMap.get("db").get(0);
|
||||
NodeDisposition table = dispMap.get("table").get(0);
|
||||
List<NodeDisposition> 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<NodeDisposition> dispList) {
|
||||
return nodeDispExportation(DispUtils.getDispMap(dispList));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 输出节点 节点处理
|
||||
*
|
||||
* @param dispMap 节点整理后的Map配置信息
|
||||
* @return 新增sql
|
||||
*/
|
||||
private static String nodeDispExportation(Map<String, List<NodeDisposition>> dispMap) {
|
||||
// 拼接新增表
|
||||
NodeDisposition db = dispMap.get("toDb").get(0);
|
||||
String dbTable = "`" + db.getDispDesc() + "`." + db.getDispValue();
|
||||
// 根据表结构拼接新增字段
|
||||
List<NodeDisposition> fieldList = dispMap.get("toFields");
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
fieldList.forEach(field -> map.put(field.getDispDesc(),field.getDispValue().toString()));
|
||||
List<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue