import pandas as pd
from datetime import datetime
import os

# 配置参数
CONFIG = {
    'input_csv': r'C:\Users\Administrator\Desktop\European Local O&M\all-active-0522.csv.csv',
    'output_dir': './output',
    'required_columns': ['car_vins', 'active_status', 'MATERIAL', 'Area','active_date']
}
df=pd.read_csv(CONFIG['input_csv'])
df['active_date'] = pd.to_datetime(df['active_date'], errors='coerce')
active_df = df[df['active_status'] == 'active']
daily_count = active_df.groupby(active_df['active_date'].dt.date).size().reset_index(name='active_count')
print(daily_count)
daily_count['active_date'] = pd.to_datetime(daily_count['active_date'])
daily_count.set_index('active_date', inplace=True)
weekly_count = daily_count.resample('W-MON').sum().reset_index()  # 每周从周一开始

weekly_count.columns = ['week_start', 'weekly_active_count']
weekly_count['week_number'] = pd.to_datetime(weekly_count['week_start']).dt.isocalendar().week
weekly_count['year'] = pd.to_datetime(weekly_count['week_start']).dt.isocalendar().year
# 转换为 datetime 类型
weekly_count['week_start'] = pd.to_datetime(weekly_count['week_start'])

# 提取年份后两位、月份
weekly_count['year'] = weekly_count['week_start'].dt.year % 100
weekly_count['month'] = weekly_count['week_start'].dt.month

# 分组内生成「每月第几周」编号（用 cumcount）
weekly_count['week_in_month'] = weekly_count.groupby(['year', 'month']).cumcount() + 1

# 拼接成格式：25-1-4
weekly_count['year_month_week'] = (
    weekly_count['year'].astype(str) + '-' +
    weekly_count['month'].astype(str) + '-' +
    weekly_count['week_in_month'].astype(str)
)

print(weekly_count)

# 获取输出目录路径
output_dir = CONFIG['output_dir']
os.makedirs(output_dir, exist_ok=True)  # 自动创建目录（如果不存在）

# 拼接 Excel 文件路径
output_path = os.path.join(output_dir, 'active_car_number.xlsx')

# 导出
with pd.ExcelWriter(output_path, engine='openpyxl') as writer:
    daily_count.reset_index().to_excel(writer, sheet_name='Daily', index=False)
    weekly_count.to_excel(writer, sheet_name='Weekly', index=False)

print(f"已成功导出至：{output_path}")
