/// <summary> /// 新增流程(对数据库操作) /// </summary> /// <param name="ApprovalData"></param> /// <param name="APPDataResult"></param> /// <returns></returns> public DataResult AddFlow2(OracleConnection con, SubmitData submitData, DataResult dataResult, ref string msg) { // WorkflowRuntime workflowRuntime = null; WorkflowInstance instance = null; try { msg += "获取定义的流程" + DateTime.Now.ToString() + "\r\n"; #region 获取定义的流程 LogHelper.WriteLog("获取定义的流程.GetFlowByModelName:submitData.ApprovalUser.DepartmentID=" + submitData.ApprovalUser.DepartmentID + ";OrgType='" + ((int)submitData.FlowType).ToString() + "'"); List<FLOW_MODELFLOWRELATION_T> MODELFLOWRELATION = GetFlowByModelName(con, submitData.ApprovalUser.CompanyID, submitData.ApprovalUser.DepartmentID, submitData.ModelCode, ((int)submitData.FlowType).ToString());//对数据库操作 if (MODELFLOWRELATION == null || MODELFLOWRELATION.Count == 0) { dataResult.FlowResult = FlowResult.FAIL; dataResult.Err = "没有找到可使用的流程"; return dataResult; } msg += "获取定义的流程完成" + DateTime.Now.ToString() + "\r\n"; #endregion FLOW_MODELFLOWRELATION_T flowRelation = MODELFLOWRELATION[0]; FLOW_FLOWDEFINE_T flowDefine = flowRelation.FLOW_FLOWDEFINE_T; if (flowDefine.RULES != null && flowDefine.RULES.Trim() == "") { flowDefine.RULES = null; } //if (string.IsNullOrEmpty(flowDefine.RULES.Trim())) //{ // flowDefine.RULES = null; //} msg += " SMTWorkFlowManage.CreateWorkFlowRuntime(true)" + DateTime.Now.ToString() + "\r\n"; workflowRuntime = SMTWorkFlowManage.CreateWorkFlowRuntime(true); instance = SMTWorkFlowManage.CreateWorkflowInstance(workflowRuntime, flowDefine.XOML, flowDefine.RULES); LogHelper.WriteLog("AddFlow2创建工作流实例ID=" + instance.InstanceId); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { instance = null; }; msg += " SMTWorkFlowManage.CreateWorkFlowRuntime(true)完成" + DateTime.Now.ToString() + "\r\n"; #region master赋值 FLOW_FLOWRECORDMASTER_T master = new FLOW_FLOWRECORDMASTER_T(); master.INSTANCEID = instance.InstanceId.ToString(); master.BUSINESSOBJECT = submitData.XML; master.FORMID = submitData.FormID; master.MODELCODE = submitData.ModelCode; master.ACTIVEROLE = Utility.GetActiveRlue(flowDefine.LAYOUT); master.FLOWTYPE = ((int)submitData.FlowType).ToString(); master.FLOWSELECTTYPE = ((int)submitData.FlowSelectType).ToString(); master.FLOWCODE = flowDefine.FLOWCODE; #endregion #region 获取下一状态数据 msg += " 获取下一状态数据" + DateTime.Now.ToString() + "\r\n"; DateTime star = DateTime.Now; GetUserByFlow2(submitData.ApprovalUser.CompanyID, flowDefine.XOML, flowDefine.RULES, master.ACTIVEROLE, submitData.XML, submitData.ApprovalUser.UserID, submitData.ApprovalUser.PostID, submitData.FlowType, ref dataResult, ref msg); DateTime end = DateTime.Now; msg += " 获取下一状态数据完成" + DateTime.Now.ToString() + "\r\n"; string str = Service2.DateDiff(end, star); if (dataResult.FlowResult == FlowResult.FAIL) { return dataResult; } submitData.NextStateCode = dataResult.AppState; if (dataResult.IsCountersign) { #region if (dataResult.FlowResult == FlowResult.Countersign) { if (submitData.DictCounterUser == null || submitData.DictCounterUser.Keys.Count == 0) { return dataResult; } } else { if (submitData.DictCounterUser == null || submitData.DictCounterUser.Keys.Count == 0) { submitData.DictCounterUser = dataResult.DictCounterUser; } } #endregion } else { #region if (dataResult.FlowResult == FlowResult.MULTIUSER) { if (submitData.NextApprovalUser == null || (Utility.GetString(submitData.NextApprovalUser.UserID) == "" || Utility.GetString(submitData.NextApprovalUser.UserName) == "")) { return dataResult; } } else { if (submitData.DictCounterUser == null || submitData.DictCounterUser.Keys.Count == 0) { submitData.NextApprovalUser = dataResult.UserInfo[0]; } } #endregion } #endregion #region 实体赋值 FLOW_FLOWRECORDDETAIL_T entity = new FLOW_FLOWRECORDDETAIL_T(); entity.FLOW_FLOWRECORDMASTER_T = master; entity.CREATECOMPANYID = submitData.ApprovalUser.CompanyID; entity.CREATEDEPARTMENTID = submitData.ApprovalUser.DepartmentID; entity.CREATEPOSTID = submitData.ApprovalUser.PostID; entity.CREATEUSERID = submitData.ApprovalUser.UserID; entity.CREATEUSERNAME = submitData.ApprovalUser.UserName; #endregion msg += " 处理kpi时间" + DateTime.Now.ToString() + "\r\n"; #region 处理kpi时间 string KPITime = ""; #region 加入缓存 string pscResult = CacheProvider.GetCache<string>(flowRelation.MODELFLOWRELATIONID); if (string.IsNullOrEmpty(pscResult)) { PerformanceServiceWS.PerformanceServiceClient psc = new PerformanceServiceWS.PerformanceServiceClient(); pscResult = psc.GetKPIPointsByBusinessCode(flowRelation.MODELFLOWRELATIONID);//调用服务 CacheProvider.Add<string>(flowRelation.MODELFLOWRELATIONID, pscResult); psc.Close(); } #endregion //PerformanceServiceWS.PerformanceServiceClient psc = new PerformanceServiceWS.PerformanceServiceClient(); //string pscResult = psc.GetKPIPointsByBusinessCode(flowRelation.MODELFLOWRELATIONID);//调用服务 //psc.Close(); if (!string.IsNullOrEmpty(pscResult)) { XElement xe = XElement.Parse(pscResult); Func<XElement, bool> f = (x) => { XAttribute xid = x.Attribute("id"); XAttribute xvalue = x.Attribute("value"); if (xid == null || xvalue == null) return false; else { if (xid.Value == dataResult.AppState) return true; else return false; } }; XElement FlowNode = xe.Elements("FlowNode").FirstOrDefault(f); if (FlowNode != null) { KPITime = FlowNode.Attribute("value").Value; } } dataResult.KPITime = KPITime; master.KPITIMEXML = pscResult; #endregion msg += " 处理kpi时间完成" + DateTime.Now.ToString() + "\r\n"; FlowDataType.FlowData FlowData = new FlowDataType.FlowData(); FlowData.xml = submitData.XML; if (!dataResult.IsCountersign) { #region 非会签 UserInfo AppUser = new UserInfo(); //下一审核人 AppUser = submitData.NextApprovalUser; dataResult.UserInfo.Clear(); dataResult.UserInfo.Add(AppUser); UserInfo AgentAppUser = GetAgentUserInfo(submitData.ModelCode, AppUser.UserID);//查询是否启用了代理人 dataResult = DoFlowRecord2(con, workflowRuntime, instance, entity, submitData.NextStateCode, AppUser, AgentAppUser, submitData.SubmitFlag, submitData.FlowType); //处理流程数据 dataResult.IsCountersign = false; dataResult.AgentUserInfo = AgentAppUser; #endregion } else { msg += " 会签" + DateTime.Now.ToString() + "\r\n"; #region 会签 //Tracer.Debug("-----DoFlowRecord_Add:" + DateTime.Now.ToString()+"\n"); dataResult.DictCounterUser = submitData.DictCounterUser; Dictionary<UserInfo, UserInfo> dictAgentUserInfo = GetAgentUserInfo2(submitData.ModelCode, submitData.DictCounterUser); dataResult = DoFlowRecord_Add(con, workflowRuntime, instance, entity, submitData.NextStateCode, submitData.DictCounterUser, dictAgentUserInfo, submitData.SubmitFlag, submitData.FlowType); //处理流程数据 //Tracer.Debug("-----DoFlowRecord_AddEnd:" + DateTime.Now.ToString()+"\n"); dataResult.IsCountersign = true; dataResult.DictAgentUserInfo = dictAgentUserInfo; #endregion msg += "会签完成" + DateTime.Now.ToString() + "\r\n"; } msg += "激发流程引擎执行到一下流程" + DateTime.Now.ToString() + "\r\n"; #region 激发流程引擎执行到一下流程 string ss = ""; int n = 0; if (dataResult.AppState == null || dataResult.AppState == "") { msg += " workflowRuntime.GetService<FlowEvent>()" + DateTime.Now.ToString() + "\r\n"; workflowRuntime.GetService<FlowEvent>().OnDoFlow(instance.InstanceId, FlowData); //激发流程引擎执行到一下流程 msg += " workflowRuntime.GetService<FlowEvent>()完成" + DateTime.Now.ToString() + "\r\n"; } else { StateMachineWorkflowInstance workflowinstance = new StateMachineWorkflowInstance(workflowRuntime, instance.InstanceId); workflowinstance.SetState(dataResult.AppState); //流程跳转到指定节点 while (true) { ss += (n++).ToString() + "|" + workflowinstance.CurrentStateName + "\r\n"; string stateName = workflowinstance.CurrentStateName; if (stateName != null && stateName.ToUpper().IndexOf("STATE") >= 0) { break; } } } #endregion msg += "激发流程引擎执行到一下流程完成" + DateTime.Now.ToString() + "\r\n"; msg += "System.Threading.Thread.Sleep(1000)" + DateTime.Now.ToString() + "\r\n"; //System.Threading.Thread.Sleep(1000);//当前用到 dataResult.ModelFlowRelationID = flowRelation.MODELFLOWRELATIONID; //返回关联ID dataResult.KPITime = KPITime; //dataResult.CanSendMessage = true; if (submitData.FlowType == FlowType.Task) { dataResult.SubModelCode = Utility.GetSubModelCode(master.ACTIVEROLE, dataResult.AppState); //返回下一子模块代码 } msg += "System.Threading.Thread.Sleep(1000)完成" + DateTime.Now.ToString() + "\r\n"; return dataResult; } catch (Exception e) { throw new Exception(e.Message); } finally { instance = null; SMTWorkFlowManage.ColseWorkFlowRuntime(workflowRuntime); } }
/// <summary> /// 新增流程(对数据库操作) /// </summary> /// <param name="ApprovalData"></param> /// <param name="APPDataResult"></param> /// <returns></returns> public DataResult AddFlow2(OracleConnection con, SubmitData submitData, DataResult dataResult, ref FlowUser user) { // WorkflowRuntime workflowRuntime = null; WorkflowInstance instance = null; try { #region 获取定义的流程 user.TrackingMessage += "获取定义的流程.GetFlowByModelName:submitData.ApprovalUser.DepartmentID=" + submitData.ApprovalUser.DepartmentID + ";OrgType='" + ((int)submitData.FlowType).ToString() + "'"; List<FLOW_MODELFLOWRELATION_T> MODELFLOWRELATION = GetFlowByModelName(con, submitData.ApprovalUser.CompanyID, submitData.ApprovalUser.DepartmentID, submitData.ModelCode, ((int)submitData.FlowType).ToString(), ref user);//对数据库操作 if (MODELFLOWRELATION == null || MODELFLOWRELATION.Count == 0) { dataResult.FlowResult = FlowResult.FAIL; dataResult.Err = "没有找到可使用的流程"; if (submitData.ApprovalUser.CompanyID == user.CompayID && submitData.ApprovalUser.DepartmentID == user.DepartmentID) { dataResult.Err = "没有找到公司[ " + user.CompayName + " ]下部门[ " + user.DepartmentName + " ]的匹配流程返回"; } else { dataResult.Err = "没有找到公司[ " + user.CompayName + " ]的可使用匹配流程"; } return dataResult; } #endregion FLOW_MODELFLOWRELATION_T flowRelation = MODELFLOWRELATION[0];//只取其中一条流程 FLOW_FLOWDEFINE_T flowDefine = flowRelation.FLOW_FLOWDEFINE_T; user.FlowCode = flowDefine.FLOWCODE;//流程代码 user.FlowName = flowDefine.DESCRIPTION;//流程名称 if (flowDefine.RULES != null && flowDefine.RULES.Trim() == "") { flowDefine.RULES = null; } workflowRuntime = SMTWorkFlowManage.CreateWorkFlowRuntime(true); instance = SMTWorkFlowManage.CreateWorkflowInstance(workflowRuntime, flowDefine.XOML, flowDefine.RULES); LogHelper.WriteLog("新增 FormID=" + user.FormID + " 流程名称=" + flowDefine.DESCRIPTION + "("+flowDefine.FLOWCODE+") 提交人=" + user.UserName + " 公司名称=" + user.CompayName + " 部门名称=" + user.DepartmentName + " 岗位名称=" + user.PostName + " WorkflowInstance ID=" + instance.InstanceId.ToString()); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { instance = null; }; #region master赋值 FLOW_FLOWRECORDMASTER_T master = new FLOW_FLOWRECORDMASTER_T(); master.INSTANCEID = instance.InstanceId.ToString(); master.BUSINESSOBJECT = submitData.XML; master.FORMID = submitData.FormID; master.MODELCODE = submitData.ModelCode; master.ACTIVEROLE = Utility.GetActiveRlue(flowDefine.LAYOUT); master.FLOWTYPE = ((int)submitData.FlowType).ToString(); master.FLOWSELECTTYPE = ((int)submitData.FlowSelectType).ToString(); master.FLOWCODE = flowDefine.FLOWCODE; #endregion #region 获取下一状态数据 user.TrackingMessage += "FORMID=" + user.FormID + "获取下一状态数据(开始)"; GetUserByFlow2(submitData.ApprovalUser.CompanyID, flowDefine.XOML, flowDefine.RULES, master.ACTIVEROLE, submitData.XML, submitData.ApprovalUser.UserID, submitData.ApprovalUser.PostID, submitData.FlowType, ref dataResult, ref user); LogHelper.WriteLog("FormID=" + user.FormID + " 获取下一状态数据! dataResult.FlowResult=" + dataResult.FlowResult.ToString()); user.TrackingMessage += "FORMID=" + user.FormID + "获取下一状态数据(结束)"; if (dataResult.FlowResult == FlowResult.FAIL) { return dataResult; } submitData.NextStateCode = dataResult.AppState; if (dataResult.IsCountersign) { #region 检查会签是角色是否有审核人员 #region 记录日志 if(submitData.DictCounterUser!=null) { LogHelper.WriteLog("FormID=" + user.FormID + " submitData.DictCounterUser="******"FormID=" + user.FormID + " dataResult.DictCounterUser="******"FormID=" + user.FormID + " submitData.DictCounterUser 会签角色里没有发现有审核人员,所以返回!"); return dataResult; } } else { if (submitData.DictCounterUser == null || submitData.DictCounterUser.Keys.Count == 0) { submitData.DictCounterUser = dataResult.DictCounterUser; } } #endregion } else { #region 检查非会签角色里是否有审核人员 if (dataResult.FlowResult == FlowResult.MULTIUSER) { LogHelper.WriteLog("FormID=" + user.FormID + " 发现有多个审核人员!"); if (submitData.NextApprovalUser == null || (Utility.GetString(submitData.NextApprovalUser.UserID) == "" || Utility.GetString(submitData.NextApprovalUser.UserName) == "")) { LogHelper.WriteLog("FormID=" + user.FormID + " 发现有多个审核人员!但下一审核人为空,所以返回选择审核人!"); return dataResult; } else { LogHelper.WriteLog("FormID=" + user.FormID + " 发现有多个审核人员,但发现下一审核人不为空 usrid="+(Utility.GetString(submitData.NextApprovalUser.UserID)+" 姓名="+Utility.GetString(submitData.NextApprovalUser.UserName))); } } else { if (submitData.DictCounterUser == null || submitData.DictCounterUser.Keys.Count == 0) { submitData.NextApprovalUser = dataResult.UserInfo[0]; } } #endregion } #endregion #region 实体赋值 当提交人为空时,创建人变成单据所属人,如果不为空,则创建人保存为系统登录人;创建公司,部门,岗位,仍然保存单据所属人的公司,部门,岗位 FLOW_FLOWRECORDDETAIL_T entity = new FLOW_FLOWRECORDDETAIL_T(); entity.FLOW_FLOWRECORDMASTER_T = master; entity.CREATECOMPANYID = submitData.ApprovalUser.CompanyID; entity.CREATEDEPARTMENTID = submitData.ApprovalUser.DepartmentID ; entity.CREATEPOSTID = submitData.ApprovalUser.PostID; entity.CREATEUSERID = string.IsNullOrEmpty(submitData.SumbitUserID) ? submitData.ApprovalUser.UserID : submitData.SumbitUserID; entity.CREATEUSERNAME = string.IsNullOrEmpty(submitData.SumbitUserName) ? submitData.ApprovalUser.UserName : submitData.SumbitUserName; #endregion user.TrackingMessage += " 处理kpi时间\r\n"; #region 处理kpi时间 string KPITime = ""; #region 加入缓存 string pscResult = CacheProvider.GetCache<string>(flowRelation.MODELFLOWRELATIONID); if (string.IsNullOrEmpty(pscResult)) { PerformanceServiceWS.PerformanceServiceClient psc = new PerformanceServiceWS.PerformanceServiceClient(); pscResult = psc.GetKPIPointsByBusinessCode(flowRelation.MODELFLOWRELATIONID);//调用服务 CacheProvider.Add<string>(flowRelation.MODELFLOWRELATIONID, pscResult); psc.Close(); } #endregion //PerformanceServiceWS.PerformanceServiceClient psc = new PerformanceServiceWS.PerformanceServiceClient(); //string pscResult = psc.GetKPIPointsByBusinessCode(flowRelation.MODELFLOWRELATIONID);//调用服务 //psc.Close(); if (!string.IsNullOrEmpty(pscResult)) { XElement xe = XElement.Parse(pscResult); Func<XElement, bool> f = (x) => { XAttribute xid = x.Attribute("id"); XAttribute xvalue = x.Attribute("value"); if (xid == null || xvalue == null) return false; else { if (xid.Value == dataResult.AppState) return true; else return false; } }; XElement FlowNode = xe.Elements("FlowNode").FirstOrDefault(f); if (FlowNode != null) { KPITime = FlowNode.Attribute("value").Value; } } dataResult.KPITime = KPITime; master.KPITIMEXML = pscResult; #endregion user.TrackingMessage += " 处理kpi时间完成\r\n"; FlowDataType.FlowData FlowData = new FlowDataType.FlowData(); FlowData.xml = submitData.XML; if (!dataResult.IsCountersign) { #region 确定非会签的下一个审核人 UserInfo AppUser = new UserInfo(); //下一审核人 AppUser = submitData.NextApprovalUser; dataResult.UserInfo.Clear(); dataResult.UserInfo.Add(AppUser); UserInfo AgentAppUser = GetAgentUserInfo(submitData.ModelCode, AppUser.UserID);//查询是否启用了代理人 dataResult = DoFlowRecord2(con, workflowRuntime, instance, entity, submitData.NextStateCode, AppUser, AgentAppUser, submitData.SubmitFlag, submitData.FlowType,ref user); //处理流程数据 dataResult.IsCountersign = false; dataResult.AgentUserInfo = AgentAppUser; #endregion } else { user.TrackingMessage += " 会签\r\n"; #region 确定会签角色里的审核人员 //Tracer.Debug("-----DoFlowRecord_Add:" + DateTime.Now.ToString()+"\n"); dataResult.DictCounterUser = submitData.DictCounterUser; Dictionary<UserInfo, UserInfo> dictAgentUserInfo = GetAgentUserInfo2(submitData.ModelCode, submitData.DictCounterUser); dataResult = DoFlowRecord_Add(con, workflowRuntime, instance, entity, submitData.NextStateCode, submitData.DictCounterUser, dictAgentUserInfo, submitData.SubmitFlag, submitData.FlowType); //处理流程数据 //Tracer.Debug("-----DoFlowRecord_AddEnd:" + DateTime.Now.ToString()+"\n"); dataResult.IsCountersign = true; dataResult.DictAgentUserInfo = dictAgentUserInfo; #endregion user.TrackingMessage += "会签完成\r\n"; } user.TrackingMessage += "激发流程引擎执行到一下流程\r\n"; #region 激发流程引擎执行到一下流程 string ss = ""; int n = 0; StateMachineWorkflowInstance workflowinstance = new StateMachineWorkflowInstance(workflowRuntime, instance.InstanceId); ManualWorkflowSchedulerService scheduleService = workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) as ManualWorkflowSchedulerService; if (dataResult.AppState == null || dataResult.AppState == "") { user.TrackingMessage += " workflowRuntime.GetService<FlowEvent>()\r\n"; scheduleService.RunWorkflow(workflowinstance.InstanceId); workflowRuntime.GetService<FlowEvent>().OnDoFlow(instance.InstanceId, FlowData); //激发流程引擎执行到一下流程 scheduleService.RunWorkflow(workflowinstance.InstanceId); user.TrackingMessage += " workflowRuntime.GetService<FlowEvent>()完成\r\n"; } else { scheduleService.RunWorkflow(workflowinstance.InstanceId); workflowinstance.SetState(dataResult.AppState); //流程跳转到指定节点 } #endregion user.TrackingMessage += "激发流程引擎执行到一下流程完成\r\n"; user.TrackingMessage += "System.Threading.Thread.Sleep(1000)\r\n"; //System.Threading.Thread.Sleep(1000);//当前用到 dataResult.ModelFlowRelationID = flowRelation.MODELFLOWRELATIONID; //返回关联ID dataResult.KPITime = KPITime; //dataResult.CanSendMessage = true; if (submitData.FlowType == FlowType.Task) { dataResult.SubModelCode = Utility.GetSubModelCode(master.ACTIVEROLE, dataResult.AppState); //返回下一子模块代码 } user.TrackingMessage += "System.Threading.Thread.Sleep(1000)完成\r\n"; return dataResult; } catch (Exception e) { user.ErrorMsg += "新增流程出错 FormID=" + user.FormID + " CompayName=" + user.CompayName + "FlowName=" + user.FlowName + "异常信息:\r\n" + e.ToString() + "\r\n"; LogHelper.WriteLog("FormID=" + user.FormID + " CompayName=" + user.CompayName + "FlowName=" + user.FlowName + " 新增流程出错,异常信息:\r\n" + e.ToString()); throw new Exception("FormID=" + user.FormID+" 时间:"+DateTime.Now.ToString()+" 新增流程出错,请联系管理员! "); } finally { instance = null; SMTWorkFlowManage.ColseWorkFlowRuntime(workflowRuntime); } }