using JNPF.Common.Core.Manager;
using JNPF.Common.Extension;
using JNPF.Common.Models.WorkFlow;
using JNPF.Common.Security;
using JNPF.Systems.Interfaces.Permission;
using JNPF.TaskScheduler;
using JNPF.VisualDev.Interfaces;
using JNPF.WorkFlow.Entitys.Entity;
using JNPF.WorkFlow.Entitys.Enum;
using JNPF.WorkFlow.Entitys.Model;
using JNPF.WorkFlow.Entitys.Model.Conifg;
using JNPF.WorkFlow.Entitys.Model.Item;
using JNPF.WorkFlow.Entitys.Model.Properties;
using JNPF.WorkFlow.Interfaces.Repository;
namespace JNPF.WorkFlow.Manager;
public class FlowTaskOtherUtil
{
private readonly IFlowTaskRepository _flowTaskRepository;
private readonly IUsersService _usersService;
private readonly IRunService _runService;
private readonly IUserManager _userManager;
public FlowTaskOtherUtil(IFlowTaskRepository flowTaskRepository, IUsersService usersService, IRunService runService, IUserManager userManager)
{
_flowTaskRepository = flowTaskRepository;
_usersService = usersService;
_runService = runService;
_userManager = userManager;
}
///
/// 修改当前节点经办数据.
///
/// 任务参数.
/// 审批类型(0:拒绝,1:同意).
///
public async Task UpdateFlowTaskOperator(FlowTaskParamter flowTaskParamter, int handleStatus)
{
var updateOperatorList = new List();// 要修改的经办
// 加签
if (flowTaskParamter.freeApproverUserId.IsNotEmptyOrNull())
{
#region 添加加签经办
var freeApproverOperatorEntity = flowTaskParamter.flowTaskOperatorEntity.Copy();
freeApproverOperatorEntity.Id = SnowflakeIdHelper.NextId();
freeApproverOperatorEntity.ParentId = flowTaskParamter.flowTaskOperatorEntity.Id;
freeApproverOperatorEntity.HandleType = flowTaskParamter.flowTaskOperatorEntity.Id;
freeApproverOperatorEntity.HandleId = flowTaskParamter.freeApproverUserId;
freeApproverOperatorEntity.CreatorTime = DateTime.Now;
freeApproverOperatorEntity.Completion = 0;
freeApproverOperatorEntity.RollbackId = flowTaskParamter.freeApproverType == "1" ? flowTaskParamter.flowTaskOperatorEntity.Id : flowTaskParamter.flowTaskOperatorEntity.RollbackId;
await _flowTaskRepository.CreateTaskOperator(freeApproverOperatorEntity);
flowTaskParamter.flowTaskOperatorEntityList.Add(freeApproverOperatorEntity);
#endregion
flowTaskParamter.flowTaskOperatorEntity.State = "1";
flowTaskParamter.flowTaskOperatorEntity.HandleStatus = handleStatus;
flowTaskParamter.flowTaskOperatorEntity.Completion = 1;
flowTaskParamter.flowTaskOperatorEntity.HandleTime = DateTime.Now;
if (flowTaskParamter.freeApproverType == "1")
{
handleStatus = 10;
}
else
{
#region 加签记录
await CreateOperatorRecode(flowTaskParamter, 6);
#endregion
}
}
else
{
// 当前经办非加签经办或不存在前签
if (flowTaskParamter.flowTaskOperatorEntity.ParentId.IsNullOrEmpty() || flowTaskParamter.flowTaskOperatorEntity.RollbackId.IsNullOrEmpty() || handleStatus == 0)
{
if (flowTaskParamter.approversProperties.counterSign == 0 || IsAchievebilProportion(flowTaskParamter, handleStatus))
{
//未审批经办
updateOperatorList = GetNotCompletion(flowTaskParamter.thisFlowTaskOperatorEntityList);
}
}
else
{
flowTaskParamter.flowTaskOperatorEntity.State = "1";
// 前签发起人是否为初始经办人
var rollBackOprtator = await _flowTaskRepository.GetTaskOperatorInfo(flowTaskParamter.flowTaskOperatorEntity.RollbackId);
if (rollBackOprtator.IsNotEmptyOrNull())
{
rollBackOprtator.Id = SnowflakeIdHelper.NextId();
rollBackOprtator.State = "0";
rollBackOprtator.Completion = 0;
rollBackOprtator.HandleStatus = 0;
rollBackOprtator.HandleType = flowTaskParamter.flowTaskOperatorEntity.Id;
rollBackOprtator.HandleTime = null;
await _flowTaskRepository.CreateTaskOperator(rollBackOprtator);
}
}
flowTaskParamter.flowTaskOperatorEntity.HandleStatus = handleStatus;
flowTaskParamter.flowTaskOperatorEntity.Completion = 1;
flowTaskParamter.flowTaskOperatorEntity.HandleTime = DateTime.Now;
}
updateOperatorList.Add(flowTaskParamter.flowTaskOperatorEntity);
await _flowTaskRepository.UpdateTaskOperator(updateOperatorList);
// 经办记录
await CreateOperatorRecode(flowTaskParamter, handleStatus);
}
///
/// 获取未审经办并修改完成状态.
///
/// 当前节点所有经办.
///
public List GetNotCompletion(List thisFlowTaskOperatorEntityList)
{
var notCompletion = thisFlowTaskOperatorEntityList.FindAll(x => x.Completion == 0);
notCompletion.ForEach(item =>
{
item.Completion = 1;
});
return notCompletion;
}
///
/// 对审批人节点分组.
///
/// 所有经办.
///
public Dictionary> GroupByOperator(List flowTaskOperatorEntities)
{
var dic = new Dictionary>();
foreach (var item in flowTaskOperatorEntities.GroupBy(x => x.TaskNodeId))
{
dic.Add(item.Key, flowTaskOperatorEntities.FindAll(x => x.TaskNodeId == item.Key));
}
return dic;
}
///
/// 保存当前未完成节点下个候选人节点的候选人.
///
/// 任务参数.
public List SaveNodeCandidates(FlowTaskParamter flowTaskParamter)
{
var flowCandidateList = new List();
if (flowTaskParamter.candidateList.IsNotEmptyOrNull())
{
foreach (var item in flowTaskParamter.candidateList.Keys)
{
var node = flowTaskParamter.flowTaskNodeEntityList.Find(x => x.NodeCode == item);
if (node != null)
{
flowCandidateList.Add(new FlowCandidatesEntity()
{
Id = SnowflakeIdHelper.NextId(),
TaskId = node.TaskId,
TaskNodeId = node.Id,
HandleId = _userManager.UserId,
Account = _userManager.Account,
Candidates = string.Join(",", flowTaskParamter.candidateList[item]),
TaskOperatorId = flowTaskParamter.flowTaskOperatorEntity.Id
});
}
}
_flowTaskRepository.CreateFlowCandidates(flowCandidateList);
}
if (flowTaskParamter.errorRuleUserList.IsNotEmptyOrNull())
{
flowCandidateList.Clear();
foreach (var item in flowTaskParamter.errorRuleUserList.Keys)
{
var node = flowTaskParamter.flowTaskNodeEntityList.Find(x => x.NodeCode == item);
if (node != null)
{
flowCandidateList.Add(new FlowCandidatesEntity()
{
Id = SnowflakeIdHelper.NextId(),
TaskId = node.TaskId,
TaskNodeId = node.Id,
HandleId = _userManager.UserId,
Account = _userManager.Account,
Candidates = string.Join(",", flowTaskParamter.errorRuleUserList[item]),
TaskOperatorId = flowTaskParamter.flowTaskOperatorEntity.Id
});
}
}
_flowTaskRepository.CreateFlowCandidates(flowCandidateList);
}
return flowCandidateList;
}
///
/// 获取子流程继承父流程的表单数据.
///
/// 任务参数.
/// 子流程属性.
///
public async Task