from sqlalchemy.orm import Session
from typing import List

from app.model.bussiness_model import ProjectImgLeafer
from app.model.bussiness_model import ProjectImgLabel


def get_img_leafer(image_id: int, session: Session):
    """
    根据图片id查询图片的leafer信息
    :param image_id:
    :param session:
    :return:
    """
    img_leafer = session.query(ProjectImgLeafer).filter_by(image_id=image_id).first()
    return img_leafer


def get_img_label_list(image_id, session: Session):
    """
    根据图片id获取图片标签信息
    :param image_id:
    :param session:
    :return:
    """
    img_label_list = session.query(ProjectImgLabel).filter_by(image_id=image_id).order_by(ProjectImgLabel.label_id).all()
    return img_label_list


def save_img_leafer(leafer: ProjectImgLeafer, session: Session):
    leafer_saved = session.query(ProjectImgLeafer).filter_by(image_id=leafer.image_id).first()
    if leafer_saved is not None:
        session.delete(leafer_saved)
    session.add(leafer)
    session.commit()
    return leafer


def save_img_label_batch(image_id: int, img_labels: List[ProjectImgLabel], session: Session):
    session.query(ProjectImgLabel).filter_by(image_id=image_id).delete()
    session.add_all(img_labels)
    session.commit()
    return len(img_labels)
