| | 1 | | using Syki.Back.Features.Academic.CreateTeacher; |
| | 2 | | using Syki.Back.Features.Academic.CreateStudent; |
| | 3 | | using Syki.Back.Features.Academic.CreateDiscipline; |
| | 4 | | using Syki.Back.Features.Academic.CreateAcademicPeriod; |
| | 5 | | using Syki.Back.Features.Student.CreateStudentEnrollment; |
| | 6 | |
|
| | 7 | | namespace Syki.Back.Features.Academic.CreateClass; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Representa uma Turma. |
| | 11 | | /// </summary> |
| | 12 | | public class Class |
| | 13 | | { |
| 6203 | 14 | | public Guid Id { get; set; } |
| 253 | 15 | | public Guid InstitutionId { get; set; } |
| 271 | 16 | | public Guid DisciplineId { get; set; } |
| 337 | 17 | | public Discipline Discipline { get; set; } |
| 213 | 18 | | public Guid TeacherId { get; set; } |
| 215 | 19 | | public SykiTeacher Teacher { get; set; } |
| 789 | 20 | | public string PeriodId { get; set; } |
| 30588 | 21 | | public AcademicPeriod Period { get; set; } |
| 435 | 22 | | public int Vacancies { get; set; } |
| 1123 | 23 | | public ClassStatus Status { get; set; } |
| 8656 | 24 | | public int Workload { get; set; } |
| 485 | 25 | | public List<SykiStudent> Students { get; set; } |
| 652 | 26 | | public List<Schedule> Schedules { get; set; } |
| 864 | 27 | | public List<ExamGrade> ExamGrades { get; set; } |
| 5263 | 28 | | public List<Lesson> Lessons { get; set; } |
| | 29 | |
|
| 222 | 30 | | public string FillRatio { get; set; } |
| | 31 | |
|
| 1740 | 32 | | private Class() {} |
| | 33 | |
|
| 212 | 34 | | private Class( |
| 212 | 35 | | Guid institutionId, |
| 212 | 36 | | Guid disciplineId, |
| 212 | 37 | | Guid teacherId, |
| 212 | 38 | | string period, |
| 212 | 39 | | int vacancies, |
| 212 | 40 | | List<Schedule> schedules |
| 212 | 41 | | ) { |
| 212 | 42 | | Id = Guid.NewGuid(); |
| 212 | 43 | | InstitutionId = institutionId; |
| 212 | 44 | | DisciplineId = disciplineId; |
| 212 | 45 | | TeacherId = teacherId; |
| 212 | 46 | | PeriodId = period; |
| 212 | 47 | | Vacancies = vacancies; |
| 212 | 48 | | Status = ClassStatus.OnPreEnrollment; |
| 212 | 49 | | Schedules = schedules; |
| 212 | 50 | | Students = []; |
| 212 | 51 | | ExamGrades = []; |
| 212 | 52 | | Lessons = []; |
| 212 | 53 | | } |
| | 54 | |
|
| | 55 | | public static OneOf<Class, SykiError> New( |
| | 56 | | Guid institutionId, |
| | 57 | | Guid disciplineId, |
| | 58 | | Guid teacherId, |
| | 59 | | string period, |
| | 60 | | int vacancies, |
| | 61 | | List<Schedule> schedules |
| | 62 | | ) { |
| 220 | 63 | | var result = Validate(schedules); |
| | 64 | |
|
| 228 | 65 | | if (result.IsError()) return result.GetError(); |
| | 66 | |
|
| 212 | 67 | | return new Class(institutionId, disciplineId, teacherId, period, vacancies, schedules); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public void CreateLessons() |
| | 71 | | { |
| 629 | 72 | | var schedules = Schedules.OrderBy(x => x.Day).ThenBy(x => x.StartAt).ToList(); |
| | 73 | |
|
| 205 | 74 | | var current = Period.StartAt; |
| 30374 | 75 | | while (current < Period.EndAt) |
| | 76 | | { |
| 120922 | 77 | | foreach (var schedule in schedules) |
| | 78 | | { |
| 30292 | 79 | | if (current.DayOfWeek.Is(schedule.Day)) |
| | 80 | | { |
| 4320 | 81 | | Lessons.Add(new(Id, current, schedule.StartAt, schedule.EndAt)); |
| 4320 | 82 | | Workload += schedule.GetDiff(); |
| | 83 | | } |
| | 84 | | } |
| 30169 | 85 | | current = current.AddDays(1); |
| | 86 | | } |
| 205 | 87 | | } |
| | 88 | |
|
| | 89 | | private static OneOf<SykiSuccess, SykiError> Validate(List<Schedule> schedules) |
| | 90 | | { |
| 464 | 91 | | for (int i = 0; i < schedules.Count-1; i++) |
| | 92 | | { |
| 72 | 93 | | for (int j = i+1; j < schedules.Count; j++) |
| | 94 | | { |
| 24 | 95 | | if (schedules[i].Conflict(schedules[j])) |
| 8 | 96 | | return new ConflictingSchedules(); |
| | 97 | | } |
| | 98 | | } |
| 212 | 99 | | return new SykiSuccess(); |
| | 100 | | } |
| | 101 | |
|
| | 102 | | private string GetScheduleAsString() |
| | 103 | | { |
| 39 | 104 | | return string.Join(" | ", Schedules.OrderBy(h => h.Day).ThenBy(h => h.StartAt).ToList().ConvertAll(h => h.ToStri |
| | 105 | | } |
| | 106 | |
|
| | 107 | | private string GetWorkloadAsString() |
| | 108 | | { |
| 9 | 109 | | return Workload.MinutesToString(); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private string GetProgressAsString() |
| | 113 | | { |
| 9 | 114 | | var total = Lessons.Count; |
| 103 | 115 | | var finalized = Lessons.Count(x => x.Status == LessonStatus.Finalized); |
| 9 | 116 | | return $"{finalized}/{total}"; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public void Start() |
| | 120 | | { |
| 154 | 121 | | Status = ClassStatus.Started; |
| 154 | 122 | | ExamGrades = []; |
| 618 | 123 | | foreach (var student in Students) |
| | 124 | | { |
| 155 | 125 | | Enum.GetValues<ExamType>().ToList() |
| 620 | 126 | | .ForEach(type => ExamGrades.Add(new ExamGrade(Id, student.Id, type, 0.00M))); |
| | 127 | | } |
| 154 | 128 | | } |
| | 129 | |
|
| | 130 | | public OneOf<SykiSuccess, SykiError> Finish() |
| | 131 | | { |
| 30 | 132 | | if (Lessons.Any(x => x.Status != LessonStatus.Finalized)) |
| 2 | 133 | | return new AllClassLessonsMustHaveFinalizedStatus(); |
| | 134 | |
|
| 2 | 135 | | Status = ClassStatus.Finalized; |
| 2 | 136 | | return new SykiSuccess(); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public void SetFillRatio(int count) |
| | 140 | | { |
| 13 | 141 | | FillRatio = $"{count}/{Vacancies}"; |
| 13 | 142 | | } |
| | 143 | |
|
| | 144 | | public ClassOut ToOut() |
| | 145 | | { |
| 4548 | 146 | | var presences = Lessons.Sum(l => l.Attendances.Count(a => a.Present)); |
| 4548 | 147 | | var attendances = Lessons.Sum(l => l.Attendances.Count); |
| 200 | 148 | | return new() |
| 200 | 149 | | { |
| 200 | 150 | | Id = Id, |
| 200 | 151 | | Discipline = Discipline.Name, |
| 200 | 152 | | Teacher = Teacher.Name, |
| 200 | 153 | | Period = PeriodId, |
| 200 | 154 | | Vacancies = Vacancies, |
| 200 | 155 | | Status = Status, |
| 201 | 156 | | Schedules = Schedules.ConvertAll(h => h.ToOut()), |
| 200 | 157 | | FillRatio = FillRatio, |
| 200 | 158 | | Frequency = attendances == 0 ? 0.00M : 100M * (1M * presences / (1M * attendances)), |
| 13044 | 159 | | Lessons = Lessons.OrderBy(x => x.Date).ThenBy(x => x.StartAt).Select((l, i) => l.ToOut(i+1)).ToList(), |
| 200 | 160 | | }; |
| | 161 | | } |
| | 162 | |
|
| | 163 | | public GetAcademicClassOut ToGetAcademicClassOut() |
| | 164 | | { |
| 10 | 165 | | var students = Students.ConvertAll(x => x.ToAcademicClassStudentOut()); |
| 103 | 166 | | var lessons = Lessons.Count(x => x.Attendances.Count > 0); |
| 9 | 167 | | students.ForEach(s => |
| 9 | 168 | | { |
| 1 | 169 | | var studentExamGrades = ExamGrades.Where(g => g.StudentId == s.Id).ToList(); |
| 1 | 170 | | s.AverageNote = studentExamGrades.GetAverageNote(); |
| 27 | 171 | | var presences = Lessons.Count(x => x.Attendances.Exists(a => a.StudentId == s.Id && a.Present)); |
| 1 | 172 | | s.Frequency = lessons == 0 ? 0.00M : 100M * (1M * presences / (1M * lessons)); |
| 1 | 173 | | s.ExamGrades = studentExamGrades.OrderBy(x => x.ExamType).Select(g => g.ToOut()).ToList(); |
| 10 | 174 | | }); |
| | 175 | |
|
| 9 | 176 | | return new() |
| 9 | 177 | | { |
| 9 | 178 | | Id = Id, |
| 9 | 179 | | Discipline = Discipline.Name, |
| 9 | 180 | | Code = Discipline.Code, |
| 9 | 181 | | Teacher = Teacher.Name, |
| 9 | 182 | | Period = PeriodId, |
| 9 | 183 | | Vacancies = Vacancies, |
| 9 | 184 | | Status = Status, |
| 10 | 185 | | Schedules = Schedules.ConvertAll(h => h.ToOut()), |
| 282 | 186 | | Lessons = Lessons.OrderBy(x => x.Date).ThenBy(x => x.StartAt).Select((l, i) => l.ToOut(i+1)).ToList(), |
| 9 | 187 | | SchedulesInline = GetScheduleAsString(), |
| 9 | 188 | | Workload = GetWorkloadAsString(), |
| 9 | 189 | | Progress = GetProgressAsString(), |
| 9 | 190 | | Students = students, |
| 9 | 191 | | FillRatio = FillRatio, |
| 1 | 192 | | Frequency = students.Count > 0 ? students.Average(s => s.Frequency) : 0.00M, |
| 9 | 193 | | }; |
| | 194 | | } |
| | 195 | |
|
| | 196 | | public TeacherClassOut ToTeacherClassOut() |
| | 197 | | { |
| 87 | 198 | | var students = Students.OrderBy(x => x.Name).Select(x => x.ToTeacherClassStudentOut()).ToList(); |
| 29 | 199 | | students.ForEach(s => |
| 29 | 200 | | { |
| 116 | 201 | | var studentExamGrades = ExamGrades.Where(g => g.StudentId == s.Id).ToList(); |
| 29 | 202 | | s.AverageNote = studentExamGrades.GetAverageNote(); |
| 203 | 203 | | s.ExamGrades = studentExamGrades.OrderBy(x => x.ExamType).Select(g => g.ToOut()).ToList(); |
| 58 | 204 | | }); |
| | 205 | |
|
| 29 | 206 | | return new() |
| 29 | 207 | | { |
| 29 | 208 | | Id = Id, |
| 29 | 209 | | Discipline = Discipline.Name, |
| 29 | 210 | | Code = Discipline.Code, |
| 29 | 211 | | Period = PeriodId, |
| 29 | 212 | | Status = Status, |
| 29 | 213 | | Students = students, |
| 2019 | 214 | | Lessons = Lessons.OrderBy(x => x.Date).ThenBy(x => x.StartAt).Select((l, i) => l.ToOut(i+1)).ToList(), |
| 29 | 215 | | }; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | public TeacherClassesOut ToTeacherClassesOut() |
| | 219 | | { |
| 0 | 220 | | return new() |
| 0 | 221 | | { |
| 0 | 222 | | Id = Id, |
| 0 | 223 | | Discipline = Discipline.Name, |
| 0 | 224 | | Code = Discipline.Code, |
| 0 | 225 | | Period = PeriodId, |
| 0 | 226 | | Schedules = Schedules.ConvertAll(h => h.ToOut()), |
| 0 | 227 | | SchedulesInline = GetScheduleAsString(), |
| 0 | 228 | | }; |
| | 229 | | } |
| | 230 | | } |