ExamPlanService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 监测计划管理服务
  6. /// </summary>
  7. public class ExamPlanService : IExamPlanService, ITransient
  8. {
  9. private readonly IRepository<ExamPlan> _rep;
  10. private readonly IEducationStageYearsService _educationStageYearsService;
  11. public ExamPlanService(IRepository<ExamPlan> rep, IEducationStageYearsService educationStageYearsService)
  12. {
  13. _rep = rep;
  14. _educationStageYearsService = educationStageYearsService;
  15. }
  16. #region 创建更新
  17. /// <summary>
  18. /// 添加监测计划
  19. /// </summary>
  20. /// <param name="input"></param>
  21. /// <returns></returns>
  22. public async Task Add(AddExamPlanInput input)
  23. {
  24. var ey = await _educationStageYearsService.GetByEducationStage(input.EducationStage);
  25. //var maxExam = await _rep.Where(t => t.EducationStage == input.EducationStage && t.ExamPeriodType == input.ExamPeriodType && t.ExamType == input.ExamType && t.SemesterId == input.SemesterId).OrderByDescending(t => t.Sequence).FirstOrDefaultAsync();
  26. var maxExam = await _rep.Where(t => t.EducationStage == input.EducationStage && t.SemesterId == input.SemesterId).OrderByDescending(t => t.Sequence).FirstOrDefaultAsync();
  27. short seq = maxExam?.Sequence ?? 0;
  28. var item = input.Adapt<ExamPlan>();
  29. item.Sequence = (short)(seq + 1);
  30. item.EducationYears = ey.EducationYears;
  31. await item.InsertAsync();
  32. }
  33. /// <summary>
  34. /// 更新监测计划
  35. /// </summary>
  36. /// <param name="input"></param>
  37. /// <returns></returns>
  38. public async Task Update(UpdateExamPlanInput input)
  39. {
  40. if (!await _rep.AnyAsync(t => t.Id == input.Id))
  41. {
  42. throw Oops.Oh(ErrorCode.E2001);
  43. }
  44. var item = input.Adapt<ExamPlan>();
  45. await item.UpdateIncludeAsync(new[] { nameof(item.Name), nameof(item.FullName), nameof(item.ShortName), nameof(item.Remark), nameof(item.Config) });
  46. }
  47. /// <summary>
  48. /// 删除监测计划
  49. /// </summary>
  50. /// <param name="input"></param>
  51. /// <returns></returns>
  52. public async Task Del(BaseId input)
  53. {
  54. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  55. if (item.Status == ExamStatus.ACTIVE || item.Status == ExamStatus.STOPPED)
  56. {
  57. throw Oops.Oh(ErrorCode.E3001, "已使用监测计划");
  58. }
  59. await item.DeleteAsync();
  60. }
  61. #endregion
  62. #region 状态变更
  63. /// <summary>
  64. /// 开始监测
  65. /// </summary>
  66. /// <param name="input"></param>
  67. /// <returns></returns>
  68. public async Task Start(BaseId input)
  69. {
  70. var item = await _rep.Include(t => t.ExamOrgs).FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  71. if (item.Status != ExamStatus.READY)
  72. {
  73. throw Oops.Oh(ErrorCode.E2006);
  74. }
  75. if (!item.ExamOrgs.Any())
  76. {
  77. throw Oops.Oh(ErrorCode.E2007, "监测机构");
  78. }
  79. item.Status = ExamStatus.ACTIVE;
  80. item.BeginTime = DateTime.Now;
  81. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.BeginTime) });
  82. }
  83. /// <summary>
  84. /// 结束监测
  85. /// </summary>
  86. /// <param name="input"></param>
  87. /// <returns></returns>
  88. public async Task Stop(BaseId input)
  89. {
  90. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  91. if (item.Status != ExamStatus.ACTIVE)
  92. {
  93. throw Oops.Oh(ErrorCode.E2006);
  94. }
  95. item.Status = ExamStatus.STOPPED;
  96. item.EndTime = DateTime.Now;
  97. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.EndTime) });
  98. }
  99. /// <summary>
  100. /// 取消监测
  101. /// </summary>
  102. /// <param name="input"></param>
  103. /// <returns></returns>
  104. public async Task Cancel(BaseId input)
  105. {
  106. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  107. if (item.Status == ExamStatus.ACTIVE)
  108. {
  109. throw Oops.Oh(ErrorCode.E2006);
  110. }
  111. item.Status = ExamStatus.CANCELLED;
  112. item.EndTime = DateTime.Now;
  113. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.EndTime) });
  114. }
  115. #endregion
  116. #region 查询统计
  117. /// <summary>
  118. /// 根据ID获取监测计划
  119. /// </summary>
  120. /// <param name="id"></param>
  121. /// <returns></returns>
  122. public async Task<ExamPlanOutput> GetById(int id)
  123. {
  124. var item = await _rep.DetachedEntities.ProjectToType<ExamPlanOutput>()
  125. .FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  126. return item;
  127. //return item.Adapt<ExamPlanOutput>();
  128. }
  129. /// <summary>
  130. /// 分页查询监测计划列表
  131. /// </summary>
  132. /// <param name="input"></param>
  133. /// <returns></returns>
  134. public async Task<PageResult<ExamPlanOutput>> QueryPageList(ExamPlanPageInput input)
  135. {
  136. var query = GetQueryBase(input);
  137. var ret = await query.OrderByDescending(t => t.CreateTime).ProjectToType<ExamPlanOutput>().ToADPagedListAsync(input.PageIndex, input.PageSize);
  138. return ret;
  139. }
  140. /// <summary>
  141. /// 获取我的单据状态数量
  142. /// </summary>
  143. /// <returns></returns>
  144. public async Task<List<StatusCount>> QueryStatusCount(ExamPlanPageInput input)
  145. {
  146. var query = GetQueryBase(input);
  147. if (query == null)
  148. {
  149. return new List<StatusCount>();
  150. }
  151. var counts = await query.GroupBy(t => t.Status).Select(t => new StatusCount { Status = (int)t.Key, Count = t.Count() }).ToListAsync();
  152. return counts;
  153. }
  154. #endregion
  155. #region 私有方法
  156. /// <summary>
  157. /// 构建查询
  158. /// </summary>
  159. /// <param name="input"></param>
  160. /// <returns></returns>
  161. private IQueryable<ExamPlan> GetQueryBase(ExamPlanPageInput input)
  162. {
  163. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  164. var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
  165. var searchEndTime = !string.IsNullOrEmpty(input.SearchEndTime?.Trim());
  166. var query = _rep.DetachedEntities.Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.ShortName, $"%{input.Name.Trim()}%")))
  167. .Where(input.EducationStage.HasValue, t => t.EducationStage == input.EducationStage)
  168. //.Where(input.ExamPeriodType.HasValue, t => t.ExamPeriodType == input.ExamPeriodType)
  169. //.Where(input.ExamType.HasValue, t => t.ExamType == input.ExamType)
  170. .Where(input.SemesterId.HasValue, t => t.SemesterId == input.SemesterId)
  171. .Where(input.Status.HasValue, t => t.Status == input.Status)
  172. .Where((searchBeginTime, u => u.CreateTime >= DateTime.Parse(input.SearchBeginTime)))
  173. .Where((searchEndTime, u => u.CreateTime <= DateTime.Parse(input.SearchEndTime)));
  174. return query;
  175. }
  176. #endregion
  177. }