using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WorkFlowApi.Utility.WorkFlowEngine.Scheme; using WorkFlowApi.Utility.WorkFlowEngine.Scheme.Config; using WorkFlowApi.Utility.WorkFlowEngine.Scheme.Line; using WorkFlowApi.Utility.WorkFlowEngine.Scheme.Node; namespace WorkFlowApi.Utility.WorkFlowEngine { /// /// 工作流引擎 /// public class WFEngine : WFIEngine { #region 构造函数 /// /// /// /// public WFEngine(WFEngineConfig nWFEngineConfig) { // 初始化模板数据 config = nWFEngineConfig; wfScheme = config.ParamConfig.Scheme.ToObject(); nodesMap = new Dictionary(); foreach (var node in wfScheme.nodes) { if (!nodesMap.ContainsKey(node.id)) { nodesMap.Add(node.id, node); } if (node.type == "startround") { startNode = node; } } } #endregion #region 模板数据信息 private WFEngineConfig config; private WFScheme wfScheme = null; private Dictionary nodesMap = null; private WFNodeInfo startNode = null; #endregion #region 流程模板操作方法 /// /// 获取流程模板 /// /// public string GetScheme() { return config.ParamConfig.Scheme; } /// /// 获取流程模板 /// /// public WFScheme GetSchemeObj() { return wfScheme; } /// /// 获取开始节点 /// /// 节点信息 public WFNodeInfo GetStartNode() { return startNode; } /// /// 获取节点 /// /// 流程处理节点ID /// 节点信息 public WFNodeInfo GetNode(string nodeId) { if (nodesMap.ContainsKey(nodeId)) { return nodesMap[nodeId]; } else { return null; } } /// /// 获取两节点间的线条 /// /// 开始节点 /// 结束节点 /// 线条列表 /// public bool GetLines(string fromNodeId, string toNodeId, List list, Dictionary nodes = null) { bool res = false; if (nodes == null) { nodes = new Dictionary(); } foreach (var line in wfScheme.lines) { if (line.from == fromNodeId) { if (line.to == toNodeId) { list.Add(line); return true; } else { if (line.to == fromNodeId || nodesMap[line.to] == null || nodesMap[line.to].type == "endround") { } else if (!nodes.ContainsKey(line.to)) { nodes.Add(line.to, "1"); res = GetLines(line.to, toNodeId, list, nodes); } } if (res) { list.Add(line); return true; } } } return res; } /// /// 获取下一节点 /// /// 当前节点Id /// 节点操作码 agree 同意 disagree 不同意 lrtimeout 超时 /// /// 节点信息列表 public List GetNextNodes(string nodeId, string code, List lineList) { List nextNodes = new List(); // 找到与当前节点相连的线条 foreach (var line in wfScheme.lines) { if (line.from == nodeId) { bool isOk = false; if (string.IsNullOrEmpty(line.strategy) || line.strategy == "1") { isOk = true; } else { var codeList = line.agreeList.Split(','); foreach (string _code in codeList) { if (_code == code) { isOk = true; break; } } } if (isOk) { if (nodesMap.ContainsKey(line.to)) { nextNodes.Add(nodesMap[line.to]); switch (line.operationType) {// 绑定的操作类型 case "sql": // sql 语句 if (!string.IsNullOrEmpty(line.dbId) && !string.IsNullOrEmpty(line.strSql)) { lineList.Add(line); } break; case "interface": // interface 接口 if (!string.IsNullOrEmpty(line.strInterface)) { lineList.Add(line); } break; case "ioc": // 依赖注入 if (!string.IsNullOrEmpty(line.iocName)) { lineList.Add(line); } break; } } } } } return nextNodes; } public string GetButonList(string nodeId, List lineList) { string codelist = string.Empty; // 找到与当前节点相连的线条 foreach (var line in wfScheme.lines) { if (line.from == nodeId) { if (!line.agreeList.IsNullOrEmpty()) { codelist = codelist + ',' + line.agreeList; } } } return codelist.TrimStart(','); } /// /// 获取上一节点列表 /// /// 当前节点Id /// public List GetPreNodes(string nodeId) { List list = new List(); // 找到与当前节点相连的线条 foreach (var line in wfScheme.lines) { if (line.to == nodeId) { list.Add(line.from); } } return list; } /// /// 判断两节点是否连接 /// /// 开始节点 /// 结束节点 /// public bool IsToNode(string formNodeId, string toNodeId) { bool res = false; foreach (var line in wfScheme.lines) { if (line.from == formNodeId) { if (line.to == toNodeId) { res = true; break; } else { if (line.to == formNodeId || nodesMap[line.to] == null || nodesMap[line.to].type == "endround") { break; } else { if (IsToNode(line.to, toNodeId)) { res = true; break; } } } } } return res; } #endregion #region 流程运行操作方法 /// /// 获取配置参数信息 /// /// public WFEngineParamConfig GetConfig() { return config.ParamConfig; } /// /// 获取接下来的任务节点信息 /// /// 起始节点 /// 节点操作码 agree 同意 disagree 不同意 lrtimeout 超时 /// 是否获取下一节点审核人 /// 经过的线段需要执行操作的 /// public List GetNextTaskNode(WFNodeInfo beginNode, string code, bool isGetAuditors, List lineList,string branchnodeCode="") { List list = new List(); List nextNodeList = GetNextNodes(beginNode.id, code, lineList); //Dictionary auditers = null; //if (!string.IsNullOrEmpty(config.ParamConfig.Auditers)) //{ // auditers = config.ParamConfig.Auditers.ToObject>(); //} var auditers = config.ParamConfig.Auditers; foreach (var node in nextNodeList) { if (auditers != null && auditers.Count>0) { node.auditors = auditers; } switch (node.type) { case "branchnode":// 分支节点 list.AddRange(GetNextTaskNode(node, branchnodeCode, isGetAuditors, lineList)); break; case "startround":// 开始节点 需要重新审核 list.Add(node); config.ParamConfig.State = 1; break; case "endround":// 结束节点 list.Add(node); config.ParamConfig.State = 2; break; default: // 默认普通节点 list.Add(node); break; } } return list; } #endregion } }