Files
PVHBackend/db/DBDataUtils.py
T

114 lines
4.1 KiB
Python
Raw 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 re
from loguru import logger as log
def add_category(conn, name, description=None, parent_id=None):
cursor = conn.cursor()
try:
cursor.execute('''
INSERT OR IGNORE INTO categories (name, description, parent_id)
VALUES (?, ?, ?)
''', (name, description, parent_id))
cursor.execute('SELECT id FROM categories WHERE name = ?', (name,))
result = cursor.fetchone()
category_id = result[0] if result else None
return category_id
except sqlite3.Error as e:
log.error(f"Ошибка при добавлении категории: {e}")
return None
def add_filter(conn, name, filter_key, filter_type='single'):
cursor = conn.cursor()
cursor.execute('''
INSERT OR IGNORE INTO filters (name, filter_key, filter_type)
VALUES (?, ?, ?)
''', (name, filter_key, filter_type))
def parse_product_data(product_text):
application_match = re.search(r'(ВНУТРЕННЕЙ|НАРУЖНОЙ)', product_text)
application_type = "внутренняя" if application_match and application_match.group(1) == "ВНУТРЕННЕЙ" else "наружная"
color_match = re.search(r'цвет\s+([^)]+)', product_text)
color = color_match.group(1).strip() if color_match else None
stiffness_match = re.search(r'SN(\d+)', product_text)
stiffness_class = f"SN{stiffness_match.group(1)}" if stiffness_match else None
size_match = re.search(r'(\d+)х(\d+[,.]?\d*)х(\d+)', product_text.replace(',', '.'))
if size_match:
diameter = int(size_match.group(1))
wall_thickness = float(size_match.group(2))
length = int(size_match.group(3))
else:
diameter = wall_thickness = length = None
short_name = f"Труба ПВХ {diameter}мм {application_type} {length}мм"
return {
'short_name': short_name,
'full_name': product_text,
'diameter': diameter,
'wall_thickness': wall_thickness,
'length': length,
'color': color,
'application_type': application_type,
'stiffness_class': stiffness_class
}
def add_product(conn, product_text, category_id):
"""Добавление товара с парсингом параметров"""
cursor = conn.cursor()
product_data = parse_product_data(product_text)
try:
cursor.execute('''
INSERT INTO products (
name, full_name, description, category_id,
diameter, wall_thickness, length, color,
application_type, stiffness_class
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
product_data['short_name'],
product_data['full_name'],
f"Труба ПВХ для {product_data['application_type']} канализации",
category_id,
product_data['diameter'],
product_data['wall_thickness'],
product_data['length'],
product_data['color'],
product_data['application_type'],
product_data['stiffness_class']
))
product_id = cursor.lastrowid
filters_to_add = [
('diameter', str(product_data['diameter'])),
('length', str(product_data['length'])),
('application_type', product_data['application_type']),
('color', product_data['color']),
]
if product_data['wall_thickness']:
filters_to_add.append(('wall_thickness', str(product_data['wall_thickness'])))
if product_data['stiffness_class']:
filters_to_add.append(('stiffness_class', product_data['stiffness_class']))
for filter_key, value in filters_to_add:
if value:
cursor.execute('''
INSERT OR IGNORE INTO product_filters (product_id, filter_key, value)
VALUES (?, ?, ?)
''', (product_id, filter_key, value))
return product_id
except sqlite3.Error as e:
log.error(f"Ошибка при добавлении товара: {e}")
return None