OrdersWindow.xaml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Npgsql;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. namespace WpfApp1
  8. {
  9. public partial class OrdersWindow : Window
  10. {
  11. private const string Conn = "Host=80.250.189.52;Port=5432;Database=dbwork;Username=kotov_aa;Password=0$s%MB3&F6#";
  12. public OrdersWindow()
  13. {
  14. InitializeComponent();
  15. LoadClients();
  16. LoadOrders(null, false);
  17. }
  18. private void LoadClients()
  19. {
  20. using var conn = new NpgsqlConnection(Conn);
  21. conn.Open();
  22. var cmd = new NpgsqlCommand("SELECT customerid, companyname FROM Customers ORDER BY companyname", conn);
  23. var adapter = new NpgsqlDataAdapter(cmd);
  24. DataTable dt = new DataTable();
  25. adapter.Fill(dt);
  26. cmbClients.ItemsSource = dt.DefaultView;
  27. cmbClients.SelectedIndex = -1;
  28. }
  29. private void LoadOrders(int? clientId, bool onlyLate)
  30. {
  31. string sql = "SELECT o.orderid, c.companyname, o.orderdate, o.plandate, o.factdate, o.status " +
  32. "FROM Orders o JOIN Customers c ON o.customerid = c.customerid WHERE 1=1";
  33. var parameters = new List<NpgsqlParameter>();
  34. if (clientId.HasValue)
  35. {
  36. sql += " AND o.customerid = @cid";
  37. parameters.Add(new NpgsqlParameter("@cid", clientId.Value));
  38. }
  39. if (onlyLate)
  40. {
  41. sql += " AND (o.factdate > o.plandate OR (o.factdate IS NULL AND o.plandate < CURRENT_DATE))";
  42. }
  43. sql += " ORDER BY o.plandate DESC";
  44. using var conn = new NpgsqlConnection(Conn);
  45. conn.Open();
  46. using var cmd = new NpgsqlCommand(sql, conn);
  47. if (parameters.Count > 0) cmd.Parameters.AddRange(parameters.ToArray());
  48. var adapter = new NpgsqlDataAdapter(cmd);
  49. DataTable dt = new DataTable();
  50. adapter.Fill(dt);
  51. dgOrders.ItemsSource = dt.DefaultView;
  52. }
  53. private void BtnFilter_Click(object sender, RoutedEventArgs e)
  54. {
  55. int? clientId = cmbClients.SelectedValue != null ? Convert.ToInt32(cmbClients.SelectedValue) : null;
  56. bool onlyLate = chkLate.IsChecked == true;
  57. LoadOrders(clientId, onlyLate);
  58. }
  59. private void BtnReset_Click(object sender, RoutedEventArgs e)
  60. {
  61. cmbClients.SelectedIndex = -1;
  62. chkLate.IsChecked = false;
  63. LoadOrders(null, false);
  64. }
  65. }
  66. }