fix: bug change prefix of unexpected material

Signed-off-by: Pakin <pakin.t@forth.co.th>
This commit is contained in:
Pakin 2026-01-27 16:52:31 +07:00
parent 2b6b062664
commit 4aa9ab3cfb

View file

@ -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());
} }
@ -129,7 +135,7 @@ 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();
@ -147,13 +153,15 @@ pub fn change_prefix_by_country(
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()
&& check_allowed_change_mat_prefix(ms.id.clone())
{
ms.id = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone()); ms.id = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
} }
result.push(ms.clone()); result.push(ms.clone());
@ -164,14 +172,16 @@ pub fn change_prefix_material_settings(
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()
&& check_allowed_change_mat_prefix(rpl.materialPathId.clone())
{
rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone()); rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
} }
} }
@ -184,13 +194,15 @@ pub fn change_prefix_topping_list(
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()
&& check_allowed_change_mat_prefix(mc.materialID.clone())
{
mc.materialID = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone()); mc.materialID = assign_new_prefix_mat_id_by_case(mat_id, cprefix.clone());
} }
result.push(mc.clone()); result.push(mc.clone());
@ -200,10 +212,22 @@ pub fn change_prefix_material_code(
} }
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
} }
@ -214,15 +238,19 @@ fn modify_prefix_recipe(mut recp: Recipe01, prefix: String) -> Recipe01 {
return recp; return recp;
} }
product_code = format!("{prefix}-{rest}", rest = product_code.split_once("-").unwrap().1); product_code = format!(
"{prefix}-{rest}",
rest = product_code.split_once("-").unwrap().1
);
recp.productCode = product_code; recp.productCode = product_code;
// edit recipe // edit recipe
let mut changed_recipes_list = Vec::new(); let mut changed_recipes_list = Vec::new();
for rpl in recp.recipes.iter_mut() { for rpl in recp.recipes.iter_mut() {
if check_allowed_change_mat_prefix(rpl.materialPathId.clone()) { if check_allowed_change_mat_prefix(rpl.materialPathId.clone()) {
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, prefix.clone()); rpl.materialPathId = assign_new_prefix_mat_id_by_case(mat_id, prefix.clone());
} }
} }
@ -242,7 +270,6 @@ fn modify_prefix_recipe(mut recp: Recipe01, prefix: String) -> Recipe01 {
None None
}; };
recp recp
} }
@ -288,7 +315,12 @@ pub fn grep_latest_versions(dir_path: &str) -> Result<HashMap<String, usize>, st
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
.file_name()
.to_str()
.unwrap_or("")
.to_string()
.eq("version")
&& let Ok(mut file_res) = File::open(dt.path()) && let Ok(mut file_res) = File::open(dt.path())
{ {
let mut data = String::new(); let mut data = String::new();
@ -296,7 +328,6 @@ pub fn grep_latest_versions(dir_path: &str) -> Result<HashMap<String, usize>, st
// 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());
} }
}); });
} }