#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""汇总验收数值（读产出 Excel，不重算）。"""
import os

import pandas as pd

XL = os.path.expanduser('~/Desktop/Hermes输出-工作类/销售分析/集团维度_货号增减归因.xlsx')
ov = pd.read_excel(XL, sheet_name='集团同比总览')
inc = pd.read_excel(XL, sheet_name='各集团增量Top')
dec = pd.read_excel(XL, sheet_name='各集团减量Top')

print('实体数 =', len(ov), ' 正Δ =', int((ov['Δ'] > 0).sum()), ' 负Δ =', int((ov['Δ'] < 0).sum()))
print('货号数(全量distinct) =', pd.read_excel(XL, sheet_name='集团x货号_Δ长表')['货号'].nunique())
print('Σ25=%.2f Σ26=%.2f ΣΔ=%.2f' % (ov['2025同期'].sum(), ov['2026同期'].sum(), ov['Δ'].sum()))

print('\n── 增量 Top5 集团（含第一驱动货号）──')
for _, r in ov[ov['Δ'] > 0].nlargest(5, 'Δ').iterrows():
    d = inc[inc['集团'] == r['集团']].nlargest(1, 'Δ').iloc[0]
    print('  %-24s Δ%+10.2f (%s)  Δ%%=%s  第一驱动 %s %+.0f (占集团增量%.1f%%)'
          % (r['集团'], r['Δ'], ('%.1f万→%.1f万' % (r['2025同期'] / 1e4, r['2026同期'] / 1e4)),
             r['Δ%'], d['货号'], d['Δ'], d['占集团增量比%']))

print('\n── 减量 Top5 集团（含第一拖累货号）──')
for _, r in ov[ov['Δ'] < 0].nsmallest(5, 'Δ').iterrows():
    d = dec[dec['集团'] == r['集团']].nsmallest(1, 'Δ').iloc[0]
    print('  %-24s Δ%+10.2f (%s)  Δ%%=%s  第一拖累 %s %+.0f'
          % (r['集团'], r['Δ'], ('%.1f万→%.1f万' % (r['2025同期'] / 1e4, r['2026同期'] / 1e4)),
             r['Δ%'], d['货号'], d['Δ']))
print('\n(负Δ集团仅 %d 个 → 减量Top 实际条数 %d)' % (int((ov['Δ'] < 0).sum()), int((ov['Δ'] < 0).sum())))
