feat: implement backend core with repricing logic, case management, and multi-service architecture
This commit is contained in:
81
backend/routes/repricer.py
Normal file
81
backend/routes/repricer.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from ..db import get_db_connection, release_db_connection
|
||||
from ..middleware import token_required
|
||||
import random
|
||||
from datetime import datetime
|
||||
|
||||
repricer_blueprint = Blueprint('repricer', __name__)
|
||||
|
||||
@repricer_blueprint.route('/repricer/rules', methods=['GET'])
|
||||
@token_required
|
||||
def list_rules(user_info):
|
||||
company_id = user_info['company_id']
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT id, product_name, sku, min_price, max_price, strategy, is_active, current_price, last_run FROM repricer_rules WHERE user_id = %s", (company_id,))
|
||||
rows = cur.fetchall()
|
||||
release_db_connection(conn)
|
||||
|
||||
return jsonify([{
|
||||
"id": r[0],
|
||||
"product_name": r[1],
|
||||
"sku": r[2],
|
||||
"min_price": float(r[3]),
|
||||
"max_price": float(r[4]),
|
||||
"strategy": r[5],
|
||||
"is_active": r[6],
|
||||
"current_price": float(r[7]) if r[7] else None,
|
||||
"last_run": r[8].isoformat() if r[8] else None
|
||||
} for r in rows])
|
||||
|
||||
@repricer_blueprint.route('/repricer/rules', methods=['POST'])
|
||||
@token_required
|
||||
def save_rule(user_info):
|
||||
company_id = user_info['company_id']
|
||||
data = request.json
|
||||
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
if data.get('id'):
|
||||
cur.execute(
|
||||
"UPDATE repricer_rules SET product_name=%s, sku=%s, min_price=%s, max_price=%s, strategy=%s, is_active=%s WHERE id=%s AND user_id=%s",
|
||||
(data['product_name'], data['sku'], data['min_price'], data['max_price'], data['strategy'], data['is_active'], data['id'], company_id)
|
||||
)
|
||||
else:
|
||||
cur.execute(
|
||||
"INSERT INTO repricer_rules (user_id, product_name, sku, min_price, max_price, strategy) VALUES (%s, %s, %s, %s, %s)",
|
||||
(company_id, data['product_name'], data['sku'], data['min_price'], data['max_price'], data['strategy'])
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
release_db_connection(conn)
|
||||
return jsonify({"message": "Rule saved"})
|
||||
|
||||
@repricer_blueprint.route('/repricer/run', methods=['POST'])
|
||||
@token_required
|
||||
def run_repricer(user_info):
|
||||
"""
|
||||
Симуляция работы репрайзера: находит конкурентов и обновляет цены
|
||||
"""
|
||||
company_id = user_info['company_id']
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT id, min_price, max_price, current_price FROM repricer_rules WHERE user_id = %s AND is_active = TRUE", (company_id,))
|
||||
rules = cur.fetchall()
|
||||
|
||||
updates = []
|
||||
for r_id, min_p, max_p, curr_p in rules:
|
||||
# Симулируем цену конкурента
|
||||
comp_price = float(min_p) + random.uniform(-50, 150)
|
||||
|
||||
# Наша стратегия: на 1 рубль дешевле конкурента, но не ниже min_price
|
||||
new_price = max(float(min_p), comp_price - 1)
|
||||
new_price = min(new_price, float(max_p))
|
||||
|
||||
cur.execute("UPDATE repricer_rules SET current_price = %s, last_run = %s WHERE id = %s", (new_price, datetime.utcnow(), r_id))
|
||||
updates.append({"id": r_id, "old": float(curr_p) if curr_p else 0, "new": round(new_price, 2), "competitor": round(comp_price, 2)})
|
||||
|
||||
conn.commit()
|
||||
release_db_connection(conn)
|
||||
return jsonify({"updates": updates})
|
||||
Reference in New Issue
Block a user