LogicHelper.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using System.Text.RegularExpressions;
  2. using Common;
  3. using Feign;
  4. using Infrastructure;
  5. using LitJson;
  6. using Mapster;
  7. using Model;
  8. using Model.Custom;
  9. using Model.Source;
  10. using Services;
  11. namespace Util.Logic
  12. {
  13. public class LogicHelper
  14. {
  15. /// <summary>
  16. /// 获取逻辑项目的Json数据
  17. /// </summary>
  18. /// <param name="projectId">项目ID</param>
  19. /// <param name="request">请求参数</param>
  20. public static Dictionary<string, object> DoLogic(int projectId, string requestId, string env, string request, int nodeKind = 1)
  21. {
  22. Dictionary<string, object> result = new Dictionary<string, object>();
  23. LogicProject logicProject = new LogicProject();
  24. if (nodeKind == 1)
  25. {
  26. var logicProjectService = App.GetService<ILogicProjectService>();
  27. logicProject = logicProjectService.GetById(projectId);
  28. }
  29. else if (nodeKind == 2)
  30. {
  31. var testProjectService = App.GetService<ITestProjectService>();
  32. logicProject = testProjectService.GetById(projectId).Adapt<LogicProject>();
  33. }
  34. else if (nodeKind == 3)
  35. {
  36. var serverProjectService = App.GetService<IServerProjectService>();
  37. logicProject = serverProjectService.GetById(projectId).Adapt<LogicProject>();
  38. }
  39. if(logicProject == null)
  40. {
  41. return new Dictionary<string, object>();
  42. }
  43. List<LogItem> logDic = new List<LogItem>();
  44. try
  45. {
  46. // 记录日志
  47. logDic.Add(new LogItem()
  48. {
  49. title = "开始节点" + env,
  50. param = new Dictionary<string, object>()
  51. {
  52. { "请求参数", request },
  53. { "项目ID", projectId },
  54. { "请求ID", requestId },
  55. }
  56. });
  57. var logicLogRecordService = App.GetService<ILogicLogRecordService>();
  58. var logId = logicLogRecordService.addLogicLogRecord(new LogicLogRecord()
  59. {
  60. createTime = DateTime.Now,
  61. requestId = requestId,
  62. requestParam = request,
  63. logicProjectName = logicProject.projectName,
  64. logicId = logicProject.id,
  65. });
  66. // var logRecord = logicLogRecordService.getLogicLogRecordQuery(new LogicLogRecord(){ id = logId });
  67. string jsonData = logicProject.setData ?? string.Empty;
  68. if(string.IsNullOrEmpty(jsonData))
  69. {
  70. return new Dictionary<string, object>();
  71. }
  72. JsonData requestObj = JsonMapper.ToObject(request);
  73. JsonData jsonDataObj = JsonMapper.ToObject(jsonData);
  74. JsonData nodes = jsonDataObj["nodes"]; // 节点数据
  75. List<NodeList> nodeList = new List<NodeList>(); // 节点列表
  76. foreach(JsonData node in nodes)
  77. {
  78. JsonData id = node["id"];
  79. JsonData type = node["type"];
  80. NodeList nodeListObj = new NodeList();
  81. nodeListObj.nodeId = id.ToString();
  82. nodeListObj.nodeType = type.ToString();
  83. nodeListObj.nodeData = node;
  84. nodeList.Add(nodeListObj);
  85. }
  86. List<EdgeList> edgeList = new List<EdgeList>(); // 边列表
  87. JsonData edges = jsonDataObj["edges"]; // 边数据
  88. foreach(JsonData edge in edges)
  89. {
  90. JsonData source = edge["source"];
  91. JsonData target = edge["target"];
  92. JsonData edgeId = edge["id"];
  93. EdgeList edgeListObj = new EdgeList();
  94. edgeListObj.source = source.ToString();
  95. edgeListObj.target = target.ToString();
  96. edgeListObj.id = edgeId.ToString();
  97. edgeListObj.edgeData = edge;
  98. edgeList.Add(edgeListObj);
  99. }
  100. string startNodeId = "start"; // 起始节点ID
  101. // 获取起始节点数据
  102. List<ParamList> paramList = new List<ParamList>();
  103. NodeList startNode = nodeList.FirstOrDefault(x => x.nodeId == startNodeId) ?? new NodeList();
  104. JsonData nodeData = startNode.nodeData;
  105. JsonData input = nodeData["data"]["formData"]["input"]["value"];
  106. for(int i = 0; i < input.Count; i++)
  107. {
  108. JsonData item = input[i];
  109. string paramName = item["label"].ToString();
  110. string paramType = item["type"][0].ToString();
  111. string paramValue = requestObj[paramName].ToString();
  112. ParamList paramListObj = new ParamList();
  113. paramListObj.nodeId = startNodeId;
  114. paramListObj.paramName = paramName;
  115. paramListObj.paramType = paramType;
  116. paramListObj.paramValue = paramValue;
  117. paramListObj.paramAttribute = "startinput";
  118. paramList.Add(paramListObj);
  119. }
  120. //获取数据库信息
  121. List<string> dbIdValues = LitJsonHelper.FindAllValuesByKey(jsonDataObj, "dbId");
  122. if(dbIdValues.Count > 0) dbIdValues = dbIdValues.Where(m => !string.IsNullOrEmpty(m)).Distinct().ToList();
  123. List<string> dbTableIdValues = LitJsonHelper.FindAllValuesByKey(jsonDataObj, "dbTableId");
  124. if(dbTableIdValues.Count > 0) dbTableIdValues = dbTableIdValues.Where(m => !string.IsNullOrEmpty(m)).Distinct().ToList();
  125. string dbIdList = string.Join(",", dbIdValues);
  126. List<DatabaseInfo> databaseInfos = ISource.getDatabaseInfoListByIds(dbIdList);
  127. // 获取数据库表信息
  128. string dbTableIdList = string.Join(",", dbTableIdValues);
  129. List<DatabaseTable> tables = ISource.getDatabaseTableListByIds(dbTableIdList);
  130. // 从起始节点开始
  131. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, startNodeId, new Dictionary<string, int>(), logDic, env);
  132. // 返回结果
  133. foreach(ParamList item in paramList.Where(m => m.paramAttribute == "endoutput").ToList())
  134. {
  135. result.Add(item.paramName, item.paramValue);
  136. }
  137. startNodeId = "end"; // 起始节点ID
  138. // 获取起始节点数据
  139. startNode = nodeList.FirstOrDefault(x => x.nodeId == startNodeId) ?? new NodeList();
  140. nodeData = startNode.nodeData;
  141. input = nodeData["data"]["formData"]["output"]["value"];
  142. for(int i = 0; i < input.Count; i++)
  143. {
  144. JsonData item = input[i];
  145. string paramName = item["label"].ToString();
  146. string paramType = item["type"][0].ToString();
  147. string paramValue = item["value"].ToString();
  148. if (paramValue.Contains('.'))
  149. {
  150. string nodeId = paramValue.Substring(0, paramValue.IndexOf(":"));
  151. paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1);
  152. var paramData = paramList.FirstOrDefault(x => x.paramName == paramValue && x.nodeId == nodeId);
  153. if (paramData != null)
  154. {
  155. paramValue = paramData.paramValueTransfer;
  156. }
  157. }
  158. result.Add(paramName, paramValue);
  159. }
  160. string logPath = "/logRecord/";
  161. string logString = JsonMapper.ToJson(logDic);
  162. logString = UnicodeToChinese(logString);
  163. Function.WritePage(logPath, requestId + ".json", logString);
  164. OssHelper.Instance.Upload(Function.getPath(logPath + requestId + ".json"));
  165. // logRecord.logicLogFilePath = logPath + requestId + ".json";
  166. // logicLogRecordService.updateLogicLogRecord(logRecord);
  167. }
  168. catch(Exception ex)
  169. {
  170. RedisServer.Cache.Set("logic_error_" + requestId, ex.ToString());
  171. RedisServer.Cache.Set("logic_log_" + requestId, JsonMapper.ToJson(logDic));
  172. // Function.WriteLog(ex.ToString(), "逻辑异常");
  173. Utils.WriteLog(ex.ToString(), "逻辑异常");
  174. try
  175. {
  176. logDic.Add(new LogItem()
  177. {
  178. title = "error",
  179. detail = ex.ToString(),
  180. });
  181. string logPath = "/logRecord/";
  182. string logString = JsonMapper.ToJson(logDic);
  183. logString = UnicodeToChinese(logString);
  184. Function.WritePage(logPath, requestId + ".json", logString);
  185. OssHelper.Instance.Upload(Function.getPath(logPath + requestId + ".json"));
  186. } catch
  187. {
  188. }
  189. }
  190. try
  191. {
  192. Task.Run(async () =>
  193. {
  194. await Task.Delay(1000);
  195. // 延迟后执行查询/补偿逻辑
  196. updateLogPath(requestId, "/logRecord/" + requestId + ".json");
  197. });
  198. }
  199. catch(Exception ex)
  200. {
  201. }
  202. return result;
  203. }
  204. public static void updateLogPath(string requestId, string logPath)
  205. {
  206. var logicLogRecordService = App.GetService<ILogicLogRecordService>();
  207. LogicLogRecord logRecord = logicLogRecordService.getLogicLogRecordQuery(requestId);
  208. logRecord.logicLogFilePath = logPath;
  209. logicLogRecordService.updateLogicLogRecord(logRecord);
  210. }
  211. public static string UnicodeToChinese(string input)
  212. {
  213. return Regex.Replace(input, @"\\u([0-9a-fA-F]{4})", m =>
  214. {
  215. int code = Convert.ToInt32(m.Groups[1].Value, 16);
  216. return ((char)code).ToString();
  217. });
  218. }
  219. /// <summary>
  220. /// 运行逻辑项目的Json数据
  221. /// </summary>
  222. /// <param name="edgeList">边列表</param>
  223. /// <param name="startNodeId">起始节点ID</param>
  224. public static void sortEdge(string requestId, List<DatabaseInfo> databaseInfos, List<DatabaseTable> tables, List<EdgeList> edgeList, List<NodeList> nodeList, List<ParamList> paramList, string startNodeId, Dictionary<string, int> doEdgeCount, List<LogItem> logDic, string env)
  225. {
  226. var edges = edgeList.Where(m => m.source == startNodeId).ToList();
  227. foreach(EdgeList item in edges)
  228. {
  229. doNode(requestId, databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount, logDic, env);
  230. }
  231. }
  232. /// <summary>
  233. /// 执行节点
  234. /// </summary>
  235. /// <param name="edgeList">边列表</param>
  236. /// <param name="startNodeId">起始节点ID</param>
  237. public static void doNode(string requestId, List<DatabaseInfo> databaseInfos, List<DatabaseTable> tables, List<EdgeList> edgeList, List<NodeList> nodeList, List<ParamList> paramList, string nodeId, Dictionary<string, int> doEdgeCount, List<LogItem> logDic, string env)
  238. {
  239. NodeList node = nodeList.FirstOrDefault(x => x.nodeId == nodeId) ?? new NodeList();
  240. var edges = edgeList.Where(m => m.source == nodeId).ToList();
  241. int targetEdgeCount = edgeList.Count(m => m.target == nodeId);
  242. //记录同一层级的节点执行计数,保证当前层级的所有节点都执行完后,再执行下一层级的节点
  243. // if(doEdgeCount.ContainsKey(nodeId))
  244. // {
  245. // doEdgeCount[nodeId]++;
  246. // }
  247. // else
  248. // {
  249. // doEdgeCount.Add(nodeId, 1);
  250. // }
  251. // if(doEdgeCount[nodeId] < targetEdgeCount && targetEdgeCount > 1)
  252. // {
  253. // return;
  254. // }
  255. switch(node.nodeType)
  256. {
  257. case "ifelse": // 如果-否则节点
  258. var ifElseResult = LogicNodeHelper.ifElse(node, paramList, logDic);
  259. if(ifElseResult.Count > 0)
  260. {
  261. int ifIndex = 0;
  262. foreach(bool result in ifElseResult)
  263. {
  264. if(result)
  265. {
  266. string checkIdString = nodeId + "condition-" + ifIndex;
  267. var items = edges.Where(m => m.id.Contains(checkIdString)).ToList();
  268. foreach(EdgeList item in items)
  269. {
  270. doNode(requestId, databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount, logDic, env);
  271. }
  272. break;
  273. }
  274. ifIndex += 1;
  275. }
  276. }
  277. if(ifElseResult.Count(m => m) == 0)
  278. {
  279. string elseString = nodeId + "condition-else";
  280. var elseItems = edges.Where(m => m.id.Contains(elseString)).ToList();
  281. foreach(EdgeList item in elseItems)
  282. {
  283. doNode(requestId, databaseInfos, tables, edgeList, nodeList, paramList, item.target, doEdgeCount, logDic, env);
  284. }
  285. }
  286. break;
  287. case "iteration": // 迭代节点
  288. LogicNodeHelper.iterationChildStart(node, paramList, logDic);
  289. bool op = true;
  290. while (op)
  291. {
  292. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId + "-child-start-node", doEdgeCount, logDic, env);
  293. op = RedisServer.Cache.Get("break_" + requestId + "_" + node.nodeId) != "1";
  294. JsonData upField = node.nodeData["data"]["formData"]["output"]["value"]; // 递归字段
  295. foreach(JsonData item in upField)
  296. {
  297. string paramName = item["label"].ToString(); // 递归字段名
  298. string paramValue = item["value"].ToString(); // 递归字段值
  299. var param = paramList.FirstOrDefault(x => x.paramName == paramName && x.paramAttribute == "iterationInputField");
  300. if (param != null)
  301. {
  302. var upParam = paramList.FirstOrDefault(x => x.paramName == paramValue && x.paramAttribute == "iterationInputField");
  303. if(upParam != null)
  304. {
  305. param.paramValue = upParam.paramValue;
  306. }
  307. else
  308. {
  309. param.paramValue = "0";
  310. }
  311. }
  312. }
  313. }
  314. RedisServer.Cache.Del("break_" + requestId + "_" + node.nodeId);
  315. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  316. break;
  317. case "child_start": // 子流程起始节点
  318. // LogicNodeHelper.iterationChildStart(node, paramList);
  319. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  320. break;
  321. case "break": // 跳出循环
  322. LogicNodeHelper.iterationBreak(node, requestId, logDic);
  323. // 跳出循环后,继续执行循环后面的节点
  324. string parentNodeId = node.nodeData["parentNode"]?.ToString() ?? string.Empty;
  325. EdgeList breakEdge = edgeList.FirstOrDefault(x => x.source == parentNodeId) ?? new EdgeList();
  326. if(string.IsNullOrEmpty(breakEdge.source))
  327. {
  328. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, breakEdge.target, doEdgeCount, logDic, env);
  329. }
  330. break;
  331. case "loop": // 循环节点
  332. List<Dictionary<string, object>> loopResult = LogicNodeHelper.loop(node, paramList, logDic);
  333. foreach(Dictionary<string, object> subLoop in loopResult)
  334. {
  335. if(RedisServer.Cache.Get("break_" + node.nodeId) == "1")
  336. {
  337. break;
  338. }
  339. else
  340. {
  341. foreach(string key in subLoop.Keys)
  342. {
  343. var loopParam = paramList.FirstOrDefault(x => x.paramName == key && x.nodeId == node.nodeId && x.paramAttribute == "loop");
  344. if(loopParam != null)
  345. {
  346. loopParam.paramValue = subLoop[key]?.ToString();
  347. }
  348. else
  349. {
  350. paramList.Add(new ParamList()
  351. {
  352. nodeId = node.nodeId,
  353. paramAttribute = "loop",
  354. paramName = key,
  355. paramType = "string",
  356. paramValue = subLoop[key]?.ToString(),
  357. });
  358. }
  359. }
  360. doEdgeCount.Remove(node.nodeId + "-child-start-node");
  361. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId + "-child-start-node", doEdgeCount, logDic, env);
  362. }
  363. }
  364. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  365. break;
  366. case "code-execute": // 代码执行节点
  367. LogicNodeHelper.codeExecute(node, paramList, logDic);
  368. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  369. break;
  370. case "sql-execute": // SQL执行节点
  371. LogicDatabaseNodeHelper.dbSqlQuery(node, databaseInfos, paramList, logDic, env);
  372. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  373. break;
  374. case "insert-data": // 插入数据节点
  375. LogicDatabaseNodeHelper.dbInsert(node, databaseInfos, tables, paramList, logDic, env);
  376. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  377. break;
  378. case "update-data": // 更新数据节点
  379. LogicDatabaseNodeHelper.dbUpdate(node, databaseInfos, tables, paramList, logDic, env);
  380. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  381. break;
  382. case "query-data": // 查询数据节点
  383. LogicDatabaseNodeHelper.dbQuery(node, databaseInfos, tables, paramList, logDic, env);
  384. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  385. break;
  386. case "delete-data": // 删除数据节点
  387. LogicDatabaseNodeHelper.dbDelete(node, databaseInfos, tables, paramList, logDic, env);
  388. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  389. break;
  390. case "setredis": // 设置Redis值节点
  391. LogicCacheNodeHelper.setRedisObject(node, paramList, logDic);
  392. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  393. break;
  394. case "getredis": // 获取Redis值节点
  395. LogicCacheNodeHelper.getRedisObject(node, paramList, logDic);
  396. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  397. break;
  398. case "http-request": // HTTP请求节点
  399. LogicToolNodeHelper.httpRequest(node, paramList, logDic);
  400. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  401. break;
  402. case "apiSource": // 内部API请求节点
  403. LogicToolNodeHelper.apiSourceRequest(node, paramList, logDic);
  404. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  405. break;
  406. case "var-aggregate": // 变量聚合节点
  407. LogicNodeHelper.varAggregate(node, paramList, logDic);
  408. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  409. break;
  410. case "document-extractor": // 文档提取节点
  411. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  412. break;
  413. case "document-export": // 文档导出节点
  414. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  415. break;
  416. case "end": // 结束节点
  417. break;
  418. default:
  419. sortEdge(requestId, databaseInfos, tables, edgeList, nodeList, paramList, node.nodeId, doEdgeCount, logDic, env);
  420. break;
  421. }
  422. }
  423. public static string GetExpressionVal(string str)
  424. {
  425. if(str == "#{now}#") return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  426. if(str == "#{today}#") return DateTime.Now.ToString("yyyy-MM-dd");
  427. if(str == "#{this_month}#") return DateTime.Now.ToString("yyyy-MM");
  428. 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");
  429. 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");
  430. if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  431. if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  432. if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  433. if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  434. 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");
  435. 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");
  436. if(str.StartsWith("#{split") && str.EndsWith("}#"))
  437. {
  438. string[] data = str.Replace("#{", "").Replace("}#", "").Split(',');
  439. string text = data[1];
  440. string splitTag = data[2];
  441. string index = data[3];
  442. return text.Split(new string[]{ splitTag }, StringSplitOptions.None)[int.Parse(index)];
  443. }
  444. else if(str.StartsWith("#{") && str.EndsWith("}#"))
  445. {
  446. string[] data = str.Replace("#{", "").Replace("}#", "").Split(',');
  447. string tag = data[0];
  448. string format = data[data.Length - 1];
  449. if(format.StartsWith("yyyy"))
  450. {
  451. if(tag == "now")
  452. {
  453. if(data.Length == 2)
  454. {
  455. return DateTime.Now.ToString(format);
  456. }
  457. else if(data.Length == 4)
  458. {
  459. if(data[2] == "DAY") return DateTime.Now.AddDays(int.Parse(data[1])).ToString(format);
  460. if(data[2] == "MONTH") return DateTime.Now.AddMonths(int.Parse(data[1])).ToString(format);
  461. }
  462. }
  463. else
  464. {
  465. if(data.Length == 2)
  466. {
  467. return DateTime.Parse(tag).ToString(format);
  468. }
  469. else if(data.Length == 4)
  470. {
  471. if(data[2] == "DAY") return DateTime.Parse(tag).AddDays(int.Parse(data[1])).ToString(format);
  472. if(data[2] == "MONTH") return DateTime.Parse(tag).AddMonths(int.Parse(data[1])).ToString(format);
  473. }
  474. }
  475. }
  476. }
  477. return str;
  478. }
  479. public static string MatchExpressionVal(string str)
  480. {
  481. if(string.IsNullOrEmpty(str)) return str;
  482. MatchCollection mc = Regex.Matches(str, "#\\{.*?\\}#");
  483. foreach(Match m in mc)
  484. {
  485. str = str.Replace(m.Value, GetExpressionVal(m.Value));
  486. }
  487. return str;
  488. }
  489. public static List<Dictionary<string, object>> initNodeData(string nodeType)
  490. {
  491. if(string.IsNullOrEmpty(nodeType)) return new List<Dictionary<string, object>>();
  492. var data = new List<Dictionary<string, object>>();
  493. if(nodeType == "json")
  494. {
  495. var item = new Dictionary<string, object>();
  496. item.Add("title", "标题1");
  497. item.Add("detail", "介绍1");
  498. item.Add("id", 1);
  499. var children = new List<Dictionary<string, object>>();
  500. var childItem = new Dictionary<string, object>();
  501. childItem.Add("title", "子标题1");
  502. childItem.Add("detail", "子介绍1");
  503. childItem.Add("id", 3);
  504. children.Add(childItem);
  505. childItem = new Dictionary<string, object>();
  506. childItem.Add("title", "子标题2");
  507. childItem.Add("detail", "子介绍2");
  508. childItem.Add("id", 4);
  509. children.Add(childItem);
  510. item.Add("children", children);
  511. data.Add(item);
  512. return data;
  513. }
  514. var input = new Dictionary<string, object>();
  515. input.Add("label", "user_id");
  516. input.Add("required", true);
  517. input.Add("type", new List<string>() { "number" });
  518. input.Add("desc", "");
  519. input.Add("children", new List<object>());
  520. input.Add("_id", "mqeuhpvg-1o5m6i612c4o4ph");
  521. data.Add(input);
  522. return data;
  523. }
  524. }
  525. }