NceePlanService.cs 6.3 KB

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