Page_Servicee.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using MedicallCenter;
  2. using MedicallCenter.Clasees;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using ZXing;
  13. using ZXing.Common;
  14. namespace MedicalCenter.Pages
  15. {
  16. /// <summary>
  17. /// Логика взаимодействия для Servicee.xaml
  18. /// </summary>
  19. public partial class Page_Servicee : Page
  20. {
  21. private Service currentServis = new Service();
  22. private int pageNumber = 0;
  23. private int maxpage = 0;
  24. private int pageSize = 20;
  25. List<Serv> services = new List<Serv>();
  26. List<Serv> cur_services = new List<Serv>();
  27. List<DataGridRow> rows = new List<DataGridRow>();
  28. public Page_Servicee()
  29. {
  30. InitializeComponent();
  31. if (CurrentData.worker.Type1.id != 1)
  32. {
  33. btnDeleteService.Visibility = Visibility.Hidden;
  34. btnAddeditService.Visibility = Visibility.Hidden;
  35. }
  36. foreach (var item in CurrentData.db.Service.ToList())
  37. {
  38. services.Add(new Serv(item));
  39. }
  40. cur_services = new List<Serv>(services);
  41. DisplayDataInGrid();
  42. }
  43. class Serv
  44. {
  45. public BitmapImage image { get; set; }
  46. public int id { get; set; }
  47. public string service1 { get; set; }
  48. public Nullable<double> price { get; set; }
  49. public Nullable<int> kod_service { get; set; }
  50. public Serv(Service service)
  51. {
  52. this.id = service.id;
  53. this.service1 = service.service1;
  54. this.price = service.price;
  55. this.kod_service = service.kod_service;
  56. }
  57. public static List<Service> ToServiceList(List<Serv> servList)
  58. {
  59. return servList.Select(s => new Service
  60. {
  61. id = s.id,
  62. service1 = s.service1,
  63. price = s.price,
  64. kod_service = s.kod_service
  65. }).ToList();
  66. }
  67. }
  68. private void btnBack_Click(object sender, RoutedEventArgs e)
  69. {
  70. Manager.frame.Navigate(new Page_Home(CurrentData.worker));
  71. }
  72. private void DisplayDataInGrid()
  73. {
  74. maxpage = services.Count / pageSize;
  75. var currentPageData = services.Skip(pageNumber * pageSize).Take(pageSize);// отображаем только данные для текущей страницы
  76. SetImage(currentPageData);
  77. DataGridService.ItemsSource = currentPageData; // отображаем данные в DataGrid
  78. }
  79. private void NextPageButton_Click(object sender, RoutedEventArgs e)
  80. {
  81. if (pageNumber < maxpage)
  82. {
  83. pageNumber++; // переход на следующую страницу
  84. DisplayDataInGrid(); // отображение данных
  85. }
  86. }
  87. private void PreviousPageButton_Click(object sender, RoutedEventArgs e)
  88. {
  89. if (pageNumber > 0)
  90. {
  91. pageNumber--; // переход на предыдущую страницу
  92. DisplayDataInGrid(); // отображение данных
  93. }
  94. }
  95. private void search_GotFocus(object sender, RoutedEventArgs e)
  96. {
  97. if (search.Text == "Поиск")
  98. search.Text = "";
  99. else if (search.Text == "")
  100. search.Text = "Поиск";
  101. }
  102. private void TBoxSearch_TextChanged(object sender, TextChangedEventArgs e)
  103. {
  104. if (search.Text != "Поиск" && search.Text != "" && DataGridService != null)
  105. {
  106. string s_text = search.Text.ToLower();
  107. services = cur_services;
  108. var s1 = services.Where(n => n.service1.ToLower().Contains(s_text)).ToList();
  109. var s2 = services.Where(n => n.kod_service.ToString().ToLower().Contains(s_text)).ToList();
  110. var s3 = services.Where(n => n.price.ToString().ToLower().Contains(s_text)).ToList();
  111. services = s1.Concat(s3.Concat(s2)).ToList();
  112. DisplayDataInGrid();
  113. }
  114. else
  115. {
  116. if (DataGridService != null)
  117. {
  118. services = cur_services;
  119. DisplayDataInGrid();
  120. }
  121. }
  122. }
  123. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  124. {
  125. if (CurrentData.worker.Type1.id == 1)
  126. {
  127. Serv service = DataGridService.SelectedValue as Serv;
  128. Manager.frame.Navigate(new Page_ServiceeAddEdit(service.id, service.image));
  129. }
  130. }
  131. private void bntDeleteService_Click(object sender, RoutedEventArgs e)
  132. {
  133. var serviceForDelete = DataGridService.SelectedItems.Cast<Serv>().ToList();
  134. List<Service> serviceList = serviceForDelete.Select(s => CurrentData.db.Service.Find(s.id)).ToList();
  135. if (MessageBox.Show($"Вы точно хотите удалить следующие {serviceForDelete} записи", "Внимание",
  136. MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  137. {
  138. try
  139. {
  140. CurrentData.db.Service.RemoveRange(serviceList);
  141. CurrentData.db.SaveChanges();
  142. services.Clear();
  143. foreach (var item in CurrentData.db.Service.ToList())
  144. {
  145. services.Add(new Serv(item));
  146. }
  147. cur_services = new List<Serv>(services);
  148. DisplayDataInGrid();
  149. MessageBox.Show("Записи успешно удалены");
  150. }
  151. catch (Exception ex)
  152. {
  153. MessageBox.Show(ex.ToString());
  154. }
  155. }
  156. }
  157. private void btn_AddEditService_Click(object sender, RoutedEventArgs e)
  158. {
  159. Manager.frame.Navigate(new Page_ServiceeAddEdit(0, null));
  160. }
  161. private void SetImage(IEnumerable<Serv> list)
  162. {
  163. foreach (var item in list)
  164. {
  165. var writer = new BarcodeWriter
  166. {
  167. Format = BarcodeFormat.CODE_128,
  168. Options = new EncodingOptions
  169. {
  170. Height = 100,
  171. Width = 200
  172. }
  173. };
  174. var result = writer.Write(item.kod_service.ToString());
  175. System.Drawing.Bitmap barcodeBitmap = null;
  176. using (var stream = new MemoryStream())
  177. {
  178. result.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  179. barcodeBitmap = new System.Drawing.Bitmap(stream);
  180. stream.Position = 0;
  181. var bitmapImage = new BitmapImage();
  182. bitmapImage.BeginInit();
  183. bitmapImage.StreamSource = stream;
  184. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  185. bitmapImage.EndInit();
  186. item.image = bitmapImage;
  187. }
  188. }
  189. }
  190. }
  191. }