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 = {}
|
filter_labels = {}
|
||||||
skip_keys = {'formula', 'price_factors', 'unit_prices', 'unit', 'filters', 'min_amount', 'pricing_type', 'name'}
|
skip_keys = {'formula', 'price_factors', 'unit_prices', 'unit', 'filters', 'min_amount', 'pricing_type', 'name'}
|
||||||
|
|
||||||
|
# 先从 p.items() 中提取(直接字段)
|
||||||
for k, v in p.items():
|
for k, v in p.items():
|
||||||
if k in skip_keys:
|
if k in skip_keys:
|
||||||
continue
|
continue
|
||||||
@ -825,6 +826,17 @@ async def get_pricing_display(ppid):
|
|||||||
label = fdef.get('label', k) if isinstance(fdef, dict) else k
|
label = fdef.get('label', k) if isinstance(fdef, dict) else k
|
||||||
filter_labels[label] = v
|
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(scalar) + unit_prices(scalar) + unit
|
||||||
# 注意:生产数据中 price_factors 可能是 list、unit_prices 可能是 dict(混合格式),
|
# 注意:生产数据中 price_factors 可能是 list、unit_prices 可能是 dict(混合格式),
|
||||||
# 此时应走旧格式 formula 路径
|
# 此时应走旧格式 formula 路径
|
||||||
@ -857,40 +869,35 @@ async def get_pricing_display(ppid):
|
|||||||
}]
|
}]
|
||||||
|
|
||||||
# 处理 filters: 提取适用条件(model)到item级,区间条件放tiered
|
# 处理 filters: 提取适用条件(model)到item级,区间条件放tiered
|
||||||
model_filters = {}
|
# 注意:filters 已在上方提取(支持 dict 和 list 两种格式)
|
||||||
if 'filters' in p:
|
# tiered 仅用于价格不同的阶梯定价
|
||||||
tiered_pricing = []
|
tiered_pricing = []
|
||||||
|
if isinstance(p.get('filters'), list):
|
||||||
for fi in p['filters']:
|
for fi in p['filters']:
|
||||||
fi_copy = fi.copy()
|
if not isinstance(fi, dict):
|
||||||
raw_tier_price = fi.get('unit_prices', unit_price)
|
continue
|
||||||
fi_copy.pop('unit_prices', None)
|
raw_tier_price = fi.get('unit_prices')
|
||||||
fi_copy.pop('value_mode', None)
|
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 是适用条件,其他是阶梯定价
|
item = {'price_factors': price_factors_display}
|
||||||
is_model_filter = 'model' in fi_copy and not any(k.endswith('_tokens') for k in fi_copy.keys())
|
if filters:
|
||||||
if is_model_filter:
|
item['filters'] = filters
|
||||||
# 提取为 item 级 filters
|
item['filter_labels'] = filter_labels
|
||||||
model_filters['model'] = fi_copy['model']
|
min_amount = p.get('min_amount', 0)
|
||||||
else:
|
if min_amount:
|
||||||
# 区间条件放 tiered
|
item['min_amount'] = min_amount
|
||||||
meaningful_filters = {k: v for k, v in fi_copy.items()
|
if tiered_pricing:
|
||||||
if not k.endswith('_tokens') and k != 'value_mode'}
|
item['price_factors'][0]['tiered'] = tiered_pricing
|
||||||
if meaningful_filters and raw_tier_price != unit_price:
|
items.append(item)
|
||||||
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)
|
|
||||||
else:
|
else:
|
||||||
# 旧格式:formula
|
# 旧格式:formula
|
||||||
price_factors = p.get('price_factors', None)
|
price_factors = p.get('price_factors', None)
|
||||||
@ -921,12 +928,18 @@ async def get_pricing_display(ppid):
|
|||||||
# 生成可读价格表
|
# 生成可读价格表
|
||||||
display_lines = [f"【{getattr(r, 'name', '')}】定价:"]
|
display_lines = [f"【{getattr(r, 'name', '')}】定价:"]
|
||||||
for item in items:
|
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', []):
|
for pf in item.get('price_factors', []):
|
||||||
label = pf.get('label', pf.get('factor', ''))
|
label = pf.get('label', pf.get('factor', ''))
|
||||||
up = pf.get('unit_price')
|
up = pf.get('unit_price')
|
||||||
ul = pf.get('unit_label', '')
|
ul = pf.get('unit_label', '')
|
||||||
if up is not None:
|
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'):
|
if pf.get('tiered'):
|
||||||
for t in pf['tiered']:
|
for t in pf['tiered']:
|
||||||
t_filters = ', '.join(f"{k}={v}" for k, v in t.get('filters', {}).items())
|
t_filters = ', '.join(f"{k}={v}" for k, v in t.get('filters', {}).items())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user