S8ManualReportService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  6. public class S8ManualReportService : ITransient
  7. {
  8. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  9. private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
  10. private readonly SqlSugarRepository<AdoS8Evidence> _evidenceRep;
  11. private readonly SqlSugarRepository<AdoS8SceneConfig> _sceneRep;
  12. private readonly SqlSugarRepository<AdoS0DepartmentMaster> _deptRep;
  13. private readonly SqlSugarRepository<AdoS0LineMaster> _lineRep;
  14. public S8ManualReportService(
  15. SqlSugarRepository<AdoS8Exception> rep,
  16. SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep,
  17. SqlSugarRepository<AdoS8Evidence> evidenceRep,
  18. SqlSugarRepository<AdoS8SceneConfig> sceneRep,
  19. SqlSugarRepository<AdoS0DepartmentMaster> deptRep,
  20. SqlSugarRepository<AdoS0LineMaster> lineRep)
  21. {
  22. _rep = rep;
  23. _timelineRep = timelineRep;
  24. _evidenceRep = evidenceRep;
  25. _sceneRep = sceneRep;
  26. _deptRep = deptRep;
  27. _lineRep = lineRep;
  28. }
  29. public async Task<object> GetFormOptionsAsync(long tenantId, long factoryId)
  30. {
  31. var scenes = await _sceneRep.AsQueryable()
  32. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.Enabled)
  33. .OrderBy(x => x.SortNo)
  34. .Select(x => new { value = x.SceneCode, label = x.SceneName })
  35. .ToListAsync();
  36. var departments = await _deptRep.AsQueryable()
  37. .Where(x => x.FactoryRefId == factoryId)
  38. .OrderBy(x => x.Department)
  39. .Take(500)
  40. .Select(x => new { value = x.Id, label = x.Descr ?? x.Department })
  41. .ToListAsync();
  42. var lines = await _lineRep.AsQueryable()
  43. .Where(x => x.FactoryRefId == factoryId)
  44. .OrderBy(x => x.Line)
  45. .Take(500)
  46. .Select(x => new { value = x.Id, label = x.Describe ?? x.Line })
  47. .ToListAsync();
  48. return new
  49. {
  50. scenes,
  51. severities = new[]
  52. {
  53. new { value = "CRITICAL", label = "紧急" },
  54. new { value = "HIGH", label = "高" },
  55. new { value = "MEDIUM", label = "中" },
  56. new { value = "LOW", label = "低" }
  57. },
  58. departments,
  59. lines,
  60. materials = Array.Empty<object>()
  61. };
  62. }
  63. public async Task<AdoS8ManualReportResultDto> CreateAsync(AdoS8ManualReportCreateDto dto)
  64. {
  65. if (string.IsNullOrWhiteSpace(dto.Title)) throw new S8BizException("标题必填");
  66. if (string.IsNullOrWhiteSpace(dto.SceneCode)) throw new S8BizException("场景必填");
  67. var code = $"EX-{DateTime.Now:yyyyMMdd}-{Guid.NewGuid().ToString("N")[..8].ToUpperInvariant()}";
  68. var entity = new AdoS8Exception
  69. {
  70. TenantId = dto.TenantId,
  71. FactoryId = dto.FactoryId,
  72. ExceptionCode = code,
  73. Title = dto.Title.Trim(),
  74. Description = dto.Description,
  75. SceneCode = dto.SceneCode.Trim(),
  76. SourceType = "MANUAL",
  77. Status = "NEW",
  78. Severity = string.IsNullOrWhiteSpace(dto.Severity) ? "MEDIUM" : dto.Severity,
  79. PriorityScore = 0,
  80. PriorityLevel = "P3",
  81. OccurrenceDeptId = dto.OccurrenceDeptId,
  82. ResponsibleDeptId = dto.ResponsibleDeptId,
  83. ReporterId = dto.ReporterId,
  84. CreatedAt = DateTime.Now,
  85. IsDeleted = false
  86. };
  87. await _rep.AsTenant().UseTranAsync(async () =>
  88. {
  89. entity = await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  90. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  91. {
  92. ExceptionId = entity.Id,
  93. ActionCode = "CREATE",
  94. ActionLabel = "创建",
  95. FromStatus = null,
  96. ToStatus = "NEW",
  97. OperatorId = dto.ReporterId,
  98. ActionRemark = "主动提报",
  99. CreatedAt = DateTime.Now
  100. });
  101. }, ex => throw ex);
  102. return new AdoS8ManualReportResultDto
  103. {
  104. ExceptionId = entity.Id,
  105. ExceptionCode = entity.ExceptionCode,
  106. TaskId = entity.Id
  107. };
  108. }
  109. public async Task<AdoS8Exception?> GetAsync(long id) =>
  110. await _rep.GetByIdAsync(id);
  111. public async Task<AdoS8Evidence> AddAttachmentAsync(long id, AdoS8AttachmentCreateDto dto)
  112. {
  113. var entity = await _rep.GetFirstAsync(x => x.Id == id && !x.IsDeleted)
  114. ?? throw new S8BizException("异常不存在");
  115. if (string.IsNullOrWhiteSpace(dto.FileName) || string.IsNullOrWhiteSpace(dto.FileUrl))
  116. throw new S8BizException("附件名称和地址必填");
  117. var evidence = new AdoS8Evidence
  118. {
  119. ExceptionId = id,
  120. EvidenceType = string.IsNullOrWhiteSpace(dto.EvidenceType) ? "file" : dto.EvidenceType,
  121. FileName = dto.FileName.Trim(),
  122. FileUrl = dto.FileUrl.Trim(),
  123. SourceSystem = dto.SourceSystem,
  124. UploadedBy = dto.UploadedBy,
  125. UploadedAt = DateTime.Now,
  126. IsDeleted = false
  127. };
  128. await _evidenceRep.InsertAsync(evidence);
  129. return evidence;
  130. }
  131. }