ShowViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using ReactiveUI;
  5. using System.Linq;
  6. using AvaloniaApplication5.Models;
  7. using Microsoft.EntityFrameworkCore;
  8. using MsBox.Avalonia.Enums;
  9. using MsBox.Avalonia;
  10. namespace AvaloniaApplication5.ViewModels
  11. {
  12. internal class ShowViewModel : ViewModelBase
  13. {
  14. List<Teacher> _teacherList;
  15. public List<Teacher> TeacherList { get => _teacherList; set=> this.RaiseAndSetIfChanged(ref _teacherList, value); }
  16. public ShowViewModel()
  17. {
  18. TeacherList = MainWindowViewModel.myConnection.Teachers.
  19. Include(x => x.Gender).
  20. Include(x => x.TeacherDisciplines).ThenInclude(x => x.Discipline).
  21. Include(x => x.TeacherCourses).ThenInclude(x => x.Course).ToList();
  22. }
  23. public void ToAddPage()
  24. {
  25. MainWindowViewModel.Instance.PageContent = new AddPage();
  26. }
  27. public async void DeleteTeacher(int idt)
  28. {
  29. // Çàïðàøèâàåì ïîäòâåðæäåíèå óäàëåíèÿ
  30. ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Ïîäòâåðæäåíèå óäàëåíèÿ",
  31. "Ïîäòâåðäèòü óäàëåíèå ó÷èòåëÿ?", ButtonEnum.YesNo).ShowAsync();
  32. if (result == ButtonResult.Yes)
  33. {
  34. // Åñëè ïîëüçîâàòåëü ïîäòâåðäèë, óäàëÿåì ó÷èòåëÿ
  35. Teacher deleteTeacher = MainWindowViewModel.myConnection.Teachers.First(x => x.Id == idt);
  36. MainWindowViewModel.myConnection.Teachers.Remove(deleteTeacher);
  37. MainWindowViewModel.myConnection.SaveChanges();
  38. await MessageBoxManager.GetMessageBoxStandard("Ñîîáùåíèå", "Ó÷èòåëü óäàëåí", ButtonEnum.Ok).ShowAsync();
  39. MainWindowViewModel.Instance.PageContent = new Show();
  40. }
  41. else
  42. {
  43. // Åñëè ïîëüçîâàòåëü îòìåíèë, ìîæíî, íàïðèìåð, ïîêàçàòü ñîîáùåíèå
  44. await MessageBoxManager.GetMessageBoxStandard("Îòìåíà", "Óäàëåíèå îòìåíåíî.", ButtonEnum.Ok).ShowAsync();
  45. }
  46. }
  47. string _message = "";
  48. public string Message
  49. {
  50. get => _message;
  51. set => this.RaiseAndSetIfChanged(ref _message, value);
  52. }
  53. public void UpdateTeacher(int idt)
  54. {
  55. MainWindowViewModel.Instance.PageContent = new AddPage(idt);
  56. }
  57. public bool _genderm = false;
  58. public bool Genderm
  59. {
  60. get => _genderm;
  61. set
  62. {
  63. _genderm = value;
  64. AllFilters();
  65. }
  66. }
  67. public bool _genderg = false;
  68. public bool Genderg
  69. {
  70. get => _genderg;
  71. set
  72. {
  73. _genderg = value;
  74. AllFilters();
  75. }
  76. }
  77. string _searchTeacher;
  78. public string SearchTeacher
  79. {
  80. get => _searchTeacher;
  81. set
  82. {
  83. _searchTeacher = value;
  84. AllFilters();
  85. }
  86. }
  87. bool _checkTeacher = false;
  88. public bool CheckTeacher
  89. {
  90. get => _checkTeacher;
  91. set
  92. {
  93. _checkTeacher = value;
  94. AllFilters();
  95. }
  96. }
  97. bool _phone = false;
  98. public bool Phone
  99. {
  100. get => _phone;
  101. set
  102. {
  103. _phone = value;
  104. AllFilters();
  105. }
  106. }
  107. List<Discipline> _disciplinesfilter = [new Discipline() { Id = 0, Name = "Âñå äèñöèïëèíû" }, .. MainWindowViewModel.myConnection.Disciplines.ToList()];
  108. public List<Discipline> DisciplinesFilter { get => _disciplinesfilter; }
  109. Discipline _selecteddiscipline = null;
  110. public Discipline SelectedDiscipline
  111. {
  112. get
  113. {
  114. if (_selecteddiscipline == null) return _disciplinesfilter[0];
  115. else { return _selecteddiscipline; }
  116. }
  117. set
  118. {
  119. _selecteddiscipline = value;
  120. AllFilters();
  121. }
  122. }
  123. int _selectedSort = 0;
  124. bool _Visible = false;
  125. bool _Up = true;
  126. bool _Down = true;
  127. public int SelectedSort { get => _selectedSort; set { _selectedSort = value; AllFilters(); } }
  128. public bool Visible { get => _Visible; set => this.RaiseAndSetIfChanged(ref _Visible, value); }
  129. public bool Up { get => _Up; set { this.RaiseAndSetIfChanged(ref _Up, value); AllFilters(); } }
  130. public bool Down { get => _Down; set { this.RaiseAndSetIfChanged(ref _Down, value); AllFilters(); } }
  131. void AllFilters()
  132. {
  133. TeacherList = MainWindowViewModel.myConnection.Teachers.
  134. Include(x => x.Gender).
  135. Include(x => x.TeacherDisciplines).ThenInclude(x => x.Discipline).
  136. Include(x => x.TeacherCourses).ThenInclude(x => x.Course).
  137. ToList();
  138. if (!string.IsNullOrEmpty(_searchTeacher)) {
  139. TeacherList = TeacherList.Where(x => x.FIO.ToLower().Contains(_searchTeacher.ToLower())).ToList();
  140. }
  141. if (_genderm || _genderg)
  142. {
  143. TeacherList = TeacherList.Where(x => (_genderm && x.GenderId ==1)|| (_genderg && x.GenderId == 2)).ToList();
  144. }
  145. if (_phone)
  146. {
  147. TeacherList = TeacherList.Where(x => x.Phone == "").ToList();
  148. }
  149. if (_selecteddiscipline != null && _selecteddiscipline.Id != 0)
  150. {
  151. TeacherList = TeacherList.Where(x => x.TeacherDisciplines.Any(y => y.Discipline.Id == _selecteddiscipline.Id)).ToList();
  152. }
  153. switch (_selectedSort)
  154. {
  155. case 0:
  156. Visible = false;
  157. break;
  158. case 1:
  159. Visible = true;
  160. if (_Down)
  161. {
  162. TeacherList = TeacherList.OrderByDescending(x => x.Experience).ToList();
  163. }
  164. else
  165. {
  166. TeacherList = TeacherList.OrderBy(x => x.Experience).ToList();
  167. }
  168. break;
  169. case 2:
  170. Visible = true;
  171. if (_Down)
  172. {
  173. TeacherList = TeacherList.OrderByDescending(x => x.LastName).ToList();
  174. }
  175. else
  176. {
  177. TeacherList = TeacherList.OrderBy(x => x.LastName).ToList();
  178. }
  179. break;
  180. case 3:
  181. Visible = true;
  182. if (_Down)
  183. {
  184. TeacherList = TeacherList.OrderByDescending(x => x.summa).ToList();
  185. }
  186. else
  187. {
  188. TeacherList = TeacherList.OrderBy(x => x.summa).ToList();
  189. }
  190. break;
  191. }
  192. }
  193. }
  194. }