S8ExceptionService.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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" };
  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(q.DeptId.HasValue, (e, sc) => e.ResponsibleDeptId == q.DeptId!.Value || e.OccurrenceDeptId == q.DeptId!.Value)
  36. .WhereIF(q.TimeoutFlag.HasValue, (e, sc) => e.TimeoutFlag == q.TimeoutFlag!.Value)
  37. .WhereIF(q.BeginTime.HasValue, (e, sc) => e.CreatedAt >= q.BeginTime!.Value)
  38. .WhereIF(q.EndTime.HasValue, (e, sc) => e.CreatedAt <= q.EndTime!.Value)
  39. .WhereIF(!string.IsNullOrWhiteSpace(q.ProcessNodeCode), (e, sc) => e.ProcessNodeCode == q.ProcessNodeCode)
  40. .WhereIF(!string.IsNullOrWhiteSpace(q.RelatedObjectCode), (e, sc) => e.RelatedObjectCode == q.RelatedObjectCode)
  41. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  42. (e, sc) => e.Title.Contains(q.Keyword!) || e.ExceptionCode.Contains(q.Keyword!));
  43. var total = await query.CountAsync();
  44. var list = await query
  45. .OrderBy((e, sc) => e.CreatedAt, OrderByType.Desc)
  46. .Select((e, sc) => new AdoS8ExceptionListItemDto
  47. {
  48. Id = e.Id,
  49. ExceptionCode = e.ExceptionCode,
  50. Title = e.Title,
  51. Status = e.Status,
  52. Severity = e.Severity,
  53. PriorityScore = e.PriorityScore,
  54. PriorityLevel = e.PriorityLevel,
  55. SceneCode = e.SceneCode,
  56. SceneName = sc.SceneName,
  57. ResponsibleDeptId = e.ResponsibleDeptId,
  58. AssigneeId = e.AssigneeId,
  59. SlaDeadline = e.SlaDeadline,
  60. TimeoutFlag = e.TimeoutFlag,
  61. CreatedAt = e.CreatedAt,
  62. ClosedAt = e.ClosedAt
  63. })
  64. .ToPageListAsync(q.Page, q.PageSize);
  65. foreach (var r in list)
  66. {
  67. r.StatusLabel = S8Labels.StatusLabel(r.Status);
  68. r.SeverityLabel = S8Labels.SeverityLabel(r.Severity);
  69. }
  70. await FillDisplayNamesAsync(list);
  71. return (total, list);
  72. }
  73. public async Task<object> GetFilterOptionsAsync(long tenantId, long factoryId)
  74. {
  75. var scenes = await _rep.Context.Queryable<AdoS8SceneConfig>()
  76. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.Enabled)
  77. .OrderBy(x => x.SortNo)
  78. .Select(x => new { value = x.SceneCode, label = x.SceneName })
  79. .ToListAsync();
  80. var departments = await _deptRep.AsQueryable()
  81. .Where(x => x.FactoryRefId == factoryId)
  82. .OrderBy(x => x.Department)
  83. .Take(500)
  84. .Select(x => new { value = x.Id, label = x.Descr ?? x.Department })
  85. .ToListAsync();
  86. return new
  87. {
  88. statuses = S8Labels.StatusOptions(),
  89. severities = S8Labels.SeverityOptions(),
  90. scenes,
  91. departments
  92. };
  93. }
  94. public async Task<AdoS8ExceptionDetailDto?> GetDetailAsync(long id, long tenantId, long factoryId)
  95. {
  96. var rows = await _rep.Context.Queryable<AdoS8Exception>()
  97. .LeftJoin<AdoS8SceneConfig>((e, sc) =>
  98. e.TenantId == sc.TenantId && e.FactoryId == sc.FactoryId && e.SceneCode == sc.SceneCode)
  99. .Where((e, sc) => e.Id == id && e.TenantId == tenantId && e.FactoryId == factoryId && !e.IsDeleted)
  100. .Select((e, sc) => new AdoS8ExceptionDetailDto
  101. {
  102. Id = e.Id,
  103. ExceptionCode = e.ExceptionCode,
  104. Title = e.Title,
  105. Description = e.Description,
  106. Status = e.Status,
  107. Severity = e.Severity,
  108. PriorityScore = e.PriorityScore,
  109. PriorityLevel = e.PriorityLevel,
  110. SceneCode = e.SceneCode,
  111. SceneName = sc.SceneName,
  112. SourceType = e.SourceType,
  113. OccurrenceDeptId = e.OccurrenceDeptId,
  114. ResponsibleDeptId = e.ResponsibleDeptId,
  115. ResponsibleGroupId = e.ResponsibleGroupId,
  116. AssigneeId = e.AssigneeId,
  117. ReporterId = e.ReporterId,
  118. SlaDeadline = e.SlaDeadline,
  119. TimeoutFlag = e.TimeoutFlag,
  120. CreatedAt = e.CreatedAt,
  121. ClosedAt = e.ClosedAt,
  122. AssignedAt = e.AssignedAt,
  123. UpdatedAt = e.UpdatedAt
  124. })
  125. .Take(1)
  126. .ToListAsync();
  127. if (rows.Count == 0) return null;
  128. var d = rows[0];
  129. d.StatusLabel = S8Labels.StatusLabel(d.Status);
  130. d.SeverityLabel = S8Labels.SeverityLabel(d.Severity);
  131. await FillDisplayNamesAsync(new[] { d });
  132. return d;
  133. }
  134. private async Task FillDisplayNamesAsync(IEnumerable<AdoS8ExceptionListItemDto> rows)
  135. {
  136. var list = rows.ToList();
  137. if (list.Count == 0) return;
  138. var deptIds = list
  139. .Select(x => x.ResponsibleDeptId)
  140. .Concat(list.OfType<AdoS8ExceptionDetailDto>().Select(x => x.OccurrenceDeptId))
  141. .Where(x => x > 0)
  142. .Distinct()
  143. .ToList();
  144. var empIds = list
  145. .Select(x => x.AssigneeId ?? 0L)
  146. .Concat(list.OfType<AdoS8ExceptionDetailDto>().Select(x => x.ReporterId ?? 0L))
  147. .Where(x => x > 0)
  148. .Distinct()
  149. .ToList();
  150. var deptMap = deptIds.Count == 0
  151. ? new Dictionary<long, string>()
  152. : (await _deptRep.AsQueryable()
  153. .Where(x => deptIds.Contains(x.Id))
  154. .Select(x => new { x.Id, Name = x.Descr ?? x.Department })
  155. .ToListAsync())
  156. .ToDictionary(x => x.Id, x => x.Name);
  157. var empMap = empIds.Count == 0
  158. ? new Dictionary<long, string>()
  159. : (await _empRep.AsQueryable()
  160. .Where(x => empIds.Contains(x.Id))
  161. .Select(x => new { x.Id, Name = x.Name ?? x.Employee })
  162. .ToListAsync())
  163. .ToDictionary(x => x.Id, x => x.Name);
  164. foreach (var row in list)
  165. {
  166. if (row is AdoS8ExceptionDetailDto detail)
  167. {
  168. detail.ResponsibleDeptName = deptMap.GetValueOrDefault(detail.ResponsibleDeptId);
  169. detail.OccurrenceDeptName = deptMap.GetValueOrDefault(detail.OccurrenceDeptId);
  170. detail.AssigneeName = detail.AssigneeId.HasValue ? empMap.GetValueOrDefault(detail.AssigneeId.Value) : null;
  171. detail.ReporterName = detail.ReporterId.HasValue ? empMap.GetValueOrDefault(detail.ReporterId.Value) : null;
  172. }
  173. else
  174. {
  175. row.AssigneeName = row.AssigneeId.HasValue ? empMap.GetValueOrDefault(row.AssigneeId.Value) : null;
  176. }
  177. }
  178. }
  179. }