EditProductWindow.xaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Shapes;
  14. using sql_pagining.models;
  15. using Dapper;
  16. using MySqlConnector;
  17. using System.ComponentModel;
  18. using Microsoft.Win32;
  19. namespace sql_pagining.Windows
  20. {
  21. public partial class EditProductWindow : Window, INotifyPropertyChanged
  22. {
  23. public event PropertyChangedEventHandler PropertyChanged;
  24. private void Invalidate()
  25. {
  26. if (PropertyChanged != null)
  27. PropertyChanged(this, new PropertyChangedEventArgs("currentProduct"));
  28. }
  29. public Visibility DeleteProductVisibly
  30. {
  31. get
  32. {
  33. if (currentProduct != null)
  34. {
  35. return currentProduct.ID == 0 ? Visibility.Collapsed : Visibility.Visible;
  36. }
  37. else
  38. {
  39. return Visibility.Collapsed;
  40. }
  41. }
  42. }
  43. public Product currentProduct { get; set; }
  44. public List<ProductType> productTypeList { get; set; }
  45. public int selectedProductIndex { get; set; } = -1;
  46. public void saveProduct(Product product)
  47. {
  48. using (MySqlConnection db = new MySqlConnection(connectionString))
  49. {
  50. if (product.ID == 0)
  51. {
  52. using (MySqlCommand cmd = new MySqlCommand("INSERT INTO Product (ProductTypeID, Title, ArticleNumber, Description, Image, ProductionPersonCount, ProductionWorkshopNumber, MinCostForAgent) " +
  53. "VALUES (@ProductTypeID, @Title, @ArticleNumber, @Description, @Image, @ProductionPersonCount, @ProductionWorkshopNumber, @MinCostForAgent)", db))
  54. {
  55. cmd.Parameters.AddWithValue("@ProductTypeID", product.ProductTypeID);
  56. cmd.Parameters.AddWithValue("@Title", product.Title);
  57. cmd.Parameters.AddWithValue("@ArticleNumber", product.ArticleNumber);
  58. cmd.Parameters.AddWithValue("@Description", product.Description);
  59. cmd.Parameters.AddWithValue("@Image", product.Image);
  60. cmd.Parameters.AddWithValue("@ProductionPersonCount", product.ProductionPersonCount);
  61. cmd.Parameters.AddWithValue("@ProductionWorkshopNumber", product.ProductionWorkshopNumber);
  62. cmd.Parameters.AddWithValue("@MinCostForAgent", product.MinCostForAgent);
  63. db.Open();
  64. cmd.ExecuteNonQuery();
  65. product.ID = (int)cmd.LastInsertedId;
  66. db.Close();
  67. }
  68. }
  69. else
  70. {
  71. db.Execute("UPDATE Product SET Title=@Title, ProductTypeID=@ProductTypeID, " +
  72. "ArticleNumber=@ArticleNumber, Description=@Description, " +
  73. "Image=@Image, ProductionPersonCount=@ProductionPersonCount, " +
  74. "ProductionWorkshopNumber=@ProductionWorkshopNumber, " +
  75. "MinCostForAgent=@MinCostForAgent " +
  76. "WHERE ID=@ID", product);
  77. }
  78. }
  79. }
  80. public EditProductWindow(Product editWindow)
  81. {
  82. InitializeComponent();
  83. DataContext = this;
  84. currentProduct = editWindow;
  85. if (currentProduct != null)
  86. {
  87. root.Title = currentProduct.ID == 0 ? "Новый продукт" : "Редактирование продукта";
  88. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  89. if (currentProduct.ID > 0)
  90. {
  91. selectedProductIndex = productTypeList.FindIndex(pt => pt.ID == currentProduct.ProductTypeID);
  92. }
  93. }
  94. else
  95. {
  96. MessageBox.Show("Продукт не был передан.");
  97. }
  98. }
  99. public static class DBDataProvider
  100. {
  101. public static string connectionString = "Server=kolei.ru; User ID=sbahtina; Password=010906; Database=sbahtina";
  102. }
  103. private void ChangeImage_Click(object sender, RoutedEventArgs e)
  104. {
  105. OpenFileDialog GetImageDialog = new OpenFileDialog();
  106. GetImageDialog.Filter = "Файлы изображений: (*.png, *.jpg)|*.png;*.jpg";
  107. GetImageDialog.InitialDirectory = Environment.CurrentDirectory;
  108. if (GetImageDialog.ShowDialog() == true)
  109. {
  110. currentProduct.Image = GetImageDialog.FileName.Substring(Environment.CurrentDirectory.Length);
  111. Invalidate();
  112. }
  113. }
  114. private string connectionString;
  115. private void SaveButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. try
  118. {
  119. if (ProductTypeComboBox.SelectedItem != null)
  120. {
  121. currentProduct.ProductTypeID = ((ProductType)ProductTypeComboBox.SelectedItem).ID;
  122. }
  123. else
  124. {
  125. throw new Exception("Не выбран тип продукта");
  126. }
  127. if (!ValidatePrice(currentProduct.Price))
  128. {
  129. throw new Exception("Некорректная стоимость продукта. Допустимо не более двух знаков после запятой и положительное число.");
  130. }
  131. if (!ValidateArticle(currentProduct.ArticleNumber, currentProduct.ID))
  132. {
  133. throw new Exception("Артикул уже существует.");
  134. }
  135. Globals.dataProvider.saveProduct(currentProduct);
  136. DialogResult = true;
  137. }
  138. catch (Exception ex)
  139. {
  140. MessageBox.Show(ex.Message);
  141. }
  142. }
  143. private bool ValidatePrice(decimal price)
  144. {
  145. return price >= 0 && Math.Round(price, 2) == price;
  146. }
  147. private bool ValidateArticle(string article, int productId)
  148. {
  149. return Globals.dataProvider.GetProductByArticle(article)?.ID != productId;
  150. }
  151. private void DeleteProductButton_Click(object sender, RoutedEventArgs e)
  152. {
  153. try
  154. {
  155. var saleCount = Globals.dataProvider.saleCount(currentProduct.ID);
  156. if (saleCount > 0)
  157. {
  158. throw new Exception("Нельзя удалять продукт с продажами.");
  159. }
  160. Globals.dataProvider.removeProductMaterial(currentProduct.ID);
  161. Globals.dataProvider.removePriceHistory(currentProduct.ID);
  162. Globals.dataProvider.removeProduct(currentProduct.ID);
  163. DialogResult = true;
  164. }
  165. catch (Exception ex)
  166. {
  167. MessageBox.Show(ex.Message);
  168. }
  169. }
  170. private void ProductTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  171. {
  172. if (ProductTypeComboBox.SelectedItem != null)
  173. {
  174. currentProduct.ProductTypeID = ((ProductType)ProductTypeComboBox.SelectedItem).ID;
  175. }
  176. }
  177. }
  178. }