#!/usr/bin/env python3
"""
检查Service层命名规范
"""

import os
import re
from pathlib import Path

def check_service_naming():
    """检查Service层命名规范"""
    base_path = Path("/Users/zhangxiang/Codes/Novalon/novalon-manage-system/novalon-manage-api/manage-sys/src/main/java")
    
    print("=" * 60)
    print("Service层命名规范检查")
    print("=" * 60)
    
    # 查找所有Service接口
    service_interfaces = []
    for java_file in base_path.rglob("*Service.java"):
        content = java_file.read_text()
        if f"interface I" in content or re.search(r'interface\s+I\w+Service', content):
            service_interfaces.append(java_file)
    
    print(f"\n找到 {len(service_interfaces)} 个Service接口:")
    for interface in sorted(service_interfaces):
        interface_name = interface.stem
        print(f"  ✅ {interface_name}")
    
    # 查找所有Service实现类
    service_impls = []
    for java_file in base_path.rglob("*Service*.java"):
        if "impl" in str(java_file) or "handler" in str(java_file):
            content = java_file.read_text()
            if "class" in content and "Service" in content:
                service_impls.append(java_file)
    
    print(f"\n找到 {len(service_impls)} 个Service实现类:")
    
    issues = []
    for impl in sorted(service_impls):
        impl_name = impl.stem
        content = impl.read_text()
        
        # 检查是否实现了接口
        implements_match = re.search(r'implements\s+(\w+)', content)
        if implements_match:
            interface_name = implements_match.group(1)
            
            # 检查命名规范
            if interface_name.startswith('I'):
                expected_impl_name = interface_name[1:]  # 移除I前缀
                
                # 特殊情况：ExceptionLogServiceImpl是适配器
                if impl_name == "ExceptionLogServiceImpl":
                    print(f"  ✅ {impl_name} (适配器类)")
                elif impl_name == expected_impl_name:
                    print(f"  ✅ {impl_name} implements {interface_name}")
                else:
                    print(f"  ⚠️  {impl_name} implements {interface_name}")
                    print(f"      建议重命名为: {expected_impl_name}")
                    issues.append((impl, impl_name, expected_impl_name))
            else:
                print(f"  ℹ️  {impl_name} implements {interface_name} (非标准接口)")
        else:
            print(f"  ❓ {impl_name} (未找到implements关键字)")
    
    # 检查是否有不符合规范的命名
    print("\n" + "=" * 60)
    if issues:
        print(f"发现 {len(issues)} 个命名不规范的问题:")
        for impl, current_name, expected_name in issues:
            print(f"  - {current_name} -> {expected_name}")
            print(f"    文件: {impl}")
    else:
        print("✅ 所有Service命名都符合规范！")
    
    print("=" * 60)

if __name__ == "__main__":
    check_service_naming()
