FlowService.Get.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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;
  7. using GxPress.Entity.WorkFlowDto;
  8. using GxPress.Entity.WorkProcessDto;
  9. using GxPress.EnumConst;
  10. using GxPress.Result.App.Flow;
  11. namespace GxPress.Service.Implement
  12. {
  13. public partial class FlowService
  14. {
  15. public async Task<FlowDto> GetAsync(int flowId)
  16. {
  17. var flow = await _flowRepository.GetAsync(flowId);
  18. if (flow == null) return null;
  19. var fieldValues = await _flowFieldValueRepository.GetListAsync(flowId);
  20. var fields = new List<FlowField>();
  21. foreach (var flowFieldValue in fieldValues)
  22. {
  23. fields.Add(new FlowField
  24. {
  25. Id = flowFieldValue.FieldId,
  26. Type = flowFieldValue.FieldType,
  27. Value = GetFieldValue(flowFieldValue)
  28. });
  29. }
  30. var dto = new FlowDto
  31. {
  32. Id = flowId,
  33. ProcessId = flow.ProcessId,
  34. UserId = flow.UserId,
  35. State = flow.State,
  36. CreatedDate = flow.CreatedDate,
  37. Guid = flow.Guid,
  38. ProcessNodeId = flow.ProcessNodeId,
  39. Fields = fields
  40. };
  41. //获取审核和抄送人
  42. var flowToDos = await _flowTodoRepository.GetAllAsync(Q.Where(nameof(Entity.WorkFlow.FlowTodo.FlowId), flowId));
  43. if (flowToDos.Count() > 0)
  44. {
  45. dto.ApproverCheckUserIds = flowToDos.Where(n => n.Type == nameof(TodoTypeConst.ApproverCheck)).Select(n => n.UserId).ToList();
  46. dto.CarbonCopyUserIds = flowToDos.Where(n => n.Type == nameof(TodoTypeConst.CarbonCopy)).Select(n => n.UserId).ToList();
  47. }
  48. return dto;
  49. }
  50. public async Task<(string Title, List<string> Summaries)> GetFlowTitleAndSummaryAsync(User user, int processId, int flowId)
  51. {
  52. var process = await _processRepository.GetAsync(processId);
  53. if (process != null)
  54. {
  55. var fieldList = await _processFieldRepository.GetListAsync(processId);
  56. var valueList = await _flowFieldValueRepository.GetListAsync(flowId);
  57. var summaries = new List<string>();
  58. if (fieldList != null)
  59. {
  60. foreach (var processField in fieldList)
  61. {
  62. var value = valueList?.FirstOrDefault(v => v.FieldId == processField.Id);
  63. if (value != null)
  64. {
  65. summaries.Add($"{processField.Label}:{GetFieldValue(value)}");
  66. }
  67. }
  68. }
  69. return (GetFlowName(user, process), summaries);
  70. }
  71. return (GetFlowName(user, process), new List<string>());
  72. }
  73. public async Task<FlowResult> GetFlowResult(int flowId, int userId = 0)
  74. {
  75. var flowDto = await GetAsync(flowId);
  76. var user = await _userRepository.GetAsync(flowDto.UserId);
  77. var avatarUrl = await _userRepository.GetAvatarUrlAsync(flowDto.UserId);
  78. var process = await _processRepository.GetAsync(flowDto.ProcessId);
  79. if (process == null)
  80. throw new GxPress.Common.Exceptions.BusinessException("工作流不存在");
  81. var processFields = await _processFieldRepository.GetListAsync(flowDto.ProcessId);
  82. var valueList = await _flowFieldValueRepository.GetListAsync(flowId);
  83. var formFields = new List<FormField>();
  84. if (processFields != null)
  85. {
  86. foreach (var field in processFields)
  87. {
  88. var flowFieldValue = valueList.FirstOrDefault(x => x.FieldId == field.Id);
  89. formFields.Add(new FormField
  90. {
  91. Id = field.Id,
  92. Label = field.Label,
  93. Placeholder = field.Placeholder,
  94. Required = field.Required,
  95. Sort = field.Sort,
  96. Type = field.Type,
  97. Unit = field.Unit,
  98. Options = StringUtils.StringCollectionToStringList(field.Options),
  99. Value = GetFieldValue(flowFieldValue)
  100. });
  101. }
  102. }
  103. var nodes = new List<NodeResult>
  104. {
  105. new NodeResult
  106. {
  107. AvatarUrl = avatarUrl,
  108. Name = user.Name,
  109. Action = NodeStateConstUtils.GetText(NodeStateConst.Start),
  110. CreatedDate = flowDto.CreatedDate
  111. }
  112. };
  113. var todoList = await _flowTodoRepository.GetListByFlowIdAsync(flowId);
  114. var carbonCopies = new List<string>();
  115. foreach (var flowTodo in todoList)
  116. {
  117. if (flowTodo.Type == nameof(TodoTypeConst.ApproverCheck))
  118. {
  119. nodes.Add(new NodeResult
  120. {
  121. AvatarUrl = await _userRepository.GetAvatarUrlAsync(flowTodo.UserId),
  122. Name = await _userRepository.GetNameAsync(flowTodo.UserId),
  123. Action = GetTodoAction(flowTodo),
  124. CreatedDate = flowTodo.LastModifiedDate,
  125. Message = flowTodo.Message
  126. });
  127. }
  128. else if (flowTodo.Type == nameof(TodoTypeConst.CarbonCopy))
  129. {
  130. var nodeUserName = await _userRepository.GetNameAsync(flowTodo.UserId);
  131. carbonCopies.Add(nodeUserName);
  132. }
  133. }
  134. // var checkMessages = new List<MessageResult>();
  135. // var messages = await _flowMessageRepository.GetListByFlowIdAsync(flowId, nameof(TodoTypeConst.ApproverCheck));
  136. // foreach (var message in messages)
  137. // {
  138. // checkMessages.Add(new MessageResult
  139. // {
  140. // AvatarUrl = await _userRepository.GetAvatarUrlAsync(message.UserId),
  141. // Name = await _userRepository.GetNameAsync(message.UserId),
  142. // Message = message.Message,
  143. // CreatedDate = message.CreatedDate
  144. // });
  145. // }
  146. //获取系统配置抄送人和审核人
  147. var processNodeDtos = await GetNodesAsync(flowDto, await _processService.GetNodesAsync(flowDto.ProcessId, 0));
  148. //系统抄送人
  149. var carbonCopyUserIds = new List<int>();
  150. //系统审核人
  151. var approverCheckUserIds = new List<int>();
  152. foreach (var processNodeDto in processNodeDtos)
  153. {
  154. //抄送人
  155. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Start))
  156. {
  157. carbonCopyUserIds.AddRange(await GetListUserAsync(processNodeDto));
  158. }
  159. //插入审核人
  160. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Approver))
  161. {
  162. approverCheckUserIds.AddRange(await GetListUserAsync(processNodeDto));
  163. //break;
  164. }
  165. //抄送人
  166. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.End))
  167. {
  168. carbonCopyUserIds.AddRange(await GetListUserAsync(processNodeDto));
  169. break;
  170. }
  171. }
  172. var flowResult = new FlowResult
  173. {
  174. ProcessId = process.Id,
  175. AvatarUrl = avatarUrl,
  176. UserName = user.Name,
  177. Name = GetFlowName(user, process),
  178. FlowState = flowDto.State,
  179. No = flowDto.No,
  180. DepartmentName = await _departmentRepository.GetNameAsync(user.DepartmentId),
  181. CreatedDate = flowDto.CreatedDate,
  182. FormFields = formFields,
  183. Nodes = nodes,
  184. CarbonCopies = string.Join(",", carbonCopies),
  185. UserId = flowDto.UserId
  186. // CheckMessages = checkMessages
  187. };
  188. //流程名称
  189. flowResult.ProcessName = process.Name;
  190. //抄送人
  191. flowResult.CarbonCopyUsers = await _userRepository.GeFlowUsers(carbonCopyUserIds, flowId, nameof(TodoTypeConst.CarbonCopy));
  192. //审核人
  193. flowResult.ApproverCheckUsers = await _userRepository.GeFlowUsers(approverCheckUserIds, flowId, nameof(TodoTypeConst.ApproverCheck));
  194. if (userId > 0)
  195. {
  196. //获取副流程
  197. var flowToDo = await _flowTodoRepository.GetAsync(Q.Where(nameof(Entity.WorkFlow.FlowTodo.UserId), userId).Where(nameof(Entity.WorkFlow.FlowTodo.FlowId), flowId).Where(nameof(Entity.WorkFlow.FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck)).OrderByDesc(nameof(Entity.WorkFlow.FlowTodo.CreatedDate)));
  198. if (flowToDo != null)
  199. {
  200. if (flowToDo.IsDone && flowToDo.IsChecked)
  201. flowResult.State = FlowState.Checked;
  202. else if (flowToDo.IsDone && !flowToDo.IsChecked)
  203. {
  204. if (flowToDo.DoneType == 1)
  205. flowResult.State = FlowState.Meeting;
  206. else if (flowToDo.DoneType == 2)
  207. flowResult.State = FlowState.Wait;
  208. else
  209. flowResult.State = FlowState.Denied;
  210. }
  211. else if (flowToDo.IsDone && !flowToDo.IsChecked)
  212. flowResult.State = FlowState.Denied;
  213. else
  214. {
  215. flowResult.State = FlowState.Checking;
  216. }
  217. }
  218. else
  219. {
  220. flowResult.State = FlowState.Checked;
  221. }
  222. }
  223. //获取会议上传的文件流
  224. var flowAttachments = await flowAttachmentRepository.GetAllAsync(Q.Where(nameof(Entity.WorkFlow.FlowAttachment.TypeId), 1).Where(nameof(Entity.WorkFlow.FlowAttachment.FlowId), flowId).OrderByDesc(nameof(Entity.WorkFlow.FlowAttachment.CreatedDate)));
  225. flowResult.FlowAttachments = flowAttachments.ToList();
  226. return flowResult;
  227. }
  228. }
  229. }