ToursViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using Avalonia.Controls;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Logging;
  8. using ReactiveUI;
  9. using TourAgent.Models;
  10. using TourAgent.Views;
  11. namespace TourAgent.ViewModels
  12. {
  13. public class HWindowViewModel : ViewModelBase
  14. {
  15. public static UP_SmirnovContext DB = new UP_SmirnovContext();
  16. UserControl us = new Views.Menu();
  17. public UserControl US
  18. {
  19. get => us;
  20. set => this.RaiseAndSetIfChanged(ref us, value);
  21. }
  22. MenuViewModel menuVM = new MenuViewModel();
  23. public MenuViewModel MenuVM
  24. {
  25. get => menuVM;
  26. set => menuVM = value;
  27. }
  28. ToursViewModel _toursVM = new ToursViewModel();
  29. public ToursViewModel ToursVM
  30. {
  31. get => _toursVM;
  32. set => _toursVM = value;
  33. }
  34. HotelsViewModel _hotelsVM = new HotelsViewModel();
  35. public HotelsViewModel HotelsVM
  36. {
  37. get => _hotelsVM;
  38. set => _hotelsVM = value;
  39. }
  40. public void ChangePageToHotels()
  41. {
  42. US = new Hotels();
  43. }
  44. public void ChangePageToTours()
  45. {
  46. US = new Tours();
  47. }
  48. public void ToMenu()
  49. {
  50. US = new Views.Menu();
  51. }
  52. }
  53. public class ToursViewModel : ReactiveObject
  54. {
  55. UP_SmirnovContext DB = new UP_SmirnovContext();
  56. private List<Tour>? _tours;
  57. public List<Tour>? Tours { get => _tours; set => this.RaiseAndSetIfChanged(ref _tours, value); }
  58. private static List<string>? _types;
  59. public static List<string>? Types { get => _types; set => _types = value; }
  60. private static List<string>? _sort;
  61. public static List<string>? Sort { get => _sort; set => _sort = value; }
  62. private decimal _priceOfAll;
  63. public decimal PriceOfAll
  64. {
  65. get => _priceOfAll;
  66. set => this.RaiseAndSetIfChanged(ref _priceOfAll, value);
  67. }
  68. private bool _checkOrNot = false;
  69. public bool CheckOrNot
  70. {
  71. get => _checkOrNot;
  72. set
  73. {
  74. _checkOrNot = value;
  75. Filter();
  76. }
  77. }
  78. private string? _findTour;
  79. public string? FindTour
  80. {
  81. get => _findTour;
  82. set
  83. {
  84. _findTour = value;
  85. Filter();
  86. }
  87. }
  88. private string? _selectedItem;
  89. public string? SelectedItem
  90. {
  91. get
  92. {
  93. return _selectedItem;
  94. }
  95. set
  96. {
  97. _selectedItem = value;
  98. Filter();
  99. }
  100. }
  101. private string? _selectedSort;
  102. public string? SelectedSort
  103. {
  104. get
  105. {
  106. return _selectedSort;
  107. }
  108. set
  109. {
  110. _selectedSort = value;
  111. Filter();
  112. }
  113. }
  114. public ToursViewModel()
  115. {
  116. Tours = DB.Tours.ToList();
  117. Types = DB.Types.Select(x=>x.Name).ToList();
  118. Types.Insert(0, "Âñå");
  119. SelectedItem = Types[0];
  120. Sort = new List<string> { "Áåç ñîðòèðîâêè", "Ïî âîçðàñòàíèþ öåíû", "Ïî óáûâàíèþ öåíû" };
  121. SelectedSort = Sort[0];
  122. }
  123. public void Filter()
  124. {
  125. Tours = DB.Tours.ToList();
  126. PriceOfAll = 0;
  127. if (SelectedItem != "Âñå")
  128. {
  129. List <TypeOfTour> TypesForFilter = DB.TypeOfTours.ToList();
  130. List <int> IdForFilter = new List<int>();
  131. switch (SelectedItem)
  132. {
  133. case "Ìåæäóíàðîäíûé òóðèçì":
  134. TypesForFilter = TypesForFilter.Where(x=>x.TypeId==1).ToList();
  135. IdForFilter = TypesForFilter.Select(x=>x.TourId).ToList();
  136. Tours = Tours.Where(x=> IdForFilter.Contains(x.Id)).ToList();
  137. break;
  138. case "Âíóòðåííèé òóðèçì":
  139. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 2).ToList();
  140. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  141. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  142. break;
  143. case "Ëå÷åáíî-îçäîðîâèòåëüíûå òóðû":
  144. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 3).ToList();
  145. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  146. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  147. break;
  148. case "Ïëÿæíûå òóðû":
  149. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 4).ToList();
  150. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  151. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  152. break;
  153. case "Ýêñêóðñèîííûå òóðû":
  154. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 5).ToList();
  155. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  156. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  157. break;
  158. case "Ñïåöèàëèçèðîâàííûå äåòñêèå òóðû":
  159. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 6).ToList();
  160. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  161. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  162. break;
  163. case "Îáñëóæèâàíèå êîðïîðàòèâíûõ êëèåíòîâ ïî çàêàçó":
  164. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 7).ToList();
  165. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  166. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  167. break;
  168. case "Ãîðíîëûæíûå êóðîðòû":
  169. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 8).ToList();
  170. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  171. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  172. break;
  173. case "Êóëüòóðíî-èñòîðè÷åñêèå òóðû":
  174. TypesForFilter = TypesForFilter.Where(x => x.TypeId == 9).ToList();
  175. IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
  176. Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. if(CheckOrNot)
  183. {
  184. Tours = Tours.Where(x => x.IsActual[0]==true).ToList();
  185. }
  186. if(FindTour!=null && FindTour != "")
  187. {
  188. Tours = Tours.Where(x=>x.Name.ToUpper().Contains(FindTour.ToString().ToUpper()) || x.DescriptionWithoutNull.ToUpper().Contains(FindTour.ToString().ToUpper())).ToList();
  189. }
  190. if(SelectedSort != "Áåç ñîðòèðîâêè")
  191. {
  192. switch(SelectedSort)
  193. {
  194. case "Ïî âîçðàñòàíèþ öåíû":
  195. Tours = Tours.OrderBy(x=>x.Price).ToList();
  196. break;
  197. case "Ïî óáûâàíèþ öåíû":
  198. Tours = Tours.OrderByDescending(x => x.Price).ToList();
  199. break;
  200. }
  201. }
  202. foreach(Tour t in Tours)
  203. {
  204. PriceOfAll += (t.Price * t.TicketCount);
  205. }
  206. }
  207. }
  208. }