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,
"default": "0"
},
{
"name": "providerid",
"title": "供应商ID",
"type": "str",
"length": 32
},
{
"name": "created_by",
"title": "创建人",

View File

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