CostWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Microsoft.Win32;
  2. using mysql_connector2.res;
  3. using MySqlConnector;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. using Dapper;
  19. namespace mysql_connector2.Windows
  20. {
  21. public partial class CostWindow : Window, INotifyPropertyChanged
  22. {
  23. public event PropertyChangedEventHandler PropertyChanged;
  24. private void Invalidate(string name = "currentProduct")
  25. {
  26. if (PropertyChanged != null)
  27. PropertyChanged(this, new PropertyChangedEventArgs(name));
  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 CostWindow(Product costWindow)
  81. {
  82. InitializeComponent();
  83. DataContext = this;
  84. currentProduct = costWindow;
  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 CostWindow(ProductMaterial selectedMaterial)
  100. {
  101. this.selectedMaterial = selectedMaterial;
  102. }
  103. public static class DBDataProvider
  104. {
  105. public static string connectionString = "Server=kolei.ru; User ID=akapralov; Password=300806; Database=akapralov";
  106. }
  107. public IEnumerable<ProductMaterial> productMaterialList
  108. {
  109. get
  110. {
  111. return Globals.dataProvider.getProductMaterials(currentProduct.ID);
  112. }
  113. }
  114. private void ChangeImage_Click(object sender, RoutedEventArgs e)
  115. {
  116. OpenFileDialog GetImageDialog = new OpenFileDialog();
  117. GetImageDialog.Filter = "Файлы изображений: (*.png, *.jpg)|*.png;*.jpg";
  118. GetImageDialog.InitialDirectory = Environment.CurrentDirectory;
  119. if (GetImageDialog.ShowDialog() == true)
  120. {
  121. currentProduct.Image = GetImageDialog.FileName.Substring(Environment.CurrentDirectory.Length);
  122. Invalidate();
  123. }
  124. }
  125. private string connectionString;
  126. private ProductMaterial selectedMaterial;
  127. private void SaveButton_Click(object sender, RoutedEventArgs e)
  128. {
  129. try
  130. {
  131. if (ProductTypeComboBox.SelectedItem != null)
  132. {
  133. currentProduct.ProductTypeID = ((ProductType)ProductTypeComboBox.SelectedItem).ID;
  134. }
  135. else
  136. {
  137. throw new Exception("Не выбран тип продукта");
  138. }
  139. if (!ValidatePrice(currentProduct.Price))
  140. {
  141. throw new Exception("Некорректная стоимость продукта. Допустимо не более двух знаков после запятой и положительное число.");
  142. }
  143. if (!ValidateArticle(currentProduct.ArticleNumber, currentProduct.ID))
  144. {
  145. throw new Exception("Артикул уже существует.");
  146. }
  147. Globals.dataProvider.saveProduct(currentProduct);
  148. DialogResult = true;
  149. }
  150. catch (Exception ex)
  151. {
  152. MessageBox.Show(ex.Message);
  153. }
  154. }
  155. private bool ValidatePrice(decimal price)
  156. {
  157. return price >= 0 && Math.Round(price, 2) == price;
  158. }
  159. private bool ValidateArticle(string article, int productId)
  160. {
  161. return Globals.dataProvider.GetProductByArticle(article)?.ID != productId;
  162. }
  163. private void ProductListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  164. {
  165. var product = (sender as ListBox).SelectedItem as Product;
  166. var newCostWindow = new CostWindow(product);
  167. if ((bool)newCostWindow.ShowDialog())
  168. {
  169. Invalidate();
  170. }
  171. }
  172. private void AddMaterialButton_Click(object sender, RoutedEventArgs e)
  173. {
  174. var newProductMaterial = new ProductMaterial
  175. {
  176. ProductID = currentProduct.ID,
  177. MaterialID = 0,
  178. Count = 0
  179. };
  180. var addMaterialWindow = new AddMaterial(newProductMaterial);
  181. if (addMaterialWindow.ShowDialog() == true)
  182. {
  183. Invalidate("productMaterialList");
  184. }
  185. }
  186. private void DeleteMaterialTextBlock_MouseDown(object sender, MouseButtonEventArgs e)
  187. {
  188. var productMaterial = (sender as TextBlock).Tag as ProductMaterial;
  189. if (productMaterial != null)
  190. {
  191. Globals.dataProvider.deleteProductMaterial(productMaterial);
  192. Invalidate("productMaterialList");
  193. var textBlock = sender as TextBlock;
  194. if (textBlock != null)
  195. {
  196. textBlock.Visibility = Visibility.Collapsed;
  197. }
  198. }
  199. }
  200. private void DeleteProductButton_Click(object sender, RoutedEventArgs e)
  201. {
  202. try
  203. {
  204. var saleCount = Globals.dataProvider.saleCount(currentProduct.ID);
  205. if (saleCount > 0)
  206. {
  207. throw new Exception("Нельзя удалять продукт с продажами.");
  208. }
  209. Globals.dataProvider.removeProductMaterial(currentProduct.ID);
  210. Globals.dataProvider.removePriceHistory(currentProduct.ID);
  211. Globals.dataProvider.removeProduct(currentProduct.ID);
  212. DialogResult = true;
  213. }
  214. catch (Exception ex)
  215. {
  216. MessageBox.Show(ex.Message);
  217. }
  218. }
  219. private void ProductTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  220. {
  221. if (ProductTypeComboBox.SelectedItem != null)
  222. {
  223. currentProduct.ProductTypeID = ((ProductType)ProductTypeComboBox.SelectedItem).ID;
  224. }
  225. }
  226. private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  227. {
  228. var listView = sender as ListView;
  229. var selectedMaterial = listView.SelectedItem as ProductMaterial;
  230. if (selectedMaterial != null)
  231. {
  232. var costWindow = new CostWindow(selectedMaterial);
  233. if (costWindow.ShowDialog() == true)
  234. {
  235. Invalidate("productMaterialList");
  236. }
  237. }
  238. }
  239. private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
  240. {
  241. var searchText = SearchTextBox.Text.ToLower();
  242. var filteredMaterials = productMaterialList.Where(pm =>
  243. pm.MaterialTitle.ToLower().Contains(searchText)
  244. ).ToList();
  245. ProductMaterialsListView.ItemsSource = filteredMaterials;
  246. }
  247. }
  248. }