LogicHelperBak.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // using System.Text.RegularExpressions;
  2. // using Common;
  3. // using Feign;
  4. // using Infrastructure;
  5. // using LitJson;
  6. // using Model.Custom;
  7. // using Model.Source;
  8. // using Services;
  9. // namespace Util.Logic
  10. // {
  11. // public class LogicHelperBak
  12. // {
  13. // /// <summary>
  14. // /// 获取逻辑项目的Json数据
  15. // /// </summary>
  16. // /// <param name="projectId">项目ID</param>
  17. // /// <param name="request">请求参数</param>
  18. // public static Dictionary<string, object> DoLogic(int projectId, string request)
  19. // {
  20. // var logicProjectService = App.GetService<ILogicProjectService>();
  21. // var logicProject = logicProjectService.GetById(projectId);
  22. // if(logicProject == null)
  23. // {
  24. // return new Dictionary<string, object>();
  25. // }
  26. // string jsonData = logicProject.setData ?? string.Empty;
  27. // if(string.IsNullOrEmpty(jsonData))
  28. // {
  29. // return new Dictionary<string, object>();
  30. // }
  31. // JsonData requestObj = JsonMapper.ToObject(request);
  32. // JsonData jsonDataObj = JsonMapper.ToObject(jsonData);
  33. // JsonData nodes = jsonDataObj["nodes"]; // 节点数据
  34. // List<NodeList> nodeList = new List<NodeList>(); // 节点列表
  35. // foreach(JsonData node in nodes)
  36. // {
  37. // JsonData id = node["id"];
  38. // JsonData type = node["type"];
  39. // NodeList nodeListObj = new NodeList();
  40. // nodeListObj.nodeId = id.ToString();
  41. // nodeListObj.nodeType = type.ToString();
  42. // nodeListObj.nodeData = node;
  43. // nodeList.Add(nodeListObj);
  44. // }
  45. // List<EdgeList> edgeList = new List<EdgeList>(); // 边列表
  46. // JsonData edges = jsonDataObj["edges"]; // 边数据
  47. // foreach(JsonData edge in edges)
  48. // {
  49. // JsonData source = edge["source"];
  50. // JsonData target = edge["target"];
  51. // EdgeList edgeListObj = new EdgeList();
  52. // edgeListObj.source = source.ToString();
  53. // edgeListObj.target = target.ToString();
  54. // edgeListObj.edgeData = edge;
  55. // edgeList.Add(edgeListObj);
  56. // }
  57. // string startNodeId = "start"; // 起始节点ID
  58. // // 获取起始节点数据
  59. // List<ParamList> paramList = new List<ParamList>();
  60. // NodeList startNode = nodeList.FirstOrDefault(x => x.nodeId == startNodeId) ?? new NodeList();
  61. // JsonData nodeData = startNode.nodeData;
  62. // JsonData input = nodeData["data"]["formData"]["input"]["value"];
  63. // for(int i = 0; i < input.Count; i++)
  64. // {
  65. // JsonData item = input[i];
  66. // string paramName = item["label"].ToString();
  67. // string paramType = item["type"][0].ToString();
  68. // string paramValue = requestObj[paramName].ToString();
  69. // ParamList paramListObj = new ParamList();
  70. // paramListObj.nodeId = startNodeId;
  71. // paramListObj.paramName = paramName;
  72. // paramListObj.paramType = paramType;
  73. // paramListObj.paramValue = paramValue;
  74. // paramListObj.paramAttribute = "startinput";
  75. // paramList.Add(paramListObj);
  76. // }
  77. // //获取数据库信息
  78. // List<string> dbIdValues = LitJsonHelper.FindAllValuesByKey(jsonDataObj, "dbId");
  79. // if(dbIdValues.Count > 0) dbIdValues = dbIdValues.Where(m => !string.IsNullOrEmpty(m)).Distinct().ToList();
  80. // List<string> dbTableIdValues = LitJsonHelper.FindAllValuesByKey(jsonDataObj, "dbTableId");
  81. // if(dbTableIdValues.Count > 0) dbTableIdValues = dbTableIdValues.Where(m => !string.IsNullOrEmpty(m)).Distinct().ToList();
  82. // string dbIdList = string.Join(",", dbIdValues);
  83. // List<DatabaseInfo> databaseInfos = ISource.getDatabaseInfoListByIds(dbIdList);
  84. // // 获取数据库表信息
  85. // string dbTableIdList = string.Join(",", dbTableIdValues);
  86. // List<DatabaseTable> tables = ISource.getDatabaseTableListByIds(dbTableIdList);
  87. // // 从起始节点开始
  88. // sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, startNodeId, new Dictionary<string, int>());
  89. // // 返回结果
  90. // Dictionary<string, object> result = new Dictionary<string, object>();
  91. // foreach(ParamList item in paramList.Where(m => m.paramAttribute == "endoutput").ToList())
  92. // {
  93. // result.Add(item.paramName, item.paramValue);
  94. // }
  95. // return result;
  96. // }
  97. // /// <summary>
  98. // /// 运行逻辑项目的Json数据
  99. // /// </summary>
  100. // /// <param name="edgeList">边列表</param>
  101. // /// <param name="startNodeId">起始节点ID</param>
  102. // public static void sortEdge(List<DatabaseInfo> databaseInfos, List<DatabaseTable> tables, List<EdgeList> edgeList, List<NodeList> nodeList, List<ParamList> paramList, string startNodeId, Dictionary<string, int> doEdgeCount)
  103. // {
  104. // var edges = edgeList.Where(m => m.source == startNodeId).ToList();
  105. // foreach(EdgeList item in edges)
  106. // {
  107. // //记录同一层级的节点执行计数,保证当前层级的所有节点都执行完后,再执行下一层级的节点
  108. // if(doEdgeCount.ContainsKey(startNodeId))
  109. // {
  110. // doEdgeCount[startNodeId]++;
  111. // }
  112. // else
  113. // {
  114. // doEdgeCount.Add(startNodeId, 1);
  115. // }
  116. // int edgeCount = doEdgeCount[startNodeId];
  117. // NodeList node = nodeList.FirstOrDefault(x => x.nodeId == item.target) ?? new NodeList();
  118. // int targetEdgeCount = edgeList.Count(m => m.target == node.nodeId);
  119. // switch(node.nodeType)
  120. // {
  121. // case "ifelse": // 如果-否则节点
  122. // var ifElseResult = LogicNodeHelper.ifElse(node, paramList);
  123. // if(ifElseResult.Count > 0)
  124. // {
  125. // foreach(bool result in ifElseResult)
  126. // {
  127. // if(result)
  128. // {
  129. // // if (node.nodeData.ToJson().Contains("\"parentNode\""))
  130. // // {
  131. // // string pNodeId = node.nodeData["parentNode"]?.ToString() ?? string.Empty;
  132. // // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, pNodeId + "-child-start-node", doEdgeCount);
  133. // // }
  134. // // else
  135. // // {
  136. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  137. // // }
  138. // break;
  139. // }
  140. // }
  141. // }
  142. // break;
  143. // case "iteration": // 迭代节点
  144. // // var iterationParamList = LogicNodeHelper.iteration(node, paramList);
  145. // if(edgeCount >= edges.Count || targetEdgeCount == 1)
  146. // {
  147. // bool op = true;
  148. // while (op)
  149. // {
  150. // sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId + "-child-start-node", doEdgeCount);
  151. // op = RedisServer.Cache.Get("break_" + node.nodeId) != "1";
  152. // }
  153. // sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  154. // }
  155. // break;
  156. // case "child_start": // 子流程起始节点
  157. // LogicNodeHelper.iterationChildStart(node, paramList);
  158. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  159. // break;
  160. // case "break": // 跳出循环
  161. // LogicNodeHelper.iterationBreak(node, "");
  162. // // 跳出循环后,继续执行循环后面的节点
  163. // string parentNodeId = node.nodeData["parentNode"]?.ToString() ?? string.Empty;
  164. // EdgeList breakEdge = edgeList.FirstOrDefault(x => x.source == parentNodeId) ?? new EdgeList();
  165. // if(string.IsNullOrEmpty(breakEdge.source))
  166. // {
  167. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, breakEdge.target, doEdgeCount);
  168. // }
  169. // break;
  170. // case "loop": // 循环节点
  171. // if(edgeCount >= edges.Count || targetEdgeCount == 1)
  172. // {
  173. // List<Dictionary<string, object>> loopResult = LogicNodeHelper.loop(node, paramList);
  174. // foreach(Dictionary<string, object> subLoop in loopResult)
  175. // {
  176. // if(RedisServer.Cache.Get("break_" + node.nodeId) == "1")
  177. // {
  178. // break;
  179. // }
  180. // else
  181. // {
  182. // foreach(string key in subLoop.Keys)
  183. // {
  184. // var loopParam = paramList.FirstOrDefault(x => x.paramName == key && x.nodeId == node.nodeId && x.paramAttribute == "loop");
  185. // if(loopParam != null)
  186. // {
  187. // loopParam.paramValue = subLoop[key]?.ToString();
  188. // }
  189. // else
  190. // {
  191. // paramList.Add(new ParamList()
  192. // {
  193. // nodeId = node.nodeId,
  194. // paramAttribute = "loop",
  195. // paramName = key,
  196. // paramType = "string",
  197. // paramValue = subLoop[key]?.ToString(),
  198. // });
  199. // }
  200. // }
  201. // doEdgeCount.Remove(node.nodeId + "-child-start-node");
  202. // sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId + "-child-start-node", doEdgeCount);
  203. // }
  204. // }
  205. // sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  206. // }
  207. // break;
  208. // case "code-execute": // 代码执行节点
  209. // LogicNodeHelper.codeExecute(node, paramList);
  210. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  211. // break;
  212. // case "sql-execute": // SQL执行节点
  213. // LogicDatabaseNodeHelper.dbSqlQuery(node, databaseInfos, paramList);
  214. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  215. // break;
  216. // case "insert-data": // 插入数据节点
  217. // break;
  218. // case "update-data": // 更新数据节点
  219. // break;
  220. // case "query-data": // 查询数据节点
  221. // LogicDatabaseNodeHelper.dbQuery(node, databaseInfos, tables, paramList);
  222. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  223. // break;
  224. // case "delete-data": // 删除数据节点
  225. // break;
  226. // case "http-request": // HTTP请求节点
  227. // LogicToolNodeHelper.httpRequest(node, paramList);
  228. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  229. // break;
  230. // case "var-aggregate": // 变量聚合节点
  231. // LogicNodeHelper.varAggregate(node, paramList);
  232. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  233. // break;
  234. // case "end": // 结束节点
  235. // break;
  236. // default:
  237. // if(edgeCount >= edges.Count) sortEdge(databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount);
  238. // break;
  239. // }
  240. // }
  241. // }
  242. // public static string GetExpressionVal(string str)
  243. // {
  244. // if(str == "#{now}#") return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  245. // if(str == "#{today}#") return DateTime.Now.ToString("yyyy-MM-dd");
  246. // if(str == "#{this_month}#") return DateTime.Now.ToString("yyyy-MM");
  247. // if(str.StartsWith("#{now") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
  248. // if(str.StartsWith("#{now") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
  249. // if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  250. // if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  251. // if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  252. // if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  253. // if(str.StartsWith("#{") && str.EndsWith("DAY}#")) return DateTime.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[0]).AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
  254. // if(str.StartsWith("#{") && str.EndsWith("MONTH}#")) return DateTime.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[0]).AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
  255. // if(str.StartsWith("#{split") && str.EndsWith("}#"))
  256. // {
  257. // string[] data = str.Replace("#{", "").Replace("}#", "").Split(',');
  258. // string text = data[1];
  259. // string splitTag = data[2];
  260. // string index = data[3];
  261. // return text.Split(new string[]{ splitTag }, StringSplitOptions.None)[int.Parse(index)];
  262. // }
  263. // else if(str.StartsWith("#{") && str.EndsWith("}#"))
  264. // {
  265. // string[] data = str.Replace("#{", "").Replace("}#", "").Split(',');
  266. // string tag = data[0];
  267. // string format = data[data.Length - 1];
  268. // if(format.StartsWith("yyyy"))
  269. // {
  270. // if(tag == "now")
  271. // {
  272. // if(data.Length == 2)
  273. // {
  274. // return DateTime.Now.ToString(format);
  275. // }
  276. // else if(data.Length == 4)
  277. // {
  278. // if(data[2] == "DAY") return DateTime.Now.AddDays(int.Parse(data[1])).ToString(format);
  279. // if(data[2] == "MONTH") return DateTime.Now.AddMonths(int.Parse(data[1])).ToString(format);
  280. // }
  281. // }
  282. // else
  283. // {
  284. // if(data.Length == 2)
  285. // {
  286. // return DateTime.Parse(tag).ToString(format);
  287. // }
  288. // else if(data.Length == 4)
  289. // {
  290. // if(data[2] == "DAY") return DateTime.Parse(tag).AddDays(int.Parse(data[1])).ToString(format);
  291. // if(data[2] == "MONTH") return DateTime.Parse(tag).AddMonths(int.Parse(data[1])).ToString(format);
  292. // }
  293. // }
  294. // }
  295. // }
  296. // return str;
  297. // }
  298. // public static string MatchExpressionVal(string str)
  299. // {
  300. // if(string.IsNullOrEmpty(str)) return str;
  301. // MatchCollection mc = Regex.Matches(str, "#\\{.*?\\}#");
  302. // foreach(Match m in mc)
  303. // {
  304. // str = str.Replace(m.Value, GetExpressionVal(m.Value));
  305. // }
  306. // return str;
  307. // }
  308. // }
  309. // }