S8ExceptionService.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Admin.NET.Plugin.AiDOP.Dto.S8;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Manufacturing;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
  4. using Admin.NET.Plugin.AiDOP.Entity.S8;
  5. using Admin.NET.Plugin.AiDOP.Infrastructure;
  6. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  7. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  8. public class S8ExceptionService : ITransient
  9. {
  10. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  11. private readonly SqlSugarRepository<AdoS0DepartmentMaster> _deptRep;
  12. private readonly SqlSugarRepository<AdoS0EmployeeMaster> _empRep;
  13. public S8ExceptionService(
  14. SqlSugarRepository<AdoS8Exception> rep,
  15. SqlSugarRepository<AdoS0DepartmentMaster> deptRep,
  16. SqlSugarRepository<AdoS0EmployeeMaster> empRep)
  17. {
  18. _rep = rep;
  19. _deptRep = deptRep;
  20. _empRep = empRep;
  21. }
  22. public async Task<(int total, List<AdoS8ExceptionListItemDto> list)> GetPagedAsync(AdoS8ExceptionQueryDto q)
  23. {
  24. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  25. var pendingStatuses = new[] { "NEW", "ASSIGNED", "IN_PROGRESS", "PENDING_VERIFICATION" };
  26. var query = _rep.Context.Queryable<AdoS8Exception>()
  27. .LeftJoin<AdoS8SceneConfig>((e, sc) =>
  28. e.TenantId == sc.TenantId && e.FactoryId == sc.FactoryId && e.SceneCode == sc.SceneCode)
  29. .Where((e, sc) => e.TenantId == q.TenantId && e.FactoryId == q.FactoryId && !e.IsDeleted)
  30. .WhereIF(!string.IsNullOrWhiteSpace(q.Status), (e, sc) => e.Status == q.Status)
  31. .WhereIF(string.IsNullOrWhiteSpace(q.Status) && q.StatusBucket == "pending",
  32. (e, sc) => pendingStatuses.Contains(e.Status))
  33. .WhereIF(!string.IsNullOrWhiteSpace(q.Severity), (e, sc) => e.Severity == q.Severity)
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.SceneCode), (e, sc) => e.SceneCode == q.SceneCode)
  35. .WhereIF(!string.IsNullOrWhiteSpace(q.ModuleCode), (e, sc) => e.ModuleCode == q.ModuleCode)
  36. .WhereIF(q.DeptId.HasValue, (e, sc) => e.ResponsibleDeptId == q.DeptId!.Value || e.OccurrenceDeptId == q.DeptId!.Value)
  37. .WhereIF(q.TimeoutFlag.HasValue, (e, sc) => e.TimeoutFlag == q.TimeoutFlag!.Value)
  38. .WhereIF(q.BeginTime.HasValue, (e, sc) => e.CreatedAt >= q.BeginTime!.Value)
  39. .WhereIF(q.EndTime.HasValue, (e, sc) => e.CreatedAt <= q.EndTime!.Value)
  40. .WhereIF(!string.IsNullOrWhiteSpace(q.ProcessNodeCode), (e, sc) => e.ProcessNodeCode == q.ProcessNodeCode)
  41. .WhereIF(!string.IsNullOrWhiteSpace(q.RelatedObjectCode), (e, sc) => e.RelatedObjectCode == q.RelatedObjectCode)
  42. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  43. (e, sc) => e.Title.Contains(q.Keyword!) || e.ExceptionCode.Contains(q.Keyword!));
  44. var total = await query.CountAsync();
  45. var list = await query
  46. .OrderBy((e, sc) => e.CreatedAt, OrderByType.Desc)
  47. .Select((e, sc) => new AdoS8ExceptionListItemDto
  48. {
  49. Id = e.Id,
  50. FactoryId = e.FactoryId,
  51. ExceptionCode = e.ExceptionCode,
  52. Title = e.Title,
  53. Status = e.Status,
  54. Severity = e.Severity,
  55. PriorityScore = e.PriorityScore,
  56. PriorityLevel = e.PriorityLevel,
  57. SceneCode = e.SceneCode,
  58. SceneName = sc.SceneName,
  59. ResponsibleDeptId = e.ResponsibleDeptId,
  60. AssigneeId = e.AssigneeId,
  61. SlaDeadline = e.SlaDeadline,
  62. TimeoutFlag = e.TimeoutFlag,
  63. CreatedAt = e.CreatedAt,
  64. ClosedAt = e.ClosedAt
  65. })
  66. .ToPageListAsync(q.Page, q.PageSize);
  67. foreach (var r in list)
  68. {
  69. r.StatusLabel = S8Labels.StatusLabel(r.Status);
  70. r.SeverityLabel = S8Labels.SeverityLabel(r.Severity);
  71. }
  72. await FillDisplayNamesAsync(list);
  73. return (total, list);
  74. }
  75. public async Task<object> GetFilterOptionsAsync(long tenantId, long factoryId)
  76. {
  77. var scenes = await _rep.Context.Queryable<AdoS8SceneConfig>()
  78. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.Enabled)
  79. .OrderBy(x => x.SortNo)
  80. .Select(x => new { value = x.SceneCode, label = x.SceneName })
  81. .ToListAsync();
  82. var departments = await _deptRep.AsQueryable()
  83. .Where(x => x.FactoryRefId == factoryId)
  84. .OrderBy(x => x.Department)
  85. .Take(500)
  86. .Select(x => new { value = x.Id, label = x.Descr ?? x.Department })
  87. .ToListAsync();
  88. return new
  89. {
  90. statuses = S8Labels.StatusOptions(),
  91. severities = S8Labels.SeverityOptions(),
  92. scenes,
  93. departments
  94. };
  95. }
  96. public async Task<AdoS8ExceptionDetailDto?> GetDetailAsync(long id, long tenantId, long factoryId)
  97. {
  98. var rows = await _rep.Context.Queryable<AdoS8Exception>()
  99. .LeftJoin<AdoS8SceneConfig>((e, sc) =>
  100. e.TenantId == sc.TenantId && e.FactoryId == sc.FactoryId && e.SceneCode == sc.SceneCode)
  101. .Where((e, sc) => e.Id == id && e.TenantId == tenantId && e.FactoryId == factoryId && !e.IsDeleted)
  102. .Select((e, sc) => new AdoS8ExceptionDetailDto
  103. {
  104. Id = e.Id,
  105. FactoryId = e.FactoryId,
  106. ExceptionCode = e.ExceptionCode,
  107. Title = e.Title,
  108. Description = e.Description,
  109. Status = e.Status,
  110. Severity = e.Severity,
  111. PriorityScore = e.PriorityScore,
  112. PriorityLevel = e.PriorityLevel,
  113. SceneCode = e.SceneCode,
  114. SceneName = sc.SceneName,
  115. SourceType = e.SourceType,
  116. OccurrenceDeptId = e.OccurrenceDeptId,
  117. ResponsibleDeptId = e.ResponsibleDeptId,
  118. ResponsibleGroupId = e.ResponsibleGroupId,
  119. AssigneeId = e.AssigneeId,
  120. ReporterId = e.ReporterId,
  121. SlaDeadline = e.SlaDeadline,
  122. TimeoutFlag = e.TimeoutFlag,
  123. CreatedAt = e.CreatedAt,
  124. ClosedAt = e.ClosedAt,
  125. AssignedAt = e.AssignedAt,
  126. UpdatedAt = e.UpdatedAt,
  127. ActiveFlowInstanceId = e.ActiveFlowInstanceId,
  128. ActiveFlowBizType = e.ActiveFlowBizType,
  129. VerifierId = e.VerifierId,
  130. VerificationAssignedAt = e.VerificationAssignedAt,
  131. VerifiedAt = e.VerifiedAt,
  132. VerificationResult = e.VerificationResult,
  133. VerificationRemark = e.VerificationRemark,
  134. })
  135. .Take(1)
  136. .ToListAsync();
  137. if (rows.Count == 0) return null;
  138. var d = rows[0];
  139. d.StatusLabel = S8Labels.StatusLabel(d.Status);
  140. d.SeverityLabel = S8Labels.SeverityLabel(d.Severity);
  141. await FillDisplayNamesAsync(new[] { d });
  142. return d;
  143. }
  144. private async Task FillDisplayNamesAsync(IEnumerable<AdoS8ExceptionListItemDto> rows)
  145. {
  146. var list = rows.ToList();
  147. if (list.Count == 0) return;
  148. var deptIds = list
  149. .Select(x => x.ResponsibleDeptId)
  150. .Concat(list.OfType<AdoS8ExceptionDetailDto>().Select(x => x.OccurrenceDeptId))
  151. .Where(x => x > 0)
  152. .Distinct()
  153. .ToList();
  154. var empIds = list
  155. .Select(x => x.AssigneeId ?? 0L)
  156. .Concat(list.OfType<AdoS8ExceptionDetailDto>().Select(x => x.ReporterId ?? 0L))
  157. .Concat(list.OfType<AdoS8ExceptionDetailDto>().Select(x => x.VerifierId ?? 0L))
  158. .Where(x => x > 0)
  159. .Distinct()
  160. .ToList();
  161. var deptMap = deptIds.Count == 0
  162. ? new Dictionary<long, string>()
  163. : (await _deptRep.AsQueryable()
  164. .Where(x => deptIds.Contains(x.Id))
  165. .Select(x => new { x.Id, Name = x.Descr ?? x.Department })
  166. .ToListAsync())
  167. .ToDictionary(x => x.Id, x => x.Name);
  168. var empMap = empIds.Count == 0
  169. ? new Dictionary<long, string>()
  170. : (await _empRep.AsQueryable()
  171. .Where(x => empIds.Contains(x.Id))
  172. .Select(x => new { x.Id, Name = x.Name ?? x.Employee })
  173. .ToListAsync())
  174. .ToDictionary(x => x.Id, x => x.Name);
  175. foreach (var row in list)
  176. {
  177. if (row is AdoS8ExceptionDetailDto detail)
  178. {
  179. detail.ResponsibleDeptName = deptMap.GetValueOrDefault(detail.ResponsibleDeptId);
  180. detail.OccurrenceDeptName = deptMap.GetValueOrDefault(detail.OccurrenceDeptId);
  181. detail.AssigneeName = detail.AssigneeId.HasValue ? empMap.GetValueOrDefault(detail.AssigneeId.Value) : null;
  182. detail.ReporterName = detail.ReporterId.HasValue ? empMap.GetValueOrDefault(detail.ReporterId.Value) : null;
  183. detail.VerifierName = detail.VerifierId.HasValue ? empMap.GetValueOrDefault(detail.VerifierId.Value) : null;
  184. }
  185. else
  186. {
  187. row.ResponsibleDeptName = deptMap.GetValueOrDefault(row.ResponsibleDeptId);
  188. row.AssigneeName = row.AssigneeId.HasValue ? empMap.GetValueOrDefault(row.AssigneeId.Value) : null;
  189. }
  190. }
  191. }
  192. }