FlowService.Actions1.Add.cs 8.3 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. //Common.Sms.MasSms.SendSmsTemplate(new List<string>() { user.Phone }, "c9bdae1685514a9b821299720e2c5fe8");
  62. await smsService.SendSmsTemplate(new List<string>() { user.Phone }, "c9bdae1685514a9b821299720e2c5fe8", flow.Id, AllTypeConst.Flow.GetHashCode());
  63. }
  64. }
  65. }
  66. if (flow.CarbonCopyUserIds != null)
  67. {
  68. foreach (var userId in flow.CarbonCopyUserIds)
  69. {
  70. var todo = new FlowTodo
  71. {
  72. ProcessId = flow.ProcessId,
  73. FlowId = flow.Id,
  74. UserId = userId,
  75. Type = nameof(TodoTypeConst.CarbonCopy),
  76. IsDone = false,
  77. IsOperate = false
  78. };
  79. await _flowTodoRepository.InsertAsync(todo);
  80. }
  81. }
  82. var nodes = await _processService.GetAllAsync(flow.ProcessId);
  83. foreach (var processNodeDto in nodes.Where(n => n.ParentId == 0))
  84. {
  85. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Start))
  86. {
  87. await NodeExecuteAsync(flow, processNodeDto);
  88. }
  89. //插入审核人
  90. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Approver) && processNodeDto.ParentId == 0)
  91. {
  92. await NodeExecuteAsync(flow, processNodeDto);
  93. //break;
  94. }
  95. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Switch))
  96. {
  97. //工作流条件
  98. await NodeExecuteSwitchAsync(flow, processNodeDto);
  99. }
  100. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.End))
  101. {
  102. await NodeExecuteAsync(flow, processNodeDto);
  103. break;
  104. }
  105. }
  106. //创建工作流
  107. foreach (var item in flow.flowAttachments)
  108. {
  109. item.FlowId = flow.Id;
  110. item.TypeId = 1;
  111. }
  112. await flowAttachmentRepository.InsertsAsync(flow.flowAttachments);
  113. return flow.Id;
  114. }
  115. public async Task<int> ReAddAsync(int flowId)
  116. {
  117. var flow = await GetAsync(flowId);
  118. if (flow == null) return 0;
  119. flow.Id = await _flowRepository.InsertAsync(new Flow
  120. {
  121. ProcessId = flow.ProcessId,
  122. State = FlowState.Checking,
  123. UserId = flow.UserId
  124. });
  125. //获取上一个的文件
  126. var flowAttachments = (await flowAttachmentRepository.GetAllAsync(flowId)).ToList();
  127. foreach (var item in flowAttachments)
  128. {
  129. item.FlowId = flow.Id;
  130. item.TypeId = 1;
  131. }
  132. await flowAttachmentRepository.InsertsAsync(flowAttachments);
  133. if (flow.Fields != null)
  134. {
  135. foreach (var flowField in flow.Fields)
  136. {
  137. await _flowFieldValueRepository.InsertAsync(new FlowFieldValue
  138. {
  139. ProcessId = flow.ProcessId,
  140. FlowId = flow.Id,
  141. FieldId = flowField.Id,
  142. UserId = flow.UserId,
  143. FieldType = flowField.Type,
  144. StringValue = flowField.Value,
  145. StringValues = flowField.Value,
  146. IntValue = StringUtils.ToInt(flowField.Value)
  147. });
  148. }
  149. }
  150. for (int i = 0; i < flow.ApproverCheckUserIds.Count(); i++)
  151. {
  152. var todo = new FlowTodo
  153. {
  154. ProcessId = flow.ProcessId,
  155. FlowId = flow.Id,
  156. UserId = flow.ApproverCheckUserIds[i],
  157. Type = nameof(TodoTypeConst.ApproverCheck),
  158. IsDone = false,
  159. IsOperate = i == 0,
  160. };
  161. await _flowTodoRepository.InsertAsync(todo);
  162. }
  163. if (flow.ApproverCheckUserIds != null)
  164. {
  165. foreach (var userId in flow.ApproverCheckUserIds)
  166. {
  167. var todo = new FlowTodo
  168. {
  169. ProcessId = flow.ProcessId,
  170. FlowId = flow.Id,
  171. UserId = userId,
  172. Type = nameof(TodoTypeConst.CarbonCopy),
  173. IsDone = false
  174. };
  175. await _flowTodoRepository.InsertAsync(todo);
  176. }
  177. }
  178. var nodes = await GetNodesAsync(flow, await _processService.GetNodesAsync(flow.ProcessId, 0));
  179. if (nodes != null)
  180. {
  181. foreach (var processNodeDto in nodes)
  182. {
  183. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Start) ||
  184. processNodeDto.Type == nameof(ProcessNodeTypeConst.End))
  185. {
  186. await NodeExecuteAsync(flow, processNodeDto);
  187. }
  188. else if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Approver))
  189. {
  190. await NodeExecuteAsync(flow, processNodeDto);
  191. }
  192. }
  193. }
  194. await DeleteAsync(flow.Id);
  195. return flow.Id;
  196. }
  197. /// <summary>
  198. /// 上会待定
  199. /// </summary>
  200. /// <param name="flowId"></param>
  201. /// <param name="userId"></param>
  202. /// <returns></returns>
  203. public async Task<bool> ActionsWait(int flowId, int userId)
  204. {
  205. var flowTode = await _flowTodoRepository.GetAsync(flowId);
  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. }