Commit d5814f86 by 江名洋

fix(analysis): 修复门店列表列定位与阶梯赔付算法

- 门店列表读取改为按表头名称自动定位列,不再依赖固定列号
- 修复阶梯赔付计算:每区间独立用各自阈值计算缺口,而非复用最高阈值缺口
- 输出Excel添加自动筛选并取消首行合并单元格
- 兼容旧模板"电池电池"写法
1 parent 04e42144
......@@ -187,23 +187,51 @@ GROUP_LABELS = [
class AnalysisService:
"""问界生态系数分析服务"""
# 门店列表必需列名(用于最终输出)
STORE_REQUIRED_COLS = [
"门店名", "兜底指标", "兜底类型",
"特殊情况1", "特殊情况2",
"赔付系数_整车延保", "赔付系数_安心包", "赔付系数_电池",
"兜底截止日期",
]
@staticmethod
def read_store_list(file_path: str) -> pd.DataFrame:
"""读取门店列表Excel(0625模板7列:门店信息 + 赔偿系数 + 兜底截止日期)"""
"""读取门店列表Excel,根据表头名称自动定位列(不依赖列号)"""
df_raw = pd.read_excel(file_path, header=None)
# 第1行表头,第2行子表头(整车延保/安心包/电池电池),第3行起为数据
data_start = 2 # 0-indexed, row 3 in Excel
# Row 0: 一级表头,前向填充处理合并单元格(如"赔付系数"F1:H1)
header_main = df_raw.iloc[0].ffill().astype(str).str.strip()
# Row 1: 二级表头(赔付系数的子列:整车延保/安心包/电池)
header_sub = df_raw.iloc[1].fillna("").astype(str).str.strip()
# 按名称构建列映射
# 子表头名称规范化(兼容旧模板"电池电池"等写法)
SUB_HEADER_NORMALIZE = {"电池电池": "电池"}
col_map = {}
for i in range(len(df_raw.columns)):
h0 = header_main.iloc[i]
if h0 == "赔付系数":
sub = header_sub.iloc[i]
sub = SUB_HEADER_NORMALIZE.get(sub, sub)
col_map[i] = f"赔付系数_{sub}" if sub else f"赔付系数_{i}"
elif h0 == "门店":
col_map[i] = "门店名"
elif h0 in ("兜底指标", "兜底类型", "特殊情况1", "特殊情况2", "兜底截止日期"):
col_map[i] = h0
# 其他未知列忽略
# 数据从第3行开始(0-indexed row 2)
data_start = 2
df = df_raw.iloc[data_start:].copy()
col_names = {
0: "门店名", 1: "兜底指标", 2: "兜底类型",
3: "赔付系数_整车延保", 4: "赔付系数_安心包", 5: "赔付系数_电池",
6: "兜底截止日期",
}
df = df.rename(columns=col_names)
for c in col_names.values():
df = df.rename(columns=col_map)
# 确保必需列都存在,缺失的置 None
for c in AnalysisService.STORE_REQUIRED_COLS:
if c not in df.columns:
df[c] = None
df = df[[c for c in col_names.values()]]
df = df[AnalysisService.STORE_REQUIRED_COLS]
# 去重:同一门店名只保留第一条
before = len(df)
df = df.drop_duplicates(subset=["门店名"], keep="first")
......@@ -787,6 +815,14 @@ class AnalysisService:
reason = "no stores" if len(nature_stores) == 0 else "no factory data"
logger.info(f"Skipping nature section ({reason})")
# 设置自动筛选(覆盖表头行到数据末行)
# 取消第1行合并单元格(避免合并单元格导致筛选灰色不可用)
for merged_range in list(ws.merged_cells.ranges):
if merged_range.min_row == 1 and merged_range.max_row == 1:
ws.unmerge_cells(str(merged_range))
last_col_letter = get_column_letter(len(OUTPUT_HEADERS))
ws.auto_filter.ref = f"A3:{last_col_letter}{next_row - 1}"
wb.save(output_path)
logger.info(f"Report generated: {output_path}")
return output_path
......@@ -1021,19 +1057,29 @@ class AnalysisService:
gap_battery = max(0, math.ceil(gap_base * target_battery - battery_orders)) if target_battery > 0 else 0
# ========== 赔付金额 (AR-AT, cols 44-46) ==========
# 多级赔付算法: 遍历赔付系数(按阈值升序),取当前转化率2所在区间的amount
# 例: "60%,1500;65%,1000" → rate<60%赔1500/单, 60%≤rate<65%赔1000/单, ≥65%不赔
def _calc_payout(levels, current_rate, gap):
if not levels or gap <= 0:
# 阶梯赔付算法(类似 IF(rate<11%, CEILING(gap_base*11%-orders,1)*800, IF(rate<15%, CEILING(gap_base*15%-orders,1)*600, 0)) ):
# - 需达标订单数 gap 统一用最高阈值计算(对应 col 41-43)
# - 赔付金额 = gap × 最低阈值对应的单价(即按最低赔付标准支付)
# 但这里的逻辑是:gap 用最高阈值算,amount 用最低区间的 amount
# 实际上正确的做法是:每个阶梯区间用各自的阈值和金额独立计算
# 例: "11%,800;15%,600" (levels 升序: [(0.11,800), (0.15,600)])
# rate<11%: 赔 CEILING(gap_base*0.11-orders, 1)*800
# 11%≤rate<15%: 赔 CEILING(gap_base*0.15-orders, 1)*600
# rate≥15%: 不赔
def _calc_payout(levels, current_rate, gap_base_val, orders_val):
if not levels:
return 0
for threshold, amount in levels: # 升序遍历
for threshold, amount in levels: # 升序遍历,第一个就是最低阈值
if current_rate < threshold:
gap = max(0, math.ceil(gap_base_val * threshold - orders_val))
if gap <= 0:
return 0
return round(gap * amount, 1)
return 0 # 当前转化率 ≥ 最高阈值,不赔
payout_yb = _calc_payout(coeff_yb, current_rate_yb, gap_yb)
payout_axb = _calc_payout(coeff_axb, current_rate_axb, gap_axb)
payout_battery = _calc_payout(coeff_battery, current_rate_battery, gap_battery)
payout_yb = _calc_payout(coeff_yb, current_rate_yb, gap_base, yb_orders)
payout_axb = _calc_payout(coeff_axb, current_rate_axb, gap_base, axb_orders)
payout_battery = _calc_payout(coeff_battery, current_rate_battery, gap_base, battery_orders)
# 非直营门店不赔付
is_direct = str(store_perf.get("operation_mode", "")).strip() == "直营"
......
No preview for this file type
......@@ -164,7 +164,7 @@ for col_letter in ['A', 'B', 'C', 'D', 'E', 'I']:
# Row2 子表头
write_sub_cell(ows, 2, 6, '整车延保')
write_sub_cell(ows, 2, 7, '安心包')
write_sub_cell(ows, 2, 8, '电池电池')
write_sub_cell(ows, 2, 8, '电池')
# 列宽
ows.column_dimensions['A'].width = 35
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!