TeachersPageViewModel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using TeacheresAndCourses.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using ReactiveUI;
  5. using System.Linq;
  6. using Microsoft.EntityFrameworkCore;
  7. using System.Diagnostics;
  8. using System.Data;
  9. using MsBox.Avalonia.Enums;
  10. using MsBox.Avalonia;
  11. namespace TeacheresAndCourses.ViewModels
  12. {
  13. public class TeachersPageViewModel : ViewModelBase
  14. {
  15. List<Teacher> teachersList;
  16. public List<Teacher> TeachersList { get => teachersList; set => this.RaiseAndSetIfChanged(ref teachersList, value); }
  17. public TeachersPageViewModel() {
  18. teachersList = MainWindowViewModel.connection.Teachers.Include(x => x.GenderNavigation).
  19. Include(x => x.TeachersAndCourses).ThenInclude(x => x.Course).
  20. Include(x => x.TeachersAndSubjects).ThenInclude(x => x.Subject).ToList();
  21. }
  22. public void ToAddingPage()
  23. {
  24. MainWindowViewModel.Instance.Uc = new AddTeacher();
  25. }
  26. //Ñâîéñòâà äëÿ âûáîðêè ïî ÔÈÎ
  27. string searchLine;
  28. public string SearchLine { get => searchLine; set { searchLine = value; AllSort(); } }
  29. // Ñâîéñòâà äëÿ âûáîðêè ïî ïðåäìåòó
  30. List<Subject> subjectsList = [new Subject() { Subjectid = 0, Title = "Âñå ïðåäìåòû" }, ..MainWindowViewModel.connection.Subjects.ToList()];
  31. public List<Subject> SubjectsList { get => subjectsList; }
  32. Subject selectedSubject = null;
  33. public Subject SelectedSubject {
  34. get {
  35. if (selectedSubject == null) return subjectsList[0];
  36. else return selectedSubject;
  37. }
  38. set {
  39. selectedSubject = value;
  40. AllSort();
  41. }
  42. }
  43. // Ñâîéñòâà äëÿ âûáîðêè ïî ïîëó
  44. int genderSort = 0;
  45. public int GenderSort { get => genderSort; set { genderSort = value; AllSort(); } }
  46. // Ñâîéñòâà äëÿ âûáîðêè ïî òåëåôîíó
  47. bool phoneNumSort = false;
  48. public bool PhoneNumSort { get => phoneNumSort; set { phoneNumSort = value; AllSort(); } }
  49. // Ñâîéñòâà äëÿ ñîðòèðîâêè
  50. int generalSort = 0;
  51. public int GeneralSort { get => generalSort; set { generalSort = value; AllSort(); } }
  52. bool up = true;
  53. public bool Up { get => up; set { this.RaiseAndSetIfChanged(ref up, value); AllSort(); } }
  54. bool down = false;
  55. public bool Down { get => down; set { this.RaiseAndSetIfChanged(ref down, value); AllSort(); } }
  56. void AllSort()
  57. {
  58. teachersList = MainWindowViewModel.connection.Teachers.Include(x => x.GenderNavigation).
  59. Include(x => x.TeachersAndCourses).ThenInclude(x => x.Course).
  60. Include(x => x.TeachersAndSubjects).ThenInclude(x => x.Subject).ToList();
  61. //ïîèñêîâàÿ ñòðîêà
  62. if (!string.IsNullOrWhiteSpace(searchLine))
  63. {
  64. TeachersList = TeachersList.Where(x => x.Lastname.ToLower().Contains(searchLine.ToLower()) || x.Firstname.ToLower().Contains(searchLine.ToLower()) || x.Patronymic.ToLower().Contains(searchLine.ToLower())).ToList();
  65. }
  66. // âûáîðêà ïî ïîëó
  67. if (genderSort==1)
  68. {
  69. TeachersList = TeachersList.Where(x=>x.Gender==1).ToList();
  70. }
  71. else if(genderSort == 2)
  72. {
  73. TeachersList = TeachersList.Where(x => x.Gender == 2).ToList();
  74. }
  75. else
  76. {
  77. TeachersList = TeachersList.ToList();
  78. }
  79. //âûáîðêà ïî ïðåäìåòó
  80. if (selectedSubject!=null && selectedSubject.Subjectid != 0)
  81. {
  82. TeachersList = TeachersList.Where(x => x.TeachersAndSubjects.Any(y => y.Subjectid == selectedSubject.Subjectid)).ToList();
  83. }
  84. //âûáîðêà ïî íàëè÷èþ íîìåðà òåëåôîíà
  85. if (phoneNumSort==true)
  86. {
  87. TeachersList = TeachersList.Where(x => x.PhoneNumber != String.Empty).ToList();
  88. }
  89. else
  90. {
  91. TeachersList = TeachersList.ToList();
  92. }
  93. //ñîðòèðîâêà
  94. switch (generalSort)
  95. {
  96. case 0:
  97. break;
  98. case 1:
  99. if (Down)
  100. {
  101. TeachersList = TeachersList.OrderByDescending(x=>x.ExperienceInMonth).ToList();
  102. }
  103. else
  104. {
  105. TeachersList = TeachersList.OrderBy(x => x.ExperienceInMonth).ToList();
  106. }
  107. break;
  108. case 2:
  109. if (Down)
  110. {
  111. TeachersList = TeachersList.OrderByDescending(x => x.Lastname).ToList();
  112. }
  113. else
  114. {
  115. TeachersList = TeachersList.OrderBy(x => x.Lastname).ToList();
  116. }
  117. break;
  118. }
  119. }
  120. public async void DeleteTeacher(int teacherId)
  121. {
  122. ButtonResult checking = await MessageBoxManager.GetMessageBoxStandard("Ïðîâåðêà", "Âû õîòèòå óäàëèòü ïîëüçîâàòåëÿ?", ButtonEnum.YesNo).ShowAsync();
  123. switch (checking)
  124. {
  125. case ButtonResult.Yes:
  126. {
  127. Teacher deleteTeacher = MainWindowViewModel.connection.Teachers.First(x => x.Teacherid == teacherId);
  128. MainWindowViewModel.connection.Teachers.Remove(deleteTeacher);
  129. MainWindowViewModel.connection.SaveChanges();
  130. MainWindowViewModel.Instance.Uc = new TeachersPage();
  131. break;
  132. }
  133. case ButtonResult.No:
  134. {
  135. break;
  136. }
  137. }
  138. }
  139. public void EditTeacher(int teacherId)
  140. {
  141. MainWindowViewModel.Instance.Uc = new AddTeacher(teacherId);
  142. }
  143. }
  144. }