fix: bug change prefix of unexpected material
Signed-off-by: Pakin <pakin.t@forth.co.th>
This commit is contained in:
parent
2b6b062664
commit
4aa9ab3cfb
1 changed files with 131 additions and 100 deletions
|
|
@ -1,7 +1,10 @@
|
|||
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::models::{self, recipe::{MaterialCode, MaterialSetting, Recipe, Recipe01, ToppingList}};
|
||||
use crate::models::{
|
||||
self,
|
||||
recipe::{MaterialCode, MaterialSetting, Recipe, Recipe01, ToppingList},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
|
|
@ -20,7 +23,10 @@ fn extract_tb_config(content: String) -> HashMap<String, String> {
|
|||
let lines = content.lines();
|
||||
|
||||
for line in lines {
|
||||
let parts: Vec<String> = line.split_once("=").map(|x| vec![x.0.to_string(), x.1.to_string()]).unwrap_or(Vec::new());
|
||||
let parts: Vec<String> = line
|
||||
.split_once("=")
|
||||
.map(|x| vec![x.0.to_string(), x.1.to_string()])
|
||||
.unwrap_or(Vec::new());
|
||||
if parts.len() == 2 {
|
||||
result.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
|
||||
}
|
||||
|
|
@ -128,8 +134,8 @@ pub fn check_allowed_change_mat_prefix(mat_id: Value) -> bool {
|
|||
}
|
||||
|
||||
pub fn change_prefix_by_country(
|
||||
recipes: Vec<Option<Recipe01>>,
|
||||
cprefix: Option<&str>
|
||||
recipes: Vec<Option<Recipe01>>,
|
||||
cprefix: Option<&str>,
|
||||
) -> Vec<Recipe01> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
|
|
@ -137,124 +143,145 @@ pub fn change_prefix_by_country(
|
|||
let prefix = cprefix.unwrap_or("").to_string();
|
||||
|
||||
for rpl in recipes.iter() {
|
||||
if let Some(rp) = rpl.clone() {
|
||||
result.push(modify_prefix_recipe(rp.clone(), prefix.clone()));
|
||||
}
|
||||
if let Some(rp) = rpl.clone() {
|
||||
result.push(modify_prefix_recipe(rp.clone(), prefix.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn change_prefix_material_settings(
|
||||
mut mat_set: Vec<MaterialSetting>,
|
||||
cprefix: String
|
||||
mut mat_set: Vec<MaterialSetting>,
|
||||
cprefix: String,
|
||||
) -> Vec<MaterialSetting> {
|
||||
let mut result = Vec::new();
|
||||
let mut result = Vec::new();
|
||||
|
||||
for ms in mat_set.iter_mut() {
|
||||
if let Some(mat_num) = ms.id.as_number() &&
|
||||
let Some(mat_id) = mat_num.as_i64() {
|
||||
ms.id = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
||||
}
|
||||
result.push(ms.clone());
|
||||
}
|
||||
for ms in mat_set.iter_mut() {
|
||||
if let Some(mat_num) = ms.id.as_number()
|
||||
&& let Some(mat_id) = mat_num.as_i64()
|
||||
&& check_allowed_change_mat_prefix(ms.id.clone())
|
||||
{
|
||||
ms.id = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
||||
}
|
||||
result.push(ms.clone());
|
||||
}
|
||||
|
||||
result
|
||||
result
|
||||
}
|
||||
|
||||
pub fn change_prefix_topping_list(
|
||||
mut topp_list: Vec<ToppingList>,
|
||||
cprefix: String
|
||||
mut topp_list: Vec<ToppingList>,
|
||||
cprefix: String,
|
||||
) -> Vec<ToppingList> {
|
||||
let mut result = Vec::new();
|
||||
let mut result = Vec::new();
|
||||
|
||||
for tpl in topp_list.iter_mut() {
|
||||
for rpl in tpl.recipes.iter_mut() {
|
||||
if let Some(mat_num) = rpl.materialPathId.as_number() &&
|
||||
let Some(mat_id) = mat_num.as_i64() {
|
||||
rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
||||
}
|
||||
for tpl in topp_list.iter_mut() {
|
||||
for rpl in tpl.recipes.iter_mut() {
|
||||
if let Some(mat_num) = rpl.materialPathId.as_number()
|
||||
&& let Some(mat_id) = mat_num.as_i64()
|
||||
&& check_allowed_change_mat_prefix(rpl.materialPathId.clone())
|
||||
{
|
||||
rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
||||
}
|
||||
}
|
||||
|
||||
result.push(tpl.clone());
|
||||
}
|
||||
|
||||
result.push(tpl.clone());
|
||||
}
|
||||
|
||||
result
|
||||
result
|
||||
}
|
||||
|
||||
pub fn change_prefix_material_code(
|
||||
mut mat_codes: Vec<MaterialCode>,
|
||||
cprefix: String
|
||||
) -> Vec<MaterialCode>{
|
||||
let mut result = Vec::new();
|
||||
mut mat_codes: Vec<MaterialCode>,
|
||||
cprefix: String,
|
||||
) -> Vec<MaterialCode> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
for mc in mat_codes.iter_mut() {
|
||||
if let Some(mat_num) = mc.materialID.as_number() &&
|
||||
let Some(mat_id) = mat_num.as_i64() {
|
||||
mc.materialID = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
||||
}
|
||||
result.push(mc.clone());
|
||||
}
|
||||
for mc in mat_codes.iter_mut() {
|
||||
if let Some(mat_num) = mc.materialID.as_number()
|
||||
&& let Some(mat_id) = mat_num.as_i64()
|
||||
&& check_allowed_change_mat_prefix(mc.materialID.clone())
|
||||
{
|
||||
mc.materialID = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
||||
}
|
||||
result.push(mc.clone());
|
||||
}
|
||||
|
||||
result
|
||||
result
|
||||
}
|
||||
|
||||
pub fn run_process_change_prefix(mut recipe: Recipe, prefix: String) -> Recipe {
|
||||
recipe.Recipe01 = change_prefix_by_country(recipe.clone().Recipe01.clone().iter().map(|r| Some(r.clone())).collect(), Some(&prefix.clone()));
|
||||
recipe.MaterialSetting = change_prefix_material_settings(recipe.clone().MaterialSetting.clone(), prefix.clone());
|
||||
recipe.Topping.ToppingList = change_prefix_topping_list(recipe.clone().Topping.ToppingList.clone(), prefix.clone());
|
||||
recipe.MaterialCode = change_prefix_material_code(recipe.clone().MaterialCode.clone(), prefix.clone());
|
||||
recipe.Recipe01 = change_prefix_by_country(
|
||||
recipe
|
||||
.clone()
|
||||
.Recipe01
|
||||
.clone()
|
||||
.iter()
|
||||
.map(|r| Some(r.clone()))
|
||||
.collect(),
|
||||
Some(&prefix.clone()),
|
||||
);
|
||||
recipe.MaterialSetting =
|
||||
change_prefix_material_settings(recipe.clone().MaterialSetting.clone(), prefix.clone());
|
||||
recipe.Topping.ToppingList =
|
||||
change_prefix_topping_list(recipe.clone().Topping.ToppingList.clone(), prefix.clone());
|
||||
recipe.MaterialCode =
|
||||
change_prefix_material_code(recipe.clone().MaterialCode.clone(), prefix.clone());
|
||||
|
||||
recipe
|
||||
recipe
|
||||
}
|
||||
|
||||
fn modify_prefix_recipe(mut recp: Recipe01, prefix: String) -> Recipe01 {
|
||||
let mut product_code = recp.productCode.clone();
|
||||
if product_code.is_empty() || product_code.len() < 13 {
|
||||
return recp;
|
||||
}
|
||||
|
||||
product_code = format!("{prefix}-{rest}", rest = product_code.split_once("-").unwrap().1);
|
||||
recp.productCode = product_code;
|
||||
|
||||
// edit recipe
|
||||
let mut changed_recipes_list = Vec::new();
|
||||
for rpl in recp.recipes.iter_mut() {
|
||||
if check_allowed_change_mat_prefix(rpl.materialPathId.clone()) {
|
||||
if let Some(mat_num) = rpl.materialPathId.as_number() &&
|
||||
let Some(mat_id) = mat_num.as_i64() {
|
||||
rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, prefix.clone());
|
||||
}
|
||||
let mut product_code = recp.productCode.clone();
|
||||
if product_code.is_empty() || product_code.len() < 13 {
|
||||
return recp;
|
||||
}
|
||||
changed_recipes_list.push(rpl.clone());
|
||||
}
|
||||
recp.recipes = changed_recipes_list;
|
||||
let mut changed_sub_menus = Vec::new();
|
||||
if let Some(mut subs) = recp.SubMenu.clone() {
|
||||
for s in subs.iter_mut() {
|
||||
changed_sub_menus.push(modify_prefix_recipe(s.clone(), prefix.clone()));
|
||||
|
||||
product_code = format!(
|
||||
"{prefix}-{rest}",
|
||||
rest = product_code.split_once("-").unwrap().1
|
||||
);
|
||||
recp.productCode = product_code;
|
||||
|
||||
// edit recipe
|
||||
let mut changed_recipes_list = Vec::new();
|
||||
for rpl in recp.recipes.iter_mut() {
|
||||
if check_allowed_change_mat_prefix(rpl.materialPathId.clone()) {
|
||||
if let Some(mat_num) = rpl.materialPathId.as_number()
|
||||
&& let Some(mat_id) = mat_num.as_i64()
|
||||
{
|
||||
rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, prefix.clone());
|
||||
}
|
||||
}
|
||||
changed_recipes_list.push(rpl.clone());
|
||||
}
|
||||
recp.recipes = changed_recipes_list;
|
||||
let mut changed_sub_menus = Vec::new();
|
||||
if let Some(mut subs) = recp.SubMenu.clone() {
|
||||
for s in subs.iter_mut() {
|
||||
changed_sub_menus.push(modify_prefix_recipe(s.clone(), prefix.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
recp.SubMenu = if recp.SubMenu.is_some() {
|
||||
Some(changed_sub_menus.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
recp.SubMenu = if recp.SubMenu.is_some() {
|
||||
Some(changed_sub_menus.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
|
||||
recp
|
||||
recp
|
||||
}
|
||||
|
||||
fn assign_new_prefix_mat_id_by_case(id: i64, prefix: String) -> Value{
|
||||
if id > MATERIAL_INTER_GUARD as i64 {
|
||||
let inverted_mat_id = id.to_string()[2..].parse::<i64>().expect("not a number");
|
||||
let new_mat_id = format!("{prefix}{inverted_mat_id}");
|
||||
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
||||
} else {
|
||||
let new_mat_id = format!("{prefix}{id}");
|
||||
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
||||
}
|
||||
fn assign_new_prefix_mat_id_by_case(id: i64, prefix: String) -> Value {
|
||||
if id > MATERIAL_INTER_GUARD as i64 {
|
||||
let inverted_mat_id = id.to_string()[2..].parse::<i64>().expect("not a number");
|
||||
let new_mat_id = format!("{prefix}{inverted_mat_id}");
|
||||
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
||||
} else {
|
||||
let new_mat_id = format!("{prefix}{id}");
|
||||
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[deprecated]
|
||||
|
|
@ -286,19 +313,23 @@ pub fn grep_latest_versions(dir_path: &str) -> Result<HashMap<String, usize>, st
|
|||
|
||||
// do check if dir contains version file
|
||||
if let Ok(dr) = std::fs::read_dir(path.clone()) {
|
||||
dr.for_each(|d| {
|
||||
if let Ok(dt) = d
|
||||
&& dt.file_name().to_str().unwrap_or("").to_string().eq("version")
|
||||
&& let Ok(mut file_res) = File::open(dt.path())
|
||||
{
|
||||
let mut data = String::new();
|
||||
let _ = file_res.read_to_string(&mut data);
|
||||
dr.for_each(|d| {
|
||||
if let Ok(dt) = d
|
||||
&& dt
|
||||
.file_name()
|
||||
.to_str()
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
.eq("version")
|
||||
&& let Ok(mut file_res) = File::open(dt.path())
|
||||
{
|
||||
let mut data = String::new();
|
||||
let _ = file_res.read_to_string(&mut data);
|
||||
|
||||
// TODO: does not support string version, i.e. dev version is 001
|
||||
vs.insert(dir_name.to_string(), data.parse::<usize>().unwrap());
|
||||
|
||||
}
|
||||
});
|
||||
// TODO: does not support string version, i.e. dev version is 001
|
||||
vs.insert(dir_name.to_string(), data.parse::<usize>().unwrap());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue