using System.Linq.Expressions;
using JNPF.Common.Core.Manager;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Models.WorkFlow;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.LinqBuilder;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.VisualDev.Entitys;
using JNPF.WorkFlow.Entitys;
using JNPF.WorkFlow.Entitys.Dto.FlowBefore;
using JNPF.WorkFlow.Entitys.Dto.FlowLaunch;
using JNPF.WorkFlow.Entitys.Dto.FlowMonitor;
using JNPF.WorkFlow.Entitys.Dto.FlowTemplate;
using JNPF.WorkFlow.Entitys.Dto.WorkFlowForm.LeaveApply;
using JNPF.WorkFlow.Entitys.Dto.WorkFlowForm.SalesOrder;
using JNPF.WorkFlow.Entitys.Entity;
using JNPF.WorkFlow.Entitys.Enum;
using JNPF.WorkFlow.Entitys.Model;
using JNPF.WorkFlow.Entitys.Model.Properties;
using JNPF.WorkFlow.Interfaces.Repository;
using Mapster;
using SqlSugar;
namespace JNPF.WorkFlow.Repository;
///
/// 流程任务数据处理.
///
public class FlowTaskRepository : IFlowTaskRepository, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly IUserManager _userManager;
private readonly ITenant _db;
///
/// 构造.
///
///
///
///
public FlowTaskRepository(
ISqlSugarRepository repository,
IUserManager userManager,
ISqlSugarClient context)
{
_repository = repository;
_userManager = userManager;
_db = context.AsTenant();
}
#region 流程列表
///
/// 列表(流程监控).
///
/// 请求参数.
///
public async Task GetMonitorList(FlowMonitorListQuery input)
{
var objList = GetCurrentUserObjId();
var templateIds = await _repository.AsSugarClient().Queryable().Where(x => objList.Contains(x.OperatorId) && x.Type == "2").Select(x => x.FlowId).Distinct().ToListAsync();
var whereLambda = LinqExpression.And();
if (!input.startTime.IsNullOrEmpty() && !input.endTime.IsNullOrEmpty())
{
var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime()));
var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime()));
whereLambda = whereLambda.And(a => SqlFunc.Between(a.creatorTime, startTime, endTime));
}
if (!input.creatorUserId.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.creatorUserId == input.creatorUserId);
if (!input.flowCategory.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowCategory == input.flowCategory);
if (!input.creatorUserId.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.creatorUserId.Contains(input.creatorUserId));
if (!input.flowId.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.templateId == input.flowId);
if (!input.status.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.status == input.status);
if (!input.flowUrgent.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowUrgent == input.flowUrgent);
if (!input.keyword.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.enCode.Contains(input.keyword) || m.fullName.Contains(input.keyword));
var list = await _repository.AsSugarClient().Queryable(
(a, b) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id))
.WhereIF(_userManager.User.IsAdministrator == 0, (a, b) => templateIds.Contains(a.TemplateId) || b.CreatorUserId == _userManager.UserId)
.Where((a, b) => a.Status > 0 && a.DeleteMark == null)
.Select((a) => new FlowMonitorListOutput()
{
completion = a.Completion,
creatorTime = a.CreatorTime,
creatorUserId = a.CreatorUserId,
description = a.Description,
enCode = a.EnCode,
flowCategory = a.FlowCategory,
flowCode = a.FlowCode,
flowId = a.FlowId,
flowName = a.FlowName,
flowUrgent = a.FlowUrgent,
fullName = a.FullName,
id = a.Id,
processId = a.ProcessId,
startTime = a.StartTime,
thisStep = a.ThisStep,
userName = SqlFunc.Subqueryable().Where(u => u.Id == a.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
status = a.Status,
sortCode = a.SortCode,
templateId = a.TemplateId,
}).MergeTable().Where(whereLambda).OrderBy(a => a.sortCode)
.OrderBy(a => a.creatorTime, OrderByType.Desc)
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(list);
}
///
/// 列表(我发起的).
///
/// 请求参数.
///
public async Task GetLaunchList(FlowLaunchListQuery input)
{
var whereLambda = LinqExpression.And();
if ("true".Equals(input.delegateType))
{
whereLambda = whereLambda.And(x => x.delegateUser == _userManager.UserId);
}
else
{
whereLambda = whereLambda.And(x => x.creatorUserId == _userManager.UserId);
}
if (!input.startTime.IsNullOrEmpty() && !input.endTime.IsNullOrEmpty())
{
var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime()));
var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime()));
whereLambda = whereLambda.And(a => SqlFunc.Between(a.creatorTime, startTime, endTime));
}
if (!input.flowCategory.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowCategory == input.flowCategory);
if (!input.flowId.IsNullOrEmpty())//flowId为流程主表id
whereLambda = whereLambda.And(x => x.templateId == input.flowId);
if (!input.status.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.status == input.status);
if (!input.flowUrgent.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowUrgent == input.flowUrgent);
if (!input.keyword.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.enCode.Contains(input.keyword) || m.fullName.Contains(input.keyword));
var list = await _repository.AsSugarClient().Queryable().Where((a) => a.DeleteMark == null).Select(a => new FlowLaunchListOutput()
{
id = a.Id,
fullName = a.FullName,
flowName = a.FlowName,
startTime = a.StartTime,
creatorTime = a.CreatorTime,
thisStep = a.ThisStep,
flowUrgent = a.FlowUrgent,
enCode = a.EnCode,
status = a.Status,
flowCategory = a.FlowCategory,
flowCode = a.FlowCode,
completion = a.Completion,
creatorUserId = a.CreatorUserId,
endTime = a.EndTime,
flowId = a.FlowId,
templateId = a.TemplateId,
sortCode = a.SortCode,
delegateUser = a.DelegateUser
}).MergeTable().Where(whereLambda).OrderBy(a => a.status).OrderBy(a => a.startTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(list);
}
///
/// 列表(待我审批).
///
/// 请求参数.
///
public async Task GetWaitList(FlowBeforeListQuery input)
{
var whereLambda = LinqExpression.And();
if (input.startTime.IsNotEmptyOrNull() && input.endTime.IsNotEmptyOrNull())
{
var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime()));
var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime()));
whereLambda = whereLambda.And(a => SqlFunc.Between(a.creatorTime, startTime, endTime));
}
if (input.flowCategory.IsNotEmptyOrNull())
whereLambda = whereLambda.And(x => x.flowCategory == input.flowCategory);
if (input.flowId.IsNotEmptyOrNull())
whereLambda = whereLambda.And(x => x.templateId == input.flowId);
if (input.keyword.IsNotEmptyOrNull())
whereLambda = whereLambda.And(m => m.enCode.Contains(input.keyword) || m.fullName.Contains(input.keyword));
if (input.creatorUserId.IsNotEmptyOrNull())
whereLambda = whereLambda.And(m => m.creatorUserId.Contains(input.creatorUserId));
if (!input.flowUrgent.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowUrgent == input.flowUrgent);
// 经办审核
var list1 = _repository.AsSugarClient().Queryable(
(a, b) => new JoinQueryInfos(JoinType.Left, a.Id == b.TaskId))
.Where((a, b) => a.Status == 1 && a.DeleteMark == null && b.Completion == 0 && b.State == "0"
&& b.HandleId == _userManager.UserId && b.CreatorTime < SqlFunc.GetDate())
.Select((a, b) => new FlowBeforeListOutput()
{
enCode = a.EnCode,
creatorUserId = a.CreatorUserId,
creatorTime = b.CreatorTime,
thisStep = a.ThisStep,
thisStepId = b.TaskNodeId,
flowCategory = a.FlowCategory,
fullName = a.FullName,
flowName = a.FlowName,
status = a.Status,
id = b.Id,
userName = SqlFunc.Subqueryable().Where(u => u.Id == a.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
description = SqlFunc.ToString(b.Description),
flowCode = a.FlowCode,
flowId = a.FlowId,
processId = a.ProcessId,
flowUrgent = a.FlowUrgent,
startTime = a.CreatorTime,
completion = a.Completion,
nodeName = b.NodeName,
templateId = a.TemplateId,
});
// 委托审核
var list2 = _repository.AsSugarClient().Queryable(
(a, b, c) => new JoinQueryInfos(JoinType.Left, a.Id == b.TaskId, JoinType.Left, (a.TemplateId == SqlFunc.ToString(c.FlowId) || SqlFunc.ToString(c.FlowName) == "全部流程")
&& c.EndTime > DateTime.Now))
.Where((a, b, c) => a.Status == 1 && a.DeleteMark == null && b.Completion == 0 && b.State == "0"
&& b.HandleId == c.UserId && c.Type == "1" && c.ToUserId == _userManager.UserId && c.DeleteMark == null
&& c.EnabledMark == 1 && c.EndTime > DateTime.Now && c.StartTime < DateTime.Now
&& b.CreatorTime < SqlFunc.GetDate())
.Select((a, b, c) => new FlowBeforeListOutput()
{
enCode = a.EnCode,
creatorUserId = a.CreatorUserId,
creatorTime = b.CreatorTime,
thisStep = a.ThisStep,
thisStepId = b.TaskNodeId,
flowCategory = a.FlowCategory,
fullName = SqlFunc.MergeString(a.FullName, "(", SqlFunc.Subqueryable().Where(u => u.Id == c.UserId).Select(u => u.RealName), "的委托)"),
flowName = a.FlowName,
status = a.Status,
id = b.Id,
userName = SqlFunc.Subqueryable().Where(u => u.Id == a.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
description = SqlFunc.ToString(b.Description),
flowCode = a.FlowCode,
flowId = a.FlowId,
processId = a.ProcessId,
flowUrgent = a.FlowUrgent,
startTime = a.CreatorTime,
completion = a.Completion,
nodeName = b.NodeName,
templateId = a.TemplateId,
});
var output = await _repository.AsSugarClient().UnionAll(list1, list2).Where(whereLambda).MergeTable().OrderBy(x => x.creatorTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(output);
}
///
/// 列表(批量审批).
///
/// 请求参数.
///
public async Task GetBatchWaitList(FlowBeforeListQuery input)
{
var whereLambda = LinqExpression.And();
if (input.startTime.IsNotEmptyOrNull() && input.endTime.IsNotEmptyOrNull())
{
var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime()));
var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime()));
whereLambda = whereLambda.And(a => SqlFunc.Between(a.creatorTime, startTime, endTime));
}
if (!input.flowCategory.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowCategory == input.flowCategory);
if (!input.flowId.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.templateId == input.flowId);
if (!input.keyword.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.enCode.Contains(input.keyword) || m.fullName.Contains(input.keyword));
if (!input.creatorUserId.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.creatorUserId.Contains(input.creatorUserId));
if (!input.flowUrgent.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowUrgent == input.flowUrgent);
if (!input.nodeCode.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.nodeCode.Contains(input.nodeCode));
//经办审核
var list1 = _repository.AsSugarClient().Queryable((a, b, c) =>
new JoinQueryInfos(JoinType.Left, a.Id == b.TaskId, JoinType.Left, b.TaskNodeId == c.Id))
.Where((a, b, c) => a.Status == 1 && a.DeleteMark == null && b.Completion == 0 && b.State == "0"
&& b.CreatorTime < SqlFunc.GetDate()
&& b.HandleId == _userManager.UserId && a.IsBatch == 1)
.Select((a, b, c) => new FlowBeforeListOutput()
{
enCode = a.EnCode,
creatorUserId = a.CreatorUserId,
creatorTime = b.CreatorTime,
thisStep = a.ThisStep,
thisStepId = b.TaskNodeId,
flowCategory = a.FlowCategory,
fullName = a.FullName,
flowName = a.FlowName,
status = a.Status,
id = b.Id,
userName = SqlFunc.Subqueryable().Where(u => u.Id == a.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
description = SqlFunc.ToString(a.Description),
flowCode = a.FlowCode,
flowId = a.FlowId,
processId = a.ProcessId,
flowUrgent = a.FlowUrgent,
startTime = a.CreatorTime,
completion = a.Completion,
nodeName = b.NodeName,
approversProperties = c.NodePropertyJson,
flowVersion = a.FlowVersion,
nodeCode = b.NodeCode,
templateId = a.TemplateId,
});
//委托审核
var list2 = _repository.AsSugarClient().Queryable((a, b, c, d) =>
new JoinQueryInfos(JoinType.Left, a.Id == b.TaskId, JoinType.Left, (a.TemplateId == SqlFunc.ToString(c.FlowId) || SqlFunc.ToString(c.FlowName) == "全部流程")
&& c.EndTime > DateTime.Now, JoinType.Left, b.TaskNodeId == d.Id))
.Where((a, b, c) => a.Status == 1 && a.DeleteMark == null && b.Completion == 0 && b.State == "0" && a.IsBatch == 1
&& b.HandleId == c.UserId && c.Type == "1" && b.CreatorTime < SqlFunc.GetDate()
&& c.ToUserId == _userManager.UserId && c.DeleteMark == null && c.EnabledMark == 1 && c.EndTime > DateTime.Now && c.StartTime < DateTime.Now)
.Select((a, b, c, d) => new FlowBeforeListOutput()
{
enCode = a.EnCode,
creatorUserId = a.CreatorUserId,
creatorTime = b.CreatorTime,
thisStep = a.ThisStep,
thisStepId = b.TaskNodeId,
flowCategory = a.FlowCategory,
fullName = SqlFunc.MergeString(a.FullName, "(", SqlFunc.Subqueryable().Where(u => u.Id == c.UserId).Select(u => u.RealName), "的委托)"),
flowName = a.FlowName,
status = a.Status,
id = b.Id,
userName = SqlFunc.Subqueryable().Where(u => u.Id == a.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
description = SqlFunc.ToString(a.Description),
flowCode = a.FlowCode,
flowId = a.FlowId,
processId = a.ProcessId,
flowUrgent = a.FlowUrgent,
startTime = a.CreatorTime,
completion = a.Completion,
nodeName = b.NodeName,
approversProperties = d.NodePropertyJson,
flowVersion = a.FlowVersion,
nodeCode = b.NodeCode,
templateId = a.TemplateId,
});
var output = await _repository.AsSugarClient().UnionAll(list1, list2).Where(whereLambda).MergeTable().OrderBy(x => x.creatorTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(output);
}
///
/// 列表(我已审批).
///
/// 请求参数.
///
public async Task GetTrialList(FlowBeforeListQuery input)
{
var statusList = new List() { 0, 1, 10 };// 同意、拒绝、前签
var whereLambda = LinqExpression.And();
if (!input.startTime.IsNullOrEmpty() && !input.endTime.IsNullOrEmpty())
{
var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime()));
var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime()));
whereLambda = whereLambda.And(a => SqlFunc.Between(a.creatorTime, startTime, endTime));
}
if (!input.flowCategory.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowCategory == input.flowCategory);
if (input.flowId.IsNotEmptyOrNull())
whereLambda = whereLambda.And(x => x.templateId == input.flowId);
if (!input.creatorUserId.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.creatorUserId.Contains(input.creatorUserId));
if (!input.keyword.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.enCode.Contains(input.keyword) || m.fullName.Contains(input.keyword));
if (!input.flowUrgent.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowUrgent == input.flowUrgent);
var list = await _repository.AsSugarClient().Queryable()
.GroupBy(it => new { it.TaskId, it.TaskNodeId, it.HandleId }).Where(a => statusList.Contains(a.HandleStatus) && a.HandleId == _userManager.UserId && a.TaskOperatorId != null)
.Select(a => new { TaskId = a.TaskId, TaskNodeId = a.TaskNodeId, HandleId = a.HandleId, HandleTime = SqlFunc.AggregateMax(a.HandleTime) })
.MergeTable().LeftJoin((a, b) => a.TaskId == b.TaskId && a.TaskNodeId == b.TaskNodeId && a.HandleId == b.HandleId)
.LeftJoin((a, b, c) => b.TaskId == c.Id).LeftJoin((a, b, c, d) => b.TaskOperatorId == d.Id)
.Where((a, b, c) => a.HandleTime == b.HandleTime && statusList.Contains(b.HandleStatus) && b.TaskOperatorId != null && b.HandleId == _userManager.UserId)
.Select((a, b, c, d) => new FlowBeforeListOutput()
{
enCode = c.EnCode,
creatorUserId = c.CreatorUserId,
creatorTime = b.HandleTime,
thisStep = c.ThisStep,
thisStepId = b.TaskNodeId,
flowCategory = c.FlowCategory,
fullName = b.HandleId == d.HandleId || c.Id == null ? c.FullName : SqlFunc.Subqueryable().Where(u => u.Id == d.HandleId).Select(u => SqlFunc.MergeString(c.FullName, "(", u.RealName, "的委托)")),
flowName = c.FlowName,
status = b.HandleStatus,
id = b.Id,
userName = SqlFunc.Subqueryable().Where(u => u.Id == c.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
description = c.Description,
flowCode = c.FlowCode,
flowId = c.FlowId,
processId = c.ProcessId,
flowUrgent = c.FlowUrgent,
startTime = c.CreatorTime,
}).MergeTable().Where(whereLambda).OrderBy(a => a.creatorTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(list);
}
///
/// 列表(抄送我的).
///
/// 请求参数
///
public async Task GetCirculateList(FlowBeforeListQuery input)
{
var whereLambda = LinqExpression.And();
if (!input.startTime.IsNullOrEmpty() && !input.endTime.IsNullOrEmpty())
{
var startTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 00:00:00}", input.startTime?.TimeStampToDateTime()));
var endTime = Convert.ToDateTime(string.Format("{0:yyyy-MM-dd 23:59:59}", input.endTime?.TimeStampToDateTime()));
whereLambda = whereLambda.And(a => SqlFunc.Between(a.creatorTime, startTime, endTime));
}
if (!input.flowCategory.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowCategory == input.flowCategory);
if (!input.flowId.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.templateId == input.flowId);
if (!input.creatorUserId.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.creatorUserId.Contains(input.creatorUserId));
if (!input.keyword.IsNullOrEmpty())
whereLambda = whereLambda.And(m => m.enCode.Contains(input.keyword) || m.fullName.Contains(input.keyword));
if (!input.flowUrgent.IsNullOrEmpty())
whereLambda = whereLambda.And(x => x.flowUrgent == input.flowUrgent);
var list = await _repository.AsSugarClient().Queryable((a, b, c, d) => new JoinQueryInfos(JoinType.Left, a.Id == b.TaskId, JoinType.Left, a.CreatorUserId == c.Id, JoinType.Left, a.FlowId == d.Id)).Where((a, b) => b.ObjectId == _userManager.UserId).Select((a, b, c, d) => new FlowBeforeListOutput()
{
enCode = a.EnCode,
creatorUserId = a.CreatorUserId,
creatorTime = b.CreatorTime,
thisStep = a.ThisStep,
thisStepId = b.TaskNodeId,
flowCategory = a.FlowCategory,
fullName = a.FullName,
flowName = a.FlowName,
status = a.Status,
id = b.Id,
userName = SqlFunc.MergeString(c.RealName, "/", c.Account),
description = a.Description,
flowCode = a.FlowCode,
flowId = a.FlowId,
processId = a.ProcessId,
formType = d.FormType,
flowUrgent = a.FlowUrgent,
startTime = a.CreatorTime,
}).MergeTable().Where(whereLambda).OrderBy(x => x.creatorTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(list);
}
///
/// 批量流程列表.
///
///
public async Task BatchFlowSelector()
{
var list = (await GetWaitList()).FindAll(x => x.IsBatch == 1);
var output = new List