TourViewModel.cs 7.9 KB

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