using Furion.DatabaseAccessor.Extensions; using YBEE.EQM.Core; namespace YBEE.EQM.Application; /// /// 学校班级信息管理服务 /// public class SchoolClassService : ISchoolClassService, ITransient { private readonly IRepository _rep; public SchoolClassService(IRepository rep) { _rep = rep; } /// /// 添加学校班级 /// /// /// public async Task Add(AddSchoolClassInput input) { if (!CurrentSysUserInfo.IsSuperAdmin) { input.SysOrgId = CurrentSysUserInfo.SysOrgId; } var check = await _rep.AnyAsync(t => t.SysOrgId == input.SysOrgId && t.EducationStage == input.EducationStage && t.GradeBeginYear == input.GradeBeginYear && t.ClassNumber == input.ClassNumber); if (check) { throw Oops.Oh(ErrorCode.E2003, "在同年级中", "班级号"); } var item = input.Adapt(); await item.InsertAsync(); } /// /// 批量添加学校班级 /// /// /// public async Task BatchAdd(BatchAddSchoolClassInput input) { if (!CurrentSysUserInfo.IsSuperAdmin) { input.SysOrgId = CurrentSysUserInfo.SysOrgId; } var maxClass = await _rep.Where(t => t.SysOrgId == input.SysOrgId && t.EducationStage == input.EducationStage && t.GradeBeginYear == input.GradeBeginYear).OrderByDescending(t => t.ClassNumber).FirstOrDefaultAsync(); List newClass = new(); for (short i = maxClass?.ClassNumber ?? 0; i < input.ClassCount;) { i++; var nc = input.Adapt(); nc.Id = GetSchoolClassId(input.EducationStage, input.SysOrgId, input.GradeBeginYear, i); nc.ClassNumber = i; SetSchoolClassName(nc, input.EducationStage, input.GradeBeginYear, i); newClass.Add(nc); } } /// /// 获取批量导入班级ID /// /// /// public async Task> GetImportSchoolClassList(ImportSchoolClassInput input) { var scs = await _rep.DetachedEntities.Where(t => t.SysOrgId == input.SysOrgId && t.EducationStage == input.ExamGrade.EducationStage && t.GradeBeginYear == input.ExamGrade.GradeBeginYear).ToListAsync(); // 不存在的班级 var newClassNumbers = (from c in input.ClassNumberList where !(from s in scs select s.ClassNumber).Contains(c) select c).ToList(); // 已存在的班级 Dictionary ret = (from s in scs where input.ClassNumberList.Contains(s.ClassNumber) select s).ToDictionary(x => x.ClassNumber, y => y.Id); ret ??= new Dictionary(); // 添加不存在的班级 List newClasses = new(); foreach (var c in newClassNumbers) { long id = GetSchoolClassId(input.ExamGrade.EducationStage, input.SysOrgId, input.ExamGrade.GradeBeginYear, c); ret.Add(c, id); SchoolClass newItem = new() { Id = id, ClassNumber = c, SysOrgId = input.SysOrgId, SysOrgBranchId = input.SysOrgBranchId, EducationStage = input.ExamGrade.EducationStage, EducationYears = input.ExamGrade.EducationYears, GradeBeginYear = input.ExamGrade.GradeBeginYear, GradeEndYear = input.ExamGrade.GradeEndYear, }; SetSchoolClassName(newItem, input.ExamGrade.EducationStage, input.ExamGrade.GradeBeginYear, c); newClasses.Add(newItem); } if (newClasses.Any()) { await _rep.InsertNowAsync(newClasses); } return ret; } /// /// 获取班级信息 /// /// /// /// /// /// public async Task GetSchoolClass(short sysOrgId, short? sysOrgBranchId, ExamGradeOutput examGrade, short classNumber) { var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.SysOrgId == sysOrgId && t.GradeBeginYear == examGrade.GradeBeginYear && t.ClassNumber == classNumber); if (item == null) { item = new() { Id = GetSchoolClassId(examGrade.EducationStage, sysOrgId, examGrade.GradeBeginYear, classNumber), ClassNumber = classNumber, SysOrgId = sysOrgId, SysOrgBranchId = sysOrgBranchId, EducationStage = examGrade.EducationStage, EducationYears = examGrade.EducationYears, GradeBeginYear = examGrade.GradeBeginYear, GradeEndYear = examGrade.GradeEndYear, }; SetSchoolClassName(item, examGrade.EducationStage, examGrade.GradeBeginYear, classNumber); await item.InsertNowAsync(); } return item.Adapt(); } //public async Task GetSchoolClass(EducationStage educationStage, short sysOrgId, short? sysOrgBranchId, short gradeBeginYear, short classNumber){ // var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.SysOrgId == sysOrgId && t.GradeBeginYear == gradeBeginYear && t.ClassNumber == classNumber); // if (item == null) // { // item = new() // { // Id = GetSchoolClassId(educationStage, sysOrgId, gradeBeginYear, classNumber), // ClassNumber = classNumber, // SysOrgId = sysOrgId, // SysOrgBranchId = sysOrgBranchId, // EducationStage = educationStage, // EducationYears = examGrade.EducationYears, // GradeBeginYear = examGrade.GradeBeginYear, // GradeEndYear = examGrade.GradeEndYear, // }; // SetSchoolClassName(item, examGrade.EducationStage, examGrade.GradeBeginYear, classNumber); // await item.InsertAsync(); // } // return item.Adapt(); //} /// /// 获取班级ID /// /// 学段 /// 机构ID /// 开始年份 /// 班号 /// public long GetSchoolClassId(EducationStage educationStage, short sysOrgId, short gradeBeginYear, short classNumber) { return long.Parse($"{(short)educationStage}{sysOrgId.ToString().PadLeft(4, '0')}{gradeBeginYear.ToString().PadLeft(4, '0')}{classNumber.ToString().PadLeft(2, '0')}"); } /// /// 根据ID获取学校班级信息 /// /// /// public async Task GetById(long id) { var item = await _rep.DetachedEntities.FirstOrDefaultAsync(x => x.Id == id) ?? throw Oops.Oh(ErrorCode.E2001); return item.Adapt(); } /// /// 根据级获取学校班级列表 /// /// /// public async Task> GetListByGradeBeginYear(short gradeBeginYear) { if (!CurrentSysUserInfo.IsSuperAdmin && CurrentSysUserInfo.SysOrgId == 0) { return new(); } var items = await _rep.DetachedEntities.Where(!CurrentSysUserInfo.IsSuperAdmin, t => t.SysOrgId == CurrentSysUserInfo.SysOrgId) .Where(gradeBeginYear != 0, t => t.GradeBeginYear == gradeBeginYear) .ProjectToType().ToListAsync(); return items; } /// /// 分页查询班级列表 /// /// /// public async Task> QueryPageList(SchoolClassPageInput input) { var name = !string.IsNullOrEmpty(input.Name?.Trim()); var ret = await _rep.DetachedEntities.Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%"))) .Where(input.SysOrgId.HasValue, t => t.SysOrgId == input.SysOrgId) .Where(input.SysOrgBranchId.HasValue, t => t.SysOrgBranchId == input.SysOrgBranchId) .Where(input.GradeBeginYear.HasValue, t => t.GradeBeginYear == input.GradeBeginYear) .Where(input.ClassNumber.HasValue, t => t.ClassNumber == input.ClassNumber) .Where(input.Status.HasValue, t => t.Status == input.Status) .Where(!CurrentSysUserInfo.IsSuperAdmin, t => t.SysOrgId == CurrentSysUserInfo.SysOrgId) .ProjectToType() .ToADPagedListAsync(input.PageIndex, input.PageSize); return ret; } #region 私有方法 /// /// 设置班级名称 /// /// 班级实体 /// 学段 /// 开始年份 /// 班号 private void SetSchoolClassName(SchoolClass sc, EducationStage educationStage, short gradeBeginYear, short classNumber) { sc.FullName = $"{educationStage.GetDescription()}{gradeBeginYear}级{classNumber}班"; sc.Name = $"{gradeBeginYear}级{classNumber}班"; sc.ShortName = $"{classNumber}班"; } #endregion }