feat(api): Refactor gpc_best_before_days function to include location mapping
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ching 2024-03-05 00:30:02 +08:00
parent 742135ac67
commit 1db92e124a

28
app.py
View File

@ -68,9 +68,13 @@ def add_product(dict_good, location):
} }
if ("gpc" in dict_good) and dict_good["gpc"]: 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: if best_before_days:
data_grocy["default_best_before_days"] = best_before_days data_grocy["default_best_before_days"] = best_before_days
if location_:
data_grocy["location_id"] = location_
# add product # add product
response_grocy = grocy.add_generic(EntityType.PRODUCTS, data_grocy) response_grocy = grocy.add_generic(EntityType.PRODUCTS, data_grocy)
@ -125,7 +129,7 @@ def add_product(dict_good, location):
return True return True
def gpc_best_before_days(Code): def gpc_best_before_days_and_location(Code, locations):
""" """
保质期 类别 保质期 类别
7 50370000中类鲜切水果或蔬菜, 50380000中类鲜切水果或蔬菜, 50350000中类未处理或未加工的新鲜叶菜类蔬菜 7 50370000中类鲜切水果或蔬菜, 50380000中类鲜切水果或蔬菜, 50350000中类未处理或未加工的新鲜叶菜类蔬菜
@ -169,12 +173,30 @@ def gpc_best_before_days(Code):
10100000, 10100000,
] ]
location_map = {item['name']: item['id'] for item in locations}
for item in gpc_data["Schema"]: for item in gpc_data["Schema"]:
if item["Code"] == Code: 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"]] codes = [item["Code"], item["Code-1"], item["Code-2"], item["Code-3"]]
for day, filter_codes in best_before_days.items(): for day, filter_codes in best_before_days.items():
if any(code in filter_codes for code in codes): 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): def convert_image_to_base64(image_content):