diff --git a/product_management/core.py b/product_management/core.py index d8ee48f..98f0174 100644 --- a/product_management/core.py +++ b/product_management/core.py @@ -1172,13 +1172,21 @@ class ProductManager: created_cats += 1 # Step 3: Process products — update existing, create new + # Local dedup: skip duplicate product_code within same import batch + seen_codes = set() for prod in products: target_cat_id = source_to_id.get(prod.get('source_category_id')) if not target_cat_id: continue + product_code = prod.get('product_code', '') resource_ref_id = prod.get('resource_ref_id', '') + # Skip duplicate product_code in this batch + if product_code in seen_codes: + continue + seen_codes.add(product_code) + # Check if product already exists by resource_ref_id + org_id existing_prod = None if resource_ref_id: @@ -1188,15 +1196,24 @@ class ProductManager: {'ref_id': resource_ref_id, 'org_id': org_id} ) + # Fallback: check by product_code + org_id (unique constraint) + if not existing_prod and product_code: + existing_prod = await sor.sqlExe( + """SELECT id FROM product + WHERE product_code = ${code}$ AND org_id = ${org_id}$""", + {'code': product_code, 'org_id': org_id} + ) + if existing_prod: # Update existing product await sor.U('product', { 'id': existing_prod[0].id, 'product_name': prod.get('product_name', ''), - 'product_code': prod.get('product_code', ''), + 'product_code': product_code, 'product_type': prod.get('product_type', ''), 'brief_intro': prod.get('brief_intro', ''), 'category_id': target_cat_id, + 'resource_ref_id': resource_ref_id, 'sort_order': str(prod.get('sort_order', 0)), 'updated_at': now })