Files
Excel-project/server/backend/api/excel_prices.py
2026-01-21 19:39:37 +03:00

31 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import math
import server.backend.services.excel as excel
from server.backend.schemas.pydantic import settings
from functools import lru_cache
@lru_cache(maxsize=1)
def fetch_nomenclature(arti:int,price:int):
xls = excel.BaseHandler(settings.PRICES_DIR)
struct = xls.struct()
if "ЦЕНЫ" in struct.sheet_names:
df = xls.read(sheet_name="ЦЕНЫ").iloc[:,[arti,price]].copy()
else:
raise Exception(f"В файле {settings.PRICES_DIR} отсутствуют необходимые листы")
df.dropna(inplace=True)
df.columns = ['description', 'price']
#df["price"] = (df["price"]+ df["price"]*0.1).apply(math.ceil)
df['description'] = df['description'].astype(str).str.upper().str.extract(f'({settings.PATTERN})')
df = df.drop_duplicates(subset='description')
return df
def processing(df):
df2 = fetch_nomenclature(1,6)
result = df.merge(
df2[['description', 'price']], #берутся столбцы из df2
left_on='arti', #столбец для сравнения в df
right_on='description', #столбец для сравнения в df2
how='left' #left join для df
).drop(columns='description') #удаление временного стобца
print(result)
not_matched = result.loc[result['price'].isna(), 'arti'].unique()
if len(not_matched) > 0:
raise ValueError(f'Не найдены значения: {not_matched}')
return result