This commit is contained in:
tanglong 2025-07-28 15:50:24 +08:00
parent c614ee4005
commit 6d73f0cb52

View File

@ -84,58 +84,57 @@ namespace YD_AllHeartRates.Api.Mqtt
return _queue.Writer.WriteAsync(msg, stoppingToken).AsTask(); return _queue.Writer.WriteAsync(msg, stoppingToken).AsTask();
}; };
List<StudentDto> studentList;
List<S_Device> devices;
var studentListCacheKey = $"{AppSettings.StudentListCacheKey}_{AppSettings.SchoolCode}";
var deviceListCacheKey = $"{AppSettings.DeviceListCacheKey}_{AppSettings.SchoolCode}";
studentList = _caching.Get<List<StudentDto>>(studentListCacheKey);
devices = _caching.Get<List<S_Device>>(deviceListCacheKey);
if (studentList == null || devices == null)
{
studentList = (
from s in _smartSportsContext.Student
join c in _smartSportsContext.Class on s.ClassId equals c.Id
join d in _smartSportsContext.Device on s.StudentNo equals d.StudentNo into sd
from d in sd.DefaultIfEmpty()
where s.SchoolCode == AppSettings.SchoolCode && s.StudentStatus == 1
select new StudentDto
{
SchoolCode = s.SchoolCode,
StudentNo = s.StudentNo,
StudentName = s.StudentName,
Sex = s.Sex,
Age = s.Age,
HeartRateId = d != null ? d.Code : "",
JumpRopeId = d != null ? d.Code : "",
ClassId = s.ClassId,
ClassName = s.ClassName,
GradeId = c.GradeId,
GradeName = c.GradeName ?? "",
DeviceType = d != null ? d.DeviceType : 0
}
).ToList();
devices = _smartSportsContext.Device.Where(x => x.SchoolCode == AppSettings.SchoolCode && !string.IsNullOrWhiteSpace(x.StudentNo)).ToList();
_caching.AddObject(studentListCacheKey, studentList, 600);
_caching.AddObject(deviceListCacheKey, devices, 600);
}
var heartRateEntities = new ConcurrentBag<HeartRateData>();
var jumpRopeEntities = new ConcurrentBag<JumpRopeData>();
var deviceHMap = devices.Where(x => x.DeviceType == 1).ToDictionary(x => x.Code, x => x.StudentNo);
var deviceJMap = devices.Where(x => x.DeviceType == 2).ToDictionary(x => x.Code, x => x.StudentNo);
var studentMap = studentList.GroupBy(x => x.StudentNo).ToDictionary(g => g.Key, g => g.First());
await foreach (var batch in ReadBatchesAsync(stoppingToken)) await foreach (var batch in ReadBatchesAsync(stoppingToken))
{ {
List<StudentDto> studentList;
List<S_Device> devices;
var studentListCacheKey = $"{AppSettings.StudentListCacheKey}_{AppSettings.SchoolCode}";
var deviceListCacheKey = $"{AppSettings.DeviceListCacheKey}_{AppSettings.SchoolCode}";
studentList = _caching.Get<List<StudentDto>>(studentListCacheKey);
devices = _caching.Get<List<S_Device>>(deviceListCacheKey);
if (studentList == null || devices == null)
{
studentList = (
from s in _smartSportsContext.Student
join c in _smartSportsContext.Class on s.ClassId equals c.Id
join d in _smartSportsContext.Device on s.StudentNo equals d.StudentNo into sd
from d in sd.DefaultIfEmpty()
where s.SchoolCode == AppSettings.SchoolCode && s.StudentStatus == 1
select new StudentDto
{
SchoolCode = s.SchoolCode,
StudentNo = s.StudentNo,
StudentName = s.StudentName,
Sex = s.Sex,
Age = s.Age,
HeartRateId = d != null ? d.Code : "",
JumpRopeId = d != null ? d.Code : "",
ClassId = s.ClassId,
ClassName = s.ClassName,
GradeId = c.GradeId,
GradeName = c.GradeName ?? "",
DeviceType = d != null ? d.DeviceType : 0
}
).ToList();
devices = _smartSportsContext.Device.Where(x => x.SchoolCode == AppSettings.SchoolCode && !string.IsNullOrWhiteSpace(x.StudentNo)).ToList();
_caching.AddObject(studentListCacheKey, studentList, 600);
_caching.AddObject(deviceListCacheKey, devices, 600);
}
var heartRateEntities = new ConcurrentBag<HeartRateData>();
var jumpRopeEntities = new ConcurrentBag<JumpRopeData>();
var deviceHMap = devices.Where(x => x.DeviceType == 1).ToDictionary(x => x.Code, x => x.StudentNo);
var deviceJMap = devices.Where(x => x.DeviceType == 2).ToDictionary(x => x.Code, x => x.StudentNo);
var studentMap = studentList.GroupBy(x => x.StudentNo).ToDictionary(g => g.Key, g => g.First());
Parallel.ForEach(batch, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, msg => Parallel.ForEach(batch, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, msg =>
{ {