from sqlalchemy.orm import Session
from app.model.user_model import User

class UserDAO:
    def __init__(self, db_session: Session):
        self.db_session = db_session

    def get_all(self):
        """获取所有用户"""
        return self.db_session.query(User).all()

    def get_by_id(self, user_id: int):
        """根据ID获取用户"""
        return self.db_session.query(User).get(user_id)

    def get_by_username(self, username: str):
        """根据用户名获取用户"""
        return self.db_session.query(User).filter_by(username=username).first()

    def get_by_email(self, email: str):
        """根据邮箱获取用户"""
        return self.db_session.query(User).filter_by(email=email).first()

    def create(self, user: User):
        """创建新用户"""
        self.db_session.add(user)
        self.db_session.commit()
        return user

    def update(self, user: User):
        """更新用户信息"""
        self.db_session.commit()
        return user

    def delete(self, user: User):
        """删除用户"""
        self.db_session.delete(user)
        self.db_session.commit()

    def exists_by_username(self, username: str):
        """检查用户名是否已存在"""
        return self.db_session.query(User).filter_by(username=username).first() is not None

    def exists_by_email(self, email: str):
        """检查邮箱是否已存在"""
        return self.db_session.query(User).filter_by(email=email).first() is not None
