FlowInstanceService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. namespace Admin.NET.Plugin.ApprovalFlow.Service;
  2. /// <summary>
  3. /// 流程实例服务
  4. /// </summary>
  5. [ApiDescriptionSettings(ApprovalFlowConst.GroupName, Order = 95)]
  6. public class FlowInstanceService : IDynamicApiController, ITransient
  7. {
  8. private readonly FlowEngineService _engine;
  9. private readonly SqlSugarRepository<ApprovalFlowInstance> _instanceRep;
  10. private readonly SqlSugarRepository<ApprovalFlowTask> _taskRep;
  11. private readonly SqlSugarRepository<ApprovalFlowLog> _logRep;
  12. public FlowInstanceService(
  13. FlowEngineService engine,
  14. SqlSugarRepository<ApprovalFlowInstance> instanceRep,
  15. SqlSugarRepository<ApprovalFlowTask> taskRep,
  16. SqlSugarRepository<ApprovalFlowLog> logRep)
  17. {
  18. _engine = engine;
  19. _instanceRep = instanceRep;
  20. _taskRep = taskRep;
  21. _logRep = logRep;
  22. }
  23. /// <summary>
  24. /// 发起流程
  25. /// </summary>
  26. [HttpPost]
  27. [ApiDescriptionSettings(Name = "Start")]
  28. [DisplayName("发起流程")]
  29. public async Task<long> Start(StartFlowInput input)
  30. {
  31. return await _engine.StartFlow(input);
  32. }
  33. /// <summary>
  34. /// 查询实例详情
  35. /// </summary>
  36. [HttpGet]
  37. [ApiDescriptionSettings(Name = "Detail")]
  38. [DisplayName("流程实例详情")]
  39. public async Task<FlowInstanceDetailOutput?> GetDetail([FromQuery] long id)
  40. {
  41. var instance = await _instanceRep.GetByIdAsync(id);
  42. if (instance == null) return null;
  43. var tasks = await _taskRep.AsQueryable()
  44. .Where(t => t.InstanceId == id)
  45. .OrderBy(t => t.CreateTime)
  46. .ToListAsync();
  47. return new FlowInstanceDetailOutput
  48. {
  49. Id = instance.Id,
  50. FlowId = instance.FlowId,
  51. BizType = instance.BizType,
  52. BizId = instance.BizId,
  53. BizNo = instance.BizNo,
  54. Title = instance.Title,
  55. InitiatorId = instance.InitiatorId,
  56. InitiatorName = instance.InitiatorName,
  57. Status = instance.Status,
  58. CurrentNodeId = instance.CurrentNodeId,
  59. StartTime = instance.StartTime,
  60. EndTime = instance.EndTime,
  61. Tasks = tasks.Select(t => new FlowTaskOutput
  62. {
  63. Id = t.Id,
  64. NodeId = t.NodeId,
  65. NodeName = t.NodeName,
  66. AssigneeId = t.AssigneeId,
  67. AssigneeName = t.AssigneeName,
  68. Status = t.Status,
  69. Comment = t.Comment,
  70. ActionTime = t.ActionTime,
  71. }).ToList(),
  72. };
  73. }
  74. /// <summary>
  75. /// 根据业务单据查询流程实例
  76. /// </summary>
  77. [HttpGet]
  78. [ApiDescriptionSettings(Name = "GetByBiz")]
  79. [DisplayName("按业务查流程")]
  80. public async Task<FlowInstanceDetailOutput?> GetByBiz([FromQuery] string bizType, [FromQuery] long bizId)
  81. {
  82. var instance = await _instanceRep.AsQueryable()
  83. .Where(i => i.BizType == bizType && i.BizId == bizId)
  84. .OrderByDescending(i => i.CreateTime)
  85. .FirstAsync();
  86. if (instance == null) return null;
  87. return await GetDetail(instance.Id);
  88. }
  89. /// <summary>
  90. /// 审批时间线
  91. /// </summary>
  92. [HttpGet]
  93. [ApiDescriptionSettings(Name = "Timeline")]
  94. [DisplayName("审批时间线")]
  95. public async Task<List<FlowTimelineItem>> GetTimeline([FromQuery] long instanceId)
  96. {
  97. return await _logRep.AsQueryable()
  98. .LeftJoin<ApprovalFlowTask>((l, t) => l.TaskId == t.Id)
  99. .Where((l, t) => l.InstanceId == instanceId)
  100. .OrderBy((l, t) => l.CreateTime)
  101. .Select((l, t) => new FlowTimelineItem
  102. {
  103. Id = l.Id,
  104. NodeId = l.NodeId,
  105. NodeName = t.NodeName,
  106. Action = l.Action,
  107. OperatorId = l.OperatorId,
  108. OperatorName = l.OperatorName,
  109. Comment = l.Comment,
  110. CreateTime = l.CreateTime,
  111. })
  112. .ToListAsync();
  113. }
  114. }