| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- using System.Text.RegularExpressions;
- using System.Web;
- using Common;
- using Infrastructure;
- using Jint;
- using LitJson;
- using Model;
- using Model.Custom;
- using Model.Source;
- namespace Util.Logic
- {
- public class LogicNodeHelper
- {
- /// <summary>
- /// 如果-否则节点
- /// </summary>
- /// <param name="node">节点数据</param>
- /// <param name="paramList">参数列表</param>
- /// <param name="parentNodeParamList">父节点参数列表</param>
- public static List<bool> ifElse(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
- List<bool> ifElseResult = new List<bool>(); // 如果-否则节点结果
- JsonData nodeData = node.nodeData;
- JsonData ifCondition = nodeData["data"]["formData"]["ifelse"]["value"]; // 如果条件
- string paramAttribute = "";
- if (nodeData.ToJson().Contains("\"parentNode\""))
- {
- paramAttribute = "iterationInputField";
- }
- else
- {
- paramAttribute = "";
- }
- int ifIndex = 0; // 如果条件索引
- foreach (JsonData item in ifCondition)
- {
- string conditionExpression = ""; // 条件表达式
- string conditionExpressionLog = ""; // 条件表达式日志
- string logicTag = item["logic"].ToString(); // 逻辑
- JsonData conditions = item["conditions"]; // 条件
- foreach (JsonData condition in conditions)
- {
- string oper = condition["operator"].ToString(); // 运算符
- string value = condition["value"].ToString(); // 值
- string variable = condition["variable"].ToString(); // 变量
- if (variable.Contains("."))
- {
- string nodeId = variable.Substring(0, variable.IndexOf(":"));
- variable = variable.Substring(variable.LastIndexOf(".") + 1);
- if(variable.EndsWith("_Init"))
- {
- variable = variable.Replace("_Init", "");
- }
- ParamList param = paramList.FirstOrDefault(x => x.paramName == variable && x.nodeId == nodeId) ?? paramList.FirstOrDefault(x => x.paramName == variable) ?? new ParamList();
- if (!string.IsNullOrEmpty(param.paramName))
- {
- // string paramValue = param.paramValueTransfer;
- value = LogicHelper.MatchExpressionVal(value);
- if(value.Contains("."))
- {
- string nId = value.Substring(0, value.IndexOf(":"));
- value = value.Substring(value.LastIndexOf(".") + 1);
- if(value.EndsWith("_Init"))
- {
- value = value.Replace("_Init", "");
- }
- var paramc = paramList.FirstOrDefault(x => x.paramName == value && x.nodeId == nId);
- if (paramc != null)
- {
- value = paramc.paramValueTransfer;
- }
- }
- string paramType = param.paramType;
- conditionExpression += operatorTransfer(param, oper, value);
- conditionExpressionLog += operatorTransfer(param, oper, value, true);
- if (logicTag == "and")
- {
- conditionExpression += " && ";
- conditionExpressionLog += " && ";
- continue;
- }
- else if (logicTag == "or")
- {
- conditionExpression += " || ";
- conditionExpressionLog += " || ";
- continue;
- }
- }
- else
- {
- conditionExpression += "false"; // 如果参数未找到,则条件表达式为false
- conditionExpressionLog += "false"; // 如果参数未找到,则条件表达式为false
- if (logicTag == "and")
- {
- conditionExpression += " && ";
- conditionExpressionLog += " && ";
- }
- else if (logicTag == "or")
- {
- conditionExpression += " || ";
- conditionExpressionLog += " || ";
- }
- }
- }
- }
- if (conditionExpression.EndsWith(" && ") || conditionExpression.EndsWith(" || "))
- {
- conditionExpression = conditionExpression.Substring(0, conditionExpression.Length - 4);
- }
- if (conditionExpressionLog.EndsWith(" && ") || conditionExpressionLog.EndsWith(" || "))
- {
- conditionExpressionLog = conditionExpressionLog.Substring(0, conditionExpressionLog.Length - 4);
- }
- bool conditionResult = Utils.EvaluateBoolean(conditionExpression);
- ifElseResult.Add(conditionResult);
- condi.Add("条件" + ifIndex, conditionExpressionLog);
- condi.Add("结果" + ifIndex, conditionResult);
- ifIndex += 1;
- }
- // 记录日志
- logDic.Add(new LogItem()
- {
- title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
- param = condi
- });
- return ifElseResult;
- }
- /// <summary>
- /// 循环节点
- /// </summary>
- /// <param name="node">节点数据</param>
- /// <param name="paramList">参数列表</param>
- public static List<Dictionary<string, object>> loop(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- JsonData nodeData = node.nodeData;
- JsonData inputField = nodeData["data"]["formData"]["loopVar"]["value"];
- if(inputField.Count > 0)
- {
- string paramType = inputField[0]["type"][0].ToString(); // 字段类型
- if(paramType == "array")
- {
- string paramValue = inputField[0]["value"].ToString(); // 字段值
- Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
- if(paramValue.Contains("."))
- {
- paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1);
- var param = paramList.FirstOrDefault(x => x.paramName == paramValue);
- if (param != null)
- {
- paramValue = param.paramValueTransfer;
- }
- }
- condi.Add("循环来源", paramValue);
- List<Dictionary<string, object>> loopResult = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(paramValue) ?? new List<Dictionary<string, object>>();
- logDic.Add(new LogItem()
- {
- title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
- param = condi
- });
- return loopResult;
- }
- }
- return new List<Dictionary<string, object>>();
- }
- /// <summary>
- /// 迭代节点
- /// </summary>
- /// <param name="node">节点数据</param>
- /// <param name="paramList">参数列表</param>
- public static void iteration(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- JsonData nodeData = node.nodeData;
- string inputField = nodeData["data"]["formData"]["input"]["value"]?.ToString() ?? string.Empty; // 迭代字段
- inputField = inputField.Substring(inputField.LastIndexOf(".") + 1);
- ParamList inputFieldParam = paramList.FirstOrDefault(x => x.paramName == inputField) ?? new ParamList(); // 迭代字段值
- var param = paramList.FirstOrDefault(x => x.paramName == inputField && x.paramAttribute == "iterationInputField");
- if(param == null)
- {
- paramList.Add(new ParamList()
- {
- nodeId = node.nodeId,
- paramAttribute = "iterationInputField",
- paramName = inputFieldParam.paramName,
- paramType = inputFieldParam.paramType,
- paramValue = inputFieldParam.paramValue
- });
- }
- else
- {
- param.paramName = inputFieldParam.paramName;
- param.paramType = inputFieldParam.paramType;
- param.paramValue = inputFieldParam.paramValue;
- }
- }
- /// <summary>
- /// 迭代子流程起始节点
- /// </summary>
- /// <param name="node"></param>
- /// <param name="paramList"></param>
- public static void iterationChildStart(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
- JsonData nodeData = node.nodeData;
- string inputField = nodeData["data"]["formData"]["input"]["value"]?.ToString() ?? string.Empty; // 迭代字段
- inputField = inputField.Substring(inputField.LastIndexOf(".") + 1);
- ParamList inputFieldParam = paramList.FirstOrDefault(x => x.paramName == inputField) ?? new ParamList(); // 迭代字段值
- bool iterationInputFieldCheck = paramList.Any(x => x.paramName == inputField && x.paramAttribute == "iterationInputField");
- if(!iterationInputFieldCheck)
- {
- paramList.Add(new ParamList()
- {
- nodeId = node.nodeId,
- paramAttribute = "iterationInputField",
- paramName = inputFieldParam.paramName,
- paramType = inputFieldParam.paramType,
- paramValue = inputFieldParam.paramValue
- });
- condi.Add(inputFieldParam.paramName, inputFieldParam.paramValue);
- }
- logDic.Add(new LogItem()
- {
- title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
- param = condi
- });
- }
- /// <summary>
- /// 跳出循环节点
- /// </summary>
- /// <param name="node"></param>
- /// <param name="paramList"></param>
- public static void iterationBreak(NodeList node, string requestId, List<LogItem> logDic)
- {
- string parentNodeId = node.nodeData["parentNode"]?.ToString() ?? string.Empty;
- RedisServer.Cache.Set("break_" + requestId + "_" + parentNodeId, "1", 60); // 设置循环跳出标志,过期时间10分钟
- logDic.Add(new LogItem()
- {
- title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
- detail = "跳出循环"
- });
- }
- /// <summary>
- /// 代码执行节点
- /// </summary>
- /// <param name="node"></param>
- /// <param name="paramList"></param>
- public static void codeExecute(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
- JsonData nodeData = node.nodeData;
- JsonData inputParam = nodeData["data"]["formData"]["code"]["value"]["inputVar"]; // 输出参数
- Dictionary<string, string> inputDic = new Dictionary<string, string>();
- foreach (JsonData param in inputParam)
- {
- string paramName = param["label"].ToString(); // 字段名
- string paramValue = param["value"].ToString(); // 字段值
- if(paramValue.Contains("."))
- {
- string nId = paramValue.Substring(0, paramValue.IndexOf(":"));
- paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1);
- if (paramValue.EndsWith("_Init"))
- {
- paramValue = paramValue.Replace("_Init", "");
- }
- var paramc = paramList.FirstOrDefault(x => x.paramName == paramValue && x.nodeId == nId);
- if (paramc != null)
- {
- paramValue = paramc.paramValueTransfer;
- }
- }
- inputDic.Add(paramName, paramValue);
- }
- string code = nodeData["data"]["formData"]["code"]["value"]["code"]["content"].ToString(); // 代码
- JsonData outputParam = nodeData["data"]["formData"]["output"]["value"]; // 输出参数
- var engine = new Engine();
- MatchCollection mc = Regex.Matches(code, "\\$\\{.*?\\}");
- foreach(Match m in mc)
- {
- string field = m.Value.Replace("${", "").Replace("}", "");
- code = code.Replace("${" + field + "}", inputDic[field]);
- code = code.Replace("{" + field + "}", inputDic[field]);
- }
- engine.Execute(code);
- foreach (JsonData param in outputParam)
- {
- string paramName = param["label"].ToString(); // 字段名
- if (paramName.EndsWith("_Init"))
- {
- paramName = paramName.Replace("_Init", "");
- }
- string paramValue = engine.GetValue(paramName).ToString();
- var paramItem = paramList.FirstOrDefault(x => x.paramName == paramName);
- if(paramItem != null)
- {
- Utils.WriteLog("代码执行1:" + paramValue + ";");
- paramItem.paramValue = paramValue;
- }
- else
- {
- Utils.WriteLog("代码执行2:" + node.nodeId + "-" + paramValue + ";");
- paramList.Add(new ParamList()
- {
- nodeId = node.nodeId,
- paramName = paramName,
- paramValue = paramValue,
- paramType = param["type"][0].ToString(),
- });
- }
- condi.Add(paramName, paramValue);
- }
- Utils.WriteLog("代码执行:" + JsonMapper.ToJson(condi) + ";");
- logDic.Add(new LogItem()
- {
- title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
- param = condi,
- detail = HttpUtility.UrlEncode(code)
- });
- }
- /// <summary>
- /// 变量聚合节点
- /// </summary>
- /// <param name="node">节点数据</param>
- /// <param name="paramList">参数列表</param>
- /// <param name="parentNodeParamList">父节点参数列表</param>
- public static void varAggregate(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
- JsonData nodeData = node.nodeData;
- JsonData aggregateFields = nodeData["data"]["formData"]["output"]["value"]; // 聚合字段
- string paramAttribute = "";
- if (nodeData.ToJson().Contains("\"parentNode\""))
- {
- paramAttribute = "iterationInputField";
- }
- foreach (JsonData item in aggregateFields)
- {
- string paramName = item["label"].ToString(); // 聚合字段名
- bool updateFlag = true;
- if(paramName.EndsWith("_Init"))
- {
- updateFlag = false;
- paramName = paramName.Replace("_Init", "");
- }
- string paramValue = item["value"].ToString(); // 聚合字段值
- string nId = "";
- if (paramValue.Contains(":"))
- {
- nId = paramValue.Substring(0, paramValue.IndexOf(":"));
- }
- if(paramValue.Contains("."))
- {
- paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1);
- var param = paramList.FirstOrDefault(x => x.paramName == paramValue && x.nodeId == nId) ?? paramList.FirstOrDefault(x => x.paramName == paramValue) ?? new ParamList();
- paramValue = param.paramValue;
- }
- string paramType = item["type"][0].ToString(); // 聚合字段类型
- if(!paramList.Any(x => x.paramName == paramName && x.nodeId == nId))
- {
- paramList.Add(new ParamList()
- {
- nodeId = node.nodeId,
- paramAttribute = paramAttribute,
- paramName = paramName,
- paramValue = paramValue,
- paramType = paramType
- });
- condi.Add(paramName, paramValue);
- }
- else
- {
- var paramItem = paramList.FirstOrDefault(x => x.paramName == paramName && x.nodeId == nId) ?? paramList.FirstOrDefault(x => x.paramName == paramName) ?? new ParamList();
- if (paramValue.StartsWith("+"))
- {
- int newValue = int.Parse(paramItem.paramValue) + int.Parse(paramValue.TrimStart('+'));
- paramValue = newValue.ToString();
- }
- if(updateFlag)
- {
- paramItem.paramValue = paramValue;
- condi.Add(paramName, paramValue);
- }
- }
- }
- logDic.Add(new LogItem()
- {
- title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
- param = condi
- });
- }
- /// <summary>
- /// 运算符转换
- /// </summary>
- /// <param name="oper">运算符</param>
- /// <returns>转换后的运算符</returns>
- public static string operatorTransfer(ParamList param, string oper, string paramValue, bool isLog = false)
- {
- if(param.paramType == "number")
- {
- param.paramValue = Function.CheckNum(param.paramValue);
- }
- string condiLeftData = param.paramValue;
- if(isLog)
- {
- condiLeftData = param.paramName + "(" + param.paramValue + ")";
- }
- if (oper == "equal") //等于
- {
- if(param.paramType == "string" || param.paramType == "time")
- {
- return "'" + condiLeftData + "' == '" + paramValue + "'";
- }
- return condiLeftData + " == " + paramValue;
- }
- else if (oper == "not_equal") //不等于
- {
- if(param.paramType == "string" || param.paramType == "time")
- {
- return "'" + condiLeftData + "' != '" + paramValue + "'";
- }
- return condiLeftData + " != " + paramValue;
- }
- else if (oper == "greater") //大于
- {
- return condiLeftData + " > " + paramValue;
- }
- else if (oper == "greater_equal") //大于等于
- {
- return condiLeftData + " >= " + paramValue;
- }
- else if (oper == "less") //小于
- {
- return condiLeftData + " < " + paramValue;
- }
- else if (oper == "less_equal") //小于等于
- {
- return condiLeftData + " <= " + paramValue;
- }
- else if (oper == "between") //在之间
- {
- string[] betweenValues = paramValue.Split(',');
- return condiLeftData + " >= " + betweenValues[0] + " && " + condiLeftData + " <= " + betweenValues[1];
- }
- else if (oper == "not_between") //不在之间
- {
- string[] betweenValues = paramValue.Split(',');
- return "(" + condiLeftData + " < " + betweenValues[0] + " || " + condiLeftData + " > " + betweenValues[1] + ")";
- }
- else if (oper == "contains") //包含
- {
- return "Contains('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "not_contains") //不包含
- {
- return "!Contains('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "starts_with") //以...开头
- {
- return "StartsWith('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "ends_with") //以...结尾
- {
- return "EndsWith('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "matches_regex") //匹配正则表达式
- {
- return "Match('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "not_matches_regex") //不匹配正则表达式
- {
- return "!Match('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "is_empty")
- {
- return "IsNullOrEmpty('" + condiLeftData + "')";
- }
- else if (oper == "is_not_empty") //不为空
- {
- return "!" + "IsNullOrEmpty('" + condiLeftData + "')";
- }
- else if (oper == "is_null") //为空
- {
- return "('" + condiLeftData + "'=='{}' || '" + condiLeftData + "'=='[]')";
- }
- else if (oper == "is_not_null") //不为空
- {
- return "('" + condiLeftData + "'!='{}' || '" + condiLeftData + "'!='[]')";
- }
- else if (oper == "in") //在...中
- {
- return "In('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "not_in") //不在...中
- {
- return "!In('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "array_contains") //数组包含
- {
- return "In('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "array_not_contains") //数组不包含
- {
- return "!In('" + condiLeftData + "', '" + paramValue + "')";
- }
- else if (oper == "array_length_equal") //数组长度等于
- {
- return "ArrayLen('" + condiLeftData + "') == " + paramValue;
- }
- else if (oper == "array_length_greater")
- {
- return "ArrayLen('" + condiLeftData + "') > " + paramValue;
- }
- else if (oper == "array_length_less") //数组长度小于
- {
- return "ArrayLen('" + condiLeftData + "') < " + paramValue;
- }
- else if (oper == "is_true") //为真
- {
- return condiLeftData + " == true";
- }
- else if (oper == "is_false") //为假
- {
- return condiLeftData + " == false";
- }
- else if (oper == "date_equal") //日期等于
- {
- return "'" + condiLeftData + "' == '" + paramValue + "'";
- }
- else if (oper == "date_before") //日期之前
- {
- return "'" + condiLeftData + "' < '" + paramValue + "'";
- }
- else if (oper == "date_after") //日期之后
- {
- return "'" + condiLeftData + "' > '" + paramValue + "'";
- }
- else if (oper == "date_between") //日期之间
- {
- string[] betweenValues = paramValue.Split(" - ", StringSplitOptions.None);
- return "'" + condiLeftData + "' >= '" + betweenValues[0] + "' && '" + condiLeftData + "' <= '" + betweenValues[1] + "'";
- }
- return "";
- }
-
- }
- }
|