#!/usr/bin/env python3
"""B3: 营业额产值统计表 → 中东财务数据看板 (2026-06-08)"""
import pandas as pd, warnings, json, numpy as np
from pathlib import Path
warnings.filterwarnings('ignore')

DATA_DATE = '2026-06-08'
RAW_DIR = Path(f'/mnt/y/Openclaw_Hub/03.资源/实施项目 wiki/raw/月报数据/{DATA_DATE}')
OUT_DIR = Path(f'/mnt/y/Openclaw_Hub/03.资源/实施项目 wiki/dashboard/data/{DATA_DATE}/cleaned')
OUT_DIR.mkdir(parents=True, exist_ok=True)

df = pd.read_excel(RAW_DIR / '2026年06月项目营业额、产值统计表.xls', 
                   sheet_name='2026年营业额、产值统计表', header=None)

# Headers from Row 1
headers = {}
for c in range(df.shape[1]):
    v = df.iloc[1, c]
    if pd.notna(v):
        headers[c] = str(v).strip()

# Extract Middle East data (大区='中东')
me_rows = []
for r in range(2, df.shape[0]):
    if str(df.iloc[r, 0]).strip() == '中东':
        row_data = {}
        for c, name in headers.items():
            row_data[name] = df.iloc[r, c]
        me_rows.append(row_data)

df_me = pd.DataFrame(me_rows)
print(f'Middle East: {len(df_me)} projects')

# Filter active projects
active = df_me[df_me['项目阶段'] == '活跃在建'].copy()
print(f'活跃在建: {len(active)}')

# Key financial columns
key_cols = ['大区','驻外机构','国家/地区','项目编码','项目阶段','工程状态',
    '项目类别','产业链（十链）','项目参与方式','项目经理',
    '年度计划营业额','本月营业收入','本年营业收入','项目累计营业收入',
    '工程应收款','签约合同额（不含税）','签约合同额中国港湾占额（不含税）',
    '批准合同额（不含税）','批准合同额中国港湾占额（含税）']

# Also extract 6月 columns (43-56) and 两金/收款 (61-68)
for c in range(43, 57):
    if c in headers:
        key_cols.append(headers[c] + f'_col{c}')
        df_me[f'6月_col{c}'] = pd.to_numeric(df_me.iloc[:, c], errors='coerce') if c < df_me.shape[1] else np.nan
        active[f'6月_col{c}'] = pd.to_numeric(active.iloc[:, c], errors='coerce') if c < active.shape[1] else np.nan

# Rename project name column
df_me['项目名称'] = df_me['地区/驻外机构/项目'] if '地区/驻外机构/项目' in df_me.columns else ''
active['项目名称'] = active['地区/驻外机构/项目'] if '地区/驻外机构/项目' in active.columns else ''

# Numeric conversion
for col in ['年度计划营业额','本月营业收入','本年营业收入','项目累计营业收入',
            '工程应收款','签约合同额中国港湾占额（含税）','批准合同额（不含税）']:
    if col in df_me.columns:
        df_me[col + '_num'] = pd.to_numeric(df_me[col], errors='coerce')
        active[col + '_num'] = pd.to_numeric(active[col], errors='coerce')

# Summary
print(f'\n📊 中东活跃在建项目财务汇总 (万美元):')
print(f'  项目数: {len(active)}')
print(f'  签约合同额总计: {active["签约合同额中国港湾占额（含税）_num"].sum():,.0f}')
print(f'  本年营业收入总计: {active["本年营业收入_num"].sum():,.0f}')
print(f'  本月营业收入总计: {active["本月营业收入_num"].sum():,.0f}')
print(f'  累计营业收入总计: {active["项目累计营业收入_num"].sum():,.0f}')

# Top projects by revenue
active_sorted = active.sort_values('本年营业收入_num', ascending=False)
print(f'\n本年营收Top 10:')
for _, r in active_sorted.head(10).iterrows():
    name = str(r.get('地区/驻外机构/项目', str(r.iloc[3])))[:50]
    print(f'  {name:50s} 本年{r["本年营业收入_num"]:>10,.0f} 合同{r["签约合同额中国港湾占额（含税）_num"]:>12,.0f}')

# Output
# Convert all to string for parquet
for df_out in [df_me, active]:
    for col in df_out.columns:
        if df_out[col].dtype == 'object':
            try: df_out[col] = df_out[col].astype(str)
            except: pass

df_me.to_parquet(OUT_DIR / 'revenue_all_me.parquet', index=False)
active.to_parquet(OUT_DIR / 'revenue_active.parquet', index=False)
df_me.to_csv(OUT_DIR / 'revenue_all_me.csv', index=False, encoding='utf-8-sig')
active.to_csv(OUT_DIR / 'revenue_active.csv', index=False, encoding='utf-8-sig')

report = {
    'all_me': len(df_me),
    'active': len(active),
    'total_contract': float(active['签约合同额中国港湾占额（含税）_num'].sum()),
    'annual_revenue': float(active['本年营业收入_num'].sum()),
    'monthly_revenue': float(active['本月营业收入_num'].sum()),
    'cumulative_revenue': float(active['项目累计营业收入_num'].sum()),
}
with open(OUT_DIR / 'revenue_validation.json', 'w', encoding='utf-8') as f:
    json.dump(report, f, ensure_ascii=False, indent=2)

print(f'\n✅ revenue_*.parquet done')
