feat: product模型添加providerid字段 + 导入改为增量模式

- product.json: 添加providerid字段(供应商ID)
- core.py: import_categories_and_products改为增量模式
  已有产品跳过不再更新, 仅创建新资源对应的产品
  创建产品时自动设置providerid为资源模块返回的值
This commit is contained in:
yumoqing 2026-07-13 14:53:53 +08:00
parent 5f277949ae
commit 3096190f6d
2 changed files with 35 additions and 39 deletions

View File

@ -118,6 +118,12 @@
"length": 32, "length": 32,
"default": "0" "default": "0"
}, },
{
"name": "providerid",
"title": "供应商ID",
"type": "str",
"length": 32
},
{ {
"name": "created_by", "name": "created_by",
"title": "创建人", "title": "创建人",

View File

@ -1009,7 +1009,7 @@ class ProductManager:
created_cats = 0 created_cats = 0
skipped_cats = 0 skipped_cats = 0
created_prods = 0 created_prods = 0
updated_prods = 0 skipped_prods = 0
async with DBPools().sqlorContext(dbname) as sor: async with DBPools().sqlorContext(dbname) as sor:
# Step 2: Process categories — skip existing, create new # Step 2: Process categories — skip existing, create new
@ -1045,8 +1045,8 @@ class ProductManager:
}) })
created_cats += 1 created_cats += 1
# Step 3: Process products — update existing, create new # Step 3: Process products — skip existing, create new only
# Local dedup: skip duplicate product_code within same import batch # Incremental mode: only import products not already in the system
seen_codes = set() seen_codes = set()
for prod in products: for prod in products:
target_cat_id = source_to_id.get(prod.get('source_category_id')) target_cat_id = source_to_id.get(prod.get('source_category_id'))
@ -1079,46 +1079,36 @@ class ProductManager:
) )
if existing_prod: if existing_prod:
# Update existing product skipped_prods += 1
await sor.U('product', { continue
'id': existing_prod[0].id,
'product_name': prod.get('product_name', ''), # Create new product
'product_code': product_code, prod_id = getID()
'product_type': prod.get('product_type', ''), await sor.C('product', {
'brief_intro': prod.get('brief_intro', ''), 'id': prod_id,
'category_id': target_cat_id, 'category_id': target_cat_id,
'resource_ref_id': resource_ref_id, 'product_code': prod.get('product_code', ''),
'sort_order': str(prod.get('sort_order', 0)), 'resource_ref_id': resource_ref_id,
'updated_at': now 'product_name': prod.get('product_name', ''),
}) 'product_type': prod.get('product_type', ''),
updated_prods += 1 'brief_intro': prod.get('brief_intro', ''),
else: 'status': '1',
# Create new product 'price_type': '1',
prod_id = getID() 'price': '0',
await sor.C('product', { 'currency': 'CNY',
'id': prod_id, 'sort_order': str(prod.get('sort_order', 0)),
'category_id': target_cat_id, 'org_id': org_id,
'product_code': prod.get('product_code', ''), 'providerid': prod.get('providerid', ''),
'resource_ref_id': resource_ref_id, 'created_by': user_id,
'product_name': prod.get('product_name', ''), 'created_at': now,
'product_type': prod.get('product_type', ''), 'updated_at': now
'brief_intro': prod.get('brief_intro', ''), })
'status': '1', created_prods += 1
'price_type': '1',
'price': '0',
'currency': 'CNY',
'sort_order': str(prod.get('sort_order', 0)),
'org_id': org_id,
'created_by': user_id,
'created_at': now,
'updated_at': now
})
created_prods += 1
return { return {
'success': True, 'success': True,
'message': f'导入完成: 新增 {created_cats} 个类别, {created_prods} 个产品; ' 'message': f'导入完成: 新增 {created_cats} 个类别, {created_prods} 个产品; '
f'跳过 {skipped_cats} 个已有类别; 更新 {updated_prods} 个已有产品' f'跳过 {skipped_cats} 个已有类别, {skipped_prods} 个已有产品'
} }
# ─── Resource Module Interface Dispatcher ─── # ─── Resource Module Interface Dispatcher ───