ExamCourseService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 ExamCourseService : IExamCourseService, ITransient
  9. {
  10. private readonly IRepository<ExamCourse> _rep;
  11. public ExamCourseService(IRepository<ExamCourse> rep)
  12. {
  13. _rep = rep;
  14. }
  15. /// <summary>
  16. /// 添加监测科目
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <returns></returns>
  20. public async Task Add(AddExamCourseInput input)
  21. {
  22. if (await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.ExamGradeId == input.ExamGradeId && t.CourseId == input.CourseId))
  23. {
  24. throw Oops.Oh(ErrorCode.E2002);
  25. }
  26. var item = input.Adapt<ExamCourse>();
  27. item.ScoreReportConfig = JSON.Serialize(input.ScoreReportConfig);
  28. await item.InsertAsync();
  29. }
  30. /// <summary>
  31. /// 更新监测科目
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <returns></returns>
  35. public async Task Update(UpdateExamCourseInput input)
  36. {
  37. if (await _rep.AnyAsync(t => t.Id != input.Id && t.ExamPlanId == input.ExamPlanId && t.ExamGradeId == input.ExamGradeId && t.CourseId == input.CourseId))
  38. {
  39. throw Oops.Oh(ErrorCode.E2002);
  40. }
  41. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  42. item.ScoreReportConfig = JSON.Serialize(input.ScoreReportConfig);
  43. await item.UpdateAsync();
  44. }
  45. /// <summary>
  46. /// 删除监测科目
  47. /// </summary>
  48. /// <param name="input"></param>
  49. /// <returns></returns>
  50. public async Task Del(BaseId input)
  51. {
  52. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  53. await item.DeleteAsync();
  54. }
  55. /// <summary>
  56. /// 根据ID获取监测科目信息
  57. /// </summary>
  58. /// <param name="id"></param>
  59. /// <returns></returns>
  60. public async Task<ExamCourseOutput> GetById(int id)
  61. {
  62. var item = await _rep.DetachedEntities.ProjectToType<ExamCourseOutput>().FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  63. return item;
  64. }
  65. /// <summary>
  66. /// 根据监测年级查询监测学科列表
  67. /// </summary>
  68. /// <param name="input"></param>
  69. /// <returns></returns>
  70. public async Task<List<ExamCourseOutput>> QueryList(QueryExamCourseInput input)
  71. {
  72. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId)
  73. .Where(input.GradeId.HasValue, t => t.GradeId == input.GradeId)
  74. .ProjectToType<ExamCourseOutput>()
  75. .OrderBy(t => t.GradeId).ThenBy(t => t.CourseId)
  76. .ToListAsync();
  77. return items;
  78. }
  79. /// <summary>
  80. /// 查询监测学科列表
  81. /// </summary>
  82. /// <param name="examPlanId"></param>
  83. /// <returns></returns>
  84. public async Task<List<ExamCourseOutput>> GetListByExamPlanId(int examPlanId)
  85. {
  86. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId)
  87. .ProjectToType<ExamCourseOutput>()
  88. .OrderBy(t => t.GradeId).ThenBy(t => t.CourseId)
  89. .ToListAsync();
  90. return items;
  91. }
  92. }