FlowService.Actions1.Add.cs 9.6 KB

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