from datetime import datetime, timezone
import os
import csv

# 设置路径参数
base_dir = r'C:\Users\Administrator\Desktop\European Local O&M'
result_dir = os.path.join(base_dir, 'italy_result')
os.makedirs(result_dir, exist_ok=True)  # 自动创建目录

# 获取日期
current_date = datetime.now().strftime("%m%d")

# 加载激活状态数据
active_file = os.path.join(base_dir, f'all-active-{current_date}.csv.csv')  # 确保文件名正确
active_status = {}

with open(active_file, encoding='utf-8') as f:
    csv_reader = csv.reader(f)
    next(csv_reader)
    for row in csv_reader:
        vin = row[0].strip().upper()
        status = row[1].strip().lower()
        active_status[vin] = 'active' if status == 'active' else 'NO'

# 加载需要核实的VIN列表
vin_list = []
with open(os.path.join(base_dir, 'eu516.csv'), 'r', encoding='utf-8') as f:
    csv_reader = csv.reader(f)
    next(csv_reader)
    for row in csv_reader:
        vin_list.append(row[0].strip().upper())

# 输出文件路径
output_all = os.path.join(result_dir, f'italy-result{current_date}.csv')
output_no = os.path.join(result_dir, f'no-italy-result{current_date}.csv')

# 写入意大利所有VIN的状态
with open(output_all, 'w', newline='', encoding='utf-8') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(['VIN', 'active-status'])
    for vin in vin_list:
        status = active_status.get(vin, 'NO')
        csv_writer.writerow([vin, status])

# 统计信息并生成未激活车辆清单
activated_count = 0
deactivated_count = 0

# 当前UTC时间
current_utc = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")

with open(output_all, encoding='utf-8') as f_in, \
     open(output_no, 'w', newline='', encoding='utf-8') as f_out:

    reader = csv.DictReader(f_in)
    writer = csv.DictWriter(f_out, fieldnames=reader.fieldnames)
    writer.writeheader()

    for row in reader:
        status = row['active-status'].strip().lower()
        if status == 'active':
            activated_count += 1
        elif status == 'no':
            deactivated_count += 1
            writer.writerow(row)

# 控制台输出统计信息
print(f'''
统计时间：{current_utc}
激活车辆数：{activated_count}
未激活车辆数：{deactivated_count}

📁 所有结果已保存至：{result_dir}
''')
