| | 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.Teacher.CreateClassActivity; |
| | 5 | | using Syki.Back.Features.Teacher.AddClassActivityNote; |
| | 6 | | using Syki.Back.Features.Academic.CreateAcademicPeriod; |
| | 7 | |
|
| | 8 | | namespace Syki.Back.Features.Academic.CreateClass; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Turma |
| | 12 | | /// </summary> |
| | 13 | | public class Class |
| | 14 | | { |
| 17448 | 15 | | public Guid Id { get; set; } |
| 832 | 16 | | public Guid InstitutionId { get; set; } |
| 804 | 17 | | public Guid DisciplineId { get; set; } |
| 826 | 18 | | public Discipline Discipline { get; set; } |
| 2320 | 19 | | public string PeriodId { get; set; } |
| 93018 | 20 | | public AcademicPeriod Period { get; set; } |
| 1332 | 21 | | public int Vacancies { get; set; } |
| 3326 | 22 | | public ClassStatus Status { get; set; } |
| 26234 | 23 | | public int Workload { get; set; } |
| | 24 | |
|
| 0 | 25 | | public Guid? CampusId { get; set; } |
| 704 | 26 | | public Guid? TeacherId { get; set; } |
| 632 | 27 | | public SykiTeacher Teacher { get; set; } |
| | 28 | |
|
| 1970 | 29 | | public List<Schedule> Schedules { get; set; } |
| 15778 | 30 | | public List<ClassLesson> Lessons { get; set; } |
| 922 | 31 | | public List<SykiStudent> Students { get; set; } |
| 1340 | 32 | | public List<ClassActivity> Activities { get; set; } |
| 688 | 33 | | public List<StudentClassNote> Notes { get; set; } |
| | 34 | |
|
| 644 | 35 | | public string FillRatio { get; set; } |
| | 36 | |
|
| 5412 | 37 | | private Class() {} |
| | 38 | |
|
| 686 | 39 | | private Class( |
| 686 | 40 | | Guid institutionId, |
| 686 | 41 | | Guid disciplineId, |
| 686 | 42 | | Guid teacherId, |
| 686 | 43 | | string period, |
| 686 | 44 | | int vacancies, |
| 686 | 45 | | List<Schedule> schedules |
| 686 | 46 | | ) { |
| 686 | 47 | | Id = Guid.CreateVersion7(); |
| 686 | 48 | | InstitutionId = institutionId; |
| 686 | 49 | | DisciplineId = disciplineId; |
| 686 | 50 | | TeacherId = teacherId; |
| 686 | 51 | | PeriodId = period; |
| 686 | 52 | | Vacancies = vacancies; |
| 686 | 53 | | Status = ClassStatus.OnPreEnrollment; |
| 686 | 54 | | Schedules = schedules; |
| 686 | 55 | | Lessons = []; |
| 686 | 56 | | Students = []; |
| 686 | 57 | | Activities = []; |
| 686 | 58 | | Notes = []; |
| 686 | 59 | | } |
| | 60 | |
|
| | 61 | | public static OneOf<Class, SykiError> New( |
| | 62 | | Guid institutionId, |
| | 63 | | Guid disciplineId, |
| | 64 | | Guid teacherId, |
| | 65 | | string period, |
| | 66 | | int vacancies, |
| | 67 | | List<Schedule> schedules |
| | 68 | | ) { |
| 702 | 69 | | var result = Validate(schedules); |
| | 70 | |
|
| 718 | 71 | | if (result.IsError) return result.Error; |
| | 72 | |
|
| 686 | 73 | | return new Class(institutionId, disciplineId, teacherId, period, vacancies, schedules); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private static OneOf<SykiSuccess, SykiError> Validate(List<Schedule> schedules) |
| | 77 | | { |
| 1452 | 78 | | for (int i = 0; i < schedules.Count-1; i++) |
| | 79 | | { |
| 144 | 80 | | for (int j = i+1; j < schedules.Count; j++) |
| | 81 | | { |
| 48 | 82 | | if (schedules[i].Conflict(schedules[j])) |
| 16 | 83 | | return new ConflictingSchedules(); |
| | 84 | | } |
| | 85 | | } |
| 686 | 86 | | return new SykiSuccess(); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public void CreateLessons() |
| | 90 | | { |
| 662 | 91 | | var schedules = Schedules.OrderBy(x => x.Day).ThenBy(x => x.StartAt).ToList(); |
| | 92 | |
|
| 614 | 93 | | var number = 1; |
| 614 | 94 | | var current = Period.StartAt; |
| 92386 | 95 | | while (current < Period.EndAt) |
| | 96 | | { |
| 367580 | 97 | | foreach (var schedule in schedules) |
| | 98 | | { |
| 92018 | 99 | | if (current.DayOfWeek.Is(schedule.Day)) |
| | 100 | | { |
| 13102 | 101 | | Lessons.Add(new(Id, number, current, schedule.StartAt, schedule.EndAt)); |
| 13102 | 102 | | Workload += schedule.GetDiff(); |
| 13102 | 103 | | number++; |
| | 104 | | } |
| | 105 | | } |
| 91772 | 106 | | current = current.AddDays(1); |
| | 107 | | } |
| 614 | 108 | | } |
| | 109 | |
|
| | 110 | | public void Start() |
| | 111 | | { |
| 476 | 112 | | Status = ClassStatus.Started; |
| 476 | 113 | | } |
| | 114 | |
|
| | 115 | | public OneOf<SykiSuccess, SykiError> AddActivity(ClassActivity activity) |
| | 116 | | { |
| 852 | 117 | | var sum = Activities.Where(x => x.Note == activity.Note).Sum(x => x.Weight); |
| | 118 | |
|
| 346 | 119 | | if (sum + activity.Weight > 100) return new InvalidClassActivityWeight(); |
| | 120 | |
|
| 266 | 121 | | Activities.Add(activity); |
| | 122 | |
|
| 266 | 123 | | return new SykiSuccess(); |
| | 124 | | } |
| | 125 | |
|
| | 126 | | public List<ClassNoteRemainingWeightsOut> GetNotesRemainingWeights() |
| | 127 | | { |
| 8 | 128 | | var weights = new List<ClassNoteRemainingWeightsOut>(); |
| 64 | 129 | | foreach (var note in Enum.GetValues<ClassNoteType>()) |
| | 130 | | { |
| 128 | 131 | | var sum = Activities.Where(x => x.Note == note).Sum(x => x.Weight); |
| 24 | 132 | | weights.Add(new() { Note = note, Weight = 100 - sum }); |
| | 133 | | } |
| | 134 | |
|
| 8 | 135 | | return weights; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | public OneOf<SykiSuccess, SykiError> Finish() |
| | 139 | | { |
| 62 | 140 | | if (Lessons.Any(x => x.Status != ClassLessonStatus.Finalized)) |
| 4 | 141 | | return new AllClassLessonsMustHaveFinalizedStatus(); |
| | 142 | |
|
| 4 | 143 | | Status = ClassStatus.Finalized; |
| 4 | 144 | | return new SykiSuccess(); |
| | 145 | | } |
| | 146 | |
|
| | 147 | | private string GetScheduleAsString() |
| | 148 | | { |
| 42 | 149 | | return string.Join(" | ", Schedules.OrderBy(h => h.Day).ThenBy(h => h.StartAt).ToList().ConvertAll(h => h.ToStri |
| | 150 | | } |
| | 151 | |
|
| | 152 | | private string GetWorkloadAsString() |
| | 153 | | { |
| 16 | 154 | | return Workload.MinutesToString(); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | private string GetProgressAsString() |
| | 158 | | { |
| 16 | 159 | | var total = Lessons.Count; |
| 198 | 160 | | var finalized = Lessons.Count(x => x.Status == ClassLessonStatus.Finalized); |
| 16 | 161 | | return $"{finalized}/{total}"; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | public void SetFillRatio(int count) |
| | 165 | | { |
| 24 | 166 | | FillRatio = $"{count}/{Vacancies}"; |
| 24 | 167 | | } |
| | 168 | |
|
| | 169 | | // OUTS |
| | 170 | |
|
| | 171 | | public ClassOut ToOut() |
| | 172 | | { |
| 13762 | 173 | | var presences = Lessons.Sum(l => l.Attendances.Count(a => a.Present)); |
| 13762 | 174 | | var attendances = Lessons.Sum(l => l.Attendances.Count); |
| 604 | 175 | | return new() |
| 604 | 176 | | { |
| 604 | 177 | | Id = Id, |
| 604 | 178 | | Discipline = Discipline.Name, |
| 604 | 179 | | Teacher = Teacher.Name, |
| 604 | 180 | | Period = PeriodId, |
| 604 | 181 | | Vacancies = Vacancies, |
| 604 | 182 | | Status = Status, |
| 606 | 183 | | Schedules = Schedules.ConvertAll(h => h.ToOut()), |
| 604 | 184 | | FillRatio = FillRatio, |
| 604 | 185 | | Frequency = attendances == 0 ? 0.00M : 100M * (1M * presences / (1M * attendances)), |
| 26316 | 186 | | Lessons = Lessons.OrderBy(x => x.Number).Select(x => x.ToOut()).ToList(), |
| 604 | 187 | | }; |
| | 188 | | } |
| | 189 | |
|
| | 190 | | public GetAcademicClassOut ToGetAcademicClassOut() |
| | 191 | | { |
| 16 | 192 | | return new() |
| 16 | 193 | | { |
| 16 | 194 | | Id = Id, |
| 16 | 195 | | Discipline = Discipline.Name, |
| 16 | 196 | | Code = Discipline.Code, |
| 16 | 197 | | Teacher = Teacher.Name, |
| 16 | 198 | | Period = PeriodId, |
| 16 | 199 | | Vacancies = Vacancies, |
| 16 | 200 | | Status = Status, |
| 18 | 201 | | Schedules = Schedules.ConvertAll(h => h.ToOut()), |
| 364 | 202 | | Lessons = Lessons.OrderBy(x => x.Number).Select(x => x.ToOut()).ToList(), |
| 16 | 203 | | SchedulesInline = GetScheduleAsString(), |
| 16 | 204 | | Workload = GetWorkloadAsString(), |
| 16 | 205 | | Progress = GetProgressAsString(), |
| 16 | 206 | | FillRatio = FillRatio, |
| 16 | 207 | | }; |
| | 208 | | } |
| | 209 | |
|
| | 210 | | public TeacherClassOut ToTeacherClassOut() |
| | 211 | | { |
| 2 | 212 | | return new() |
| 2 | 213 | | { |
| 2 | 214 | | Id = Id, |
| 2 | 215 | | Discipline = Discipline.Name, |
| 2 | 216 | | Code = Discipline.Code, |
| 2 | 217 | | Period = PeriodId, |
| 2 | 218 | | Status = Status, |
| 2 | 219 | | }; |
| | 220 | | } |
| | 221 | |
|
| | 222 | | public StudentClassOut ToStudentClassOut(decimal n1, decimal n2, decimal n3, decimal average, decimal frequency) |
| | 223 | | { |
| 0 | 224 | | return new() |
| 0 | 225 | | { |
| 0 | 226 | | Id = Id, |
| 0 | 227 | | Discipline = Discipline.Name, |
| 0 | 228 | | Code = Discipline.Code, |
| 0 | 229 | | Period = PeriodId, |
| 0 | 230 | | Status = Status, |
| 0 | 231 | | N1 = n1, |
| 0 | 232 | | N2 = n2, |
| 0 | 233 | | N3 = n3, |
| 0 | 234 | | Average = average, |
| 0 | 235 | | Frequency = frequency, |
| 0 | 236 | | }; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | public TeacherClassesOut ToTeacherClassesOut() |
| | 240 | | { |
| 0 | 241 | | return new() |
| 0 | 242 | | { |
| 0 | 243 | | Id = Id, |
| 0 | 244 | | Discipline = Discipline.Name, |
| 0 | 245 | | Code = Discipline.Code, |
| 0 | 246 | | Period = PeriodId, |
| 0 | 247 | | Schedules = Schedules.ConvertAll(h => h.ToOut()), |
| 0 | 248 | | SchedulesInline = GetScheduleAsString(), |
| 0 | 249 | | }; |
| | 250 | | } |
| | 251 | |
|
| | 252 | | public TeacherCurrentClassOut ToTeacherCurrentClassOut() |
| | 253 | | { |
| 0 | 254 | | return new() |
| 0 | 255 | | { |
| 0 | 256 | | Id = Id, |
| 0 | 257 | | Discipline = Discipline.Name, |
| 0 | 258 | | }; |
| | 259 | | } |
| | 260 | |
|
| | 261 | | public StudentCurrentClassOut ToStudentCurrentClassOut() |
| | 262 | | { |
| 0 | 263 | | return new() |
| 0 | 264 | | { |
| 0 | 265 | | Id = Id, |
| 0 | 266 | | Discipline = Discipline.Name, |
| 0 | 267 | | }; |
| | 268 | | } |
| | 269 | | } |