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 BatchInsert(List schoolClasses) { if(!schoolClasses.Any()) { return; } var selectSqls = schoolClasses.Select(t => { string sobid = "NULL"; if (t.SysOrgBranchId.HasValue) { sobid = t.SysOrgBranchId.ToString(); } string fullName = BuildFullName(t.EducationStage, t.GradeBeginYear, t.ClassNumber); string name = BuildName(t.GradeBeginYear, t.ClassNumber); string shortName = BuildShortName(t.ClassNumber); return @$" SELECT {t.Id} AS id, {t.SysOrgId} soid, {sobid} sobid, {(short)t.EducationYears} es, {t.ClassNumber} cn, '{name}' n, '{fullName}' fn, '{shortName}' sn, {t.GradeBeginYear} gby, {t.GradeEndYear} gey, {t.EducationYears} ey "; }); string selectUnionAll = string.Join("UNION ALL", selectSqls); string insertSql = @$" INSERT INTO school_class(id, sys_org_id, sys_org_branch_id, education_stage, class_number, `name`, full_name, short_name, grade_begin_year, grade_end_year, education_years, `status`, create_sys_user_id, create_time, is_deleted) SELECT id, soid, sobid, es, cn, n, fn, sn, gby, gey, ey, 1, 2, CURRENT_TIMESTAMP, 0 FROM ({selectUnionAll} ) AS SCS WHERE id NOT IN (SELECT id FROM school_class) "; await _rep.SqlNonQueryAsync(insertSql); } /// /// 获取班级信息 /// /// /// /// /// /// 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 static void SetSchoolClassName(SchoolClass sc, EducationStage educationStage, short gradeBeginYear, short classNumber) { sc.FullName = BuildFullName(educationStage, gradeBeginYear, classNumber); sc.Name = BuildName(gradeBeginYear, classNumber); sc.ShortName = BuildShortName(classNumber); } private static string BuildFullName(EducationStage educationStage, short gradeBeginYear, short classNumber) { return $"{educationStage.GetDescription()}{gradeBeginYear}级{classNumber}班"; } private static string BuildName(short gradeBeginYear, short classNumber) { return $"{gradeBeginYear}级{classNumber}班"; } private static string BuildShortName(short classNumber) { return $"{classNumber}班"; } #endregion }