LateOrdersPage.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using ComputerShopWpf.Services;
  7. using Npgsql;
  8. namespace ComputerShopWpf.Views
  9. {
  10. public partial class LateOrdersPage : Page
  11. {
  12. private readonly DatabaseService _database = new DatabaseService();
  13. public LateOrdersPage()
  14. {
  15. InitializeComponent();
  16. LoadClients();
  17. }
  18. private void LoadClients()
  19. {
  20. try
  21. {
  22. DataTable table = _database.ExecuteSelect(
  23. "SELECT client_id, last_name || ' ' || first_name AS client_name FROM clients ORDER BY last_name, first_name;");
  24. ClientsComboBox.ItemsSource = table.DefaultView;
  25. if (ClientsComboBox.Items.Count > 0)
  26. {
  27. ClientsComboBox.SelectedIndex = 0;
  28. }
  29. }
  30. catch (Exception ex)
  31. {
  32. ShowDatabaseError(ex);
  33. }
  34. }
  35. private void ShowLateOrdersButton_Click(object sender, RoutedEventArgs e)
  36. {
  37. if (ClientsComboBox.SelectedValue == null)
  38. {
  39. MessageBox.Show("Выберите клиента.", "Просроченные заказы", MessageBoxButton.OK, MessageBoxImage.Information);
  40. return;
  41. }
  42. try
  43. {
  44. int clientId = Convert.ToInt32(ClientsComboBox.SelectedValue);
  45. DataTable table = _database.ExecuteSelect(
  46. "SELECT * FROM fn_get_late_orders_by_client(@client_id);",
  47. new Dictionary<string, object> { { "@client_id", clientId } });
  48. LateOrdersDataGrid.ItemsSource = table.DefaultView;
  49. CountTextBlock.Text = "Найдено записей: " + table.Rows.Count;
  50. if (table.Rows.Count == 0)
  51. {
  52. MessageBox.Show("У выбранного клиента нет просроченных заказов.", "Просроченные заказы", MessageBoxButton.OK, MessageBoxImage.Information);
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. ShowDatabaseError(ex);
  58. }
  59. }
  60. private void ShowOrderItemsButton_Click(object sender, RoutedEventArgs e)
  61. {
  62. DataRowView row = LateOrdersDataGrid.SelectedItem as DataRowView;
  63. if (row == null)
  64. {
  65. MessageBox.Show("Выберите заказ.", "Просроченные заказы", MessageBoxButton.OK, MessageBoxImage.Information);
  66. return;
  67. }
  68. int orderId = Convert.ToInt32(row["order_id"]);
  69. string orderInfo = "Просроченный заказ №" + orderId + ", клиент: " + Convert.ToString(row["client_name"]);
  70. NavigationService.Navigate(new OrderItemsPage(orderId, orderInfo));
  71. }
  72. private static void ShowDatabaseError(Exception ex)
  73. {
  74. PostgresException postgresException = ex as PostgresException;
  75. if (postgresException != null)
  76. {
  77. MessageBox.Show("Ошибка PostgreSQL: " + postgresException.MessageText, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  78. return;
  79. }
  80. NpgsqlException npgsqlException = ex as NpgsqlException;
  81. if (npgsqlException != null)
  82. {
  83. MessageBox.Show("Ошибка подключения к базе данных: " + npgsqlException.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  84. return;
  85. }
  86. MessageBox.Show("Ошибка: " + ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  87. }
  88. }
  89. }