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 rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
|
||||||
use serde_json::Value;
|
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::collections::HashMap;
|
||||||
use std::{fs::File, io::Read};
|
use std::{fs::File, io::Read};
|
||||||
|
|
||||||
|
|
@ -20,7 +23,10 @@ fn extract_tb_config(content: String) -> HashMap<String, String> {
|
||||||
let lines = content.lines();
|
let lines = content.lines();
|
||||||
|
|
||||||
for line in 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 {
|
if parts.len() == 2 {
|
||||||
result.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
|
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(
|
pub fn change_prefix_by_country(
|
||||||
recipes: Vec<Option<Recipe01>>,
|
recipes: Vec<Option<Recipe01>>,
|
||||||
cprefix: Option<&str>
|
cprefix: Option<&str>,
|
||||||
) -> Vec<Recipe01> {
|
) -> Vec<Recipe01> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
|
|
@ -137,124 +143,145 @@ pub fn change_prefix_by_country(
|
||||||
let prefix = cprefix.unwrap_or("").to_string();
|
let prefix = cprefix.unwrap_or("").to_string();
|
||||||
|
|
||||||
for rpl in recipes.iter() {
|
for rpl in recipes.iter() {
|
||||||
if let Some(rp) = rpl.clone() {
|
if let Some(rp) = rpl.clone() {
|
||||||
result.push(modify_prefix_recipe(rp.clone(), prefix.clone()));
|
result.push(modify_prefix_recipe(rp.clone(), prefix.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn change_prefix_material_settings(
|
pub fn change_prefix_material_settings(
|
||||||
mut mat_set: Vec<MaterialSetting>,
|
mut mat_set: Vec<MaterialSetting>,
|
||||||
cprefix: String
|
cprefix: String,
|
||||||
) -> Vec<MaterialSetting> {
|
) -> Vec<MaterialSetting> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
for ms in mat_set.iter_mut() {
|
for ms in mat_set.iter_mut() {
|
||||||
if let Some(mat_num) = ms.id.as_number() &&
|
if let Some(mat_num) = ms.id.as_number()
|
||||||
let Some(mat_id) = mat_num.as_i64() {
|
&& let Some(mat_id) = mat_num.as_i64()
|
||||||
ms.id = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
&& check_allowed_change_mat_prefix(ms.id.clone())
|
||||||
}
|
{
|
||||||
result.push(ms.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(
|
pub fn change_prefix_topping_list(
|
||||||
mut topp_list: Vec<ToppingList>,
|
mut topp_list: Vec<ToppingList>,
|
||||||
cprefix: String
|
cprefix: String,
|
||||||
) -> Vec<ToppingList> {
|
) -> Vec<ToppingList> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
for tpl in topp_list.iter_mut() {
|
for tpl in topp_list.iter_mut() {
|
||||||
for rpl in tpl.recipes.iter_mut() {
|
for rpl in tpl.recipes.iter_mut() {
|
||||||
if let Some(mat_num) = rpl.materialPathId.as_number() &&
|
if let Some(mat_num) = rpl.materialPathId.as_number()
|
||||||
let Some(mat_id) = mat_num.as_i64() {
|
&& let Some(mat_id) = mat_num.as_i64()
|
||||||
rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
&& 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(
|
pub fn change_prefix_material_code(
|
||||||
mut mat_codes: Vec<MaterialCode>,
|
mut mat_codes: Vec<MaterialCode>,
|
||||||
cprefix: String
|
cprefix: String,
|
||||||
) -> Vec<MaterialCode>{
|
) -> Vec<MaterialCode> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
for mc in mat_codes.iter_mut() {
|
for mc in mat_codes.iter_mut() {
|
||||||
if let Some(mat_num) = mc.materialID.as_number() &&
|
if let Some(mat_num) = mc.materialID.as_number()
|
||||||
let Some(mat_id) = mat_num.as_i64() {
|
&& let Some(mat_id) = mat_num.as_i64()
|
||||||
mc.materialID = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
|
&& check_allowed_change_mat_prefix(mc.materialID.clone())
|
||||||
}
|
{
|
||||||
result.push(mc.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 {
|
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.Recipe01 = change_prefix_by_country(
|
||||||
recipe.MaterialSetting = change_prefix_material_settings(recipe.clone().MaterialSetting.clone(), prefix.clone());
|
recipe
|
||||||
recipe.Topping.ToppingList = change_prefix_topping_list(recipe.clone().Topping.ToppingList.clone(), prefix.clone());
|
.clone()
|
||||||
recipe.MaterialCode = change_prefix_material_code(recipe.clone().MaterialCode.clone(), prefix.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 {
|
fn modify_prefix_recipe(mut recp: Recipe01, prefix: String) -> Recipe01 {
|
||||||
let mut product_code = recp.productCode.clone();
|
let mut product_code = recp.productCode.clone();
|
||||||
if product_code.is_empty() || product_code.len() < 13 {
|
if product_code.is_empty() || product_code.len() < 13 {
|
||||||
return recp;
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
changed_recipes_list.push(rpl.clone());
|
|
||||||
}
|
product_code = format!(
|
||||||
recp.recipes = changed_recipes_list;
|
"{prefix}-{rest}",
|
||||||
let mut changed_sub_menus = Vec::new();
|
rest = product_code.split_once("-").unwrap().1
|
||||||
if let Some(mut subs) = recp.SubMenu.clone() {
|
);
|
||||||
for s in subs.iter_mut() {
|
recp.productCode = product_code;
|
||||||
changed_sub_menus.push(modify_prefix_recipe(s.clone(), prefix.clone()));
|
|
||||||
|
// 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() {
|
recp.SubMenu = if recp.SubMenu.is_some() {
|
||||||
Some(changed_sub_menus.clone())
|
Some(changed_sub_menus.clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
recp
|
||||||
recp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assign_new_prefix_mat_id_by_case(id: i64, prefix: String) -> Value{
|
fn assign_new_prefix_mat_id_by_case(id: i64, prefix: String) -> Value {
|
||||||
if id > MATERIAL_INTER_GUARD as i64 {
|
if id > MATERIAL_INTER_GUARD as i64 {
|
||||||
let inverted_mat_id = id.to_string()[2..].parse::<i64>().expect("not a number");
|
let inverted_mat_id = id.to_string()[2..].parse::<i64>().expect("not a number");
|
||||||
let new_mat_id = format!("{prefix}{inverted_mat_id}");
|
let new_mat_id = format!("{prefix}{inverted_mat_id}");
|
||||||
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
||||||
} else {
|
} else {
|
||||||
let new_mat_id = format!("{prefix}{id}");
|
let new_mat_id = format!("{prefix}{id}");
|
||||||
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
return serde_json::json!(new_mat_id.parse::<i64>().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deprecated]
|
#[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
|
// do check if dir contains version file
|
||||||
if let Ok(dr) = std::fs::read_dir(path.clone()) {
|
if let Ok(dr) = std::fs::read_dir(path.clone()) {
|
||||||
dr.for_each(|d| {
|
dr.for_each(|d| {
|
||||||
if let Ok(dt) = d
|
if let Ok(dt) = d
|
||||||
&& dt.file_name().to_str().unwrap_or("").to_string().eq("version")
|
&& dt
|
||||||
&& let Ok(mut file_res) = File::open(dt.path())
|
.file_name()
|
||||||
{
|
.to_str()
|
||||||
let mut data = String::new();
|
.unwrap_or("")
|
||||||
let _ = file_res.read_to_string(&mut data);
|
.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
|
// TODO: does not support string version, i.e. dev version is 001
|
||||||
vs.insert(dir_name.to_string(), data.parse::<usize>().unwrap());
|
vs.insert(dir_name.to_string(), data.parse::<usize>().unwrap());
|
||||||
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue