ListPage.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using chernovik_company2.Pages;
  2. using Microsoft.Office.Interop.Word;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using Word = Microsoft.Office.Interop.Word;
  18. namespace chernovik_company2
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для ListPage.xaml
  22. /// </summary>
  23. public partial class ListPage : System.Windows.Controls.Page
  24. {
  25. List<Material>MaterialsStart= BaseClass.Base.Material.ToList();
  26. List<Material> MaterialsFilter = BaseClass.Base.Material.ToList(); //не используется
  27. List<Material> MaterialsSearch = BaseClass.Base.Material.ToList(); //не используется
  28. int sortType = -1;
  29. public ListPage()
  30. {
  31. InitializeComponent();
  32. LVMaterials.ItemsSource = MaterialsStart;
  33. Filter.Items.Add("Все типы");
  34. List<MaterialType> mt = BaseClass.Base.MaterialType.ToList();
  35. for (int i = 0; i < mt.Count; i++)
  36. {
  37. Filter.Items.Add(mt[i].Title);
  38. }
  39. Filter.SelectedIndex = 0;
  40. }
  41. private void TbSupplier_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. TextBlock tb = (TextBlock)sender;
  44. int index = Convert.ToInt32(tb.Uid);
  45. List<MaterialSupplier> mtList = BaseClass.Base.MaterialSupplier.Where(x => x.MaterialID == index).ToList();
  46. string str = "";
  47. foreach (MaterialSupplier item in mtList)
  48. {
  49. str += item.Supplier.Title + ", ";
  50. }
  51. if (mtList.Count == 0)
  52. {
  53. tb.Text = "Поставщики отсуствуют.";
  54. }
  55. else
  56. {
  57. tb.Text = "Поставщики:" + str.Substring(0, str.Length - 2);
  58. }
  59. }
  60. public void GetData(string search = "", string filter = "")
  61. {
  62. var list = BaseClass.Base.Material.ToList();
  63. int countBefore = list.Count;
  64. if ( !string.IsNullOrWhiteSpace(search))
  65. {
  66. list = list.Where(m => m.Title.Contains(search) || m.Title.ToLower().Contains(search) || m.Description.Contains(search)).ToList();
  67. }
  68. if (filter != "Все типы")
  69. {
  70. list = list.Where(m => m.MaterialType.Title == filter).ToList();
  71. }
  72. if (sortType == 1)
  73. {
  74. switch (Sort.SelectedIndex)
  75. {
  76. case 0: list = list.OrderByDescending(m => m.Title).ToList(); break;
  77. case 1: list = list.OrderByDescending(m => m.CountInStock).ToList(); break;
  78. case 2: list = list.OrderByDescending(m => m.Cost).ToList(); break;
  79. }
  80. }
  81. if (sortType == 0)
  82. {
  83. switch (Sort.SelectedIndex)
  84. {
  85. case 0: list = list.OrderBy(m => m.Title).ToList(); break;
  86. case 1: list = list.OrderBy(m => m.CountInStock).ToList(); break;
  87. case 2: list = list.OrderBy(m => m.Cost).ToList(); break;
  88. }
  89. }
  90. LVMaterials.ItemsSource = list;
  91. TBCount.Text = $"Общее количество записей: {list.Count} из {countBefore}";
  92. }
  93. private void Filters() //не используется
  94. {
  95. int index = Filter.SelectedIndex;
  96. if (index != 0)
  97. {
  98. MaterialsFilter = MaterialsStart.Where(x => x.MaterialTypeID == index).ToList();
  99. }
  100. else
  101. {
  102. MaterialsFilter = MaterialsStart;
  103. }
  104. }
  105. private void Search_Loaded(object sender, RoutedEventArgs e)
  106. {
  107. if (Search.Text != "")
  108. {
  109. MaterialsSearch = MaterialsStart.Where(x=> x.Title.Contains(Search.Text)).ToList();
  110. }
  111. }
  112. private void Search_TextChanged(object sender, TextChangedEventArgs e)
  113. {
  114. if (Filter != null)
  115. {
  116. GetData(Search.Text, Filter.Text);
  117. }
  118. }
  119. private void Filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  120. {
  121. GetData(Search.Text, (Filter.SelectedValue.ToString()));
  122. }
  123. private void BtnDesc_Click(object sender, RoutedEventArgs e)
  124. {
  125. sortType = 1;
  126. GetData(Search.Text, Filter.Text);
  127. }
  128. private void BtnAsc_Click(object sender, RoutedEventArgs e)
  129. {
  130. sortType = 0;
  131. GetData(Search.Text, Filter.Text);
  132. }
  133. private void BtnEdit_Click(object sender, RoutedEventArgs e)
  134. {
  135. Button btn = (Button)sender;
  136. int id = int.Parse(btn.Uid);
  137. FrameClass.FrameMain.Navigate(new CreateOrUpdate(id));
  138. }
  139. private void LVMaterials_SelectionChanged(object sender, SelectionChangedEventArgs e)
  140. {
  141. if (LVMaterials.SelectedItems.Count >= 2)
  142. {
  143. BtnChangeMinCount.Visibility = Visibility.Visible;
  144. }
  145. else
  146. {
  147. BtnChangeMinCount.Visibility = Visibility.Hidden;
  148. }
  149. }
  150. private void BtnChangeMinCount_Click(object sender, RoutedEventArgs e)
  151. {
  152. List<Material> materials = new List<Material>();
  153. foreach (var item in LVMaterials.SelectedItems)
  154. {
  155. materials.Add((Material)item);
  156. }
  157. new ChangeMinCount(materials).Show();
  158. }
  159. private void BtnAddNew_Click(object sender, RoutedEventArgs e)
  160. {
  161. FrameClass.FrameMain.Navigate(new CreateOrUpdate());
  162. }
  163. private void ExportToWord_Click(object sender, RoutedEventArgs e)
  164. {
  165. var allUsers = BaseClass.Base.MaterialType.ToList();
  166. var allCategories = BaseClass.Base.Material.ToList();
  167. var application = new Word.Application();
  168. Word.Document document = application.Documents.Add();
  169. foreach (var Material in allUsers)
  170. {
  171. Word.Paragraph userParagraph = document.Paragraphs.Add();
  172. Word.Range userRange = userParagraph.Range;
  173. userParagraph.set_Style("Обычный");
  174. userRange.InsertParagraphAfter();
  175. Word.Paragraph tableParagraph = document.Paragraphs.Add();
  176. Word.Range tableRange = tableParagraph.Range;
  177. Word.Table matTable = document.Tables.Add(tableRange, allCategories.Count()+1, 3);
  178. matTable.Borders.InsideLineStyle = matTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
  179. matTable.Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  180. Word.Range cellRange;
  181. cellRange = matTable.Cell(1, 1).Range;
  182. cellRange.Text = "Материал";
  183. cellRange = matTable.Cell(1, 2).Range;
  184. cellRange.Text = "ТипМатериала";
  185. cellRange = matTable.Cell(1, 3).Range;
  186. cellRange.Text = "нинаю";
  187. matTable.Rows[1].Range.Bold = 1;
  188. for (int i = 0; i < allCategories.Count(); i++)
  189. {
  190. var currentMaterial = allCategories[i];
  191. cellRange = matTable.Cell(i + 2, 2).Range;
  192. //Word.InlineShape imageShape = cellRange.InlineShapes.AddPicture(AppDomain.CurrentDomain.BaseDirectory + "..\\...\\" + currentMaterial.Image);
  193. cellRange = matTable.Cell(i + 2, 1).Range;
  194. cellRange.Text = currentMaterial.Title;
  195. }
  196. }
  197. document.SaveAs2(@"X:\bebra\Text1.docx");
  198. }
  199. }
  200. }