ExamPaperQuestionMajorService.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using YBEE.EQM.Core;
  2. namespace YBEE.EQM.Application;
  3. /// <summary>
  4. /// 试卷大题管理服务
  5. /// </summary>
  6. public class ExamPaperQuestionMajorService : IExamPaperQuestionMajorService, ITransient
  7. {
  8. private readonly IRepository<ExamPaperQuestionMajor> _rep;
  9. public ExamPaperQuestionMajorService(IRepository<ExamPaperQuestionMajor> rep)
  10. {
  11. _rep = rep;
  12. }
  13. /// <summary>
  14. /// 添加
  15. /// </summary>
  16. /// <param name="input"></param>
  17. /// <returns></returns>
  18. public async Task Add(AddExamPaperQuestionMajorInput input)
  19. {
  20. var item = input.Adapt<ExamPaperQuestionMajor>();
  21. await _rep.InsertNowAsync(item);
  22. }
  23. /// <summary>
  24. /// 更新
  25. /// </summary>
  26. /// <param name="input"></param>
  27. /// <returns></returns>
  28. public async Task Update(UpdateExamPaperQuestionMajorInput input)
  29. {
  30. var item = input.Adapt<ExamPaperQuestionMajor>();
  31. if (!await _rep.AnyAsync(t => t.Id == item.Id))
  32. {
  33. throw Oops.Oh(ErrorCode.E2001, "试卷大题");
  34. }
  35. await _rep.UpdateAsync(item);
  36. }
  37. /// <summary>
  38. /// 删除
  39. /// </summary>
  40. /// <param name="input"></param>
  41. /// <returns></returns>
  42. public async Task Del(BaseId input)
  43. {
  44. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001, "试卷大题");
  45. if (await _rep.Change<ExamPaperQuestionMinor>().AnyAsync(t => t.ExamPaperQuestionMajorId == input.Id))
  46. {
  47. var ms = await _rep.Change<ExamPaperQuestionMinor>().Where(t => t.ExamPaperQuestionMajorId == input.Id).ToListAsync();
  48. foreach (var m in ms)
  49. {
  50. m.ExamPaperQuestionMajorId = null;
  51. }
  52. await _rep.Change<ExamPaperQuestionMinor>().UpdateAsync(ms);
  53. }
  54. await _rep.DeleteAsync(item);
  55. }
  56. /// <summary>
  57. /// 根据试卷ID获取大题列表
  58. /// </summary>
  59. /// <param name="examPaperId"></param>
  60. /// <returns></returns>
  61. public async Task<List<ExamPaperQuestionMajorOutput>> GetListByExamPaperId(int examPaperId)
  62. {
  63. var items = await _rep.DetachedEntities.Where(t => t.ExamPaperId == examPaperId).ProjectToType<ExamPaperQuestionMajorOutput>().OrderBy(t => t.Sequence).ToListAsync();
  64. return items;
  65. }
  66. }