product_management/wwwroot/api/backfill_providerid.dspy
yumoqing cfa9b321c4 fix: 导入去除冗余org_id过滤 + 添加providerid回填dspy
- import_category_products: 移除org_id条件(树控件已做过滤)
- backfill_providerid: 从llm.providerid回填product.providerid
2026-07-13 15:36:31 +08:00

54 lines
1.7 KiB
Plaintext

providerid = params_kw.get('providerid', '')
org_id = params_kw.get('org_id', '')
# Build filter conditions
conditions = ["providerid IS NULL OR providerid = ''"]
params = {}
if providerid:
conditions.append("providerid = ${pid}$")
params['pid'] = providerid
if org_id:
conditions.append("org_id = ${oid}$")
params['oid'] = org_id
where_clause = " AND ".join(conditions)
# 1. Count products to update
async with DBPools().sqlorContext('sage') as sor:
count_rows = await sor.sqlExe(
f"SELECT COUNT(*) as cnt FROM product WHERE {where_clause}",
params
)
total = count_rows[0].cnt if count_rows else 0
if total == 0:
return json.dumps({"status": "ok", "message": "没有需要回填的产品"})
# 2. Update providerid from llm table (product.resource_ref_id = llm.id)
async with DBPools().sqlorContext('llmage') as sor:
llms = await sor.sqlExe(
"SELECT id, providerid FROM llm WHERE status = 'published'", {}
)
llm_pid = {r.id: r.providerid for r in llms if r.providerid}
# 3. Get products and update
updated = 0
async with DBPools().sqlorContext('sage') as sor:
rows = await sor.sqlExe(
f"SELECT id, resource_ref_id, product_name FROM product WHERE {where_clause}",
params
)
for p in rows:
pid = llm_pid.get(p.resource_ref_id, '')
if pid:
await sor.sqlExe(
"UPDATE product SET providerid = ${pid}$ WHERE id = ${id}$",
{'pid': pid, 'id': p.id}
)
updated += 1
return json.dumps({
"status": "ok",
"message": f"回填完成: 共{total}条空providerid, 成功回填{updated}条"
}, ensure_ascii=False)