from sqlalchemy.orm import Session
from sqlalchemy import asc

from app.model.bussiness_model import ProjectTrain
from app.model.schemas.project_train_schemas import ProjectTrainOut


def add_train(train: ProjectTrain, session: Session):
    """
    新增训练结果
    :param train:
    :param session:
    :return:
    """
    session.add(train)
    session.commit()


def get_train_list(project_id: int, session: Session):
    """
    根据项目id查询训练列表
    :param project_id:
    :param session:
    :return:
    """
    query = session.query(ProjectTrain).filter_by(project_id=project_id).order_by(asc(ProjectTrain.id))
    train_list = [ProjectTrainOut.from_orm(train) for train in query.all()]
    return train_list


def get_train(train_id: int, session: Session):
    """
    根据id查询训练信息
    :param train_id:
    :param session:
    :return:
    """
    train = session.query(ProjectTrain).filter_by(id=train_id).first()
    return train
