ShowTourPageViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using AvaloniaApplicationTest.Models;
  2. using Microsoft.EntityFrameworkCore;
  3. using ReactiveUI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace AvaloniaApplicationTest.ViewModels
  8. {
  9. public class ShowTourPageViewModel : ReactiveObject
  10. {
  11. public List<Tour> LBTours
  12. {
  13. get => lbTours;
  14. set=> this.RaiseAndSetIfChanged(ref lbTours, value);
  15. }
  16. public List<string> CBTypes
  17. {
  18. get
  19. {
  20. types.AddRange(MainWindowViewModel.myConnection.Types.Select(x => x.Name));
  21. return types;
  22. }
  23. }
  24. public List<string> CBTypesSort { get => types_sort; }
  25. public string SearchText
  26. {
  27. get => text;
  28. set
  29. {
  30. this.RaiseAndSetIfChanged(ref text, value);
  31. Filter();
  32. }
  33. }
  34. public bool SelectedActual
  35. {
  36. get => selectedActual;
  37. set
  38. {
  39. selectedActual = value;
  40. Filter();
  41. }
  42. }
  43. public string ValueSelected
  44. {
  45. get => data;
  46. set
  47. {
  48. this.RaiseAndSetIfChanged(ref data, value);
  49. Filter();
  50. }
  51. }
  52. public string ValueSelectedSort
  53. {
  54. get => selected_sort;
  55. set
  56. {
  57. this.RaiseAndSetIfChanged(ref selected_sort, value);
  58. Filter();
  59. }
  60. }
  61. public int AllPrice
  62. {
  63. get => all_price;
  64. set
  65. {
  66. this.RaiseAndSetIfChanged(ref all_price, value);
  67. }
  68. }
  69. bool selectedActual = false;
  70. List<string> types = new List<string>() { "Все типы" };
  71. List<string> types_sort = new List<string>() { "Без сортировки", "По возрастанию", "По убыванию" };
  72. string data = "Все типы";
  73. string selected_sort = "Без сортировки";
  74. string text = "";
  75. List<Tour> lbTours = MainWindowViewModel.myConnection.Tours.ToList();
  76. int all_price = 0;
  77. private void Filter()
  78. {
  79. List<Tour> filtered_tours = MainWindowViewModel.myConnection.Tours.ToList(); ;
  80. if (SearchText.Length > 0)
  81. {
  82. filtered_tours = MainWindowViewModel.myConnection.Tours.Where(x=> x.Name.ToLower().Contains(SearchText.ToLower())|| x.Description.ToLower().Contains(SearchText.ToLower())).ToList();
  83. }
  84. if (ValueSelected!="Все типы" && ValueSelected!=null)
  85. {
  86. Models.Type type = MainWindowViewModel.myConnection.Types.FirstOrDefault(x => x.Name == ValueSelected);
  87. List<Tour> tours = MainWindowViewModel.myConnection.Tours.Include(x => x.Types).Where(x => x.Types.Where(x => x.Name == type.Name).Count() != 0).ToList();
  88. filtered_tours = filtered_tours.Intersect(tours).ToList();
  89. }
  90. if (ValueSelectedSort == "По возрастанию")
  91. {
  92. filtered_tours = filtered_tours.OrderBy(x => x.Price).ToList();
  93. }
  94. if (ValueSelected == "По убыванию")
  95. {
  96. filtered_tours = filtered_tours.OrderBy(x=> x.Price).Reverse().ToList();
  97. }
  98. if (SelectedActual)
  99. {
  100. filtered_tours = filtered_tours.Where(x => x.Status == "Актуален").ToList();
  101. }
  102. int sum = 0;
  103. foreach (Tour tour in filtered_tours)
  104. {
  105. sum += Convert.ToInt32(tour.Price*tour.TicketCount);
  106. }
  107. AllPrice = sum;
  108. LBTours = filtered_tours;
  109. }
  110. }
  111. }