using System.Text.RegularExpressions;
using Feign;
using Infrastructure;
using LitJson;
using Model.Custom;
using Model.Source;
using Services;
namespace Util.Logic
{
public class LogicHelper
{
///
/// 获取逻辑项目的Json数据
///
/// 项目ID
/// 请求参数
public static Dictionary DoLogic(int projectId, string request)
{
var logicProjectService = App.GetService();
var logicProject = logicProjectService.GetById(projectId);
if(logicProject == null)
{
return new Dictionary();
}
string jsonData = logicProject.setData ?? string.Empty;
if(string.IsNullOrEmpty(jsonData))
{
return new Dictionary();
}
JsonData requestObj = JsonMapper.ToObject(request);
JsonData jsonDataObj = JsonMapper.ToObject(jsonData);
JsonData nodes = jsonDataObj["nodes"]; // 节点数据
List nodeList = new List(); // 节点列表
foreach(JsonData node in nodes)
{
JsonData id = node["id"];
JsonData type = node["type"];
NodeList nodeListObj = new NodeList();
nodeListObj.nodeId = id.ToString();
nodeListObj.nodeType = type.ToString();
nodeListObj.nodeData = node;
nodeList.Add(nodeListObj);
}
List edgeList = new List(); // 边列表
JsonData edges = jsonDataObj["edges"]; // 边数据
foreach(JsonData edge in edges)
{
JsonData source = edge["source"];
JsonData target = edge["target"];
EdgeList edgeListObj = new EdgeList();
edgeListObj.source = source.ToString();
edgeListObj.target = target.ToString();
edgeListObj.edgeData = edge;
edgeList.Add(edgeListObj);
}
string startNodeId = "start"; // 起始节点ID
// 获取起始节点数据
List paramList = new List();
NodeList startNode = nodeList.FirstOrDefault(x => x.nodeId == startNodeId) ?? new NodeList();
JsonData nodeData = startNode.nodeData;
JsonData input = nodeData["data"]["formData"]["input"]["value"];
for(int i = 0; i < input.Count; i++)
{
JsonData item = input[i];
string paramName = item["label"].ToString();
string paramType = item["type"][0].ToString();
string paramValue = item["value"].ToString();
ParamList paramListObj = new ParamList();
paramListObj.nodeId = startNodeId;
paramListObj.paramName = paramName;
paramListObj.paramType = paramType;
paramListObj.paramValue = paramValue;
paramList.Add(paramListObj);
}
//获取数据库信息
var matches = Regex.Matches(jsonData, "\"dbId\":.*");
string dbIdList = string.Empty;
foreach(Match match in matches)
{
dbIdList += match.Value.Replace(" ", "").Replace("\"dbId\":", "").Replace("\"", "") + ",";
}
dbIdList = dbIdList.TrimEnd(',');
List databaseInfos = ISource.getDatabaseInfoListByIds(dbIdList);
// 获取数据库表信息
var tableMatches = Regex.Matches(jsonData, "\"dbTableId\":.*");
string dbTableIdList = string.Empty;
foreach(Match match in tableMatches)
{
dbTableIdList += match.Value.Replace(" ", "").Replace("\"dbTableId\":", "").Replace("\"", "") + ",";
}
dbTableIdList = dbTableIdList.TrimEnd(',');
List tables = ISource.getDatabaseTableListByIds(dbTableIdList);
// 从起始节点开始
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, startNodeId);
// 返回结果
Dictionary result = new Dictionary();
foreach(ParamList item in paramList.Where(m => m.paramAttribute == "endoutput").ToList())
{
result.Add(item.paramName, item.paramValue);
}
return result;
}
///
/// 运行逻辑项目的Json数据
///
/// 边列表
/// 起始节点ID
public static void sortEdge(List databaseInfos, List tables, List edgeList, List nodeList, List paramList, string startNodeId)
{
foreach(EdgeList item in edgeList)
{
if(item.source == startNodeId)
{
NodeList node = nodeList.FirstOrDefault(x => x.nodeId == item.target) ?? new NodeList();
switch(node.nodeType)
{
case "ifelse": // 如果-否则节点
var ifElseResult = LogicNodeHelper.ifElse(node, paramList);
if(ifElseResult.Count > 0)
{
foreach(bool result in ifElseResult)
{
if(result)
{
if (node.nodeData.ToJson().Contains("\"parentNode\""))
{
string pNodeId = node.nodeData["parentNode"]?.ToString() ?? string.Empty;
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, pNodeId + "-child-start-node");
}
else
{
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
}
break;
}
}
}
break;
case "iteration": // 迭代节点
// var iterationParamList = LogicNodeHelper.iteration(node, paramList);
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId + "-child-start-node");
break;
case "child-start": // 子流程起始节点
var iterationParamList = LogicNodeHelper.iterationChildStart(node, paramList);
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
break;
case "break": // 跳出循环
// LogicNodeHelper.iterationBreak(node, paramList);
// 跳出循环后,继续执行循环后面的节点
string parentNodeId = node.nodeData["parentNode"]?.ToString() ?? string.Empty;
EdgeList breakEdge = edgeList.FirstOrDefault(x => x.source == parentNodeId) ?? new EdgeList();
if(string.IsNullOrEmpty(breakEdge.source))
{
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, breakEdge.target);
}
break;
case "loop": // 循环节点
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId + "-child-start-node");
break;
case "code-execute": // 代码执行节点
LogicNodeHelper.codeExecute(node, paramList);
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
break;
case "sql-execute": // SQL执行节点
LogicDatabaseNodeHelper.dbSqlQuery(node, databaseInfos, paramList);
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
break;
case "insert-data": // 插入数据节点
break;
case "update-data": // 更新数据节点
break;
case "query-data": // 查询数据节点
LogicDatabaseNodeHelper.dbQuery(node, databaseInfos, tables, paramList);
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
break;
case "delete-data": // 删除数据节点
break;
case "http-request": // HTTP请求节点
LogicToolNodeHelper.httpRequest(node, paramList);
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
break;
case "end": // 结束节点
break;
default:
sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target);
break;
}
}
}
}
}
}