#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修复Entity中getter/setter的Date转换问题
"""

import os
import re
from pathlib import Path

def fix_entity_getters(file_path: Path) -> bool:
    """修复Entity类中getter/setter的错误Date转换"""
    try:
        content = file_path.read_text(encoding='utf-8')
        original = content

        # 修复 getXxx() 方法: return xxx != null ? new Date(xxx.getTime()) : null;
        # 改为: return xxx;
        content = re.sub(
            r'return (\w+) != null \? new Date\(\1\.getTime\(\)\) : null;',
            r'return \1;',
            content
        )

        # 修复 setXxx(Date) 方法: this.xxx = xxx != null ? new Date(xxx.getTime()) : null;
        # 改为: this.xxx = xxx;
        content = re.sub(
            r'this\.(\w+) = \1 != null \? new Date\(\1\.getTime\(\)\) : null;',
            r'this.\1 = \1;',
            content
        )

        if content != original:
            file_path.write_text(content, encoding='utf-8')
            return True
        return False
    except Exception as e:
        print(f"  错误: {e}")
        return False

def main():
    base_path = Path('services')
    fixed = 0

    # 只处理merchant-service的Entity
    entity_files = list(Path('services/merchant-service/src/main/java/com/fzx/entity').glob('*.java'))

    print(f"修复 Entity getter/setter 中的Date问题...")
    for file_path in entity_files:
        if fix_entity_getters(file_path):
            print(f"  ✅ {file_path.name}")
            fixed += 1

    print(f"\n修复了 {fixed} 个文件")

if __name__ == '__main__':
    main()
