JewelryMain.xaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace Практическая__5
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для JewelryMain.xaml
  19. /// </summary>
  20. public partial class JewelryMain : Page
  21. {
  22. public JewelryMain()
  23. {
  24. InitializeComponent();
  25. init();
  26. }
  27. private void init()
  28. {
  29. JewelryList.ItemsSource = BaseConnect.kotBase.tbJewelry.ToList();
  30. List<tbTypeJewelry> typeJewelry = BaseConnect.kotBase.tbTypeJewelry.ToList();
  31. tbTypeJewelry all = new tbTypeJewelry()
  32. {
  33. idType = 0,
  34. nameType = "Все типы"
  35. };
  36. CBType.Items.Add(all);
  37. foreach (tbTypeJewelry type in typeJewelry)
  38. {
  39. CBType.Items.Add(type);
  40. }
  41. CBType.DisplayMemberPath = "nameType";
  42. CBType.SelectedValuePath = "idType";
  43. }
  44. private void CompositionJewelryLoaded(object sender, RoutedEventArgs e)
  45. {
  46. TextBlock tbk = (TextBlock)sender;
  47. int index = Convert.ToInt32(tbk.Uid);
  48. List<tbCompositionJewelry> list = BaseConnect.kotBase.tbCompositionJewelry.Where(tb => tb.idJewelry == index).ToList();
  49. if (list.Count > 0)
  50. {
  51. string value = "";
  52. foreach (tbCompositionJewelry a in list)
  53. {
  54. value += a.tbBatchMaterial.tbMaterial.nameMaterial + ", ";
  55. }
  56. value = value.Remove(value.Length - 2);
  57. tbk.Text = value;
  58. }
  59. else tbk.Text = "Состовляющие украшения неизвестны";
  60. }
  61. private void OtherPriceLoaded(object sender, RoutedEventArgs e)
  62. {
  63. TextBlock tbk = (TextBlock)sender;
  64. int index = Convert.ToInt32(tbk.Uid);
  65. List<tbCompositionJewelry> list = BaseConnect.kotBase.tbCompositionJewelry.Where(tb => tb.idJewelry == index).ToList();
  66. if (list.Count > 0)
  67. {
  68. double sum = 0;
  69. foreach (tbCompositionJewelry a in list)
  70. {
  71. tbBatchMaterial price = BaseConnect.kotBase.tbBatchMaterial.Where(tb => tb.idBatch == a.idBatch).ToList().Last();
  72. if (price != null)
  73. {
  74. sum += price.price * a.count;
  75. }
  76. }
  77. tbk.Text = "Себестоимость изделия " + sum + " тыс. руб";
  78. }
  79. }
  80. private void BTBackClick(object sender, RoutedEventArgs e)
  81. {
  82. FrameClass.frameMain.NavigationService.Navigate(new AdminMain());
  83. }
  84. private void BTAddClick(object sender, RoutedEventArgs e)
  85. {
  86. FrameClass.frameMain.NavigationService.Navigate(new JewelryAdd());
  87. }
  88. private void BtnDelJewelryClick(object sender, RoutedEventArgs e)
  89. {
  90. Button btn = (Button)sender;
  91. int index = Convert.ToInt32(btn.Uid);
  92. tbJewelry thisJewelry = BaseConnect.kotBase.tbJewelry.Where(tb => tb.idJewelry == index).ToList().LastOrDefault();
  93. MessageBoxResult result = MessageBox.Show("Вы действительно хотите Удалить это украшение?", "Удалить украшение", MessageBoxButton.YesNo, MessageBoxImage.Question);
  94. if (result == MessageBoxResult.Yes)
  95. {
  96. BaseConnect.kotBase.tbJewelry.Remove(thisJewelry);
  97. BaseConnect.kotBase.SaveChanges();
  98. FrameClass.frameMain.NavigationService.Navigate(new JewelryMain());
  99. MessageBox.Show("Украшение успешно удалено!");
  100. }
  101. }
  102. private void BtnEditJewelryClick(object sender, RoutedEventArgs e)
  103. {
  104. Button btn = (Button)sender;
  105. int index = Convert.ToInt32(btn.Uid);
  106. MessageBoxResult result = MessageBox.Show("Вы действительно хотите Отредактировать это украшение?", "Редактировать информацию об украшении", MessageBoxButton.YesNo, MessageBoxImage.Question);
  107. if (result == MessageBoxResult.Yes)
  108. {
  109. FrameClass.frameMain.NavigationService.Navigate(new JewelryAdd(index));
  110. }
  111. }
  112. private void BTFilterClick(object sender, RoutedEventArgs e)
  113. {
  114. if (GFilter.Visibility == Visibility.Visible)
  115. {
  116. GFilter.Visibility = Visibility.Collapsed;
  117. }
  118. else
  119. {
  120. GFilter.Visibility = Visibility.Visible;
  121. }
  122. }
  123. private void Filter()
  124. {
  125. List<tbJewelry> listJewelry = new List<tbJewelry>();
  126. //тип украшения
  127. tbTypeJewelry thisType = (tbTypeJewelry)CBType.SelectedItem;
  128. if(CBType.SelectedIndex != 0)
  129. {
  130. listJewelry = BaseConnect.kotBase.tbJewelry.Where(tb => tb.tbTypeJewelry.nameType == thisType.nameType).ToList();
  131. }
  132. else
  133. {
  134. listJewelry = BaseConnect.kotBase.tbJewelry.ToList();
  135. }
  136. //поиск по названию
  137. if (!string.IsNullOrWhiteSpace(TBSearchName.Text)) // если строка не пустая и если она не состоит из пробелов
  138. {
  139. listJewelry = listJewelry.Where(tb => tb.name.ToLower().Contains(TBSearchName.Text.ToLower())).ToList();
  140. }
  141. //наличие в магазине
  142. if (CBAvailability.IsChecked == true)
  143. {
  144. List<tbOrderItems> OrderItems = BaseConnect.kotBase.tbOrderItems.ToList();
  145. List<tbJewelry> newList = new List<tbJewelry>();
  146. foreach(tbJewelry jewelry in listJewelry)
  147. {
  148. tbOrderItems availability = OrderItems.Where(tb => tb.idJewelry == jewelry.idJewelry).FirstOrDefault();
  149. if (availability == null)
  150. {
  151. newList.Add(jewelry);
  152. }
  153. }
  154. listJewelry = newList;
  155. }
  156. //сортировка
  157. if(RBPriceUp.IsChecked == true)
  158. {
  159. listJewelry.Sort((tb, tb2) => tb.price.CompareTo(tb2.price));
  160. }
  161. if(RBPriceDown.IsChecked == true)
  162. {
  163. listJewelry.Sort((tb, tb2) => tb.price.CompareTo(tb2.price));
  164. listJewelry.Reverse();
  165. }
  166. //запись о количестве используемых материалов
  167. foreach(tbJewelry element in listJewelry)
  168. {
  169. List<tbCompositionJewelry> composition = BaseConnect.kotBase.tbCompositionJewelry.Where(tb => tb.idJewelry == element.idJewelry).ToList();
  170. if(composition != null)
  171. {
  172. element.count = composition.Count;
  173. }
  174. else
  175. {
  176. element.count = 0;
  177. }
  178. }
  179. if(RBCountUp.IsChecked == true)
  180. {
  181. listJewelry.Sort((tb, tb2) => tb.count.CompareTo(tb2.count));
  182. }
  183. if(RBCountDown.IsChecked == true)
  184. {
  185. listJewelry.Sort((tb, tb2) => tb.count.CompareTo(tb2.count));
  186. listJewelry.Reverse();
  187. }
  188. JewelryList.ItemsSource = listJewelry;
  189. }
  190. private void CountChecked(object sender, RoutedEventArgs e)
  191. {
  192. Filter();
  193. }
  194. private void PriceChecked(object sender, RoutedEventArgs e)
  195. {
  196. Filter();
  197. }
  198. private void AvailabilityChecked(object sender, RoutedEventArgs e)
  199. {
  200. Filter();
  201. }
  202. private void AvailabilityUnchecked(object sender, RoutedEventArgs e)
  203. {
  204. Filter();
  205. }
  206. private void CBTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
  207. {
  208. Filter();
  209. }
  210. private void TBSearchNameTextChanged(object sender, TextChangedEventArgs e)
  211. {
  212. Filter();
  213. }
  214. }
  215. }