SemesterService.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 学期信息管理服务
  6. /// </summary>
  7. public class SemesterService : ISemesterService, ITransient
  8. {
  9. private readonly IRepository<Semester> _rep;
  10. public SemesterService(IRepository<Semester> rep)
  11. {
  12. _rep = rep;
  13. }
  14. /// <summary>
  15. /// 添加学期
  16. /// </summary>
  17. /// <returns></returns>
  18. public async Task Add()
  19. {
  20. var lastItem = await _rep.AsQueryable().OrderByDescending(t => t.Id).FirstOrDefaultAsync() ?? throw Oops.Oh(ErrorCode.E1007, "学期");
  21. Semester item = new();
  22. // ID规则:2023~2024学年上期:20232,2023~2024学年下期:20241
  23. if (lastItem.SemesterType == SemesterType.FIRST_SEMESTER)
  24. {
  25. item.SemesterType = SemesterType.SECOND_SEMESTER;
  26. item.BeginYear = lastItem.BeginYear;
  27. item.EndYear = lastItem.EndYear;
  28. item.Id = (short)(item.EndYear * 10 + (short)SemesterType.FIRST_SEMESTER);
  29. item.Name = $"{item.BeginYear}至{item.EndYear}学年下学期";
  30. item.ShortName = $"{item.BeginYear}~{item.EndYear}下";
  31. item.NickName = $"{item.BeginYear}至{item.EndYear}学年春季学期";
  32. item.NickShortName = $"{item.EndYear}春";
  33. }
  34. else
  35. {
  36. item.SemesterType = SemesterType.FIRST_SEMESTER;
  37. item.BeginYear = lastItem.EndYear;
  38. item.EndYear = (short)(lastItem.BeginYear + 1);
  39. item.Id = (short)(item.BeginYear * 10 + (short)SemesterType.SECOND_SEMESTER);
  40. item.Name = $"{item.BeginYear}至{item.EndYear}学年上学期";
  41. item.ShortName = $"{item.BeginYear}~{item.EndYear}上";
  42. item.NickName = $"{item.BeginYear}至{item.EndYear}学年秋季学期";
  43. item.NickShortName = $"{item.BeginYear}秋";
  44. }
  45. await item.InsertAsync();
  46. }
  47. /// <summary>
  48. /// 设置当前学期
  49. /// </summary>
  50. /// <param name="input"></param>
  51. /// <returns></returns>
  52. public async Task SetCurrent(BaseId<short> input)
  53. {
  54. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2002);
  55. item.IsCurrent = true;
  56. var oitems = await _rep.Where(t => t.Id != input.Id).ToListAsync();
  57. foreach (var oi in oitems) { oi.IsCurrent = false; }
  58. await _rep.UpdateAsync();
  59. }
  60. /// <summary>
  61. /// 根据ID获取单个学期
  62. /// </summary>
  63. /// <param name="id"></param>
  64. /// <returns></returns>
  65. public async Task<SemesterOutput> GetById(int id)
  66. {
  67. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id);
  68. return item.Adapt<SemesterOutput>();
  69. }
  70. /// <summary>
  71. /// 获取当前学期
  72. /// </summary>
  73. /// <returns></returns>
  74. public async Task<SemesterOutput> GetCurrent()
  75. {
  76. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.IsCurrent == true);
  77. return item.Adapt<SemesterOutput>();
  78. }
  79. /// <summary>
  80. /// 获取全部学期信息列表
  81. /// </summary>
  82. /// <returns></returns>
  83. public async Task<List<SemesterOutput>> GetAllList()
  84. {
  85. var items = await _rep.Where(t => t.IsDeleted == false).ProjectToType<SemesterOutput>().OrderByDescending(t => t.Id).ToListAsync();
  86. return items;
  87. }
  88. }