Commit ec0b36eb by zhouhaibao

fix(data-report): 修复数据报告中的精度计算和格式化问题

- 修改比例字段格式化逻辑,统一保留2位小数并防止四舍五入误差
- 提高平均值计算精度,避免Decimal量化过程中的精度损失
- 添加原始值诊断日志,便于调试百分比字段的数据流转问题
1 parent 315f21a5
...@@ -405,8 +405,9 @@ def _fix_number_format(fmt, attr_name): ...@@ -405,8 +405,9 @@ def _fix_number_format(fmt, attr_name):
if not fmt or '%' not in fmt: if not fmt or '%' not in fmt:
return fmt return fmt
if _is_ratio_field(attr_name): if _is_ratio_field(attr_name):
# 真正的比例字段 → 转字面百分号(DB 已存 1-100,不应再自动乘 100) # 真正的比例字段 → 统一保留 2 位小数 + 字面 "%"
return fmt.replace('%', '"%"') # 防模板整数百分比格式(0%)四舍五入:DB 存 1.5 显示成 2%
return '0.00"%"'
else: else:
# 金额/数值字段被误配 % → 去掉百分号字符,保留数字精度 # 金额/数值字段被误配 % → 去掉百分号字符,保留数字精度
cleaned = fmt.replace('%', '').strip() cleaned = fmt.replace('%', '').strip()
......
...@@ -1053,10 +1053,10 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie ...@@ -1053,10 +1053,10 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie
result[sn] = {} result[sn] = {}
for f, v in fields.items(): for f, v in fields.items():
if isinstance(v, list): if isinstance(v, list):
# 数值 [sum, count] → 取平均 # 数值 [sum, count] → 取平均(高精度保留,避免 0.015 被 quantize(0.01) 提前四舍五入成 0.02)
total, cnt = v total, cnt = v
result[sn][f] = (total / Decimal(str(cnt))).quantize( result[sn][f] = (total / Decimal(str(cnt))).quantize(
Decimal('0.01'), rounding=ROUND_HALF_UP) if cnt > 0 else Decimal('0') Decimal('0.000001'), rounding=ROUND_HALF_UP) if cnt > 0 else Decimal('0')
else: else:
result[sn][f] = v result[sn][f] = v
return result return result
...@@ -1191,6 +1191,13 @@ def parse_commerce_policy_inshop(file_path, biz_type='1.1'): ...@@ -1191,6 +1191,13 @@ def parse_commerce_policy_inshop(file_path, biz_type='1.1'):
default = {f: Decimal('0') for f in numeric_fields} default = {f: Decimal('0') for f in numeric_fields}
# 百分比字段:Excel 里写 1.5% → pandas 读 0.015,需要 ×100 存 1-100 # 百分比字段:Excel 里写 1.5% → pandas 读 0.015,需要 ×100 存 1-100
pct_fields = {'frontline_incentive_ratio'} pct_fields = {'frontline_incentive_ratio'}
# === 诊断:打印 raw 原始值(×100 之前)===
if raw:
sample_sn = list(raw.keys())[:2]
logger.info(f"[商务政策-店中店] 诊断 raw 值: ")
for sn in sample_sn:
fr_val = raw.get(sn, {}).get('frontline_incentive_ratio', 'MISSING')
logger.info(f" {sn}: frontline_incentive_ratio raw={fr_val} type={type(fr_val).__name__}")
for sn, v in raw.items(): for sn, v in raw.items():
merged = dict(default) merged = dict(default)
merged.update({k: v[k] for k in numeric_fields if k in v}) merged.update({k: v[k] for k in numeric_fields if k in v})
...@@ -2232,7 +2239,7 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1): ...@@ -2232,7 +2239,7 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1):
INNER JOIN dj_car_life_pay_order po INNER JOIN dj_car_life_pay_order po
ON go.order_code = po.order_code AND po.deleted_flag = 'N' ON go.order_code = po.order_code AND po.deleted_flag = 'N'
LEFT JOIN dj_car_life_goods_sub_order gso LEFT JOIN dj_car_life_goods_sub_order gso
ON gopa.sub_order_code = gso.sub_order_code ON gopa.sub_order_code = gso.sub_order_code AND gso.deleted_flag = 'N'
LEFT JOIN dj_car_life_goods_order_profit_allocation gopa2 LEFT JOIN dj_car_life_goods_order_profit_allocation gopa2
ON gopa2.order_code = gopa.order_code ON gopa2.order_code = gopa.order_code
AND gopa2.order_flag = 'Y' AND gopa2.order_flag = 'Y'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!