diff --git a/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs b/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs index 42e12d6..bdf3343 100644 --- a/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs +++ b/YD_AllHeartRates.Api/Mqtt/MqttBackgroundService.cs @@ -84,58 +84,57 @@ namespace YD_AllHeartRates.Api.Mqtt return _queue.Writer.WriteAsync(msg, stoppingToken).AsTask(); }; - List studentList; - List devices; - - var studentListCacheKey = $"{AppSettings.StudentListCacheKey}_{AppSettings.SchoolCode}"; - var deviceListCacheKey = $"{AppSettings.DeviceListCacheKey}_{AppSettings.SchoolCode}"; - - studentList = _caching.Get>(studentListCacheKey); - devices = _caching.Get>(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(); - var jumpRopeEntities = new ConcurrentBag(); - - 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)) { + List studentList; + List devices; + + var studentListCacheKey = $"{AppSettings.StudentListCacheKey}_{AppSettings.SchoolCode}"; + var deviceListCacheKey = $"{AppSettings.DeviceListCacheKey}_{AppSettings.SchoolCode}"; + + studentList = _caching.Get>(studentListCacheKey); + devices = _caching.Get>(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(); + var jumpRopeEntities = new ConcurrentBag(); + + 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 => {