FlowService.Actions1.Add.cs 8.2 KB

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