# -*- coding: utf-8 -*-
import json
import os

# 读取allure-results目录中的result.json文件
results_dir = r'C:\Users\a\PyCharmMiscProject\smart-management-auto-test\dulizhan\test_case\reports\allure-results'
result_files = [f for f in os.listdir(results_dir) if f.endswith('-result.json')]

test_results = []
for result_file in result_files:
    file_path = os.path.join(results_dir, result_file)
    with open(file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
        test_results.append({
            'name': data.get('name', 'Unknown'),
            'status': data.get('status', 'unknown'),
            'statusDetails': data.get('statusDetails', {}),
            'fullName': data.get('fullName', '')
        })

# 统计结果
passed = [r for r in test_results if r['status'] == 'passed']
failed = [r for r in test_results if r['status'] == 'failed']
broken = [r for r in test_results if r['status'] == 'broken']

# 生成HTML报告
html_content = f"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JoyHub用户模块接口自动化测试报告</title>
    <style>
        body {{ font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f5f5f5; }}
        .container {{ max-width: 1200px; margin: 0 auto; }}
        h1 {{ color: #333; text-align: center; }}
        .summary {{ display: flex; justify-content: center; gap: 30px; margin: 30px 0; }}
        .stat-box {{ background: white; padding: 20px 40px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
        .stat-box.passed {{ border-top: 4px solid #52c41a; }}
        .stat-box.failed {{ border-top: 4px solid #f5222d; }}
        .stat-box.total {{ border-top: 4px solid #1890ff; }}
        .stat-value {{ font-size: 36px; font-weight: bold; }}
        .stat-label {{ color: #666; }}
        .test-list {{ background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 20px; }}
        .test-item {{ display: flex; justify-content: space-between; align-items: center; padding: 10px 15px; border-bottom: 1px solid #eee; }}
        .test-item:last-child {{ border-bottom: none; }}
        .test-name {{ flex: 1; }}
        .status {{ padding: 5px 15px; border-radius: 20px; font-size: 12px; font-weight: bold; }}
        .status.passed {{ background: #f6ffed; color: #52c41a; }}
        .status.failed {{ background: #fff2f0; color: #f5222d; }}
        .status.broken {{ background: #fff7e6; color: #fa8c16; }}
        .timestamp {{ text-align: center; color: #999; margin-top: 20px; }}
    </style>
</head>
<body>
    <div class="container">
        <h1>JoyHub用户模块接口自动化测试报告</h1>
        
        <div class="summary">
            <div class="stat-box total">
                <div class="stat-value">{len(test_results)}</div>
                <div class="stat-label">总用例数</div>
            </div>
            <div class="stat-box passed">
                <div class="stat-value">{len(passed)}</div>
                <div class="stat-label">通过</div>
            </div>
            <div class="stat-box failed">
                <div class="stat-value">{len(failed) + len(broken)}</div>
                <div class="stat-label">失败</div>
            </div>
        </div>
        
        <div class="test-list">
            <h2>测试用例详情</h2>
            {''.join([f'''
            <div class="test-item">
                <div class="test-name">{result['name']}</div>
                <div class="status {result['status']}">{result['status']}</div>
            </div>
            ''' for result in test_results])}
        </div>
        
        <div class="timestamp">测试时间: {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</div>
    </div>
</body>
</html>
"""

# 保存HTML报告
report_path = r'C:\Users\a\PyCharmMiscProject\smart-management-auto-test\dulizhan\test_case\reports\test_report.html'
with open(report_path, 'w', encoding='utf-8') as f:
    f.write(html_content)

print(f"测试报告已生成: {report_path}")