FlowService.Actions1.Add.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using Datory;
  4. using GxPress.Common.Tools;
  5. using GxPress.Entity.WorkFlow;
  6. using GxPress.Entity.WorkFlowDto;
  7. using GxPress.EnumConst;
  8. namespace GxPress.Service.Implement
  9. {
  10. public partial class FlowService
  11. {
  12. /// <summary>
  13. /// 添加工作流
  14. /// </summary>
  15. /// <param name="flow"></param>
  16. /// <returns></returns>
  17. public async Task<int> AddAsync(FlowDto flow)
  18. {
  19. flow.Id = await _flowRepository.InsertAsync(new Entity.WorkFlow.Flow
  20. {
  21. State = FlowState.Checking,
  22. ProcessId = flow.ProcessId,
  23. UserId = flow.UserId,
  24. IsRead = false
  25. });
  26. foreach (var flowField in flow.Fields)
  27. {
  28. await _flowFieldValueRepository.InsertAsync(new FlowFieldValue
  29. {
  30. ProcessId = flow.ProcessId,
  31. FlowId = flow.Id,
  32. FieldId = flowField.Id,
  33. UserId = flow.UserId,
  34. FieldType = flowField.Type,
  35. StringValue = flowField.Value,
  36. StringValues = flowField.Value,
  37. IntValue = StringUtils.ToInt(flowField.Value)
  38. });
  39. }
  40. //添加审核人
  41. if (flow.ApproverCheckUserIds != null)
  42. {
  43. for (int i = 0; i < flow.ApproverCheckUserIds.Count(); i++)
  44. {
  45. var todo = new FlowTodo
  46. {
  47. ProcessId = flow.ProcessId,
  48. FlowId = flow.Id,
  49. UserId = flow.ApproverCheckUserIds[i],
  50. Type = nameof(TodoTypeConst.ApproverCheck),
  51. IsDone = false,
  52. IsOperate = i == 0,
  53. };
  54. await _flowTodoRepository.InsertAsync(todo);
  55. }
  56. }
  57. if (flow.CarbonCopyUserIds != null)
  58. {
  59. foreach (var userId in flow.CarbonCopyUserIds)
  60. {
  61. var todo = new FlowTodo
  62. {
  63. ProcessId = flow.ProcessId,
  64. FlowId = flow.Id,
  65. UserId = userId,
  66. Type = nameof(TodoTypeConst.CarbonCopy),
  67. IsDone = false,
  68. IsOperate = false
  69. };
  70. await _flowTodoRepository.InsertAsync(todo);
  71. }
  72. }
  73. var nodes = await _processService.GetAllAsync(flow.ProcessId);
  74. foreach (var processNodeDto in nodes.Where(n => n.ParentId == 0))
  75. {
  76. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Start))
  77. {
  78. await NodeExecuteAsync(flow, processNodeDto);
  79. }
  80. //插入审核人
  81. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Approver) && processNodeDto.ParentId == 0)
  82. {
  83. await NodeExecuteAsync(flow, processNodeDto);
  84. //break;
  85. }
  86. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Switch))
  87. {
  88. //工作流条件
  89. await NodeExecuteSwitchAsync(flow,processNodeDto);
  90. }
  91. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.End))
  92. {
  93. await NodeExecuteAsync(flow, processNodeDto);
  94. break;
  95. }
  96. }
  97. //创建工作流
  98. foreach (var item in flow.flowAttachments)
  99. {
  100. item.FlowId = flow.Id;
  101. item.TypeId = 1;
  102. }
  103. await flowAttachmentRepository.InsertsAsync(flow.flowAttachments);
  104. return flow.Id;
  105. }
  106. public async Task<int> ReAddAsync(int flowId)
  107. {
  108. var flow = await GetAsync(flowId);
  109. if (flow == null) return 0;
  110. flow.Id = await _flowRepository.InsertAsync(new Entity.WorkFlow.Flow
  111. {
  112. ProcessId = flow.ProcessId,
  113. State = FlowState.Checking,
  114. UserId = flow.UserId
  115. });
  116. //获取上一个的文件
  117. var flowAttachments = (await flowAttachmentRepository.GetAllAsync(flowId)).ToList();
  118. foreach (var item in flowAttachments)
  119. {
  120. item.FlowId = flow.Id;
  121. item.TypeId = 1;
  122. }
  123. await flowAttachmentRepository.InsertsAsync(flowAttachments);
  124. if (flow.Fields != null)
  125. {
  126. foreach (var flowField in flow.Fields)
  127. {
  128. await _flowFieldValueRepository.InsertAsync(new FlowFieldValue
  129. {
  130. ProcessId = flow.ProcessId,
  131. FlowId = flow.Id,
  132. FieldId = flowField.Id,
  133. UserId = flow.UserId,
  134. FieldType = flowField.Type,
  135. StringValue = flowField.Value,
  136. StringValues = flowField.Value,
  137. IntValue = StringUtils.ToInt(flowField.Value)
  138. });
  139. }
  140. }
  141. for (int i = 0; i < flow.ApproverCheckUserIds.Count(); i++)
  142. {
  143. var todo = new FlowTodo
  144. {
  145. ProcessId = flow.ProcessId,
  146. FlowId = flow.Id,
  147. UserId = flow.ApproverCheckUserIds[i],
  148. Type = nameof(TodoTypeConst.ApproverCheck),
  149. IsDone = false,
  150. IsOperate = i == 0,
  151. };
  152. await _flowTodoRepository.InsertAsync(todo);
  153. }
  154. if (flow.ApproverCheckUserIds != null)
  155. {
  156. foreach (var userId in flow.ApproverCheckUserIds)
  157. {
  158. var todo = new FlowTodo
  159. {
  160. ProcessId = flow.ProcessId,
  161. FlowId = flow.Id,
  162. UserId = userId,
  163. Type = nameof(TodoTypeConst.CarbonCopy),
  164. IsDone = false
  165. };
  166. await _flowTodoRepository.InsertAsync(todo);
  167. }
  168. }
  169. var nodes = await GetNodesAsync(flow, await _processService.GetNodesAsync(flow.ProcessId, 0));
  170. if (nodes != null)
  171. {
  172. foreach (var processNodeDto in nodes)
  173. {
  174. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Start) ||
  175. processNodeDto.Type == nameof(ProcessNodeTypeConst.End))
  176. {
  177. await NodeExecuteAsync(flow, processNodeDto);
  178. }
  179. else if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Approver))
  180. {
  181. await NodeExecuteAsync(flow, processNodeDto);
  182. }
  183. }
  184. }
  185. await DeleteAsync(flow.Id);
  186. return flow.Id;
  187. }
  188. /// <summary>
  189. /// 上会待定
  190. /// </summary>
  191. /// <param name="flowId"></param>
  192. /// <param name="userId"></param>
  193. /// <returns></returns>
  194. public async Task<bool> ActionsWait(int flowId, int userId)
  195. {
  196. var flow = await GetAsync(flowId);
  197. if (flow == null) return false;
  198. var flowTode = await _flowTodoRepository.GetAsync(Q.Where(nameof(Entity.WorkFlow.FlowTodo.FlowId), flowId).Where(nameof(Entity.WorkFlow.FlowTodo.UserId), userId));
  199. if (flowTode == null)
  200. return false;
  201. flowTode.IsDone = true;
  202. flowTode.DoneType = 2;
  203. flowTode.IsChecked = false;
  204. return await _flowTodoRepository.UpdateAsync(flowTode);
  205. }
  206. }
  207. }