ExamDataReportService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Furion.DatabaseAccessor.Extensions;
  2. using Furion.JsonSerialization;
  3. using YBEE.EQM.Core;
  4. namespace YBEE.EQM.Application;
  5. /// <summary>
  6. /// 监测数据上报类型管理服务
  7. /// </summary>
  8. public class ExamDataReportService : IExamDataReportService, ITransient
  9. {
  10. private readonly IRepository<ExamDataReport> _rep;
  11. private readonly IResourceFileService _resourceFileService;
  12. public ExamDataReportService(IRepository<ExamDataReport> rep, IResourceFileService resourceFileService)
  13. {
  14. _rep = rep;
  15. _resourceFileService = resourceFileService;
  16. }
  17. #region 创建更新
  18. /// <summary>
  19. /// 添加上报类型
  20. /// </summary>
  21. /// <param name="input"></param>
  22. /// <returns></returns>
  23. public async Task Add(AddExamDataReportInput input)
  24. {
  25. if (await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.Type == input.Type))
  26. {
  27. throw Oops.Oh(ErrorCode.E2002, "该类型");
  28. }
  29. var item = input.Adapt<ExamDataReport>();
  30. await item.InsertAsync();
  31. }
  32. /// <summary>
  33. /// 更新上报类型
  34. /// </summary>
  35. /// <param name="input"></param>
  36. /// <returns></returns>
  37. public async Task Update(UpdateExamDataReportInput input)
  38. {
  39. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  40. item.Remark = input.Remark;
  41. await item.UpdateIncludeAsync(new[] { nameof(item.BeginTime), nameof(item.EndTime), nameof(item.Remark) });
  42. }
  43. /// <summary>
  44. /// 删除上报类型
  45. /// </summary>
  46. /// <param name="input"></param>
  47. /// <returns></returns>
  48. public async Task Del(BaseId input)
  49. {
  50. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  51. if (item.Status != ExamStatus.READY)
  52. {
  53. throw Oops.Oh(ErrorCode.E3001);
  54. }
  55. await item.DeleteAsync();
  56. }
  57. #endregion
  58. #region 状态处理
  59. /// <summary>
  60. /// 开始
  61. /// </summary>
  62. /// <param name="input"></param>
  63. /// <returns></returns>
  64. public async Task Start(BaseId input)
  65. {
  66. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  67. if (item.Status != ExamStatus.READY)
  68. {
  69. throw Oops.Oh(ErrorCode.E2006);
  70. }
  71. item.Status = ExamStatus.ACTIVE;
  72. item.BeginTime = DateTime.Now;
  73. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.BeginTime) });
  74. }
  75. /// <summary>
  76. /// 结束
  77. /// </summary>
  78. /// <param name="input"></param>
  79. /// <returns></returns>
  80. public async Task Stop(BaseId input)
  81. {
  82. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  83. if (item.Status != ExamStatus.ACTIVE)
  84. {
  85. throw Oops.Oh(ErrorCode.E2006);
  86. }
  87. item.Status = ExamStatus.STOPPED;
  88. item.EndTime = DateTime.Now;
  89. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.EndTime) });
  90. }
  91. /// <summary>
  92. /// 取消
  93. /// </summary>
  94. /// <param name="input"></param>
  95. /// <returns></returns>
  96. public async Task Cancel(BaseId input)
  97. {
  98. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  99. if (item.Status == ExamStatus.ACTIVE)
  100. {
  101. throw Oops.Oh(ErrorCode.E2006);
  102. }
  103. item.Status = ExamStatus.CANCELLED;
  104. item.EndTime = DateTime.Now;
  105. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.EndTime) });
  106. }
  107. /// <summary>
  108. /// 添加附件
  109. /// </summary>
  110. /// <param name="input"></param>
  111. /// <returns></returns>
  112. public async Task AddAttachment(AddAttachmentInput input)
  113. {
  114. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.SourceId) ?? throw Oops.Oh(ErrorCode.E2001);
  115. item.Attachments = AttachmentUtil.InsertInto(item.Attachments, input.Adapt<AttachmentItem>());
  116. await item.UpdateIncludeAsync(new[] { nameof(item.Attachments) });
  117. }
  118. /// <summary>
  119. /// 删除附件
  120. /// </summary>
  121. /// <param name="input"></param>
  122. /// <returns></returns>
  123. public async Task DelAttachment(DeleteAttachmentInput input)
  124. {
  125. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.SourceId) ?? throw Oops.Oh(ErrorCode.E2001);
  126. var attachments = AttachmentUtil.GetList(item.Attachments);
  127. var a = attachments.FirstOrDefault(t => t.FileId == input.FileId);
  128. if (a != null)
  129. {
  130. attachments.Remove(a);
  131. item.Attachments = JSON.Serialize(attachments);
  132. await item.UpdateIncludeAsync(new[] { nameof(item.Attachments) });
  133. await _resourceFileService.Del(new() { Id = a.FileId });
  134. if (a.ThumbFileId.HasValue && a.ThumbFileId > 0)
  135. {
  136. await _resourceFileService.Del(new() { Id = a.ThumbFileId.Value });
  137. }
  138. }
  139. }
  140. #endregion
  141. #region 查询
  142. /// <summary>
  143. /// 根据ID获取监测数据上报类型
  144. /// </summary>
  145. /// <param name="id"></param>
  146. /// <returns></returns>
  147. public async Task<ExamDataReportOutput> GetById(int id)
  148. {
  149. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  150. return item.Adapt<ExamDataReportOutput>();
  151. }
  152. /// <summary>
  153. /// 根据监测计划ID获取数据上报类型列表
  154. /// </summary>
  155. /// <param name="examPlanId"></param>
  156. /// <returns></returns>
  157. public async Task<List<ExamDataReportOutput>> GetListByExamPlanId(int examPlanId)
  158. {
  159. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType<ExamDataReportOutput>().OrderBy(t => t.Type).ToListAsync();
  160. var orgCount = await _rep.Change<ExamOrg>().DetachedEntities.Where(t => t.ExamPlanId == examPlanId).CountAsync();
  161. foreach (var item in items)
  162. {
  163. var reportedCount = await _rep.Change<ExamOrgDataReport>().DetachedEntities.Where(t => t.Type == item.Type && t.ExamOrg.ExamPlanId == examPlanId && t.Status == DataReportStatus.REPORTED).CountAsync();
  164. item.ReportedCount = reportedCount;
  165. item.Count = orgCount;
  166. }
  167. return items;
  168. }
  169. #endregion
  170. }