From 4840a136a132030a65e4af30400b1e6d1be26841 Mon Sep 17 00:00:00 2001 From: ultrageese Date: Tue, 9 Jun 2026 14:36:55 +1000 Subject: [PATCH] =?UTF-8?q?=D0=B0=D0=B9=20=D0=B4=D1=83=D1=80=D0=B0=D1=87?= =?UTF-8?q?=D0=BE=D0=BA=20=D0=B7=D0=B0=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=B7=D0=B0=D0=B1=D1=8B=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/src/database.rs | 4 +- code/src/worker_tab.rs | 150 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 141 insertions(+), 13 deletions(-) diff --git a/code/src/database.rs b/code/src/database.rs index 9c313ba..60a41d6 100644 --- a/code/src/database.rs +++ b/code/src/database.rs @@ -117,10 +117,10 @@ impl DBOperator { } Ok(false) } - pub async fn add_worker(&self, worker: Worker) -> Result { + pub async fn add_worker(&self, worker: WorkerRow) -> Result { // sqlx::query!("INSERT INTO Brewery.worker (id, position_id, hire_date, is_fired, full_name) VALUES(0, ?, ?, ?, ?);",worker.position.id, worker.hire_date ,worker.is_fired,worker.full_name) match sqlx::query("INSERT INTO Brewery.worker (id, position_id, hire_date, is_fired, full_name) VALUES(0, ?, ?, ?, ?);") - .bind(worker.position.id) + .bind(worker.position_id) .bind(worker.hire_date) .bind(worker.is_fired) .bind(worker.full_name) diff --git a/code/src/worker_tab.rs b/code/src/worker_tab.rs index a62dc2d..63fa5c1 100644 --- a/code/src/worker_tab.rs +++ b/code/src/worker_tab.rs @@ -3,11 +3,12 @@ use egui::mutex::RwLock; use egui_extras::{Column, TableBuilder}; use std::collections::HashMap; use std::default::Default; +use std::ops::Deref; use std::str::FromStr; use std::sync::Arc; use crate::database::*; -use crate::models::{ModalStatus, ModalWinState, Position, Worker}; +use crate::models::{ModalStatus, ModalWinState, Position, Worker, WorkerRow}; use crate::tab::*; pub struct WorkerTabViewer { db_oper: DBOperator, @@ -63,8 +64,22 @@ impl WorkerTabViewer { ui.horizontal(|ui| { if ui.button("Добавить").clicked() { self.process_worker_state.is_open = true; - self.process_worker_state.data.insert("id".to_string(), "0".to_string()); - self.process_worker_ + self.process_worker_state + .data + .insert("id".to_string(), "0".to_string()); + self.process_worker_state + .data + .insert("full_name".to_string(), "".to_string()); + self.process_worker_state.data.insert( + "position_id".to_string(), + self.positions.read()[0].name.clone(), + ); + self.process_worker_state + .data + .insert("is_fired".to_string(), false.to_string()); + self.process_worker_state + .data + .insert("hire_date".to_string(), "".to_string()); }; if ui.button("Обновить").clicked() { self.update_workers(); @@ -250,14 +265,124 @@ impl WorkerTabViewer { } }); } - if self.process_worker_state.is_open{ - egui::Modal::new(egui::Id::new("process_worker_modal")).show(ui.ctx(), |ui|{ - if ui.button("Закрыть").clicked(){ - self.process_worker_state.is_open = false; - } + if self.process_worker_state.is_open { + egui::Modal::new(egui::Id::new("process_worker_modal")).show(ui.ctx(), |ui| { + if ui.button("Закрыть").clicked() { + self.process_worker_state.is_open = false; + } + if self.process_worker_state.status != ModalStatus::Remove { + egui::Grid::new("process_worker_grid").show(ui, |ui| { + ui.label("Имя:"); + ui.add( + egui::TextEdit::singleline( + self.process_worker_state.data.get_mut("full_name").unwrap(), + ) + .hint_text("Иванов Иван Иванович"), + ); + ui.end_row(); - }); - } + ui.label("Должность:"); + let posname = self + .positions + .read() + .clone() + .iter() + .find(|p| { + p.id.to_string() + == *self.process_worker_state.data.get("position_id").unwrap() + }) + .map(|m| m.name.clone()); + egui::ComboBox::new("process_worker_combobox", "") + .selected_text(posname.unwrap_or("Выберите".to_string())) + .show_ui(ui, |ui| { + for pos in self.positions.read().clone().iter() { + ui.selectable_value( + self.process_worker_state + .data + .get_mut("position_id") + .unwrap(), + pos.id.to_string(), + pos.name.clone(), + ); + } + }); + ui.end_row(); + + let mut size = ui.spacing().interact_size; + size.x = 200.0; + ui.label("Дата найма:"); + ui.add_sized( + size, + egui::TextEdit::singleline( + self.process_worker_state.data.get_mut("hire_date").unwrap(), + ) + .hint_text("1970-01-23"), + ); + }); + + self.process_worker_state.can_finish = self.check_date( + self.process_worker_state + .data + .get("hire_date") + .unwrap() + .clone(), + ); + } + let btext = match self.process_worker_state.status { + ModalStatus::Add => "Добавить", + ModalStatus::Edit => "Сохранить", + ModalStatus::Remove => "Уволить", + }; + if !self.process_worker_state.can_finish { + ui.label("Проверьте правильность всех полей"); + } + let resp = ui.add_enabled( + self.process_worker_state.can_finish, + egui::Button::new(btext), + ); + if resp.clicked() { + match self.process_worker_state.status { + ModalStatus::Add => { + self.rt.block_on(async { + self.db_oper.add_worker(WorkerRow { + id: self + .process_worker_state + .data + .get("id") + .unwrap() + .parse::() + .unwrap(), + full_name: *self + .process_worker_state + .data + .get("full_name") + .unwrap(), + position_id: self + .process_worker_state + .data + .get("position_id") + .unwrap() + .parse::() + .unwrap(), + hire_date: chrono::NaiveDate::parse_from_str( + &self.edit_worker_hire_date, + "%Y-%m-%d", + ) + .unwrap() + .and_hms_opt(0, 0, 0) + .unwrap() + .and_local_timezone(chrono::Local) + .unwrap(), + is_fired: false, + }) + }); + } + ModalStatus::Edit => {} + ModalStatus::Remove => {} + } + } + }); + } TableBuilder::new(ui) .column(Column::auto()) .column(Column::auto()) @@ -304,7 +429,6 @@ impl WorkerTabViewer { }); } }); - } fn show_position(&mut self, ui: &mut egui::Ui) { @@ -488,6 +612,10 @@ impl WorkerTabViewer { }); } } + + fn check_date(&self, date: String) -> bool { + chrono::NaiveDate::parse_from_str(&date, "%Y-%m-%d").is_ok() + } } impl egui_dock::TabViewer for WorkerTabViewer {