refactor: centralize QWEN fitment saving via save_qwen_fitment()
- Added save_qwen_fitment() in inventory_vehicle_compat.py to centralize inserting QWEN results into inventory_vehicle_compat - Simplified inventory_bp.py create_item() and auto_match_item_vehicles() to use the centralized function, removing duplicated INSERT logic
This commit is contained in:
@@ -369,3 +369,37 @@ def batch_add_compatibilities(tenant_conn, inventory_id, mye_ids, source='manual
|
||||
tenant_conn.commit()
|
||||
cur.close()
|
||||
return inserted
|
||||
|
||||
|
||||
def save_qwen_fitment(tenant_conn, inventory_id, fitment_result):
|
||||
"""Save QWEN fitment results into inventory_vehicle_compat.
|
||||
|
||||
Args:
|
||||
tenant_conn: Connection to tenant DB.
|
||||
inventory_id: The inventory item ID.
|
||||
fitment_result: Dict from get_vehicle_fitment() with 'vehicles' list.
|
||||
|
||||
Returns:
|
||||
int: Number of compatibilities inserted.
|
||||
"""
|
||||
vehicles = fitment_result.get('vehicles', [])
|
||||
if not vehicles:
|
||||
return 0
|
||||
|
||||
inserted = 0
|
||||
cur = tenant_conn.cursor()
|
||||
for v in vehicles:
|
||||
mye_id = v.get('mye_id')
|
||||
if not mye_id:
|
||||
continue
|
||||
cur.execute("""
|
||||
INSERT INTO inventory_vehicle_compat
|
||||
(inventory_id, model_year_engine_id, source, confidence, created_at)
|
||||
VALUES (%s, %s, 'qwen_ai', %s, NOW())
|
||||
ON CONFLICT (inventory_id, model_year_engine_id) DO NOTHING
|
||||
""", (inventory_id, mye_id, fitment_result.get('confidence', 0)))
|
||||
if cur.rowcount > 0:
|
||||
inserted += 1
|
||||
tenant_conn.commit()
|
||||
cur.close()
|
||||
return inserted
|
||||
|
||||
Reference in New Issue
Block a user