namespace SafeCampus.Application.Services.Business.PersonSetInfoService;

public class PersonSetInfoService:DbRepository<PersonSetInfo>, IPersonSetInfoService
{
    public async Task<bool> Add(PersonSetInfoDto input)
    {
        var model = input.Adapt<PersonSetInfo>();
        var oldModel = await GetFirstAsync(x => x.PersonSetId == input.PersonSetId);
        if (oldModel != null)
        {
            throw Oops.Oh("该班级id已存在");
        }
        await InsertAsync(model);
        return true;
    }

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

    public async Task<bool> Update(PersonSetInfoDto input)
    {
        
        var model = await GetFirstAsync(p => p.PersonSetId == input.PersonSetId);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        var res = input.Adapt(model);
        await UpdateAsync(res);
        return true;
    }

    public async Task<List<PersonSetInfoDto>> GetPageList(long? majorId, string setName)
    {
        var list =await Context.Queryable<PersonSetInfo>()
            .Includes(x=>x.MajorInfoItem,x=>x.DepartmentInfoItem)
            .Includes(x => x.ClassTeacherItem, st => st.SysUserItem)
            .Where(x=>x.PersonSetId!=SafeCampusConst.ZDRY)
            .WhereIF(majorId.HasValue,x=>x.MajorId==majorId)
            .WhereIF(!string.IsNullOrEmpty(setName),x=>x.PersonSetName.Contains(setName))
            .ToListAsync();
        return list.Adapt<List<PersonSetInfoDto>>();
    }
}