65 lines
2.8 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 购买弹窗实时算价:按当前输入参数试算售价(与 purchase_realtime 扣款同口径,只读不写)
# 存储:storage_gb × valid_months(meter_mode=direct 直购)
# 产线:charge_mode month/year(年付=月价×10,months 因子由定价映射解析)
from ahserver.serverenv import ServerEnv
product_id = params_kw.get('product_id', '')
userid = await get_user()
if not product_id:
result = {'success': False, 'message': '缺少产品ID'}
elif not userid:
result = {'success': False, 'message': '请先登录'}
else:
try:
userorgid = await get_userorgid()
dbname = get_module_dbname('product_management')
async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe(
"SELECT id, product_type FROM product WHERE id=${id}$ AND status='1'",
{'id': product_id})
await sor.sqlExe("COMMIT", {})
if not recs:
raise Exception('产品不存在或已下架')
product_type = getattr(recs[0], 'product_type', '') or ''
# usage_data 构造与 purchase_realtime 完全一致(同口径预览,所见即所付)
usage_data = None
if product_type == 'workspace_storage':
storage_gb = float(params_kw.get('storage_gb') or 0)
valid_months = max(1, int(params_kw.get('valid_months') or 1))
if storage_gb <= 0:
result = {'success': False, 'message': '请填写购买容量(GB)'}
else:
usage_data = {'meter_mode': 'direct', 'storage_gb': storage_gb,
'valid_months': valid_months}
elif product_type == 'pipeline':
cm = params_kw.get('charge_mode') or 'month'
if cm not in ('month', 'year'):
cm = 'month'
usage_data = {'charge_mode': cm}
else:
usage_data = {}
if usage_data is not None:
env = ServerEnv()
cost = await env.calculate_product_cost(
product_id=product_id, usage_data=usage_data,
user_org_id=userorgid)
if not cost or cost.get('success') is False:
result = {'success': False,
'message': (cost or {}).get('message', '定价计算失败')}
else:
result = {
'success': True,
'amount': float(cost.get('amount', 0) or 0),
'original_amount': float(cost.get('original_amount', 0) or 0),
'discount': float(cost.get('discount', 1.0) or 1.0),
'product_type': product_type,
}
except Exception as e:
debug(f'calc_price error: {format_exc()}')
result = {'success': False, 'message': str(e)[:200]}
return json.dumps(result, ensure_ascii=False)