namespace SafeCampus.Application.Services.Business.ClassRoomCallTaskService;

public class ClassRoomCallTaskService : DbRepository<ClassRoomCallTask> ,IClassRoomCallTaskService
{
    public async Task<bool> Add(ClassRoomCallTaskDto input)
    {
        var model = input.Adapt<ClassRoomCallTask>();
        await InsertAsync(model);
        return true;
    }

    public async Task<bool> Remove(long id)
    {
        var model = await GetFirstAsync(x => x.Id == id);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        await DeleteAsync(model);
        var classRoomCall = ChangeRepository<DbRepository<ClassRoomCall>>();//切换仓储
        await classRoomCall.DeleteAsync(x => x.TaskId == model.TaskId);
        return true;
    }

    public async Task<SqlSugarPagedList<ClassRoomCallTaskDto>> GetPageList(ClassRoomCallTaskSearch search)
    {
        var query = Context.Queryable<ClassRoomCallTask>()
            .Includes(x => x.CameraInfoItem)
            .WhereIF(!string.IsNullOrEmpty(search.PersonSetId), x => x.PersonSetId == search.PersonSetId)
            .WhereIF(!string.IsNullOrEmpty(search.TaskId), x => x.TaskId == search.TaskId)
            .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
            .WhereIF(search.StartTime.HasValue, x => x.CreateTime >= search.StartTime)
            .WhereIF(search.EndTime.HasValue, x => x.EndTime <= search.EndTime);

        var list = await query.OrderByDescending(x => x.CreateTime)
            .ToPagedListAsyncMapster<ClassRoomCallTask, ClassRoomCallTaskDto>(search.PageNum, search.PageSize);
        return list;
    }
}