S8ExceptionService.cs 8.5 KB

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