OpenAuth.Net/OpenAuth.App/FlowInstanceApp.cs

483 lines
21 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
2018-03-23 15:00:35 +08:00
using Newtonsoft.Json.Linq;
2018-03-20 23:15:09 +08:00
using OpenAuth.App.Flow;
2018-03-12 23:44:05 +08:00
using OpenAuth.App.Request;
2017-11-30 17:47:41 +08:00
using OpenAuth.App.Response;
using OpenAuth.App.SSO;
2017-11-29 20:49:14 +08:00
using OpenAuth.Repository.Domain;
namespace OpenAuth.App
{
/// <summary>
/// 工作流实例表操作
/// </summary>
2018-03-23 17:47:44 +08:00
public class FlowInstanceApp : BaseApp<FlowInstance>
{
2018-04-10 10:53:48 +08:00
public RevelanceManagerApp RevelanceManagerApp { get; set; }
2018-03-30 17:35:18 +08:00
#region API
/// <summary>
/// 创建一个实例
/// </summary>
/// <returns></returns>
2018-03-23 15:00:35 +08:00
public bool CreateInstance(JObject obj)
{
2018-03-23 15:00:35 +08:00
var flowInstance = obj.ToObject<FlowInstance>();
2018-03-21 17:36:31 +08:00
//获取提交的表单数据
2018-03-23 15:00:35 +08:00
var frmdata = new JObject();
foreach (var property in obj.Properties().Where(U => U.Name.Contains("data_")))
{
frmdata[property.Name] = property.Value;
}
flowInstance.FrmData = JsonHelper.Instance.Serialize(frmdata);
2018-03-21 17:36:31 +08:00
//创建运行实例
2018-03-23 15:00:35 +08:00
var wfruntime = new FlowRuntime(flowInstance);
2018-03-21 17:36:31 +08:00
var user = AuthUtil.GetCurrentUser();
2018-03-23 15:00:35 +08:00
#region
2018-03-21 17:36:31 +08:00
flowInstance.ActivityId = wfruntime.runtimeModel.nextNodeId;
flowInstance.ActivityType = wfruntime.GetNextNodeType();//-1无法运行,0会签开始,1会签结束,2一般节点,4流程运行结束
2018-03-21 17:36:31 +08:00
flowInstance.ActivityName = wfruntime.runtimeModel.nextNode.name;
flowInstance.PreviousId = wfruntime.runtimeModel.currentNodeId;
flowInstance.CreateUserId = user.User.Id;
flowInstance.CreateUserName = user.User.Account;
flowInstance.MakerList = (wfruntime.GetNextNodeType() != 4 ? GetMakerList(wfruntime) : "");//当前节点可执行的人信息
2018-03-21 17:36:31 +08:00
flowInstance.IsFinish = (wfruntime.GetNextNodeType() == 4 ? 1 : 0);
UnitWork.Add(flowInstance);
2018-03-21 17:36:31 +08:00
#endregion
#region
2018-03-21 17:36:31 +08:00
FlowInstanceOperationHistory processOperationHistoryEntity = new FlowInstanceOperationHistory
{
InstanceId = flowInstance.Id,
CreateUserId = user.User.Id,
CreateUserName = user.User.Name,
CreateDate = DateTime.Now,
Content = "【创建】"
2018-03-21 17:36:31 +08:00
+ user.User.Name
+ "创建了一个流程进程【"
2018-03-21 17:36:31 +08:00
+ flowInstance.Code + "/"
+ flowInstance.CustomName + "】"
2018-03-21 17:36:31 +08:00
};
UnitWork.Add(processOperationHistoryEntity);
2018-03-21 17:36:31 +08:00
#endregion
#region
2018-03-21 17:36:31 +08:00
FlowInstanceTransitionHistory processTransitionHistoryEntity = new FlowInstanceTransitionHistory
{
2018-03-21 17:36:31 +08:00
InstanceId = flowInstance.Id,
FromNodeId = wfruntime.runtimeModel.currentNodeId,
2018-03-23 17:47:44 +08:00
FromNodeName = wfruntime.runtimeModel.currentNode.name,
2018-03-21 17:36:31 +08:00
FromNodeType = wfruntime.runtimeModel.currentNodeType,
ToNodeId = wfruntime.runtimeModel.nextNodeId,
ToNodeName = wfruntime.runtimeModel.nextNode.name,
ToNodeType = wfruntime.runtimeModel.nextNodeType,
IsFinish = wfruntime.runtimeModel.nextNodeType == 4?1:0,
TransitionSate = 0,
CreateUserId = user.User.Id,
CreateUserName = user.User.Name
2018-03-21 17:36:31 +08:00
};
#endregion
2018-03-23 17:47:44 +08:00
2018-03-21 17:36:31 +08:00
UnitWork.Add(processTransitionHistoryEntity);
UnitWork.Save();
return true;
}
/// <summary>
/// 节点审核
/// </summary>
2018-03-26 00:34:49 +08:00
/// <param name="instanceId"></param>
/// <returns></returns>
2018-03-26 00:34:49 +08:00
public bool NodeVerification(string instanceId, bool flag, string description = "")
{
var user = AuthUtil.GetCurrentUser().User;
2018-03-26 00:34:49 +08:00
FlowInstance flowInstance = Get(instanceId);
FlowInstanceOperationHistory flowInstanceOperationHistory = new FlowInstanceOperationHistory
{
InstanceId = instanceId,
CreateUserId = user.Id,
CreateUserName = user.Name,
CreateDate = DateTime.Now
};//操作记录
2018-03-26 00:34:49 +08:00
FlowRuntime wfruntime = new FlowRuntime(flowInstance);
2018-03-27 17:46:49 +08:00
var tag = new Tag
{
UserName = user.Name,
UserId = user.Id,
Description = description
};
#region
2018-04-07 22:00:29 +08:00
if (flowInstance.ActivityType == 0)//当前节点是会签节点
2018-03-26 00:34:49 +08:00
{
2018-03-27 17:46:49 +08:00
tag.Taged = 1;
2018-04-07 22:00:29 +08:00
wfruntime.MakeTagNode(wfruntime.runtimeModel.currentNodeId, tag);//标记会签节点状态
2018-04-07 22:00:29 +08:00
string verificationNodeId = ""; //寻找当前登陆用户可审核的节点Id
List<string> nodelist = wfruntime.GetCountersigningNodeIdList(wfruntime.runtimeModel.currentNodeId);
foreach (string item in nodelist)
{
var makerList = GetMakerList(wfruntime.runtimeModel.nodes[item]
, wfruntime.runtimeModel.flowInstanceId);
2018-04-07 22:00:29 +08:00
if (makerList == "-1") continue;
if (makerList.Split(',').Any(one => user.Id == one))
{
2018-04-07 22:00:29 +08:00
verificationNodeId = item;
}
}
if (verificationNodeId != "")
{
if (flag)
{
2018-03-27 17:46:49 +08:00
tag.Taged = 1;
flowInstanceOperationHistory.Content = "【" + wfruntime.runtimeModel.nodes[verificationNodeId].name + "】【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "】同意,备注:" + description;
}
else
{
2018-03-27 17:46:49 +08:00
tag.Taged = -1;
flowInstanceOperationHistory.Content = "【" + wfruntime.runtimeModel.nodes[verificationNodeId].name + "】【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "】不同意,备注:" + description;
}
2018-03-26 00:34:49 +08:00
2018-04-07 22:00:29 +08:00
wfruntime.MakeTagNode(verificationNodeId, tag);//标记审核节点状态
string confluenceres = wfruntime.NodeConfluence(verificationNodeId, tag);
2018-03-27 17:46:49 +08:00
switch (confluenceres)
2018-03-26 00:34:49 +08:00
{
case "-1"://不通过
2018-03-26 00:34:49 +08:00
flowInstance.IsFinish = 3;
break;
2018-04-07 22:00:29 +08:00
case "1"://等待,当前节点还是会签开始节点,不跳转
2018-03-26 00:34:49 +08:00
break;
default://通过
2018-03-26 00:34:49 +08:00
flowInstance.PreviousId = flowInstance.ActivityId;
flowInstance.ActivityId = wfruntime.runtimeModel.nextNodeId;
flowInstance.ActivityType = wfruntime.runtimeModel.nextNodeType;//-1无法运行,0会签开始,1会签结束,2一般节点,4流程运行结束
2018-03-26 00:34:49 +08:00
flowInstance.ActivityName = wfruntime.runtimeModel.nextNode.name;
flowInstance.IsFinish = (wfruntime.runtimeModel.nextNodeType == 4 ? 1 : 0);
flowInstance.MakerList = (wfruntime.runtimeModel.nextNodeType == 4 ? "" : GetMakerList(wfruntime));//当前节点可执行的人信息
2018-03-27 17:46:49 +08:00
#region
UnitWork.Add(new FlowInstanceTransitionHistory
2018-03-27 17:46:49 +08:00
{
InstanceId = flowInstance.Id,
CreateUserId = user.Id,
CreateUserName = user.Name,
2018-03-27 17:46:49 +08:00
FromNodeId = wfruntime.runtimeModel.currentNodeId,
FromNodeName = wfruntime.runtimeModel.currentNode.name,
FromNodeType = wfruntime.runtimeModel.currentNodeType,
ToNodeId = wfruntime.runtimeModel.nextNodeId,
ToNodeName = wfruntime.runtimeModel.nextNode.name,
ToNodeType = wfruntime.runtimeModel.nextNodeType,
IsFinish = wfruntime.runtimeModel.nextNodeType ==4?1:0,
2018-03-27 17:46:49 +08:00
TransitionSate = 0
});
2018-03-26 00:34:49 +08:00
#endregion
2018-03-30 17:35:18 +08:00
2018-03-26 00:34:49 +08:00
break;
}
}
else
{
throw (new Exception("审核异常,找不到审核节点"));
}
}
2018-03-26 00:34:49 +08:00
#endregion
#region
else//一般审核
{
2018-03-26 00:34:49 +08:00
if (flag)
{
2018-03-27 17:46:49 +08:00
tag.Taged = 1;
wfruntime.MakeTagNode(wfruntime.runtimeModel.currentNodeId, tag);
2018-03-26 00:34:49 +08:00
flowInstance.PreviousId = flowInstance.ActivityId;
flowInstance.ActivityId = wfruntime.runtimeModel.nextNodeId;
flowInstance.ActivityType = wfruntime.runtimeModel.nextNodeType;
flowInstance.ActivityName = wfruntime.runtimeModel.nextNode.name;
flowInstance.MakerList = wfruntime.runtimeModel.nextNodeType == 4 ? "" : GetMakerList(wfruntime);//当前节点可执行的人信息
2018-03-26 00:34:49 +08:00
flowInstance.IsFinish = (wfruntime.runtimeModel.nextNodeType == 4 ? 1 : 0);
#region
2018-03-26 00:34:49 +08:00
UnitWork.Add(new FlowInstanceTransitionHistory
2018-03-26 00:34:49 +08:00
{
InstanceId = flowInstance.Id,
CreateUserId = user.Id,
CreateUserName = user.Name,
2018-03-26 00:34:49 +08:00
FromNodeId = wfruntime.runtimeModel.currentNodeId,
FromNodeName = wfruntime.runtimeModel.currentNode.name,
FromNodeType = wfruntime.runtimeModel.currentNodeType,
ToNodeId = wfruntime.runtimeModel.nextNodeId,
ToNodeName = wfruntime.runtimeModel.nextNode.name,
ToNodeType = wfruntime.runtimeModel.nextNodeType,
IsFinish = wfruntime.runtimeModel.nextNodeType == 4 ? 1 : 0,
2018-03-26 00:34:49 +08:00
TransitionSate = 0
});
2018-03-26 00:34:49 +08:00
#endregion
flowInstanceOperationHistory.Content = "【" + wfruntime.runtimeModel.currentNode.name
+ "】【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "】同意,备注:" + description;
2018-03-26 00:34:49 +08:00
}
else
{
flowInstance.IsFinish = 3; //表示该节点不同意
2018-03-27 17:46:49 +08:00
tag.Taged = -1;
2018-03-30 17:35:18 +08:00
wfruntime.MakeTagNode(wfruntime.runtimeModel.currentNodeId, tag);
2018-03-26 00:34:49 +08:00
flowInstanceOperationHistory.Content = "【"
+ wfruntime.runtimeModel.currentNode.name + "】【"
+ DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "】不同意,备注:"
+ description;
2018-03-26 00:34:49 +08:00
}
}
2018-03-27 17:46:49 +08:00
#endregion
flowInstance.SchemeContent = JsonHelper.Instance.Serialize(wfruntime.runtimeModel.schemeContentJson);
2018-03-26 00:34:49 +08:00
UnitWork.Update(flowInstance);
UnitWork.Add(flowInstanceOperationHistory);
UnitWork.Save();
return true;
}
/// <summary>
/// 驳回
/// </summary>
/// <returns></returns>
2018-03-30 17:35:18 +08:00
public bool NodeReject(VerificationReq reqest)
{
var user = AuthUtil.GetCurrentUser().User;
2018-03-30 17:35:18 +08:00
FlowInstance flowInstance = Get(reqest.FlowInstanceId);
2018-03-26 00:34:49 +08:00
FlowRuntime wfruntime = new FlowRuntime(flowInstance);
2018-03-26 00:34:49 +08:00
string resnode = "";
2018-03-30 17:35:18 +08:00
if (string.IsNullOrEmpty(reqest.NodeRejectStep))
2018-03-26 00:34:49 +08:00
{
resnode = wfruntime.RejectNode();
}
2018-03-26 00:34:49 +08:00
else
{
2018-03-30 17:35:18 +08:00
resnode = reqest.NodeRejectStep;
}
2018-03-27 17:46:49 +08:00
var tag = new Tag
{
2018-03-30 17:35:18 +08:00
Description = reqest.VerificationOpinion,
2018-03-27 17:46:49 +08:00
Taged = 0,
UserId = user.Id,
UserName = user.Name
};
wfruntime.MakeTagNode(wfruntime.runtimeModel.currentNodeId, tag);
flowInstance.IsFinish = 4;//4表示驳回需要申请者重新提交表单
2018-03-26 00:34:49 +08:00
if (resnode != "")
{
flowInstance.PreviousId = flowInstance.ActivityId;
flowInstance.ActivityId = resnode;
2018-03-30 17:35:18 +08:00
flowInstance.ActivityType = wfruntime.GetNodeType(resnode);
2018-03-26 00:34:49 +08:00
flowInstance.ActivityName = wfruntime.runtimeModel.nodes[resnode].name;
flowInstance.MakerList = GetMakerList(wfruntime.runtimeModel.nodes[resnode], flowInstance.PreviousId);//当前节点可执行的人信息
#region
2018-03-30 17:35:18 +08:00
UnitWork.Add(new FlowInstanceTransitionHistory
2018-03-30 17:35:18 +08:00
{
InstanceId = flowInstance.Id,
CreateUserId = user.Id,
CreateUserName = user.Name,
2018-03-30 17:35:18 +08:00
FromNodeId = wfruntime.runtimeModel.currentNodeId,
FromNodeName = wfruntime.runtimeModel.currentNode.name,
FromNodeType = wfruntime.runtimeModel.currentNodeType,
ToNodeId = wfruntime.runtimeModel.nextNodeId,
ToNodeName = wfruntime.runtimeModel.nextNode.name,
ToNodeType = wfruntime.runtimeModel.nextNodeType,
IsFinish = wfruntime.runtimeModel.nextNodeType == 4 ? 1 : 0,
2018-03-30 17:35:18 +08:00
TransitionSate = 1
});
2018-03-26 00:34:49 +08:00
#endregion
}
2018-03-30 17:35:18 +08:00
UnitWork.Update(flowInstance);
UnitWork.Add(new FlowInstanceOperationHistory
{
InstanceId = reqest.FlowInstanceId
,CreateUserId = user.Id
,CreateUserName = user.Name
,CreateDate = DateTime.Now
,Content = "【"
+ wfruntime.runtimeModel.currentNode.name
+ "】【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "】驳回,备注:"
+ reqest.VerificationOpinion
});
2018-03-26 00:34:49 +08:00
UnitWork.Save();
return true;
}
#endregion
/// <summary>
/// 寻找该节点执行人
/// </summary>
/// <param name="wfruntime"></param>
/// <returns></returns>
2018-03-20 23:15:09 +08:00
private string GetMakerList(FlowRuntime wfruntime)
{
string makerList = "";
if (wfruntime.runtimeModel.nextNodeId == "-1")
{
throw (new Exception("无法寻找到下一个节点"));
}
if (wfruntime.runtimeModel.nextNodeType == 0)//如果是会签节点
{
List<string> _nodelist = wfruntime.GetCountersigningNodeIdList(wfruntime.runtimeModel.nextNodeId);
string _makerList = "";
foreach (string item in _nodelist)
{
_makerList = GetMakerList(wfruntime.runtimeModel.nodes[item], wfruntime.runtimeModel.flowInstanceId);
if (_makerList == "-1")
{
throw (new Exception("无法寻找到会签节点的审核者,请查看流程设计是否有问题!"));
}
if (_makerList == "1")
{
throw (new Exception("会签节点的审核者不能为所有人,请查看流程设计是否有问题!"));
}
if (makerList != "")
{
makerList += ",";
}
makerList += _makerList;
}
}
else
{
makerList = GetMakerList(wfruntime.runtimeModel.nextNode, wfruntime.runtimeModel.flowInstanceId);
if (makerList == "-1")
{
throw (new Exception("无法寻找到节点的审核者,请查看流程设计是否有问题!"));
}
}
return makerList;
}
/// <summary>
/// 寻找该节点执行人
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
2018-03-23 17:47:44 +08:00
private string GetMakerList(FlowNode node, string processId)
{
string makerList = "";
2018-03-30 17:35:18 +08:00
if (node.setInfo == null)
{
makerList = "-1";
2018-03-30 17:35:18 +08:00
}
else
{
if (node.setInfo.NodeDesignate == Setinfo.ALL_USER)//所有成员
{
makerList = "1";
}
else if (node.setInfo.NodeDesignate == Setinfo.SPECIAL_USER)//指定成员
{
makerList = GenericHelpers.ArrayToString(node.setInfo.NodeDesignateData.users, makerList);
if (makerList == "")
2018-03-23 17:47:44 +08:00
{
makerList = "-1";
}
}
2018-04-10 10:53:48 +08:00
else if (node.setInfo.NodeDesignate == Setinfo.SPECIAL_ROLE) //指定角色
{
2018-04-10 10:53:48 +08:00
var users = RevelanceManagerApp.Get(Define.USERROLE, false, node.setInfo.NodeDesignateData.roles);
makerList = GenericHelpers.ArrayToString(users, makerList);
if (makerList == "")
{
makerList = "-1";
}
}
}
return makerList;
}
2018-03-30 17:35:18 +08:00
/// <summary>
/// 审核流程
/// <para>李玉宝于2017-01-20 15:44:45</para>
/// </summary>
2018-03-26 00:34:49 +08:00
public void Verification(VerificationReq request)
{
//驳回
2018-03-26 00:34:49 +08:00
if (request.VerificationFinally == "3")
{
2018-03-30 17:35:18 +08:00
NodeReject(request);
}
else if (request.VerificationFinally == "2")//表示不同意
{
2018-03-26 00:34:49 +08:00
NodeVerification(request.FlowInstanceId, false, request.VerificationOpinion);
}
else if (request.VerificationFinally == "1")//表示同意
2018-03-26 00:34:49 +08:00
{
NodeVerification(request.FlowInstanceId, true, request.VerificationOpinion);
}
}
2018-03-23 17:47:44 +08:00
2018-03-12 23:44:05 +08:00
public void Update(FlowInstance flowScheme)
{
2018-05-07 14:47:16 +08:00
Repository.Update(flowScheme);
2018-03-12 23:44:05 +08:00
}
public TableData Load(QueryFlowInstanceListReq request)
{
//todo:待办/已办/我的
2018-03-12 23:44:05 +08:00
var result = new TableData();
var user = AuthUtil.GetCurrentUser();
2018-03-12 23:44:05 +08:00
if (request.type == "wait") //待办事项
2018-03-12 23:44:05 +08:00
{
result.count = UnitWork.Find<FlowInstance>(u => u.MakerList =="1" || u.MakerList.Contains(user.User.Id)).Count();
result.data = UnitWork.Find<FlowInstance>(request.page, request.limit, "CreateDate descending",
u => u.MakerList == "1" || u.MakerList.Contains(user.User.Id)).ToList();
2018-03-12 23:44:05 +08:00
}
else if (request.type == "disposed") //已办事项(即我参与过的流程)
2018-03-12 23:44:05 +08:00
{
var instances = UnitWork.Find<FlowInstanceTransitionHistory>(u => u.CreateUserId == user.User.Id)
.Select(u => u.InstanceId).Distinct();
var query = from ti in instances
join ct in UnitWork.Find<FlowInstance>(null) on ti equals ct.Id
into tmp
from ct in tmp.DefaultIfEmpty()
select ct;
result.data = query.OrderByDescending(u => u.CreateDate)
.Skip((request.page - 1) * request.limit)
.Take(request.limit).ToList();
result.count = instances.Count();
2018-03-12 23:44:05 +08:00
}
else //我的流程
2018-03-12 23:44:05 +08:00
{
result.count = UnitWork.Find<FlowInstance>(u => u.CreateUserId == user.User.Id).Count();
result.data = UnitWork.Find<FlowInstance>(request.page, request.limit,
"CreateDate descending", u => u.CreateUserId == user.User.Id).ToList();
2018-03-12 23:44:05 +08:00
}
return result;
}
}
}
2018-03-30 17:35:18 +08:00