175 lines
7.6 KiB
C#
175 lines
7.6 KiB
C#
using AutoMapper;
|
||
using AutoMapper.Internal;
|
||
using Castle.DynamicProxy.Generators;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using VOL.Business.IServices;
|
||
using VOL.Business.IServices.Norm;
|
||
using VOL.Core.CacheManager;
|
||
using VOL.Core.Extensions.AutofacManager;
|
||
using VOL.Core.ManageUser;
|
||
using VOL.Entity.DomainModels;
|
||
using VOL.Entity.DomainModels.XinWei;
|
||
using VOL.Entity.Enum;
|
||
using VOL.Model.School.Response;
|
||
using VOL.System.IRepositories;
|
||
using YD_XinWei.Api.SmartSportsEntitys;
|
||
|
||
namespace VOL.Business.Services.Norm
|
||
{
|
||
public class CacheQueryService : ICacheQueryService, IDependency
|
||
{
|
||
#region 初始化
|
||
private readonly ICacheService _cacheService;
|
||
private readonly IMapper _mapper;
|
||
private readonly IN_SportsTestResultRepository _sportsTestResultRepository;
|
||
|
||
[ActivatorUtilitiesConstructor]
|
||
public CacheQueryService(IMapper mapper,
|
||
IN_SportsTestResultRepository sportsTestResultRepository,
|
||
ICacheService cacheService)
|
||
{
|
||
_mapper = mapper;
|
||
_sportsTestResultRepository = sportsTestResultRepository;
|
||
_cacheService = cacheService;
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 获取体测成绩缓存
|
||
/// </summary>
|
||
/// <param name="queryCondition">查询条件</param>
|
||
/// <param name="modeType">模式</param>
|
||
/// <returns></returns>
|
||
public async Task<List<SportsTestValueModel>> GeSportsTestDataCacheAsync(Func<SportsTestValueModel, bool> queryCondition, Ai_ModeEnum? modeType)
|
||
{
|
||
var tenantId = UserContext.Current.TenantId;
|
||
var cacheKey = $"sportsTestResults_{tenantId}";
|
||
|
||
// 从缓存中获取数据
|
||
var sportsTestResults = _cacheService.Get<List<SportsTestValueModel>>(cacheKey);
|
||
|
||
if (sportsTestResults == null)
|
||
{
|
||
var iotTestResults = await _sportsTestResultRepository
|
||
.FindAsIQueryable(x => x.IsDisplay && x.SchoolCode == tenantId)
|
||
.ToListAsync();
|
||
var sportsIot = _mapper.Map<List<SportsTestValueModel>>(iotTestResults);
|
||
|
||
var aiTestResults = await _sportsTestResultRepository.DbContext.Set<Ai_SportsTestData>
|
||
().Where(x => x.IsDisplay && x.SchoolCode == tenantId && x.DataType == SportsTestDataType.SportsTestData)
|
||
.ToListAsync();
|
||
var sportsAi = _mapper.Map<List<SportsTestValueModel>>(aiTestResults);
|
||
|
||
//int schoolId = await _sportsTestResultRepository.DbContext.Set<S_School>().Where(x => x.SchoolCode == tenantId).Select(x => x.Id).FirstOrDefaultAsync();
|
||
//var xwTestData = await _sportsTestResultRepository.DbContext.Set<XW_SportsTestData>().Where(x => x.OrgId == schoolId).Include(x => x.StudentList).ToListAsync();
|
||
//var xwProject = await _sportsTestResultRepository.DbContext.Set<XW_TestingProject>().ToListAsync();
|
||
//var sportsXW = new List<SportsTestValueModel>();
|
||
//foreach (var data in xwTestData)
|
||
//{
|
||
// foreach (var stu in data.StudentList)
|
||
// {
|
||
// var category = xwProject.Where(x => x.ProjectId == data.ProjectId).FirstOrDefault();
|
||
|
||
// if (data.ProjectId == 2)
|
||
// {
|
||
// if (stu.Sex == 2)
|
||
// {
|
||
// category.CategoryValue = 11;
|
||
// category.CategoryEnum = "MeterRun_800";
|
||
// }
|
||
// }
|
||
|
||
// var xwModel = new SportsTestValueModel()
|
||
// {
|
||
// ClassId = Convert.ToInt32(data.ClassId),
|
||
// ClassName = data.ClassName,
|
||
// GradeId = Convert.ToInt32(data.GradeId),
|
||
// GradeName = data.GradeName,
|
||
// DataSource = DataSource.XW,
|
||
// ScoreTime = Convert.ToDateTime(data.CreateTime),
|
||
// CategoryValue = category.CategoryValue,
|
||
// CategoryEnum = category.CategoryEnum,
|
||
// SchoolCode = tenantId,
|
||
// Score = (float)(stu.Score ?? 0f),
|
||
// Sex = (SexType)stu.Sex,
|
||
// Value = (float)stu.Achievement,
|
||
// StudentNo = stu.StudentNo,
|
||
// TeacherId = stu.TeacherId ?? 0,
|
||
// AdditionalScore = (float)(stu.PlusesScore ?? 0f)
|
||
// };
|
||
|
||
// sportsXW.Add(xwModel);
|
||
// }
|
||
//}
|
||
|
||
//sportsTestResults = sportsIot.Concat(sportsAi).Concat(sportsXW).ToList();
|
||
|
||
sportsTestResults = sportsIot.Concat(sportsAi).ToList();
|
||
|
||
sportsTestResults = sportsTestResults
|
||
.GroupBy(x => new { x.StudentNo, x.CategoryValue })
|
||
.Select(g =>
|
||
{
|
||
var latestRecord = g.OrderByDescending(x => x.ScoreTime).First();
|
||
|
||
latestRecord.Score = g.Average(x => x.Score);
|
||
latestRecord.Value = g.Average(x => x.Value);
|
||
latestRecord.MotionDuration = g.Sum(x => x.MotionDuration);
|
||
|
||
//添加其他需要聚合运算的字段
|
||
|
||
return latestRecord;
|
||
})
|
||
.ToList();
|
||
|
||
// 将结果添加到缓存,缓存时间为600秒
|
||
_cacheService.AddObject(cacheKey, sportsTestResults, 3600);
|
||
}
|
||
|
||
sportsTestResults = sportsTestResults.Where(x => queryCondition(x)).ToList();
|
||
|
||
// 自由模式 体测项目
|
||
if (modeType != null && modeType == Ai_ModeEnum.FreeMode)
|
||
{
|
||
var freeModeItemTypeList = new List<int>()
|
||
{
|
||
(int)SportsTestItemType.Sit_And_Reach,
|
||
(int)SportsTestItemType.OneMinuteJumpRope,
|
||
(int)SportsTestItemType.One_Minute_Sit_Up,
|
||
(int)SportsTestItemType.Pull_Up,
|
||
};
|
||
sportsTestResults = sportsTestResults.Where(x => freeModeItemTypeList.Contains(x.CategoryValue)).ToList();
|
||
}
|
||
/*
|
||
// 课堂模式 体测项目
|
||
else if (modeType != null && modeType == Ai_ModeEnum.ClassRoomMode)
|
||
{
|
||
var classRoomModeItemTypeList = new List<int>()
|
||
{
|
||
(int)SportsTestItemType.BMI,
|
||
(int)SportsTestItemType.VitalCapacity,
|
||
(int)SportsTestItemType.MeterRun_50,
|
||
(int)SportsTestItemType.ShuttleRun_50x8,
|
||
(int)SportsTestItemType.MeterRun_1000,
|
||
(int)SportsTestItemType.MeterRun_800,
|
||
// 立定跳远
|
||
(int)SportsTestItemType.StandingLongJump,
|
||
// AI 和 IOT 共有项目
|
||
(int)SportsTestItemType.Sit_And_Reach,
|
||
(int)SportsTestItemType.OneMinuteJumpRope,
|
||
(int)SportsTestItemType.One_Minute_Sit_Up,
|
||
(int)SportsTestItemType.Pull_Up,
|
||
};
|
||
}
|
||
*/
|
||
return sportsTestResults;
|
||
}
|
||
}
|
||
}
|