supplychain/wwwroot/api/save_agreement_discount.dspy
yumoqing 9afb3c279b fix: save dspys return Message widget for visual feedback
script actiontype forbidden in bricks; use Message widget instead.
Status column reflects saved state on page load/reload.
2026-07-12 23:25:22 +08:00

61 lines
2.7 KiB
Plaintext

"""Save or update a distribution agreement product discount."""
agreement_id = params_kw.get('agreement_id')
productid = params_kw.get('productid')
prodtypeid = params_kw.get('prodtypeid') or ''
new_discount = params_kw.get('discount') or '1.0'
old_discount = params_kw.get('old_discount') or '1.0'
item_id = params_kw.get('item_id')
userorgid = (await get_userorgid()) or '0'
dbname = get_module_dbname('supplychain')
async with DBPools().sqlorContext(dbname) as sor:
if item_id:
# Existing item — update or delete
if new_discount == old_discount:
# No change, skip
pass
elif float(new_discount) <= 0:
# Delete if discount set to 0
await sor.D('distribution_agreement_items', {'id': item_id})
# Also remove product_org_auth
async with DBPools().sqlorContext('sage') as s2:
await s2.sqlExe("DELETE FROM product_org_auth WHERE auth_source_id = ${sid}$", {'sid': item_id})
else:
await sor.U('distribution_agreement_items', {
'id': item_id, 'discount': new_discount, 'updated_at': timestampstr()
})
elif float(new_discount) > 0 and new_discount != old_discount:
# New item — create
item_id = getID()
now = timestampstr()
await sor.C('distribution_agreement_items', {
'id': item_id, 'agreement_id': agreement_id, 'resellerid': userorgid,
'productid': productid, 'prodtypeid': prodtypeid,
'discount': new_discount, 'created_at': now,
})
# Sync product_org_auth (upsert — skip if exists)
async with DBPools().sqlorContext(dbname) as s2:
ag = await s2.sqlExe(
"SELECT sub_reseller_id FROM distribution_agreements WHERE id = ${aid}$",
{'aid': agreement_id}
)
if ag:
async with DBPools().sqlorContext('sage') as s3:
existing = await s3.sqlExe(
"SELECT id FROM product_org_auth WHERE product_id = ${pid}$ AND org_id = ${oid}$",
{'pid': productid, 'oid': ag[0].sub_reseller_id}
)
if not existing:
await s3.C('product_org_auth', {
'id': getID(), 'product_id': productid,
'org_id': ag[0].sub_reseller_id,
'auth_source': 'distribution_agreement', 'auth_source_id': item_id,
})
status_text = '已设置' if float(new_discount) > 0 else '已删除'
return {
'widgettype': 'Message',
'options': {'title': '保存成功', 'message': f'折扣 {status_text}: {new_discount}', 'type': 'success', 'timeout': 2}
}