YD_SmartSports.Api/VOL.Business/Services/Training/I_TrainingStudentDataService.cs
2025-06-06 16:00:39 +08:00

537 lines
24 KiB
C#

using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tea.Utils;
using VOL.Business.IRepositories;
using VOL.Business.IServices.Training;
using VOL.Business.Repositories;
using VOL.Core.CacheManager;
using VOL.Core.Extensions;
using VOL.Core.Extensions.AutofacManager;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;
using VOL.Entity.Enum;
using VOL.Model;
using VOL.System.Repositories;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace VOL.Business.Services.Training
{
public class I_TrainingStudentDataService : II_TrainingStudentDataService, IDependency
{
#region
private readonly IMapper _mapper;
private readonly ICacheService _cacheService;
private readonly S_TeacherRepository _teacherRepository;
[ActivatorUtilitiesConstructor]
public I_TrainingStudentDataService(IMapper mapper,
ICacheService cacheService,
S_TeacherRepository teacherRepository)
{
_mapper = mapper;
_cacheService = cacheService;
_teacherRepository = teacherRepository;
}
#endregion
/// <summary>
/// 获取达标率
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<List<AttainmentRateModel>> GetAttainmentRate(TrainingDataParam paramDto)
{
//var query = _teacherRepository
// .FindAsIQueryable(x => x.TrainingData.ItemType.Equals(paramDto.ItemType) && x.TrainingData.SchoolCode.Equals(UserContext.Current.TenantId));
//if (paramDto.DataSource.HasValue)
//{
// query = query.Where(x => x.TrainingData.DataSource.Equals(paramDto.DataSource));
//}
//if (paramDto.StartTime.HasValue)
//{
// query = query.Where(x => x.TrainingData.InsertTime != null && x.TrainingData.InsertTime >= paramDto.StartTime);
//}
//if (paramDto.EndTime.HasValue)
//{
// query = query.Where(x => x.TrainingData.InsertTime != null && x.TrainingData.InsertTime <= paramDto.EndTime);
//}
//var list = await query
// .Where(x => x.ResultLevel.HasValue)
// .GroupBy(x => x.TrainingData.ItemType)
// .Select(x => new AttainmentRateModel
// {
// AttainmentType = AttainmentRateType.Total.ToSafeInt() ?? 0,
// TotalNumber = x.Count(),
// AttainmentNumber = x.Where(c=>
// (
// c.TrainingData.ItemType == TrainingItemType.HeartRate.ToSafeInt() &&
// c.ResultLevel != AchievementRank.Fine.ToSafeInt()
// ) ||
// (
// c.TrainingData.ItemType != TrainingItemType.HeartRate.ToSafeInt() &&
// c.ResultLevel != AchievementRank.Fail.ToSafeInt()
// )
// ).Count()
// }).ToListAsync();
//return list;
return null;
}
/// <summary>
/// 分数比例
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<List<ScoreProportionModel>> GetScoreProportion(TrainingDataParam paramDto)
{
//var query = _teacherRepository
// .FindAsIQueryable(x => x.TrainingData.ItemType.Equals(paramDto.ItemType) && x.TrainingData.SchoolCode.Equals(UserContext.Current.TenantId));
//if (paramDto.DataSource.HasValue)
//{
// query = query.Where(x => x.TrainingData.DataSource.Equals(paramDto.DataSource));
//}
//if (paramDto.StartTime.HasValue)
//{
// query = query.Where(x => x.TrainingData.InsertTime != null && x.TrainingData.InsertTime >= paramDto.StartTime);
//}
//if (paramDto.EndTime.HasValue)
//{
// query = query.Where(x => x.TrainingData.InsertTime != null && x.TrainingData.InsertTime <= paramDto.EndTime);
//}
//var list = await query
// .Where(x => x.ResultLevel.HasValue)
// .GroupBy(x => x.ResultLevel)
// .Select(x => new ScoreProportionModel
// {
// ScoreLevel = x.Key ?? 0,
// TotalNumber = x.Count(),
// MaleNumber = x.Where(c => c.Gender == SexType.Male.ToSafeInt()).Count(),
// FemaleNumber = x.Where(c => c.Gender == SexType.Female.ToSafeInt()).Count()
// }).ToListAsync();
//return list;
return null;
}
/// <summary>
/// 训练学生详情列表
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<List<ItemTrainingStudentDataListModel>> GetItemTrainingStudentDataList(ItemTrainingStudentDataListParam paramDto)
{
var query = from tsd in _teacherRepository.DbContext.Set<I_TrainingStudentData>()
join s in _teacherRepository.DbContext.Set<S_Student>() on tsd.StudentNo equals s.StudentNo into students
from student in students.DefaultIfEmpty()
where tsd.TrainingData.Id.Equals(paramDto.TrainingId)
where tsd.TrainingData.SchoolCode.Equals(UserContext.Current.TenantId)
orderby tsd.SpeedTime
select new ItemTrainingStudentDataListModel()
{
StudentId = student.Id,
StudentDataId = tsd.Id,
StudentNo = tsd.StudentNo,
StudentName = tsd.StudentName,
Gender = tsd.Gender,
SecondsNumber = tsd.SecondsNumber,
SpeedTime = tsd.SpeedTime,
Kcal = tsd.Kcal,
Efficiency = tsd.Efficiency,
ResultLevel = tsd.ResultLevel,
JumpValue = tsd.JumpValue,
ErrorNumber = tsd.ErrorNumber,
StuAgileSpeedTimeNumber = tsd.StuAgileSpeedTimeNumber,
HeartRatePercentage = tsd.HeartRatePercentage,
HighHeartRate = tsd.HighHeartRate,
LowHeartRate = tsd.LowHeartRate,
ModelName = tsd.TrainingData.ModelName
};
if (paramDto.Gender.HasValue)
{
query = query.Where(x => x.Gender.Equals(paramDto.Gender));
}
if (paramDto.ResultLevel.HasValue)
{
query = query.Where(x => x.ResultLevel.Equals(paramDto.ResultLevel));
}
if (!paramDto.StudentName.IsNullOrEmpty())
{
query = query.Where(x => x.StudentName.Contains(paramDto.StudentName));
}
if (!paramDto.StudentNo.IsNullOrEmpty())
{
query = query.Where(x => x.StudentNo.Contains(paramDto.StudentNo));
}
var trainingStudentDataList = await query.ToListAsync();
var rankedData = trainingStudentDataList
.Select((data, index) => new ItemTrainingStudentDataListModel
{
StudentId = data.StudentId,
StudentDataId = data.StudentDataId,
StudentNo = data.StudentNo,
StudentName = data.StudentName,
Gender = data.Gender,
SecondsNumber = data.SecondsNumber,
SpeedTime = data.SpeedTime,
Kcal = data.Kcal,
Efficiency = data.Efficiency,
ResultLevel = data.ResultLevel,
JumpValue = data.JumpValue,
ErrorNumber = data.ErrorNumber,
StuAgileSpeedTimeNumber = data.StuAgileSpeedTimeNumber,
HeartRatePercentage = data.HeartRatePercentage,
HighHeartRate = data.HighHeartRate,
LowHeartRate = data.LowHeartRate,
ModelName = data.ModelName,
Ranking = index + 1
});
var list = await query
.OrderBy(c => c.StudentDataId)
.Skip((paramDto.PageIndex - 1) * paramDto.PageSize)
.Take(paramDto.PageSize)
.ToListAsync();
return list;
}
/// <summary>
/// 训练学生详情
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<TrainingStudentDataModel> GetTrainingStudentData(TrainingStudentDataParam paramDto)
{
var query = from tsd in _teacherRepository.DbContext.Set<I_TrainingStudentData>()
join s in _teacherRepository.DbContext.Set<S_Student>() on tsd.StudentNo equals s.StudentNo into students
from student in students.DefaultIfEmpty()
join g in _teacherRepository.DbContext.Set<S_Grade>() on tsd.TrainingData.GradeId equals g.Id into grades
from grade in grades.DefaultIfEmpty()
join sc in _teacherRepository.DbContext.Set<S_Class>() on tsd.TrainingData.ClassId equals sc.Id into sclasss
from sclass in sclasss.DefaultIfEmpty()
where student.Id.Equals(paramDto.StudentId)
where tsd.TrainingData.ItemType.Equals(paramDto.ItemType)
where tsd.TrainingData.SchoolCode.Equals(UserContext.Current.TenantId)
select new TrainingStudentDataModel()
{
StudentDataId = tsd.Id,
ClassId = tsd.TrainingData.ClassId,
ClassName = sclass.ClassName,
GradeId = tsd.TrainingData.GradeId,
GradeName = grade.GradeName,
GradeCode = grade.SchoolCode,
StudentId = student.Id,
StudentNo = tsd.StudentNo,
StudentName = tsd.StudentName,
StudentBirthday = student.Birthday,
StudentGender = tsd.Gender,
StudentAge = student.Age,
DataSource = tsd.TrainingData.DataSource,
StartTime = tsd.TrainingData.InsertTime,
EndTime = tsd.TrainingData.EndTime,
SecondsNumber = tsd.SecondsNumber,
SpeedTime = tsd.SpeedTime,
Kcal = tsd.Kcal,
Efficiency = tsd.Efficiency
};
if (paramDto.DataSource.HasValue)
{
query = query.Where(x => x.DataSource.Equals(paramDto.DataSource));
}
if (paramDto.StartTime.HasValue)
{
query = query.Where(x => x.StartTime != null && x.StartTime >= paramDto.StartTime);
}
if (paramDto.EndTime.HasValue)
{
query = query.Where(x => x.StartTime != null && x.StartTime <= paramDto.EndTime);
}
var modelList = await query.ToListAsync();
var model = modelList.FirstOrDefault();
if (model != null)
{
model.TotalTrainNumber = modelList.Count;
// 跳绳
if (paramDto.ItemType == TrainingItemType.Coordinate.ToSafeInt())
{
model.TotalScoreRate = modelList.Sum(c => c.Kcal);
}
// 敏捷
else if (paramDto.ItemType == TrainingItemType.Agility.ToSafeInt())
{
model.TotalScoreRate = modelList.Sum(c => c.SpeedTime);
}
// 力量
else if (paramDto.ItemType == TrainingItemType.Strength.ToSafeInt())
{
model.TotalScoreRate = modelList.Sum(c => c.SpeedTime);
}
// 平衡
else if (paramDto.ItemType == TrainingItemType.Balance.ToSafeInt())
{
model.TotalScoreRate = modelList.Sum(c => c.SpeedTime);
}
// 速度
else if (paramDto.ItemType == TrainingItemType.Speed.ToSafeInt())
{
model.TotalScoreRate = modelList.Sum(c => c.SpeedTime);
}
// 心率
else if (paramDto.ItemType == TrainingItemType.HeartRate.ToSafeInt())
{
model.TotalScoreRate = modelList.Sum(c => c.Kcal);
}
}
return model;
}
/// <summary>
/// 项目训练学生详情列表
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<List<TrainingStudentDataListModel>> GetTrainingStudentDataList(TrainingStudentDataListParam paramDto)
{
var query = from tsd in _teacherRepository.DbContext.Set<I_TrainingStudentData>()
join s in _teacherRepository.DbContext.Set<S_Student>() on tsd.StudentNo equals s.StudentNo into students
from student in students.DefaultIfEmpty()
join st in _teacherRepository.DbContext.Set<S_Teacher>() on tsd.TrainingData.TeacherId equals st.Id into teachers
from teacher in teachers.DefaultIfEmpty()
where student.Id.Equals(paramDto.StudentId)
where tsd.TrainingData.ItemType.Equals(paramDto.ItemType)
where tsd.TrainingData.SchoolCode.Equals(UserContext.Current.TenantId)
orderby tsd.TrainingData.InsertTime
select new TrainingStudentDataListModel()
{
StartTime = tsd.TrainingData.InsertTime,
EndTime = tsd.TrainingData.EndTime,
StudentId = student.Id,
TeacherName = teacher.TeacherName,
DataSource = tsd.TrainingData.DataSource,
ModelName = tsd.TrainingData.ModelName,
StudentDataId = tsd.Id,
StudentNo = tsd.StudentNo,
StudentName = tsd.StudentName,
Gender = tsd.Gender,
SecondsNumber = tsd.SecondsNumber,
SpeedTime = tsd.SpeedTime,
Kcal = tsd.Kcal,
Efficiency = tsd.Efficiency,
ResultLevel = tsd.ResultLevel,
JumpValue = tsd.JumpValue,
ErrorNumber = tsd.ErrorNumber,
StuAgileSpeedTimeNumber = tsd.StuAgileSpeedTimeNumber,
HeartRatePercentage = tsd.HeartRatePercentage,
HighHeartRate = tsd.HighHeartRate,
LowHeartRate = tsd.LowHeartRate,
ItemType = tsd.TrainingData.ItemType,
LimitNumber = tsd.TrainingData.LimitNumber,
LimitTime = tsd.TrainingData.LimitTime
};
if (paramDto.DataSource.HasValue)
{
query = query.Where(x => x.DataSource.Equals(paramDto.DataSource));
}
if (paramDto.StartTime.HasValue)
{
query = query.Where(x => x.StartTime != null && x.StartTime >= paramDto.StartTime);
}
if (paramDto.EndTime.HasValue)
{
query = query.Where(x => x.StartTime != null && x.StartTime <= paramDto.EndTime);
}
if (!paramDto.TeacherName.IsNullOrEmpty())
{
query = query.Where(x => x.TeacherName.Equals(paramDto.TeacherName));
}
if (paramDto.ResultLevel.HasValue)
{
query = query.Where(x => x.ResultLevel.Equals(paramDto.ResultLevel));
}
var list = await query
.Skip((paramDto.PageIndex - 1) * paramDto.PageSize)
.Take(paramDto.PageSize)
.ToListAsync();
return list;
}
/// <summary>
/// 训练学生趋势列表
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<List<TrainingStudentDataTrendModel>> GetTrainingStudentDataTrendList(TrainingStudentDataTrendListParam paramDto)
{
var dbQuery = from tsd in _teacherRepository.DbContext.Set<I_TrainingStudentData>()
join s in _teacherRepository.DbContext.Set<S_Student>() on tsd.StudentNo equals s.StudentNo into students
from student in students.DefaultIfEmpty()
join st in _teacherRepository.DbContext.Set<S_Teacher>() on tsd.TrainingData.TeacherId equals st.Id into teachers
from teacher in teachers.DefaultIfEmpty()
where student.Id.Equals(paramDto.StudentId)
where tsd.TrainingData.ItemType.Equals(paramDto.ItemType)
where tsd.TrainingData.SchoolCode.Equals(UserContext.Current.TenantId)
orderby tsd.TrainingData.InsertTime
select new TrainingStudentDataListModel()
{
StartTime = tsd.TrainingData.InsertTime,
EndTime = tsd.TrainingData.EndTime,
StudentId = student.Id,
TeacherName = teacher.TeacherName,
DataSource = tsd.TrainingData.DataSource,
ModelName = tsd.TrainingData.ModelName,
StudentDataId = tsd.Id,
StudentNo = tsd.StudentNo,
StudentName = tsd.StudentName,
Gender = tsd.Gender,
SecondsNumber = tsd.SecondsNumber,
SpeedTime = tsd.SpeedTime,
Kcal = tsd.Kcal,
Efficiency = tsd.Efficiency,
ResultLevel = tsd.ResultLevel,
JumpValue = tsd.JumpValue,
ErrorNumber = tsd.ErrorNumber,
StuAgileSpeedTimeNumber = tsd.StuAgileSpeedTimeNumber,
HeartRatePercentage = tsd.HeartRatePercentage,
HighHeartRate = tsd.HighHeartRate,
LowHeartRate = tsd.LowHeartRate,
ItemType = tsd.TrainingData.ItemType,
LimitNumber = tsd.TrainingData.LimitNumber,
LimitTime = tsd.TrainingData.LimitTime
};
if (paramDto.DataSource.HasValue)
{
dbQuery = dbQuery.Where(x => x.DataSource.Equals(paramDto.DataSource));
}
if (paramDto.StartTime.HasValue)
{
dbQuery = dbQuery.Where(x => x.StartTime != null && x.StartTime >= paramDto.StartTime);
}
if (paramDto.EndTime.HasValue)
{
dbQuery = dbQuery.Where(x => x.StartTime != null && x.StartTime <= paramDto.EndTime);
}
if (!paramDto.TeacherName.IsNullOrEmpty())
{
dbQuery = dbQuery.Where(x => x.TeacherName.Equals(paramDto.TeacherName));
}
if (paramDto.ResultLevel.HasValue)
{
dbQuery = dbQuery.Where(x => x.ResultLevel.Equals(paramDto.ResultLevel));
}
var query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
Number = 0
});
// 跳绳
if (paramDto.ItemType == TrainingItemType.Coordinate.ToSafeInt())
{
query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
//Number = paramDto.TrendType == TrainingTrendType.BestResult.ToSafeInt() ? tsd.Kcal :
//(paramDto.TrendType == TrainingTrendType.Average.ToSafeInt() ? tsd.SecondsNumber : 0)
});
}
// 敏捷
else if (paramDto.ItemType == TrainingItemType.Agility.ToSafeInt())
{
query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
//Number = paramDto.TrendType == TrainingTrendType.BestResult.ToSafeInt() ? tsd.SpeedTime : 0
});
}
// 力量
else if (paramDto.ItemType == TrainingItemType.Strength.ToSafeInt())
{
query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
//Number = paramDto.TrendType == TrainingTrendType.BestResult.ToSafeInt() ? tsd.SpeedTime :
//(paramDto.TrendType == TrainingTrendType.Average.ToSafeInt() ? tsd.Efficiency : 0)
});
}
// 平衡
else if (paramDto.ItemType == TrainingItemType.Balance.ToSafeInt())
{
query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
//Number = paramDto.TrendType == TrainingTrendType.BestResult.ToSafeInt() ? tsd.SpeedTime : 0
});
}
// 速度
else if (paramDto.ItemType == TrainingItemType.Speed.ToSafeInt())
{
query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
//Number = paramDto.TrendType == TrainingTrendType.BestResult.ToSafeInt() ? tsd.SpeedTime :
//(paramDto.TrendType == TrainingTrendType.Average.ToSafeInt() ? tsd.SecondsNumber : 0)
});
}
// 心率
else if (paramDto.ItemType == TrainingItemType.HeartRate.ToSafeInt())
{
query = dbQuery.Select(tsd => new TrainingStudentDataTrendModel
{
InsertTime = tsd.StartTime.GetDateTime(),
//Number = paramDto.TrendType == TrainingTrendType.BestResult.ToSafeInt() ? tsd.Kcal : 0
});
}
var list = await query
.Skip((paramDto.PageIndex - 1) * paramDto.PageSize)
.Take(paramDto.PageSize)
.ToListAsync();
return list;
}
}
}