using System; using System.Collections.Generic; using System.Linq; namespace Zurnal.RemaDateBase.DateDao { public class GroupInfo { public int TotalStudents { get; set; } public int TotalLessons { get; set; } public double AttendancePercentage { get; set; } public List StudentAttendances { get; set; } public GroupInfo(GroupDao group) { TotalStudents = group.Users.Count(); TotalLessons = group.Users.SelectMany(u => u.Group.Users).Count(); AttendancePercentage = CalculateAttendancePercentage(group); StudentAttendances = GetStudentAttendances(group); } private double CalculateAttendancePercentage(GroupDao group) { var totalAttendance = group.Users.Sum(u => u.Group.Users.Count(x => x.IsAttendensy)); var totalClasses = group.Users.Count() * group.Users.FirstOrDefault()?.Group.Users.Count() ?? 0; return totalClasses > 0 ? (double)totalAttendance / totalClasses * 100 : 0; } private List GetStudentAttendances(GroupDao group) { return group.Users.Select(u => new StudentAttendance { FIO = u.FIO, AttendedLessons = u.Group.Users.Count(x => x.UserGuid == u.UserGuid && x.IsAttendensy), MissedLessons = u.Group.Users.Count(x => x.UserGuid == u.UserGuid && !x.IsAttendensy), AttendancePercentage = (u.Group.Users.Count(x => x.UserGuid == u.UserGuid && x.IsAttendensy) / (double)TotalLessons) * 100 }).ToList(); } } public class StudentAttendance { public string FIO { get; set; } public int AttendedLessons { get; set; } public int MissedLessons { get; set; } public double AttendancePercentage { get; set; } } }