From 5ee7a48adad7d2ab4c07eaefa0e27de245cadc18 Mon Sep 17 00:00:00 2001 From: JIe Date: Mon, 9 Dec 2024 14:31:03 +0800 Subject: [PATCH] =?UTF-8?q?Mes=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mes_service.rs | 147 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/mes_service.rs diff --git a/src/mes_service.rs b/src/mes_service.rs new file mode 100644 index 0000000..09a48d4 --- /dev/null +++ b/src/mes_service.rs @@ -0,0 +1,147 @@ +use crate::download_wrapper::DownloadType; +use anyhow::Error; +use mysql::prelude::*; + +struct MesService { + pool: mysql::Pool, +} + +impl MesService { + fn new(ip: String, port: String, username: String, password: String, database: String) -> Self { + let url = format!( + "mysql://{}:{}@{}:{}/{}", + username, password, ip, port, database + ); + let pool = mysql::Pool::new(url.as_str()).unwrap(); + Self { pool } + } + + fn get_work_orders(&self) -> Result, Error> { + let mut conn = self.pool.get_conn()?; + let work_orders: Vec = + conn.query("SELECT `OrderId` FROM wisun_ordertables".to_string())?; + Ok(work_orders) + } + + fn check_station( + &self, + work_order: String, + label: String, + download_type: DownloadType, + ) -> Result { + match download_type { + DownloadType::Bootloader => { + let mut conn = self.pool.get_conn()?; + let work_order_id:Vec= conn.query(format!("SELECT `id` FROM wisun_ordertables WHERE `OrderId` = '{work_order}'"))?; + if let Some(work_order_id) = work_order_id.first(){ + let _: Vec = conn.exec(format!("INSERT INTO {work_order} (`ID`,`Barcode`) VALUES ({work_order_id},{label})").to_string(), ())?; + }else{ + return Ok(false); + } + Ok(true) + }, + DownloadType::App => { + let mut conn = self.pool.get_conn()?; + let check_result: Vec = conn.query(format!( + "SELECT `Station1` FROM {work_order} WHERE `Barcode` = {label}" + ))?; + if check_result.is_empty() { + return Ok(false); + } + Ok(check_result[0] == "1") + } + DownloadType::Rail => { + let mut conn = self.pool.get_conn()?; + let check_result: Vec = conn.query(format!( + "SELECT `Station2` FROM {work_order} WHERE `Barcode` = {label}" + ))?; + if check_result.is_empty() { + return Ok(false); + } + Ok(check_result[0] == "1") + } + } + } + + fn update_station(&self, work_order: String, label: String, download_type: DownloadType) -> Result<(), Error> { + match download_type { + DownloadType::Bootloader => { + let mut conn = self.pool.get_conn()?; + let _: Vec = conn.exec(format!( + "UPDATE {work_order} SET `Station1` = 1 WHERE `Barcode` = {label}" + ), ())?; + } + DownloadType::App => { + let mut conn = self.pool.get_conn()?; + let _: Vec = conn.exec(format!( + "UPDATE {work_order} SET `Station2` = 1 WHERE `Barcode` = {label}" + ), ())?; + } + DownloadType::Rail => { + let mut conn = self.pool.get_conn()?; + let _: Vec = conn.exec(format!( + "UPDATE {work_order} SET `Station3` = 1 WHERE `Barcode` = {label}" + ), ())?; + } + } + Ok(()) + } + + #[cfg(test)] + fn reset(&self){ + let mut conn = self.pool.get_conn().unwrap(); + let _: Vec = conn.query("UPDATE rrr SET `Station1` = 0, `Station2` = 0, `Station3` = 0 WHERE `Barcode` = 1".to_string()).unwrap(); + } +} + +#[cfg(test)] +mod test { + use crate::download_wrapper::DownloadType; + use crate::mes_service::MesService; + + #[test] + fn test_mes_service() { + let mes = MesService::new( + "192.168.1.17".to_string(), + "3306".to_string(), + "root".to_string(), + "".to_string(), + "MOBILETEK_WISUN".to_string(), + ); + } + + #[test] + fn test_get_work_orders() { + let mes = MesService::new( + "192.168.1.17".to_string(), + "3306".to_string(), + "root".to_string(), + "".to_string(), + "MOBILETEK_WISUN".to_string(), + ); + let work_orders = mes.get_work_orders().unwrap(); + assert_ne!(work_orders.len(), 0); + println!("{:?}", work_orders); + } + + #[test] + fn test_check_station_and_update_station() { + let mes = MesService::new( + "192.168.1.17".to_string(), + "3306".to_string(), + "root".to_string(), + "".to_string(), + "MOBILETEK_WISUN".to_string(), + ); + mes.reset(); + assert_eq!(mes.check_station("rrr".to_string(),"1".to_string(),DownloadType::Bootloader).unwrap(),true); + assert_eq!(mes.check_station("rrr".to_string(), "1".to_string(), DownloadType::App).unwrap(),false); + mes.update_station("rrr".to_string(), "1".to_string(), DownloadType::Bootloader).unwrap(); + assert_eq!(mes.check_station("rrr".to_string(), "1".to_string(), DownloadType::App).unwrap(),true); + assert_eq!(mes.check_station("rrr".to_string(), "1".to_string(), DownloadType::Rail).unwrap(),false); + mes.update_station("rrr".to_string(), "1".to_string(), DownloadType::App).unwrap(); + assert_eq!(mes.check_station("rrr".to_string(), "1".to_string(), DownloadType::Rail).unwrap(),true); + mes.update_station("rrr".to_string(), "1".to_string(), DownloadType::Rail).unwrap(); + } +} +