using Microsoft.Extensions.DependencyInjection; using VOL.Ai.IServices; using VOL.Core.Extensions.AutofacManager; using VOL.Entity.DomainModels.Business.People; using VOL.Entity.DomainModels; using VOL.System.IRepositories; using VOL.System.Repositories; using VOL.Model.IOT.Response; using Microsoft.EntityFrameworkCore; using VOL.Model.Ai; using VOL.Core.Utilities; using VOL.Entity.Enum; using VOL.Model.Ai.Request; using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; using VOL.Core.ManageUser; using VOL.Core.Configuration; using VOL.Core.Extensions; using Microsoft.AspNetCore.Mvc; using VOL.Model; namespace VOL.Ai.Services { public class AiService : IAiService, IDependency { private readonly IS_StudentRepository _studentRepository; private readonly IN_SportsTestCategoryRepository _sportsTestCategoryRepository; private readonly IN_HealthStandardsRepository _healthStandardsRepository; private readonly IN_SportsTestResultRepository _sportsTestResultRepository; [ActivatorUtilitiesConstructor] public AiService(IS_StudentRepository studentRepository, IN_SportsTestCategoryRepository sportsTestCategoryRepository, IN_HealthStandardsRepository healthStandardsRepository, IN_SportsTestResultRepository sportsTestResultRepository) { _studentRepository = studentRepository; _sportsTestCategoryRepository = sportsTestCategoryRepository; _healthStandardsRepository = healthStandardsRepository; _sportsTestResultRepository = sportsTestResultRepository; } public async Task AiDetail(Ai_DeviceRequest paramDto) { var res = await ( from d in _studentRepository.DbContext.Set() join a in _studentRepository.DbContext.Set() on d.Code equals a.Code join s in _studentRepository.DbContext.Set() on a.SchoolCode equals s.SchoolCode where d.Code == paramDto.Code select new Ai_DeviceDto() { Code = d.Code, Name = d.Name, SchoolName = s.SchoolName, Status = d.Status, StartTime = d.StartTime, EndTime = d.EndTime }).FirstOrDefaultAsync(); return res; } public async Task AiSchoolDetail(Ai_SchoolRequest paramDto) { var res = await ( from a in _studentRepository.DbContext.Set() join s in _studentRepository.DbContext.Set() on a.SchoolCode equals s.SchoolCode where a.Code == paramDto.Code select new Ai_SchoolDto { //SchoolId = s.Id, SchoolCode = s.SchoolCode, Name = s.SchoolName, Type = s.SchoolNatureId, Province = s.Province, City = s.City }).FirstOrDefaultAsync(); if (res == null) throw new ArgumentNullException(nameof(res)); //var gIds = await _studentRepository.DbContext.Set().Where(x => x.SchoolId == res.SchoolId).Select(x => x.GradeId).ToListAsync(); var grades = await ( from g in _studentRepository.DbContext.Set() join a in _studentRepository.DbContext.Set() on g.Id equals a.GradeId join c in _studentRepository.DbContext.Set() on g.Id equals c.GradeId into classGroup from c in classGroup.DefaultIfEmpty() //where gIds.Contains(g.Id) where g.SchoolCode.Equals(res.SchoolCode) group c by new { g.Id, g.GradeName } into gradeGroup select new Grades() { Id = gradeGroup.Key.Id, Name = gradeGroup.Key.GradeName, Class = gradeGroup.Where(c => c != null).Select(c => new Classes { Id = c.Id, Name = c.ClassName }).ToList() }).ToListAsync(); res.Grade = grades; return res; } public async Task> StudentList(Ai_StudentListRequest paramDto) { var res = await _studentRepository.DbContext.Set().Where(x => x.ClassId == paramDto.Id).Select(x => new Ai_StudentListDto() { Code = paramDto.Code, StudentNo = x.StudentNo, StudentName = x.StudentName, Sex = x.Sex }).ToListAsync(); return res; } public async Task StudentDetail(Ai_StudentDetailRequest paramDto) { var res = await ( from s in _studentRepository.DbContext.Set() join c in _studentRepository.DbContext.Set() on s.ClassId equals c.Id join g in _studentRepository.DbContext.Set() on c.GradeId equals g.Id where s.StudentNo == paramDto.StudentNo select new Ai_StudentDetailsDto { StudentNo = s.StudentNo, StudentName = s.StudentName, Age = s.Age, IdCard = s.IDCard, Sex = (int)s.Sex, Classes = c.Id.ToString(), ClassName = c.ClassName, Grade = g.Id.ToString(), GradeName = g.GradeName }).FirstOrDefaultAsync(); return res; } public async Task ResultUpload(Ai_ResultUploadRequest paramDto) { if (paramDto == null || paramDto == null) throw new Exception("参数错误"); //var exist = await _studentRepository.DbContext.Set().Where(x => x.UniqueId == paramDto.UniqueId).AnyAsync(); //if (exist) // throw new Exception("成绩信息已存在"); //var student = await ( // from s in _studentRepository.DbContext.Set() // join c in _studentRepository.DbContext.Set() on s.ClassId equals c.Id // join g in _studentRepository.DbContext.Set() on c.GradeId equals g.Id // where s.StudentNo == paramDto.StudentNo // select new // { // s.SchoolCode, // s.StudentNo, // s.StudentName, // s.Sex, // ClassId = c.Id, // c.ClassName, // GradeId = g.Id, // g.GradeName // }).FirstOrDefaultAsync(); //if (student == null) return; //var entitys = new Ai_Result() //{ // GradeId = student.GradeId, // GradeName = student.GradeName, // ClassId = student.ClassId, // ClassName = student.ClassName, // SchoolCode = student.SchoolCode, // Gender = (int)student.Sex, // //TeacherId = student.TeacherId, // StudentName = student.StudentName, // Code = paramDto.Code.ToUpper(), // CorrectJudge = paramDto.CorrectJudge, // Duration = paramDto.Duration, // ItemCode = paramDto.ItemCode, // Remark = paramDto.Remark, // StudentNo = paramDto.StudentNo, // TestResult = paramDto.TestResult, // TestTime = paramDto.TestEndTime ?? DateTime.Now, // UniqueId = paramDto.UniqueId, // WrongResults = paramDto.WrongResults //}; //await _studentRepository.DbContext.Set().AddAsync(entitys); //await _studentRepository.SaveChangesAsync(); //// 使用异步方法进行进一步处理 //_ = FurtherProcessingAsync(paramDto); } public async Task> GetAiCategoryType() { return await Task.FromResult(Tool.GetEnumDescriptions((code, description) => new Ai_ItemTypeModel { ItemCode = code, ItemTypeName = description })); } public async Task> AiGradesList(string deviceSn) { var res = await ( from a in _studentRepository.DbContext.Set() join s in _studentRepository.DbContext.Set() on a.SchoolCode equals s.SchoolCode where a.Code == deviceSn select new { SchoolId = s.Id, s.SchoolCode }).FirstOrDefaultAsync(); if (res == null) throw new ArgumentNullException(nameof(res)); //var gIds = await _studentRepository.DbContext.Set().Where(x => x.SchoolId == res.SchoolId).Select(x => x.GradeId).ToListAsync(); var grades = await ( from g in _studentRepository.DbContext.Set() join c in _studentRepository.DbContext.Set() on g.Id equals c.GradeId into classGroup from c in classGroup.DefaultIfEmpty() // where gIds.Contains(g.Id) where g.SchoolCode.Equals(res.SchoolCode) group c by new { g.Id, g.GradeName } into gradeGroup select new Grades() { Id = gradeGroup.Key.Id, Name = gradeGroup.Key.GradeName, Class = gradeGroup.Where(c => c != null).Select(c => new Classes { Id = c.Id, Name = c.ClassName }).ToList() }).ToListAsync(); return grades; } public async Task> AiScoreRanking(Ai_RankingListRequest paramDto) { var res = new List(); //var students = await ( // from s in _studentRepository.DbContext.Set() // join c in _studentRepository.DbContext.Set() on s.ClassId equals c.Id into classGroup // from c in classGroup.DefaultIfEmpty() // where s.ClassId == paramDto.ClassId // select new // { // s.Id, // s.StudentNo, // s.StudentName, // s.ClassId, // c.ClassName // }).ToListAsync(); //var studentNos = students.Select(s => s.StudentNo).ToList(); //var query = _studentRepository.DbContext.Set().Where(x => studentNos.Contains(x.StudentNo) && x.ItemCode == paramDto.ItemCode); //if (paramDto.TodayTime.HasValue) //{ // var todayStart = paramDto.TodayTime.Value.Date; // var todayEnd = todayStart.AddDays(1).AddTicks(-1); // query = query.Where(x => x.TestTime >= todayStart && x.TestTime <= todayEnd); //} //var data = await query // .OrderByDescending(x => x.TestResult) // .Select(x => new // { // x.StudentNo, // x.TestResult // }).ToListAsync(); //var topResults = data // .GroupBy(x => x.StudentNo) // .Select(g => g.First()) // .OrderByDescending(x => x.TestResult) // .ToList(); //var rankedList = topResults // .GroupBy(x => x.TestResult) // .SelectMany((group, groupIndex) => group.Select(x => new // { // Rank = groupIndex + 1, // x.StudentNo, // x.TestResult // })) // .OrderBy(x => x.Rank) // .ThenBy(x => x.StudentNo) // .ToList(); //rankedList.ForEach(x => //{ // var student = students.FirstOrDefault(s => s.StudentNo == x.StudentNo); // res.Add(new Ai_RankingListDto() // { // Name = $"{student?.StudentName} {paramDto.GradeName}{student?.ClassName}", // Ranking = x.Rank, // Score = x.TestResult // }); //}); return res; } //private async Task FurtherProcessingAsync(Ai_ResultUploadRequest paramDto) //{ // var categoryEnumString = Enum.GetName(typeof(SportsTestItemType), paramDto.ItemCode); // var healthStandards = await _healthStandardsRepository.FindAsync(x => x.CategoryEnum == categoryEnumString); // var student = await ( // from s in _studentRepository.DbContext.Set() // join c in _studentRepository.DbContext.Set() on s.ClassId equals c.Id // join g in _studentRepository.DbContext.Set() on c.GradeId equals g.Id // where s.StudentNo == paramDto.StudentNo // select new // { // s.SchoolCode, // s.StudentNo, // s.Sex, // ClassId = c.Id, // c.ClassName, // GradeId = g.Id, // g.GradeName // }).FirstOrDefaultAsync(); // if (student == null) return; // var value = Convert.ToInt32(paramDto.TestResult); // var sportsTestValueEntity = new N_SportsTestValue(); // var standard = healthStandards.Where(x => // x.GradeId == student.GradeId && // x.Sex == student.Sex && // value >= x.MinValue && // value < x.MaxValue // ).FirstOrDefault(); // if (standard != null) // { // sportsTestValueEntity.Score = standard.Score; // sportsTestValueEntity.Rank = standard.Rank; // } // sportsTestValueEntity.SchoolCode = student.SchoolCode; // sportsTestValueEntity.DataSource = DataSource.AI; // sportsTestValueEntity.CategoryValue = Convert.ToInt32(paramDto.ItemCode); // sportsTestValueEntity.CategoryEnum = categoryEnumString; // sportsTestValueEntity.GradeId = student.GradeId; // sportsTestValueEntity.GradeName = student.GradeName; // sportsTestValueEntity.ClassId = student.ClassId; // sportsTestValueEntity.ClassName = student.ClassName; // sportsTestValueEntity.TeacherId = 1; // sportsTestValueEntity.StudentNo = paramDto.StudentNo; // sportsTestValueEntity.ScoreTime = paramDto.TestEndTime ?? DateTime.Now; // sportsTestValueEntity.Value = paramDto.TestResult; // sportsTestValueEntity.WrongResults = paramDto.WrongResults; // sportsTestValueEntity.AdditionalScore = sportsTestValueEntity.Score == 100 ? 100 : 0; // await _sportsTestResultRepository.AddAsync(sportsTestValueEntity); // await _sportsTestResultRepository.SaveChangesAsync(); //} } }