From 93c9a9fc54560033e47efccdbaeec4fe51ae4e06 Mon Sep 17 00:00:00 2001 From: Pakin Date: Thu, 16 Oct 2025 09:57:44 +0700 Subject: [PATCH] add summary & change full import check condition --- src/models/recipe.rs | 188 +++++++++++++++++++++++++++++++++ src/recipe_functions/import.rs | 12 ++- 2 files changed, 195 insertions(+), 5 deletions(-) diff --git a/src/models/recipe.rs b/src/models/recipe.rs index d0a1bee..e626d90 100644 --- a/src/models/recipe.rs +++ b/src/models/recipe.rs @@ -600,6 +600,88 @@ impl Recipe { res_map } + /// Get all data in this recipe as summary, this does not contain all the field but only id or name. + /// + pub fn get_recipe_data_summary(&mut self) -> RecipeSummary { + // mapping Recipe01, MaterialSetting, Topping, MaterialCode + + let recipe01_summary = self + .Recipe01 + .clone() + .iter() + .map(|r01| Recipe01Summary { + productCode: r01.productCode.clone(), + name: r01.name.clone().unwrap_or("".to_string()), + }) + .collect::>(); + + let matset_summary = self + .MaterialSetting + .clone() + .iter() + .map(|ms| MaterialSettingSummary { + id: ms.id.clone(), + materialName: ms + .materialName + .clone() + .unwrap_or(ms.materialOtherName.clone().unwrap_or("".to_string())), + }) + .collect::>(); + + let topping_group_summary = self + .Topping + .ToppingGroup + .clone() + .iter() + .map(|tg| { + if !tg.name.is_empty() { + ToppingGroupSummary { + groupID: tg.groupID.clone(), + name: tg.name.clone(), + } + } else { + ToppingGroupSummary { + groupID: tg.groupID.clone(), + name: tg.otherName.clone(), + } + } + }) + .collect::>(); + + let topping_list_summary = self + .Topping + .ToppingList + .clone() + .iter() + .map(|tpl| { + if tpl.name.is_some() { + ToppingListSummary { + id: tpl.id.clone(), + name: tpl.name.clone().unwrap(), + } + } else { + ToppingListSummary { + id: tpl.id.clone(), + name: tpl.otherName.clone().unwrap_or("".to_string()), + } + } + }) + .collect::>(); + + RecipeSummary { + Timestamp: self.Timestamp.clone(), + MachineSetting: self.MachineSetting.clone(), + Recipe01: recipe01_summary, + MaterialSetting: matset_summary, + Topping: ToppingSummary { + ToppingGroup: topping_group_summary, + ToppingList: topping_list_summary, + }, + MaterialCode: self.MaterialCode.clone(), + extra: self.extra.clone(), + } + } + // zone import recipe // // @@ -2046,3 +2128,109 @@ impl Recipe01 { .collect() } } + +// -------------------------------------- + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct RecipeSummary { + pub Timestamp: String, + pub MachineSetting: MachineSetting, + pub Recipe01: Vec, + pub MaterialSetting: Vec, + pub Topping: ToppingSummary, + pub MaterialCode: Vec, + #[serde(flatten)] + pub extra: std::collections::HashMap, +} + +impl RecipeSummary { + pub fn recipe01_contains(&mut self, pd: String) -> bool { + let filtered = self + .Recipe01 + .iter() + .filter(|r01| r01.productCode.eq(&pd)) + .collect::>(); + !filtered.is_empty() + } + + #[allow(non_snake_case)] + pub fn materialSetting_contains(&mut self, id: Value) -> bool { + let filtered = self + .MaterialSetting + .iter() + .filter(|ms| ms.id.eq(&id)) + .collect::>(); + !filtered.is_empty() + } + + #[allow(non_snake_case)] + pub fn materialCode_contains(&mut self, mat_id: Value) -> bool { + let filtered = self + .MaterialCode + .iter() + .filter(|mc| mc.materialID.eq(&mat_id)) + .collect::>(); + + !filtered.is_empty() + } + + #[allow(non_snake_case)] + pub fn toppingList_contains(&mut self, id: Value) -> bool { + let filtered = self + .Topping + .ToppingList + .iter() + .filter(|tpl| tpl.id.eq(&id)) + .collect::>(); + + !filtered.is_empty() + } + + #[allow(non_snake_case)] + pub fn toppingGroup_contains(&mut self, gid: Value) -> bool { + let filtered = self + .Topping + .ToppingGroup + .iter() + .filter(|tg| tg.groupID.eq(&gid)) + .collect::>(); + + !filtered.is_empty() + } +} + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct Recipe01Summary { + pub productCode: String, + pub name: String, +} + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct MaterialSettingSummary { + pub id: Value, + pub materialName: String, +} + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct ToppingSummary { + pub ToppingGroup: Vec, + pub ToppingList: Vec, +} + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct ToppingGroupSummary { + pub groupID: Value, + pub name: String, +} + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct ToppingListSummary { + pub id: Value, + pub name: String, +} diff --git a/src/recipe_functions/import.rs b/src/recipe_functions/import.rs index b2fb533..effff2b 100644 --- a/src/recipe_functions/import.rs +++ b/src/recipe_functions/import.rs @@ -604,9 +604,11 @@ pub fn generate_recipe_sheet_table(country_name: &str, into_version: usize) { /// - Topping /// pub fn full_import_data(source: Recipe, mut import_target: Recipe) -> Recipe { + let mut target_summary = import_target.get_recipe_data_summary(); + // import menus source.Recipe01.iter().for_each(|r01| { - if !import_target.Recipe01.contains(r01) { + if !target_summary.recipe01_contains(r01.clone().productCode) { import_target.Recipe01.push(r01.clone()); println!( "recipe >> {} {}", @@ -618,7 +620,7 @@ pub fn full_import_data(source: Recipe, mut import_target: Recipe) -> Recipe { // import materials source.MaterialSetting.iter().for_each(|ms| { - if !import_target.MaterialSetting.contains(ms) { + if !target_summary.materialSetting_contains(ms.id.clone()) { import_target.MaterialSetting.push(ms.clone()); println!( "mat_set >> {:?} {}", @@ -629,7 +631,7 @@ pub fn full_import_data(source: Recipe, mut import_target: Recipe) -> Recipe { }); source.MaterialCode.iter().for_each(|mc| { - if !import_target.MaterialCode.contains(mc) { + if !target_summary.materialCode_contains(mc.materialID.clone()) { import_target.MaterialCode.push(mc.clone()); println!("mat_code >> {:?}", mc.materialID); } @@ -637,7 +639,7 @@ pub fn full_import_data(source: Recipe, mut import_target: Recipe) -> Recipe { // import new toppings source.Topping.ToppingList.iter().for_each(|tpl| { - if !import_target.Topping.ToppingList.contains(tpl) { + if !target_summary.toppingList_contains(tpl.id.clone()) { import_target.Topping.ToppingList.push(tpl.clone()); println!( "topping_list >> {:?} {}", @@ -647,7 +649,7 @@ pub fn full_import_data(source: Recipe, mut import_target: Recipe) -> Recipe { } }); source.Topping.ToppingGroup.iter().for_each(|tg| { - if !import_target.Topping.ToppingGroup.contains(tg) { + if !target_summary.toppingGroup_contains(tg.groupID.clone()) { import_target.Topping.ToppingGroup.push(tg.clone()); println!("topping_group >> {:?} {}", tg.groupID, tg.name); }