index.ui: 显示所有产品 + 基准折扣(全部用户) + 方案折扣, 标志'已加入/未加入' get_products.dspy: 左联全部用户基准折扣 + 营销方案专属折扣 save_discount.dspy: 验证 0<新<基准, upsert 到 discount_detail init.py: get_marketing_discount_products + get_marketing_info marketing_list.json: toolbar 指向新页面
78 lines
3.0 KiB
Plaintext
78 lines
3.0 KiB
Plaintext
# 营销方案产品折扣保存
|
||
# 规则: 0 < 新折扣 < 全部用户基准折扣
|
||
# 自动 upsert 到 discount_detail
|
||
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error', 'timeout': 3}}
|
||
|
||
try:
|
||
discountid = params_kw.get('discountid')
|
||
productid = params_kw.get('productid')
|
||
discount_val = params_kw.get('discount')
|
||
base_discount = params_kw.get('base_discount')
|
||
detail_id = params_kw.get('detail_id')
|
||
|
||
if not discountid or not productid:
|
||
raise Exception('缺少必要参数')
|
||
|
||
# 过滤 NaN/空值
|
||
if discount_val is not None:
|
||
s = str(discount_val).strip().lower()
|
||
if s in ('', 'nan', 'none', 'null'):
|
||
discount_val = None
|
||
|
||
user_orgid = await get_userorgid()
|
||
dbname = get_module_dbname('discount')
|
||
|
||
# discount为0或空 → 删除明细
|
||
if not discount_val or float(discount_val) == 0:
|
||
if detail_id:
|
||
async with DBPools().sqlorContext(dbname) as sor:
|
||
await sor.D('discount_detail', {'id': detail_id})
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '已移出营销方案', 'type': 'success', 'timeout': 3}}
|
||
else:
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': 'ok', 'type': 'success', 'timeout': 3}}
|
||
return result
|
||
|
||
new_val = float(discount_val)
|
||
|
||
# 验证: 必须大于0
|
||
if new_val <= 0:
|
||
result = {'widgettype': 'Error', 'options': {'title': '折扣无效', 'message': '折扣必须大于0', 'timeout': 5}}
|
||
return result
|
||
|
||
# 验证: 不能超过基准折扣
|
||
if base_discount:
|
||
base_val = float(base_discount)
|
||
if new_val >= base_val:
|
||
result = {'widgettype': 'Error', 'options': {'title': '折扣无效', 'message': f'营销折扣({new_val})必须小于基准折扣({base_val})', 'timeout': 5}}
|
||
return result
|
||
|
||
# 验证: 不能超过100%(1.0)
|
||
if new_val > 1.0:
|
||
result = {'widgettype': 'Error', 'options': {'title': '折扣无效', 'message': '折扣不能大于1.0', 'timeout': 5}}
|
||
return result
|
||
|
||
async with DBPools().sqlorContext(dbname) as sor:
|
||
if detail_id:
|
||
await sor.U('discount_detail', {
|
||
'id': detail_id,
|
||
'discount': new_val
|
||
})
|
||
else:
|
||
await sor.C('discount_detail', {
|
||
'id': getID(),
|
||
'discountid': discountid,
|
||
'resellerid': user_orgid,
|
||
'prodtypeid': None,
|
||
'productid': productid,
|
||
'discount': new_val
|
||
})
|
||
|
||
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': f'折扣保存成功({new_val})', 'type': 'success', 'timeout': 3}}
|
||
|
||
except Exception as e:
|
||
debug(f'save_marketing_discount error: {format_exc()}')
|
||
result['options'] = {'title': 'Error', 'message': f'保存失败: {str(e)}', 'type': 'error', 'timeout': 5}
|
||
|
||
return result
|