Workflow.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Hotline.FlowEngine.Definitions;
  2. using Hotline.Share.Enums.FlowEngine;
  3. using SqlSugar;
  4. using XF.Domain.Repository;
  5. namespace Hotline.FlowEngine.Workflows;
  6. public class Workflow : CreationEntity
  7. {
  8. public string DefinitionId { get; set; }
  9. [SugarColumn(IsNullable = true)]
  10. public string? ModuleId { get; set; }
  11. /// <summary>
  12. /// 业务模块名称
  13. /// </summary>
  14. [SugarColumn(IsNullable = true)]
  15. public string? ModuleName { get; set; }
  16. [SugarColumn(IsNullable = true)]
  17. public string? ModuleCode { get; set; }
  18. [SugarColumn(Length = 2000)]
  19. public string Title { get; set; }
  20. /// <summary>
  21. /// 到期时间
  22. /// </summary>
  23. public DateTime ExpiredTime { get; set; }
  24. public DateTime? CompleteTime { get; set; }
  25. public EWorkflowStatus Status { get; set; }
  26. /// <summary>
  27. /// 当前节点名称(会签状态此字段保存最外层会签办理节点名称)
  28. /// </summary>
  29. [SugarColumn(IsNullable = true)]
  30. public string? CurrentStepName { get; set; }
  31. /// <summary>
  32. /// 到达当前节点时间(stepBox创建时间)
  33. /// </summary>
  34. public DateTime? CurrentStepTime { get; set; }
  35. /// <summary>
  36. /// 当前会签办理节点code,嵌套会签为最外层会签办理节点code(不处于会签状态则无值)
  37. /// </summary>
  38. [SugarColumn(IsNullable = true)]
  39. public string? CurrentCountersignStepCode { get; set; }
  40. /// <summary>
  41. /// 中心直办件
  42. /// </summary>
  43. public bool IsStraight { get; set; } = true;
  44. /// <summary>
  45. /// 交办时间
  46. /// </summary>
  47. public DateTime AssignTime { get; set; }
  48. /// <summary>
  49. /// 办理时间限制(如:24小时、7个工作日)
  50. /// </summary>
  51. public string TimeLimit { get; set; }
  52. /// <summary>
  53. /// 办理意见(冗余,办理中...or 最终办理意见)
  54. /// </summary>
  55. [SugarColumn(Length = 2000)]
  56. public string Opinion { get; set; } = "办理中...";
  57. ///// <summary>
  58. ///// 外部业务唯一标识
  59. ///// </summary>
  60. //public string ExternalId { get; set; }
  61. [Navigate(NavigateType.OneToOne, nameof(DefinitionId))]
  62. public Definition Definition { get; set; }
  63. /// <summary>
  64. /// 补充信息
  65. /// </summary>
  66. [Navigate(NavigateType.OneToMany, nameof(WorkflowSupplement.WorkflowId))]
  67. public List<WorkflowSupplement> Supplements { get; set; }
  68. /// <summary>
  69. /// 接办信息
  70. /// </summary>
  71. [Navigate(NavigateType.OneToMany, nameof(WorkflowAssign.WorkflowId))]
  72. public List<WorkflowAssign> Assigns { get; set; }
  73. /// <summary>
  74. /// 主节点,依据流转进度动态生成或删除
  75. /// </summary>
  76. [SugarColumn(IsIgnore = true)]
  77. public List<WorkflowStep> StepBoxes { get; set; }
  78. [SugarColumn(IsIgnore = true)]
  79. public List<WorkflowTrace> Traces { get; set; } = new();
  80. #region Mehod
  81. /// <summary>
  82. /// 流程结束
  83. /// </summary>
  84. public void Complete()
  85. {
  86. Status = EWorkflowStatus.Completed;
  87. CompleteTime = DateTime.Now;
  88. }
  89. public void Terminate()
  90. {
  91. Status = EWorkflowStatus.Terminated;
  92. CompleteTime = DateTime.Now;
  93. }
  94. /// <summary>
  95. /// 流程进行流转
  96. /// </summary>
  97. public void FLow(string currentStepName, DateTime currentStepTime, string? currentCountersignCode = null)
  98. {
  99. CurrentStepName = currentStepName;
  100. CurrentStepTime = currentStepTime;
  101. if (!string.IsNullOrEmpty(currentCountersignCode))
  102. CurrentCountersignStepCode = currentCountersignCode;
  103. }
  104. /// <summary>
  105. /// 检查会签是否结束
  106. /// </summary>
  107. public void CompleteCountersign()
  108. {
  109. if (IsInCountersign())
  110. {
  111. var countersignStepBox = StepBoxes.First(d => d.Code == CurrentCountersignStepCode);
  112. var isCountersignOver = countersignStepBox.Steps.All(d =>
  113. d.Status is EWorkflowStepStatus.Completed &&
  114. (!d.HasStartCountersign || d.IsCountersignComplete.GetValueOrDefault()));
  115. if (isCountersignOver) //会签结束
  116. CurrentCountersignStepCode = null;
  117. }
  118. }
  119. public bool CheckIfCountersignOver()
  120. {
  121. var countersignStepBox = StepBoxes.First(d => d.Code == CurrentCountersignStepCode);
  122. var isCountersignOver = countersignStepBox.Steps.All(d =>
  123. d.Status is EWorkflowStepStatus.Completed &&
  124. (!d.HasStartCountersign || d.IsCountersignComplete.GetValueOrDefault()));
  125. return isCountersignOver;
  126. }
  127. public void EndCountersign() => CurrentCountersignStepCode = null;
  128. /// <summary>
  129. /// 更新workflow中当前停留节点,时间和会签开始节点code
  130. /// </summary>
  131. /// <param name="isStartCountersign"></param>
  132. /// <param name="stepBox"></param>
  133. public void SetWorkflowCurrentStepInfo(bool isStartCountersign, WorkflowStep stepBox)
  134. {
  135. //1.不在会签中,未发起会签(普通处理) 2.不在会签中,发起会签(保存会签节点),3.会签中,不更新
  136. if (IsInCountersign()) return;
  137. if (isStartCountersign)
  138. {
  139. FLow(stepBox.Name, stepBox.CreationTime, stepBox.Code);
  140. }
  141. else
  142. {
  143. FLow(stepBox.Name, stepBox.CreationTime);
  144. }
  145. }
  146. /// <summary>
  147. /// 流程是否处于会签中
  148. /// </summary>
  149. /// <returns></returns>
  150. public bool IsInCountersign() => !string.IsNullOrEmpty(CurrentCountersignStepCode);
  151. /// <summary>
  152. /// 结束流程会签状态
  153. /// </summary>
  154. public void CloseCountersignStatus() => CurrentCountersignStepCode = null;
  155. #endregion
  156. }