55 lines
2.1 KiB
Plaintext
55 lines
2.1 KiB
Plaintext
# Upsert折扣明细:有detail_id则更新,无则新增
|
||
# 参数: discountid, productid, category_id, discount, detail_id(可选)
|
||
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}}
|
||
|
||
try:
|
||
discountid = params_kw.get('discountid')
|
||
productid = params_kw.get('productid')
|
||
category_id = params_kw.get('category_id')
|
||
discount_val = params_kw.get('discount')
|
||
detail_id = params_kw.get('detail_id')
|
||
|
||
if not discountid or not productid:
|
||
raise Exception('缺少必要参数: discountid, productid')
|
||
|
||
# discount为0或空时,如果已有记录则删除
|
||
if not discount_val or float(discount_val) == 0:
|
||
if detail_id:
|
||
dbname = get_module_dbname('discount')
|
||
async with DBPools().sqlorContext(dbname) as sor:
|
||
await sor.D('discount_detail', {'id': detail_id})
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣已清除', 'type': 'success'}}
|
||
else:
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': 'ok', 'type': 'success'}}
|
||
return result
|
||
|
||
dbname = get_module_dbname('discount')
|
||
user_orgid = await get_userorgid()
|
||
|
||
async with DBPools().sqlorContext(dbname) as sor:
|
||
if detail_id:
|
||
# 更新
|
||
await sor.U('discount_detail', {
|
||
'id': detail_id,
|
||
'discount': float(discount_val)
|
||
})
|
||
else:
|
||
# 新增
|
||
await sor.C('discount_detail', {
|
||
'id': uuid(),
|
||
'discountid': discountid,
|
||
'resellerid': user_orgid,
|
||
'prodtypeid': category_id,
|
||
'productid': productid,
|
||
'discount': float(discount_val)
|
||
})
|
||
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣保存成功', 'type': 'success'}}
|
||
|
||
except Exception as e:
|
||
debug(f'save_discount error: {e}')
|
||
result['options'] = {'title': 'Error', 'message': f'保存失败: {str(e)}', 'type': 'error'}
|
||
|
||
return result
|