Mes相关逻辑
This commit is contained in:
parent
1ac05ab246
commit
5ee7a48ada
147
src/mes_service.rs
Normal file
147
src/mes_service.rs
Normal file
@ -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<Vec<String>, Error> {
|
||||
let mut conn = self.pool.get_conn()?;
|
||||
let work_orders: Vec<String> =
|
||||
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<bool, Error> {
|
||||
match download_type {
|
||||
DownloadType::Bootloader => {
|
||||
let mut conn = self.pool.get_conn()?;
|
||||
let work_order_id:Vec<u32>= conn.query(format!("SELECT `id` FROM wisun_ordertables WHERE `OrderId` = '{work_order}'"))?;
|
||||
if let Some(work_order_id) = work_order_id.first(){
|
||||
let _: Vec<String> = 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<String> = 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<String> = 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<String> = conn.exec(format!(
|
||||
"UPDATE {work_order} SET `Station1` = 1 WHERE `Barcode` = {label}"
|
||||
), ())?;
|
||||
}
|
||||
DownloadType::App => {
|
||||
let mut conn = self.pool.get_conn()?;
|
||||
let _: Vec<String> = conn.exec(format!(
|
||||
"UPDATE {work_order} SET `Station2` = 1 WHERE `Barcode` = {label}"
|
||||
), ())?;
|
||||
}
|
||||
DownloadType::Rail => {
|
||||
let mut conn = self.pool.get_conn()?;
|
||||
let _: Vec<String> = 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<String> = 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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user