SchoolClassService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 学校班级信息管理服务
  6. /// </summary>
  7. public class SchoolClassService : ISchoolClassService, ITransient
  8. {
  9. private readonly IRepository<SchoolClass> _rep;
  10. public SchoolClassService(IRepository<SchoolClass> rep)
  11. {
  12. _rep = rep;
  13. }
  14. /// <summary>
  15. /// 添加学校班级
  16. /// </summary>
  17. /// <param name="input"></param>
  18. /// <returns></returns>
  19. public async Task Add(AddSchoolClassInput input)
  20. {
  21. if (!CurrentSysUserInfo.IsSuperAdmin)
  22. {
  23. input.SysOrgId = CurrentSysUserInfo.SysOrgId;
  24. }
  25. var check = await _rep.AnyAsync(t => t.SysOrgId == input.SysOrgId && t.EducationStage == input.EducationStage && t.GradeBeginYear == input.GradeBeginYear && t.ClassNumber == input.ClassNumber);
  26. if (check)
  27. {
  28. throw Oops.Oh(ErrorCode.E2003, "在同年级中", "班级号");
  29. }
  30. var item = input.Adapt<SchoolClass>();
  31. await item.InsertAsync();
  32. }
  33. /// <summary>
  34. /// 批量添加学校班级
  35. /// </summary>
  36. /// <param name="input"></param>
  37. /// <returns></returns>
  38. public async Task BatchAdd(BatchAddSchoolClassInput input)
  39. {
  40. if (!CurrentSysUserInfo.IsSuperAdmin)
  41. {
  42. input.SysOrgId = CurrentSysUserInfo.SysOrgId;
  43. }
  44. var maxClass = await _rep.Where(t => t.SysOrgId == input.SysOrgId && t.EducationStage == input.EducationStage && t.GradeBeginYear == input.GradeBeginYear).OrderByDescending(t => t.ClassNumber).FirstOrDefaultAsync();
  45. List<SchoolClass> newClass = new();
  46. for (short i = maxClass?.ClassNumber ?? 0; i < input.ClassCount;)
  47. {
  48. i++;
  49. var nc = input.Adapt<SchoolClass>();
  50. nc.Id = GetSchoolClassId(input.EducationStage, input.SysOrgId, input.GradeBeginYear, i);
  51. nc.ClassNumber = i;
  52. SetSchoolClassName(nc, input.EducationStage, input.GradeBeginYear, i);
  53. newClass.Add(nc);
  54. }
  55. }
  56. /// <summary>
  57. /// 获取批量导入班级ID
  58. /// </summary>
  59. /// <param name="input"></param>
  60. /// <returns></returns>
  61. public async Task<Dictionary<short, long>> GetImportSchoolClassList(ImportSchoolClassInput input)
  62. {
  63. var scs = await _rep.DetachedEntities.Where(t => t.SysOrgId == input.SysOrgId && t.EducationStage == input.ExamGrade.EducationStage && t.GradeBeginYear == input.ExamGrade.GradeBeginYear).ToListAsync();
  64. // 不存在的班级
  65. var newClassNumbers = (from c in input.ClassNumberList
  66. where !(from s in scs select s.ClassNumber).Contains(c)
  67. select c).ToList();
  68. // 已存在的班级
  69. Dictionary<short, long> ret = (from s in scs
  70. where input.ClassNumberList.Contains(s.ClassNumber)
  71. select s).ToDictionary(x => x.ClassNumber, y => y.Id);
  72. ret ??= new Dictionary<short, long>();
  73. // 添加不存在的班级
  74. List<SchoolClass> newClasses = new();
  75. foreach (var c in newClassNumbers)
  76. {
  77. long id = GetSchoolClassId(input.ExamGrade.EducationStage, input.SysOrgId, input.ExamGrade.GradeBeginYear, c);
  78. ret.Add(c, id);
  79. SchoolClass newItem = new()
  80. {
  81. Id = id,
  82. ClassNumber = c,
  83. SysOrgId = input.SysOrgId,
  84. SysOrgBranchId = input.SysOrgBranchId,
  85. EducationStage = input.ExamGrade.EducationStage,
  86. EducationYears = input.ExamGrade.EducationYears,
  87. GradeBeginYear = input.ExamGrade.GradeBeginYear,
  88. GradeEndYear = input.ExamGrade.GradeEndYear,
  89. };
  90. SetSchoolClassName(newItem, input.ExamGrade.EducationStage, input.ExamGrade.GradeBeginYear, c);
  91. newClasses.Add(newItem);
  92. }
  93. if (newClasses.Any())
  94. {
  95. await _rep.InsertNowAsync(newClasses);
  96. }
  97. return ret;
  98. }
  99. /// <summary>
  100. /// 获取班级信息
  101. /// </summary>
  102. /// <param name="sysOrgId"></param>
  103. /// <param name="sysOrgBranchId"></param>
  104. /// <param name="examGrade"></param>
  105. /// <param name="classNumber"></param>
  106. /// <returns></returns>
  107. public async Task<SchoolClassLiteOutput> GetSchoolClass(short sysOrgId, short? sysOrgBranchId, ExamGradeOutput examGrade, short classNumber)
  108. {
  109. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.SysOrgId == sysOrgId && t.GradeBeginYear == examGrade.GradeBeginYear && t.ClassNumber == classNumber);
  110. if (item == null)
  111. {
  112. item = new()
  113. {
  114. Id = GetSchoolClassId(examGrade.EducationStage, sysOrgId, examGrade.GradeBeginYear, classNumber),
  115. ClassNumber = classNumber,
  116. SysOrgId = sysOrgId,
  117. SysOrgBranchId = sysOrgBranchId,
  118. EducationStage = examGrade.EducationStage,
  119. EducationYears = examGrade.EducationYears,
  120. GradeBeginYear = examGrade.GradeBeginYear,
  121. GradeEndYear = examGrade.GradeEndYear,
  122. };
  123. SetSchoolClassName(item, examGrade.EducationStage, examGrade.GradeBeginYear, classNumber);
  124. await item.InsertNowAsync();
  125. }
  126. return item.Adapt<SchoolClassLiteOutput>();
  127. }
  128. //public async Task<SchoolClassLiteOutput> GetSchoolClass(EducationStage educationStage, short sysOrgId, short? sysOrgBranchId, short gradeBeginYear, short classNumber){
  129. // var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.SysOrgId == sysOrgId && t.GradeBeginYear == gradeBeginYear && t.ClassNumber == classNumber);
  130. // if (item == null)
  131. // {
  132. // item = new()
  133. // {
  134. // Id = GetSchoolClassId(educationStage, sysOrgId, gradeBeginYear, classNumber),
  135. // ClassNumber = classNumber,
  136. // SysOrgId = sysOrgId,
  137. // SysOrgBranchId = sysOrgBranchId,
  138. // EducationStage = educationStage,
  139. // EducationYears = examGrade.EducationYears,
  140. // GradeBeginYear = examGrade.GradeBeginYear,
  141. // GradeEndYear = examGrade.GradeEndYear,
  142. // };
  143. // SetSchoolClassName(item, examGrade.EducationStage, examGrade.GradeBeginYear, classNumber);
  144. // await item.InsertAsync();
  145. // }
  146. // return item.Adapt<SchoolClassLiteOutput>();
  147. //}
  148. /// <summary>
  149. /// 获取班级ID
  150. /// </summary>
  151. /// <param name="educationStage">学段</param>
  152. /// <param name="sysOrgId">机构ID</param>
  153. /// <param name="gradeBeginYear">开始年份</param>
  154. /// <param name="classNumber">班号</param>
  155. /// <returns></returns>
  156. public long GetSchoolClassId(EducationStage educationStage, short sysOrgId, short gradeBeginYear, short classNumber)
  157. {
  158. return long.Parse($"{(short)educationStage}{sysOrgId.ToString().PadLeft(4, '0')}{gradeBeginYear.ToString().PadLeft(4, '0')}{classNumber.ToString().PadLeft(2, '0')}");
  159. }
  160. /// <summary>
  161. /// 根据ID获取学校班级信息
  162. /// </summary>
  163. /// <param name="id"></param>
  164. /// <returns></returns>
  165. public async Task<SchoolClassOutput> GetById(long id)
  166. {
  167. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(x => x.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  168. return item.Adapt<SchoolClassOutput>();
  169. }
  170. /// <summary>
  171. /// 根据级获取学校班级列表
  172. /// </summary>
  173. /// <param name="gradeBeginYear"></param>
  174. /// <returns></returns>
  175. public async Task<List<SchoolClassOutput>> GetListByGradeBeginYear(short gradeBeginYear)
  176. {
  177. if (!CurrentSysUserInfo.IsSuperAdmin && CurrentSysUserInfo.SysOrgId == 0)
  178. {
  179. return new();
  180. }
  181. var items = await _rep.DetachedEntities.Where(!CurrentSysUserInfo.IsSuperAdmin, t => t.SysOrgId == CurrentSysUserInfo.SysOrgId)
  182. .Where(gradeBeginYear != 0, t => t.GradeBeginYear == gradeBeginYear)
  183. .ProjectToType<SchoolClassOutput>().ToListAsync();
  184. return items;
  185. }
  186. /// <summary>
  187. /// 分页查询班级列表
  188. /// </summary>
  189. /// <param name="input"></param>
  190. /// <returns></returns>
  191. public async Task<PageResult<SchoolClassOutput>> QueryPageList(SchoolClassPageInput input)
  192. {
  193. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  194. var ret = await _rep.DetachedEntities.Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
  195. .Where(input.SysOrgId.HasValue, t => t.SysOrgId == input.SysOrgId)
  196. .Where(input.SysOrgBranchId.HasValue, t => t.SysOrgBranchId == input.SysOrgBranchId)
  197. .Where(input.GradeBeginYear.HasValue, t => t.GradeBeginYear == input.GradeBeginYear)
  198. .Where(input.ClassNumber.HasValue, t => t.ClassNumber == input.ClassNumber)
  199. .Where(input.Status.HasValue, t => t.Status == input.Status)
  200. .Where(!CurrentSysUserInfo.IsSuperAdmin, t => t.SysOrgId == CurrentSysUserInfo.SysOrgId)
  201. .ProjectToType<SchoolClassOutput>()
  202. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  203. return ret;
  204. }
  205. #region 私有方法
  206. /// <summary>
  207. /// 设置班级名称
  208. /// </summary>
  209. /// <param name="sc">班级实体</param>
  210. /// <param name="educationStage">学段</param>
  211. /// <param name="gradeBeginYear">开始年份</param>
  212. /// <param name="classNumber">班号</param>
  213. private void SetSchoolClassName(SchoolClass sc, EducationStage educationStage, short gradeBeginYear, short classNumber)
  214. {
  215. sc.FullName = $"{educationStage.GetDescription()}{gradeBeginYear}级{classNumber}班";
  216. sc.Name = $"{gradeBeginYear}级{classNumber}班";
  217. sc.ShortName = $"{classNumber}班";
  218. }
  219. #endregion
  220. }