diff --git a/code/src/app.rs b/code/src/app.rs index f579c9e..664d869 100644 --- a/code/src/app.rs +++ b/code/src/app.rs @@ -9,14 +9,15 @@ use crate::{ database::DBOperator, material_tab::MaterialTabViewer, models::{ - Equipment, LoginModalState, LoginStatus, Material, MaterialCategory, MaterialRow, - ModalStatus, ModalWinState, Position, Salary, SalaryModalState, Worker, + Equipment, EquipmentModalState, LoginModalState, LoginStatus, Material, MaterialCategory, + MaterialRow, ModalStatus, ModalWinState, Position, Salary, SalaryModalState, Worker, }, recipe_tab::RecipeTabViewer, reports::Reporter, tab::*, worker_tab::{self, *}, }; +use chrono::NaiveDate; use egui::{TextBuffer, Vec2, mutex::RwLock}; use egui_dock::{DockArea, Style, TabViewer}; use egui_extras::{Column, TableBuilder}; @@ -55,13 +56,13 @@ impl Default for App<'_> { tab_type: TabTypes::Recipe, title: "Рецепты".to_owned(), }, - Tab{ + Tab { tab_type: TabTypes::Order, title: "Заказы".to_owned(), }, - Tab{ - tab_type: TabTypes::Client, - title: "Клиенты".to_owned(), + Tab { + tab_type: TabTypes::Client, + title: "Клиенты".to_owned(), }, Tab { tab_type: TabTypes::Settings, @@ -187,7 +188,7 @@ struct MainTabViewer { material_tree: egui_dock::DockState, recipe_tab: RecipeTabViewer, rt: tokio::runtime::Runtime, - equipment_modal_state: ModalWinState, + equipment_modal_state: EquipmentModalState, salary_modal_state: SalaryModalState, is_dark_theme: bool, interface_scale_ratio: f32, @@ -236,15 +237,10 @@ impl Default for MainTabViewer { is_dark_theme: true, interface_scale_ratio: 1.2, - equipment_modal_state: ModalWinState { + equipment_modal_state: EquipmentModalState { is_open: false, can_finish: false, - data: HashMap::from([ - ("id".to_owned(), String::new()), - ("inv_number".to_owned(), String::new()), - ("maintenance_date".to_owned(), String::new()), - ("worker_id".to_owned(), String::new()), - ]), + equipment: Equipment::default(), status: ModalStatus::Add, }, salary_modal_state: SalaryModalState { @@ -325,21 +321,7 @@ impl MainTabViewer { if ui.button("Добавить").clicked() { self.equipment_modal_state.is_open = true; self.equipment_modal_state.status = ModalStatus::Add; - self.equipment_modal_state - .data - .insert("id".into(), "0".into()); - self.equipment_modal_state - .data - .insert("name".into(), "".into()); - self.equipment_modal_state - .data - .insert("inv_number".into(), "".into()); - self.equipment_modal_state - .data - .insert("worker_id".into(), "0".into()); - self.equipment_modal_state - .data - .insert("maintenance_date".into(), "".into()); + self.equipment_modal_state.equipment = Equipment::default(); } }); } @@ -396,46 +378,21 @@ impl MainTabViewer { if ui.button("Списать").clicked() { self.equipment_modal_state.is_open = true; self.equipment_modal_state.status = ModalStatus::Remove; - self.equipment_modal_state - .data - .insert("id".into(), eq.id.to_string()); - self.equipment_modal_state - .data - .insert("name".into(), eq.name.clone()); - self.equipment_modal_state - .data - .insert("inv_number".into(), eq.inv_number.clone()); - self.equipment_modal_state - .data - .insert("worker_id".into(), eq.worker.id.to_string()); + self.equipment_modal_state.equipment = eq.clone(); } } }); if row.response().double_clicked() { self.equipment_modal_state.is_open = true; self.equipment_modal_state.status = ModalStatus::Edit; - self.equipment_modal_state - .data - .insert("id".into(), eq.id.to_string()); - self.equipment_modal_state - .data - .insert("name".into(), eq.name.clone()); - self.equipment_modal_state - .data - .insert("inv_number".into(), eq.inv_number.clone()); - self.equipment_modal_state - .data - .insert("worker_id".into(), eq.worker.id.to_string()); - self.equipment_modal_state.data.insert( - "maintenance_date".into(), - eq.maintenance_date.date_naive().to_string(), - ); + self.equipment_modal_state.equipment = eq.clone(); } }); } }); if self.equipment_modal_state.is_open { egui::Modal::new(egui::Id::new("process_equipment_modal")).show(ui.ctx(), |ui| { + let mut date_okay = false; if ui.button("Закрыть").clicked() || ui.input(|i| i.key_pressed(egui::Key::Escape)) { self.equipment_modal_state.is_open = false; @@ -443,53 +400,38 @@ impl MainTabViewer { if self.equipment_modal_state.status != ModalStatus::Remove { egui::Grid::new("process_equipment_grid").show(ui, |ui| { ui.label("Название"); - let mut name = self.equipment_modal_state.data.get_mut("name").unwrap(); ui.add_sized( Vec2::new(200.0, 20.0), - egui::TextEdit::singleline(name).hint_text("Название оборудования"), + egui::TextEdit::singleline( + &mut self.equipment_modal_state.equipment.name, + ) + .hint_text("Название оборудования"), ); ui.end_row(); ui.label("Инв. номер"); ui.add( egui::TextEdit::singleline( - self.equipment_modal_state - .data - .get_mut("inv_number") - .unwrap(), + &mut self.equipment_modal_state.equipment.inv_number, ) .hint_text("НН-00"), ); ui.end_row(); ui.label("Ответственный"); - let workname = self - .workers - .read() - .clone() - .iter() - .find(|w| { - w.id.to_string() - == self - .equipment_modal_state - .data - .get("worker_id") - .unwrap() - .clone() - }) - .unwrap_or(&Worker::default()) - .full_name - .clone(); egui::ComboBox::new("process_equipment_combobox", "") - .selected_text(workname) + .selected_text( + self.equipment_modal_state + .equipment + .worker + .full_name + .clone(), + ) .show_ui(ui, |ui| { for wkrk in self.workers.read().clone().iter() { ui.selectable_value( - self.equipment_modal_state - .data - .get_mut("worker_id") - .unwrap(), - wkrk.id.to_string(), + &mut self.equipment_modal_state.equipment.worker, + wkrk.clone(), &wkrk.full_name, ); } @@ -497,142 +439,75 @@ impl MainTabViewer { ui.end_row(); ui.label("Дата последнего\nтехобслуживания"); - ui.add( - egui::TextEdit::singleline( - self.equipment_modal_state - .data - .get_mut("maintenance_date") - .unwrap(), - ) - .hint_text("1970-01-01"), - ); + let mut date = self + .equipment_modal_state + .equipment + .maintenance_date + .date_naive() + .to_string(); + let bdate = date.clone(); + ui.add(egui::TextEdit::singleline(&mut date).hint_text("1970-01-01")); + if date != bdate { + self.equipment_modal_state.equipment.maintenance_date = + match NaiveDate::parse_from_str(&date, "%Y-%m-%d") { + Ok(o) => {date_okay = true; + o + .and_hms_opt(0, 0, 0) + .unwrap() + .and_local_timezone(chrono::Local) + .unwrap()}, + Err(e) => { + date_okay = false; + NaiveDate::default() + .and_hms_opt(0, 0, 0) + .unwrap() + .and_local_timezone(chrono::Local) + .unwrap() + } + }; + } }); } else { ui.label("Вы уверены, что хотите удалить:"); ui.strong(format!( "{} | {}?", - self.equipment_modal_state.data.get("name").unwrap(), - self.equipment_modal_state.data.get("inv_number").unwrap() + self.equipment_modal_state.equipment.name, + self.equipment_modal_state.equipment.inv_number )); } - let btext = match self.equipment_modal_state.status { + let btext = match &self.equipment_modal_state.status { ModalStatus::Add => "Добавить", ModalStatus::Edit => "Сохранить", ModalStatus::Remove => "Списать", }; - if ui.button(btext).clicked() { - match self.equipment_modal_state.status { + match &self.equipment_modal_state.status { + ModalStatus::Remove => { + self.equipment_modal_state.can_finish = true; + } + _ => { + self.equipment_modal_state.can_finish = + self.equipment_modal_state.equipment.name.len() > 0 && date_okay + } + } + let resp = ui.add_enabled(self.equipment_modal_state.can_finish,egui::Button::new(btext)); + if resp.clicked() { + match &self.equipment_modal_state.status { ModalStatus::Add => self.rt.block_on(async { self.db_oper - .add_equipment(Equipment { - id: 0, - name: self - .equipment_modal_state - .data - .get("name") - .unwrap_or(&String::new()) - .clone(), - inv_number: self - .equipment_modal_state - .data - .get("inv_number") - .unwrap_or(&String::new()) - .clone(), - maintenance_date: chrono::NaiveDate::from_str( - self.equipment_modal_state - .data - .get("maintenance_date") - .unwrap_or(&"1970.01.01".to_owned()), - ) - .unwrap() - .and_hms_opt(0, 0, 0) - .unwrap() - .and_local_timezone(chrono::Local) - .unwrap(), - worker: self - .workers - .read() - .clone() - .iter() - .find(|w| { - &w.id.to_string() - == self - .equipment_modal_state - .data - .get("worker_id") - .unwrap_or(&"0".to_owned()) - }) - .unwrap() - .clone(), - is_written_off: false, - }) + .add_equipment(self.equipment_modal_state.equipment.clone()) .await .unwrap(); }), ModalStatus::Edit => self.rt.block_on(async { self.db_oper - .update_equipment(Equipment { - id: self - .equipment_modal_state - .data - .get("id") - .unwrap() - .parse::() - .unwrap(), - name: self - .equipment_modal_state - .data - .get("name") - .unwrap_or(&String::new()) - .clone(), - inv_number: self - .equipment_modal_state - .data - .get("inv_number") - .unwrap_or(&String::new()) - .clone(), - maintenance_date: chrono::NaiveDate::from_str( - self.equipment_modal_state - .data - .get("maintenance_date") - .unwrap_or(&"1970.01.01".to_owned()), - ) - .unwrap() - .and_hms_opt(0, 0, 0) - .unwrap() - .and_local_timezone(chrono::Local) - .unwrap(), - worker: self - .workers - .read() - .clone() - .iter() - .find(|w| { - &w.id.to_string() - == self - .equipment_modal_state - .data - .get("worker_id") - .unwrap_or(&"0".to_owned()) - }) - .unwrap() - .clone(), - is_written_off: false, - }) + .update_equipment(self.equipment_modal_state.equipment.clone()) .await .unwrap(); }), ModalStatus::Remove => { self.rt.block_on(async { self.db_oper - .write_off_equipment( - self.equipment_modal_state - .data - .get("id") - .unwrap() - .parse::() - .unwrap(), - ) + .write_off_equipment(self.equipment_modal_state.equipment.id) .await .unwrap(); }); diff --git a/code/src/db/mod.rs b/code/src/db/mod.rs deleted file mode 100644 index debb19d..0000000 --- a/code/src/db/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod options; -mod traits; -pub mod worker; diff --git a/code/src/db/options.rs b/code/src/db/options.rs deleted file mode 100644 index 23df086..0000000 --- a/code/src/db/options.rs +++ /dev/null @@ -1,17 +0,0 @@ -pub struct Options { - pub connection_string: String, -} -impl Options { - pub fn new() -> Self { - let settings = match config::Config::builder() - .add_source(config::File::with_name("appsettings")) - .build() - { - Ok(n) => n, - Err(_) => config::Config::default(), - }; - Self { - connection_string: settings.get("connection_string").unwrap_or_default(), - } - } -} diff --git a/code/src/db/traits.rs b/code/src/db/traits.rs deleted file mode 100644 index 778c863..0000000 --- a/code/src/db/traits.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::models::Worker; - -pub trait WorkerRepository { - async fn add(&self, worker: Worker) -> Result<(), sqlx::Error>; - async fn get_all(&self) -> Result, sqlx::Error>; - async fn get_some(&self, start: u32, amount: u32) -> Result, sqlx::Error>; -} diff --git a/code/src/db/worker.rs b/code/src/db/worker.rs deleted file mode 100644 index bd201c1..0000000 --- a/code/src/db/worker.rs +++ /dev/null @@ -1,20 +0,0 @@ -use sqlx::mysql::MySqlPool; -pub struct WorkerRepo { - pool: MySqlPool, - options: super::options::Options, -} -impl WorkerRepo { - pub fn new() -> Result { - let options = super::options::Options::new(); - let pool = match MySqlPool::connect_lazy(&options.connection_string) { - Ok(n) => n, - Err(e) => { - return Err(e); - } - }; - Ok(Self { pool, options }) - } - pub async fn add(&self, work) -} - - diff --git a/code/src/main.rs b/code/src/main.rs index 5818cbd..f47254b 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -2,7 +2,6 @@ mod app; mod database; -mod db; mod material_tab; mod models; mod production_tab; diff --git a/code/src/models.rs b/code/src/models.rs index 6f8f264..92f569d 100644 --- a/code/src/models.rs +++ b/code/src/models.rs @@ -1,5 +1,6 @@ use std::{any::TypeId, collections::HashSet, ops::Deref}; +use chrono::Local; use egui::TextBuffer; use sqlx::{ prelude::FromRow, @@ -148,6 +149,20 @@ pub struct User { is_admin: bool, worker: Worker, } +pub struct Promotion { + pub worker: Worker, + pub to_position: Position, + pub date: DateTime, +} + +#[derive(Default)] +pub struct EquipmentModalState { + pub is_open: bool, + pub can_finish: bool, + pub equipment: Equipment, + pub status: ModalStatus, +} + #[derive(Default)] pub struct ModalWinState { pub is_open: bool, diff --git a/code/src/worker_tab.rs b/code/src/worker_tab.rs index ed20b61..bb2484b 100644 --- a/code/src/worker_tab.rs +++ b/code/src/worker_tab.rs @@ -6,7 +6,6 @@ use std::default::Default; use std::str::FromStr; use std::sync::Arc; -mod db; use crate::database::*; use crate::models::{ModalStatus, ModalWinState, Position, Worker, WorkerRow};