ExamDataReportService.cs 4.7 KB

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