FlowService.Get.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 todoId, int userId = 0)
  74. {
  75. var flowId = 0;
  76. var query = Q.NewQuery();
  77. query.Where(nameof(Entity.WorkFlow.FlowTodo.Id), todoId);
  78. query.Where(nameof(Entity.WorkFlow.FlowTodo.UserId), userId);
  79. var flowTodeDto = await _flowTodoRepository.GetAsync(query);
  80. if (flowTodeDto != null)
  81. flowId = flowTodeDto.FlowId;
  82. else
  83. flowId = todoId;
  84. var flowDto = await GetAsync(flowId);
  85. var user = await _userRepository.GetAsync(flowDto.UserId);
  86. var avatarUrl = await _userRepository.GetAvatarUrlAsync(flowDto.UserId);
  87. var process = await _processRepository.GetAsync(flowDto.ProcessId);
  88. if (process == null)
  89. throw new GxPress.Common.Exceptions.BusinessException("工作流不存在");
  90. var processFields = await _processFieldRepository.GetListAsync(flowDto.ProcessId);
  91. var valueList = await _flowFieldValueRepository.GetListAsync(flowId);
  92. var formFields = new List<FormField>();
  93. if (processFields != null)
  94. {
  95. foreach (var field in processFields)
  96. {
  97. var flowFieldValue = valueList.FirstOrDefault(x => x.FieldId == field.Id);
  98. formFields.Add(new FormField
  99. {
  100. Id = field.Id,
  101. Label = field.Label,
  102. Placeholder = field.Placeholder,
  103. Required = field.Required,
  104. Sort = field.Sort,
  105. Type = field.Type,
  106. Unit = field.Unit,
  107. Options = StringUtils.StringCollectionToStringList(field.Options),
  108. Value = GetFieldValue(flowFieldValue)
  109. });
  110. }
  111. }
  112. var nodes = new List<NodeResult>
  113. {
  114. new NodeResult
  115. {
  116. AvatarUrl = avatarUrl,
  117. Name = user.Name,
  118. Action = NodeStateConstUtils.GetText(NodeStateConst.Start),
  119. CreatedDate = flowDto.CreatedDate
  120. }
  121. };
  122. var todoList = await _flowTodoRepository.GetListByFlowIdAsync(flowId);
  123. var carbonCopies = new List<string>();
  124. foreach (var flowTodo in todoList)
  125. {
  126. if (flowTodo.Type == nameof(TodoTypeConst.ApproverCheck))
  127. {
  128. nodes.Add(new NodeResult
  129. {
  130. AvatarUrl = await _userRepository.GetAvatarUrlAsync(flowTodo.UserId),
  131. Name = await _userRepository.GetNameAsync(flowTodo.UserId),
  132. Action = GetTodoAction(flowTodo),
  133. CreatedDate = flowTodo.LastModifiedDate,
  134. Message = flowTodo.Message
  135. });
  136. }
  137. else if (flowTodo.Type == nameof(TodoTypeConst.CarbonCopy))
  138. {
  139. var nodeUserName = await _userRepository.GetNameAsync(flowTodo.UserId);
  140. carbonCopies.Add(nodeUserName);
  141. }
  142. }
  143. // var checkMessages = new List<MessageResult>();
  144. // var messages = await _flowMessageRepository.GetListByFlowIdAsync(flowId, nameof(TodoTypeConst.ApproverCheck));
  145. // foreach (var message in messages)
  146. // {
  147. // checkMessages.Add(new MessageResult
  148. // {
  149. // AvatarUrl = await _userRepository.GetAvatarUrlAsync(message.UserId),
  150. // Name = await _userRepository.GetNameAsync(message.UserId),
  151. // Message = message.Message,
  152. // CreatedDate = message.CreatedDate
  153. // });
  154. // }
  155. //获取系统配置抄送人和审核人
  156. var processNodeDtos = await GetNodesAsync(flowDto, await _processService.GetNodesAsync(flowDto.ProcessId, 0));
  157. //系统抄送人
  158. var carbonCopyUserIds = new List<int>();
  159. //系统审核人
  160. var approverCheckUserIds = new List<int>();
  161. foreach (var processNodeDto in processNodeDtos)
  162. {
  163. //抄送人
  164. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Start))
  165. {
  166. carbonCopyUserIds.AddRange(await GetListUserAsync(processNodeDto));
  167. }
  168. //插入审核人
  169. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.Approver))
  170. {
  171. approverCheckUserIds.AddRange(await GetListUserAsync(processNodeDto));
  172. //break;
  173. }
  174. //抄送人
  175. if (processNodeDto.Type == nameof(ProcessNodeTypeConst.End))
  176. {
  177. carbonCopyUserIds.AddRange(await GetListUserAsync(processNodeDto));
  178. break;
  179. }
  180. }
  181. var flowResult = new FlowResult
  182. {
  183. ProcessId = process.Id,
  184. AvatarUrl = avatarUrl,
  185. UserName = user.Name,
  186. Name = GetFlowName(user, process),
  187. FlowState = flowDto.State,
  188. No = flowDto.No,
  189. DepartmentName = await _departmentRepository.GetNameAsync(user.DepartmentId),
  190. CreatedDate = flowDto.CreatedDate,
  191. FormFields = formFields,
  192. Nodes = nodes,
  193. CarbonCopies = string.Join(",", carbonCopies),
  194. UserId = flowDto.UserId
  195. // CheckMessages = checkMessages
  196. };
  197. //流程名称
  198. flowResult.ProcessName = process.Name;
  199. //抄送人
  200. flowResult.CarbonCopyUsers = await _userRepository.GeFlowUsers(carbonCopyUserIds, flowId, nameof(TodoTypeConst.CarbonCopy));
  201. //审核人
  202. flowResult.ApproverCheckUsers = await _userRepository.GeFlowUsers(approverCheckUserIds, flowId, nameof(TodoTypeConst.ApproverCheck));
  203. if (userId > 0)
  204. {
  205. //获取副流程
  206. var flowToDo = flowTodeDto;
  207. if (flowToDo != null)
  208. {
  209. if (flowToDo.IsDone && flowToDo.IsChecked)
  210. flowResult.State = FlowState.Checked;
  211. else if (flowToDo.IsDone && !flowToDo.IsChecked)
  212. {
  213. if (flowToDo.DoneType == 1)
  214. flowResult.State = FlowState.Meeting;
  215. else if (flowToDo.DoneType == 2)
  216. flowResult.State = FlowState.Wait;
  217. else
  218. flowResult.State = FlowState.Denied;
  219. }
  220. else if (flowToDo.IsDone && !flowToDo.IsChecked)
  221. flowResult.State = FlowState.Denied;
  222. else
  223. {
  224. flowResult.State = FlowState.Checking;
  225. }
  226. }
  227. else
  228. {
  229. flowResult.State = FlowState.Checked;
  230. }
  231. }
  232. //获取会议上传的文件流
  233. 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)));
  234. flowResult.FlowAttachments = flowAttachments.ToList();
  235. flowResult.IsAdmin = flowDto.UserId == userId;
  236. return flowResult;
  237. }
  238. }
  239. }