using Npgsql; using System; using System.Configuration; using System.Data; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class ReturnBookForm : Form { private readonly string connStr = ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString; private int selectedOrderId = 0; private bool isLoading = false; public ReturnBookForm() { InitializeComponent(); this.dataGridViewReaders.SelectionChanged += dataGridViewReaders_SelectionChanged; this.dataGridViewLoans.SelectionChanged += dataGridViewLoans_SelectionChanged; } private void ReturnBookForm_Load(object sender, EventArgs e) { LoadLibraryName(); } private void LoadLibraryName() { string query = "SELECT name FROM libraries WHERE id = @libId"; using (var conn = new NpgsqlConnection(connStr)) using (var cmd = new NpgsqlCommand(query, conn)) { cmd.Parameters.AddWithValue("libId", Session.LibraryId); conn.Open(); var result = cmd.ExecuteScalar(); labelLibraryName.Text = result != null ? "Библиотека: " + result.ToString() : "Библиотека: Неизвестно"; } } private void btnSearch_Click(object sender, EventArgs e) { string searchText = txtReaderSearch.Text.Trim(); if (string.IsNullOrEmpty(searchText) || searchText == "Введите фамилию или телефон") { MessageBox.Show("Введите фамилию, имя или телефон читателя"); return; } string query = @" SELECT id, name, lastname, phone, email, (SELECT COUNT(*) FROM orders WHERE user_id = u.id AND fact_return_date IS NULL) AS active_loans FROM users u WHERE role_name = 'reader' AND (name ILIKE @search OR lastname ILIKE @search OR phone ILIKE @search OR email ILIKE @search) ORDER BY lastname, name"; DataTable dt = new DataTable(); using (var conn = new NpgsqlConnection(connStr)) using (var cmd = new NpgsqlCommand(query, conn)) { cmd.Parameters.AddWithValue("search", $"%{searchText}%"); conn.Open(); using (var adapter = new NpgsqlDataAdapter(cmd)) { adapter.Fill(dt); } } dataGridViewReaders.DataSource = dt; if (dataGridViewReaders.Columns["id"] != null) dataGridViewReaders.Columns["id"].Visible = false; dataGridViewLoans.DataSource = null; selectedOrderId = 0; } private void dataGridViewReaders_SelectionChanged(object sender, EventArgs e) { if (isLoading) return; if (dataGridViewReaders.CurrentRow == null) return; int readerId = Convert.ToInt32(dataGridViewReaders.CurrentRow.Cells["id"].Value); LoadActiveLoans(readerId); } private void LoadActiveLoans(int readerId) { isLoading = true; string query = @" SELECT o.d AS order_id, b.title || ' (инв. ' || be.isbn || ')' AS book_display, o.give_date, o.return_date FROM orders o JOIN book_exemplar be ON o.book_exemplar = be.isbn JOIN book b ON be.book_id = b.id WHERE o.user_id = @readerId AND o.fact_return_date IS NULL AND be.library_id = @libId -- фильтр по библиотеке экземпляра ORDER BY o.return_date"; DataTable dt = new DataTable(); using (var conn = new NpgsqlConnection(connStr)) using (var cmd = new NpgsqlCommand(query, conn)) { cmd.Parameters.AddWithValue("readerId", readerId); cmd.Parameters.AddWithValue("libId", Session.LibraryId); conn.Open(); using (var adapter = new NpgsqlDataAdapter(cmd)) { adapter.Fill(dt); } } dataGridViewLoans.DataSource = dt; if (dataGridViewLoans.Columns["order_id"] != null) dataGridViewLoans.Columns["order_id"].Visible = false; isLoading = false; } private void dataGridViewLoans_SelectionChanged(object sender, EventArgs e) { if (dataGridViewLoans.CurrentRow == null) { selectedOrderId = 0; return; } selectedOrderId = Convert.ToInt32(dataGridViewLoans.CurrentRow.Cells["order_id"].Value); } private void buttonReturn_Click(object sender, EventArgs e) { if (selectedOrderId == 0) { MessageBox.Show("Выберите книгу для возврата", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { using (var conn = new NpgsqlConnection(connStr)) { conn.Open(); string sql = "SELECT return_book(@orderId, @condition)"; using (var cmd = new NpgsqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("orderId", selectedOrderId); cmd.Parameters.AddWithValue("condition", "good"); cmd.ExecuteNonQuery(); MessageBox.Show("Книга успешно возвращена", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); txtReaderSearch.Text = "Введите фамилию или телефон"; txtReaderSearch.ForeColor = System.Drawing.Color.Gray; dataGridViewReaders.DataSource = null; dataGridViewLoans.DataSource = null; selectedOrderId = 0; } } } catch (PostgresException ex) { MessageBox.Show($"Ошибка БД: {ex.MessageText}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"Ошибка: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void txtReaderSearch_Enter(object sender, EventArgs e) { if (txtReaderSearch.Text == "Введите фамилию или телефон") { txtReaderSearch.Text = ""; txtReaderSearch.ForeColor = System.Drawing.Color.Black; } } private void txtReaderSearch_Leave(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtReaderSearch.Text)) { txtReaderSearch.Text = "Введите фамилию или телефон"; txtReaderSearch.ForeColor = System.Drawing.Color.Gray; } } } }