fix: 支持filters列表格式 + display_text展示过滤条件
1. 从 p['filters'] 列表提取filter条件(数据库实际存储格式) 2. 兼容dict直接字段和list两种filter格式 3. tiered仅用于价格不同的阶梯定价 4. display_text展示每条定价的过滤条件
This commit is contained in:
parent
502468e0cb
commit
c1bfaca049
@ -815,6 +815,7 @@ async def get_pricing_display(ppid):
|
||||
filter_labels = {}
|
||||
skip_keys = {'formula', 'price_factors', 'unit_prices', 'unit', 'filters', 'min_amount', 'pricing_type', 'name'}
|
||||
|
||||
# 先从 p.items() 中提取(直接字段)
|
||||
for k, v in p.items():
|
||||
if k in skip_keys:
|
||||
continue
|
||||
@ -825,6 +826,17 @@ async def get_pricing_display(ppid):
|
||||
label = fdef.get('label', k) if isinstance(fdef, dict) else k
|
||||
filter_labels[label] = v
|
||||
|
||||
# 再从 p['filters'] 列表中提取(如果存在)
|
||||
raw_filters = p.get('filters')
|
||||
if isinstance(raw_filters, list):
|
||||
for fi in raw_filters:
|
||||
if isinstance(fi, dict):
|
||||
for k, v in fi.items():
|
||||
fdef = fields.get(k, {})
|
||||
filters[k] = v
|
||||
label = fdef.get('label', k) if isinstance(fdef, dict) else k
|
||||
filter_labels[label] = v
|
||||
|
||||
# 新格式:price_factors(scalar) + unit_prices(scalar) + unit
|
||||
# 注意:生产数据中 price_factors 可能是 list、unit_prices 可能是 dict(混合格式),
|
||||
# 此时应走旧格式 formula 路径
|
||||
@ -857,40 +869,35 @@ async def get_pricing_display(ppid):
|
||||
}]
|
||||
|
||||
# 处理 filters: 提取适用条件(model)到item级,区间条件放tiered
|
||||
model_filters = {}
|
||||
if 'filters' in p:
|
||||
tiered_pricing = []
|
||||
# 注意:filters 已在上方提取(支持 dict 和 list 两种格式)
|
||||
# tiered 仅用于价格不同的阶梯定价
|
||||
tiered_pricing = []
|
||||
if isinstance(p.get('filters'), list):
|
||||
for fi in p['filters']:
|
||||
fi_copy = fi.copy()
|
||||
raw_tier_price = fi.get('unit_prices', unit_price)
|
||||
fi_copy.pop('unit_prices', None)
|
||||
fi_copy.pop('value_mode', None)
|
||||
if not isinstance(fi, dict):
|
||||
continue
|
||||
raw_tier_price = fi.get('unit_prices')
|
||||
if raw_tier_price is None or raw_tier_price == unit_price:
|
||||
continue
|
||||
fi_copy = {k: v for k, v in fi.items()
|
||||
if k not in ('unit_prices', 'value_mode')
|
||||
and not k.endswith('_tokens')}
|
||||
if fi_copy:
|
||||
tiered_pricing.append({
|
||||
'filters': fi_copy,
|
||||
'unit_prices': raw_tier_price
|
||||
})
|
||||
|
||||
# 区分:model filter 是适用条件,其他是阶梯定价
|
||||
is_model_filter = 'model' in fi_copy and not any(k.endswith('_tokens') for k in fi_copy.keys())
|
||||
if is_model_filter:
|
||||
# 提取为 item 级 filters
|
||||
model_filters['model'] = fi_copy['model']
|
||||
else:
|
||||
# 区间条件放 tiered
|
||||
meaningful_filters = {k: v for k, v in fi_copy.items()
|
||||
if not k.endswith('_tokens') and k != 'value_mode'}
|
||||
if meaningful_filters and raw_tier_price != unit_price:
|
||||
tiered_pricing.append({
|
||||
'filters': meaningful_filters,
|
||||
'unit_prices': raw_tier_price
|
||||
})
|
||||
|
||||
item = {'price_factors': price_factors_display}
|
||||
if model_filters:
|
||||
item['filters'] = model_filters
|
||||
item['filter_labels'] = {'模型': model_filters['model']}
|
||||
min_amount = p.get('min_amount', 0)
|
||||
if min_amount:
|
||||
item['min_amount'] = min_amount
|
||||
if tiered_pricing:
|
||||
item['price_factors'][0]['tiered'] = tiered_pricing
|
||||
items.append(item)
|
||||
item = {'price_factors': price_factors_display}
|
||||
if filters:
|
||||
item['filters'] = filters
|
||||
item['filter_labels'] = filter_labels
|
||||
min_amount = p.get('min_amount', 0)
|
||||
if min_amount:
|
||||
item['min_amount'] = min_amount
|
||||
if tiered_pricing:
|
||||
item['price_factors'][0]['tiered'] = tiered_pricing
|
||||
items.append(item)
|
||||
else:
|
||||
# 旧格式:formula
|
||||
price_factors = p.get('price_factors', None)
|
||||
@ -921,12 +928,18 @@ async def get_pricing_display(ppid):
|
||||
# 生成可读价格表
|
||||
display_lines = [f"【{getattr(r, 'name', '')}】定价:"]
|
||||
for item in items:
|
||||
# 构建过滤条件文本
|
||||
filter_text = ''
|
||||
if item.get('filter_labels'):
|
||||
filter_parts = [f"{k}={v}" for k, v in item['filter_labels'].items()]
|
||||
filter_text = f" [{', '.join(filter_parts)}]"
|
||||
|
||||
for pf in item.get('price_factors', []):
|
||||
label = pf.get('label', pf.get('factor', ''))
|
||||
up = pf.get('unit_price')
|
||||
ul = pf.get('unit_label', '')
|
||||
if up is not None:
|
||||
display_lines.append(f" - {label}: {up} {ul}")
|
||||
display_lines.append(f" - {label}: {up} {ul}{filter_text}")
|
||||
if pf.get('tiered'):
|
||||
for t in pf['tiered']:
|
||||
t_filters = ', '.join(f"{k}={v}" for k, v in t.get('filters', {}).items())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user