工作流

WFEngine.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using WorkFlowApi.Utility.WorkFlowEngine.Scheme;
  7. using WorkFlowApi.Utility.WorkFlowEngine.Scheme.Config;
  8. using WorkFlowApi.Utility.WorkFlowEngine.Scheme.Line;
  9. using WorkFlowApi.Utility.WorkFlowEngine.Scheme.Node;
  10. namespace WorkFlowApi.Utility.WorkFlowEngine
  11. {
  12. /// <summary>
  13. /// 工作流引擎
  14. /// </summary>
  15. public class WFEngine : WFIEngine
  16. {
  17. #region 构造函数
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. /// <param name="nWFEngineConfig"></param>
  22. public WFEngine(WFEngineConfig nWFEngineConfig)
  23. {
  24. // 初始化模板数据
  25. config = nWFEngineConfig;
  26. wfScheme = config.ParamConfig.Scheme.ToObject<WFScheme>();
  27. nodesMap = new Dictionary<string, WFNodeInfo>();
  28. foreach (var node in wfScheme.nodes)
  29. {
  30. if (!nodesMap.ContainsKey(node.id))
  31. {
  32. nodesMap.Add(node.id, node);
  33. }
  34. if (node.type == "startround")
  35. {
  36. startNode = node;
  37. }
  38. }
  39. }
  40. #endregion
  41. #region 模板数据信息
  42. private WFEngineConfig config;
  43. private WFScheme wfScheme = null;
  44. private Dictionary<string, WFNodeInfo> nodesMap = null;
  45. private WFNodeInfo startNode = null;
  46. #endregion
  47. #region 流程模板操作方法
  48. /// <summary>
  49. /// 获取流程模板
  50. /// </summary>
  51. /// <returns></returns>
  52. public string GetScheme()
  53. {
  54. return config.ParamConfig.Scheme;
  55. }
  56. /// <summary>
  57. /// 获取流程模板
  58. /// </summary>
  59. /// <returns></returns>
  60. public WFScheme GetSchemeObj()
  61. {
  62. return wfScheme;
  63. }
  64. /// <summary>
  65. /// 获取开始节点
  66. /// </summary>
  67. /// <returns>节点信息</returns>
  68. public WFNodeInfo GetStartNode()
  69. {
  70. return startNode;
  71. }
  72. /// <summary>
  73. /// 获取节点
  74. /// </summary>
  75. /// <param name="nodeId">流程处理节点ID</param>
  76. /// <returns>节点信息</returns>
  77. public WFNodeInfo GetNode(string nodeId)
  78. {
  79. if (nodesMap.ContainsKey(nodeId))
  80. {
  81. return nodesMap[nodeId];
  82. }
  83. else
  84. {
  85. return null;
  86. }
  87. }
  88. /// <summary>
  89. /// 获取两节点间的线条
  90. /// </summary>
  91. /// <param name="fromNodeId">开始节点</param>
  92. /// <param name="toNodeId">结束节点</param>
  93. /// <param name="list">线条列表</param>
  94. /// <param name="nodes"></param>
  95. public bool GetLines(string fromNodeId, string toNodeId, List<WFLineInfo> list, Dictionary<string, string> nodes = null)
  96. {
  97. bool res = false;
  98. if (nodes == null)
  99. {
  100. nodes = new Dictionary<string, string>();
  101. }
  102. foreach (var line in wfScheme.lines)
  103. {
  104. if (line.from == fromNodeId)
  105. {
  106. if (line.to == toNodeId)
  107. {
  108. list.Add(line);
  109. return true;
  110. }
  111. else
  112. {
  113. if (line.to == fromNodeId || nodesMap[line.to] == null || nodesMap[line.to].type == "endround")
  114. {
  115. }
  116. else if (!nodes.ContainsKey(line.to))
  117. {
  118. nodes.Add(line.to, "1");
  119. res = GetLines(line.to, toNodeId, list, nodes);
  120. }
  121. }
  122. if (res)
  123. {
  124. list.Add(line);
  125. return true;
  126. }
  127. }
  128. }
  129. return res;
  130. }
  131. /// <summary>
  132. /// 获取下一节点
  133. /// </summary>
  134. /// <param name="nodeId">当前节点Id</param>
  135. /// <param name="code">节点操作码 agree 同意 disagree 不同意 lrtimeout 超时</param>
  136. /// <param name="lineList"></param>
  137. /// <returns>节点信息列表</returns>
  138. public List<WFNodeInfo> GetNextNodes(string nodeId, string code, List<WFLineInfo> lineList)
  139. {
  140. List<WFNodeInfo> nextNodes = new List<WFNodeInfo>();
  141. // 找到与当前节点相连的线条
  142. foreach (var line in wfScheme.lines)
  143. {
  144. if (line.from == nodeId)
  145. {
  146. bool isOk = false;
  147. if (string.IsNullOrEmpty(line.strategy) || line.strategy == "1")
  148. {
  149. isOk = true;
  150. }
  151. else
  152. {
  153. var codeList = line.agreeList.Split(',');
  154. foreach (string _code in codeList)
  155. {
  156. if (_code == code)
  157. {
  158. isOk = true;
  159. break;
  160. }
  161. }
  162. }
  163. if (isOk)
  164. {
  165. if (nodesMap.ContainsKey(line.to))
  166. {
  167. nextNodes.Add(nodesMap[line.to]);
  168. switch (line.operationType)
  169. {// 绑定的操作类型
  170. case "sql": // sql 语句
  171. if (!string.IsNullOrEmpty(line.dbId) && !string.IsNullOrEmpty(line.strSql))
  172. {
  173. lineList.Add(line);
  174. }
  175. break;
  176. case "interface": // interface 接口
  177. if (!string.IsNullOrEmpty(line.strInterface))
  178. {
  179. lineList.Add(line);
  180. }
  181. break;
  182. case "ioc": // 依赖注入
  183. if (!string.IsNullOrEmpty(line.iocName))
  184. {
  185. lineList.Add(line);
  186. }
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. return nextNodes;
  194. }
  195. public string GetButonList(string nodeId, List<WFLineInfo> lineList)
  196. {
  197. string codelist = string.Empty;
  198. // 找到与当前节点相连的线条
  199. foreach (var line in wfScheme.lines)
  200. {
  201. if (line.from == nodeId)
  202. {
  203. if (!line.agreeList.IsNullOrEmpty())
  204. {
  205. codelist = codelist + ',' + line.agreeList;
  206. }
  207. }
  208. }
  209. return codelist.TrimStart(',');
  210. }
  211. /// <summary>
  212. /// 获取上一节点列表
  213. /// </summary>
  214. /// <param name="nodeId">当前节点Id</param>
  215. /// <returns></returns>
  216. public List<string> GetPreNodes(string nodeId)
  217. {
  218. List<string> list = new List<string>();
  219. // 找到与当前节点相连的线条
  220. foreach (var line in wfScheme.lines)
  221. {
  222. if (line.to == nodeId)
  223. {
  224. list.Add(line.from);
  225. }
  226. }
  227. return list;
  228. }
  229. /// <summary>
  230. /// 判断两节点是否连接
  231. /// </summary>
  232. /// <param name="formNodeId">开始节点</param>
  233. /// <param name="toNodeId">结束节点</param>
  234. /// <returns></returns>
  235. public bool IsToNode(string formNodeId, string toNodeId)
  236. {
  237. bool res = false;
  238. foreach (var line in wfScheme.lines)
  239. {
  240. if (line.from == formNodeId)
  241. {
  242. if (line.to == toNodeId)
  243. {
  244. res = true;
  245. break;
  246. }
  247. else
  248. {
  249. if (line.to == formNodeId || nodesMap[line.to] == null || nodesMap[line.to].type == "endround")
  250. {
  251. break;
  252. }
  253. else
  254. {
  255. if (IsToNode(line.to, toNodeId))
  256. {
  257. res = true;
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. return res;
  265. }
  266. #endregion
  267. #region 流程运行操作方法
  268. /// <summary>
  269. /// 获取配置参数信息
  270. /// </summary>
  271. /// <returns></returns>
  272. public WFEngineParamConfig GetConfig()
  273. {
  274. return config.ParamConfig;
  275. }
  276. /// <summary>
  277. /// 获取接下来的任务节点信息
  278. /// </summary>
  279. /// <param name="beginNode">起始节点</param>
  280. /// <param name="code">节点操作码 agree 同意 disagree 不同意 lrtimeout 超时</param>
  281. /// <param name="isGetAuditors">是否获取下一节点审核人</param>
  282. /// <param name="lineList">经过的线段需要执行操作的</param>
  283. /// <returns></returns>
  284. public List<WFNodeInfo> GetNextTaskNode(WFNodeInfo beginNode, string code, bool isGetAuditors, List<WFLineInfo> lineList,string branchnodeCode="")
  285. {
  286. List<WFNodeInfo> list = new List<WFNodeInfo>();
  287. List<WFNodeInfo> nextNodeList = GetNextNodes(beginNode.id, code, lineList);
  288. //Dictionary<string, string> auditers = null;
  289. //if (!string.IsNullOrEmpty(config.ParamConfig.Auditers))
  290. //{
  291. // auditers = config.ParamConfig.Auditers.ToObject<Dictionary<string, string>>();
  292. //}
  293. var auditers = config.ParamConfig.Auditers;
  294. foreach (var node in nextNodeList)
  295. {
  296. if (auditers != null && auditers.Count>0)
  297. {
  298. node.auditors = auditers;
  299. }
  300. switch (node.type)
  301. {
  302. case "branchnode":// 分支节点
  303. list.AddRange(GetNextTaskNode(node, branchnodeCode, isGetAuditors, lineList));
  304. break;
  305. case "startround":// 开始节点 需要重新审核
  306. list.Add(node);
  307. config.ParamConfig.State = 1;
  308. break;
  309. case "endround":// 结束节点
  310. list.Add(node);
  311. config.ParamConfig.State = 2;
  312. break;
  313. default: // 默认普通节点
  314. list.Add(node);
  315. break;
  316. }
  317. }
  318. return list;
  319. }
  320. #endregion
  321. }
  322. }