From 1db92e124a8a83c52ea86245daab052e11c9c9c2 Mon Sep 17 00:00:00 2001 From: Ching Date: Tue, 5 Mar 2024 00:30:02 +0800 Subject: [PATCH] feat(api): Refactor gpc_best_before_days function to include location mapping --- app.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 6d3eb4c..c9653cd 100644 --- a/app.py +++ b/app.py @@ -68,9 +68,13 @@ def add_product(dict_good, location): } if ("gpc" in dict_good) and dict_good["gpc"]: - best_before_days = gpc_best_before_days(int(dict_good["gpc"])) + best_before_days, location_ = gpc_best_before_days_and_location( + int(dict_good["gpc"]), + locations) if best_before_days: data_grocy["default_best_before_days"] = best_before_days + if location_: + data_grocy["location_id"] = location_ # add product response_grocy = grocy.add_generic(EntityType.PRODUCTS, data_grocy) @@ -125,7 +129,7 @@ def add_product(dict_good, location): return True -def gpc_best_before_days(Code): +def gpc_best_before_days_and_location(Code, locations): """ 保质期(天) 类别 7 50370000(中类,鲜切水果或蔬菜), 50380000(中类,鲜切水果或蔬菜), 50350000(中类,未处理或未加工的(新鲜)叶菜类蔬菜) @@ -169,12 +173,30 @@ def gpc_best_before_days(Code): 10100000, ] + location_map = {item['name']: item['id'] for item in locations} for item in gpc_data["Schema"]: if item["Code"] == Code: + title = item["Title-1"] + location = locations[0]['name'] + if title == 'Food/Beverage/Tobacco': + if item['Title-2'] in [ + 'Bread/Bakery Products', + ]: + location = '零食柜' + else: + location = '厨房' + elif title in [ + 'Beauty/Personal Care/Hygiene', + 'Cleaning/Hygiene Products', + ]: + location = '浴室' + elif title == 'Healthcare': + location = '药柜' codes = [item["Code"], item["Code-1"], item["Code-2"], item["Code-3"]] for day, filter_codes in best_before_days.items(): if any(code in filter_codes for code in codes): - return day + return day, location_map[location] + return None, location_map[location] def convert_image_to_base64(image_content):