fix: 导入去除冗余org_id过滤 + 添加providerid回填dspy

- import_category_products: 移除org_id条件(树控件已做过滤)
- backfill_providerid: 从llm.providerid回填product.providerid
This commit is contained in:
yumoqing 2026-07-13 15:36:31 +08:00
parent 3096190f6d
commit cfa9b321c4
2 changed files with 55 additions and 2 deletions

View File

@ -0,0 +1,53 @@
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)

View File

@ -12,8 +12,8 @@ try:
# Get the selected category's resource_module
async with DBPools().sqlorContext(dbname) as sor:
rows = await sor.sqlExe(
"SELECT id, name, resource_module, org_id FROM product_category WHERE id = ${id}$ AND org_id = ${org_id}$",
{'id': category_id, 'org_id': org_id}
"SELECT id, name, resource_module FROM product_category WHERE id = ${id}$",
{'id': category_id}
)
if not rows:
raise ValueError('类别不存在或无权操作')