This commit is contained in:
tanglong 2025-07-15 10:59:06 +08:00
parent def023cff0
commit 7974cd115e
3 changed files with 35 additions and 3 deletions

View File

@ -82,6 +82,24 @@ namespace YD_AllHeartRates.Api.Services.Impl
return res; return res;
} }
/// <summary>
/// 根据班级Id获取学生列表
/// </summary>
/// <param name="classId"></param>
/// <returns></returns>
public async Task<List<StudentDto>> StudentList(int classId)
{
var res = await _sportsContext.Student.Where(x => x.SchoolCode == schoolCode && x.ClassId == classId).Select(x => new StudentDto
{
StudentNo = x.StudentNo,
StudentName = x.StudentName,
Photo = x.Photo ?? "",
Sex = x.Sex
}).ToListAsync();
return res;
}
/// <summary> /// <summary>
/// 心率数据 /// 心率数据
/// </summary> /// </summary>
@ -105,14 +123,20 @@ namespace YD_AllHeartRates.Api.Services.Impl
string studentListKey = $"students:{schoolCode}:{classId}"; string studentListKey = $"students:{schoolCode}:{classId}";
// 2. 尝试从缓存获取 // 2. 尝试从缓存获取
var studentList = _caching.Get<List<S_Student>>(studentListKey); var studentList = _caching.Get<List<StudentDto>>(studentListKey);
// 3. 如果缓存没有 → 查询数据库 + 写入缓存 // 3. 如果缓存没有 → 查询数据库 + 写入缓存
if (studentList == null) if (studentList == null)
{ {
studentList = await _sportsContext.Student studentList = await _sportsContext.Student
.Where(x => x.ClassId == classId && x.SchoolCode == schoolCode && x.StudentStatus == 1) .Where(x => x.ClassId == classId && x.SchoolCode == schoolCode && x.StudentStatus == 1)
.ToListAsync(); .Select(x => new StudentDto
{
StudentNo = x.StudentNo,
StudentName = x.StudentName,
Sex = x.Sex,
Photo = x.Photo ?? ""
}).ToListAsync();
_caching.AddObject(studentListKey, studentList, 28800); // 缓存 8 小时 _caching.AddObject(studentListKey, studentList, 28800); // 缓存 8 小时
} }
@ -196,6 +220,5 @@ namespace YD_AllHeartRates.Api.Services.Impl
return res; return res;
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using YD_AllHeartRates.Commons.Dto.HeartRateRepor;
using YD_AllHeartRates.Commons.Dto.LargeScreen; using YD_AllHeartRates.Commons.Dto.LargeScreen;
namespace YD_AllHeartRates.Api.Services.Interface namespace YD_AllHeartRates.Api.Services.Interface
@ -14,6 +15,13 @@ namespace YD_AllHeartRates.Api.Services.Interface
/// <returns></returns> /// <returns></returns>
Task<SchoolDto> SchoolInfo(); Task<SchoolDto> SchoolInfo();
/// <summary>
/// 根据班级获取学生
/// </summary>
/// <param name="classId"></param>
/// <returns></returns>
Task<List<StudentDto>> StudentList(int classId);
/// <summary> /// <summary>
/// 心率数据 /// 心率数据
/// </summary> /// </summary>

View File

@ -64,4 +64,5 @@ namespace YD_AllHeartRates.Commons.Dto.LargeScreen
/// </summary> /// </summary>
public string Name { get; set; } public string Name { get; set; }
} }
} }