namespace SafeCampus.Application.Services.Business.ClassTeacherService;

public class ClassTeacherService:DbRepository<ClassTeacher>, IClassTeacherService
{
    public async Task<bool> Add(ClassTeacherInput input)
    {
        var model = input.Adapt<ClassTeacher>();
        await InsertAsync(model);
        return true;
    }

    public async Task<bool> Update(ClassTeacherUpdateInput input)
    {
        if (!input.ID.HasValue)
        {
            throw Oops.Oh("请填写ID");
        }
        var model = await GetFirstAsync(p => p.Id == input.ID);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        var res = input.Adapt(model);
        await UpdateAsync(res);
        return true;
    }

    public async Task<bool> Delete(long id)
    {
        var model = await GetFirstAsync(x => x.Id == id);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        await DeleteAsync(model);
        return true;
    }

    public async Task<ClassTeacherDto> GetInfo(string personSetId)
    {
        var model = await Context.Queryable<ClassTeacher>()
            .Includes(x => x.SysUserItem)
            .Where(x => x.PersonSetId == personSetId).FirstAsync();
        if (model == null)
        {
            return null;
            //throw Oops.Oh("信息不存在！");
        }
        return model.Adapt<ClassTeacherDto>();
    }

    public async Task<List<ClassTeacherDto>> GetNoPageList()
    {
        var list = await Context.Queryable<ClassTeacher>()
            .Includes(x => x.SysUserItem)
            .ToListAsync();
        return list.Adapt<List<ClassTeacherDto>>();
    }
}