using Dm;

namespace SafeCampus.Application.Services.Business.ClassRoomCallService;

public class ClassRoomCallService : DbRepository<ClassRoomCall>, IClassRoomCallService
{
    public async Task<bool> Add(ClassRoomCallDto input)
    {
        var model = input.Adapt<ClassRoomCall>();
        if (string.IsNullOrEmpty(model.PersonId))
        {
            if (await IsAnyAsync(x => x.TaskId == model.TaskId && x.PersonId == model.PersonId && x.PersonSetId == model.PersonSetId))
            {
                //该批次点名任务已经出现过这个人，不在重复添加
                return true;
            }
        }
        else
        {
            if (await IsAnyAsync(x=>x.TaskId==model.TaskId&&x.TrackId==model.TrackId))
            {
                //没有匹配到人员，但是所属同一跟踪id
                //该批次点名任务已经出现过这个人，不在重复添加
                return true;
            }
        }
        await InsertAsync(model);
        return true;
    }

    public async Task<bool> Update(ClassRoomCallUpdate input)
    {
        var model = await GetFirstAsync(p => p.Id == input.Id);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        //var res = input.Adapt(model);
        model.PersonSetId = input.PersonSetId;
        model.PersonId = input.PersonId;
        await Context.Updateable(model)
            .UpdateColumns(x => new
            {
                x.PersonSetId,
                x.PersonId
            })
            .ExecuteCommandAsync();
        //await UpdateAsync(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);
        return true;
    }

    public async Task<SqlSugarPagedList<ClassRoomCallList>> GetPageList(ClassRoomCallSearch search)
    {
        var query = Context.Queryable<ClassRoomCall>()
            .Includes(x => x.CameraInfoItem)
            .WhereIF(!string.IsNullOrEmpty(search.AlarmType), x => x.AlarmType == search.AlarmType)
            .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonSetId), x => x.PersonSetId == search.PersonSetId)
            .WhereIF(!string.IsNullOrEmpty(search.TaskId), x => x.TaskId == search.TaskId)
            .WhereIF(!string.IsNullOrEmpty(search.TrackId), x => x.TrackId == search.TrackId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonId), x => x.PersonId == search.PersonId)
            .WhereIF(search.StartTick.HasValue, x => x.Tick >= search.StartTick)
            .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick);

        var list = await query.OrderByDescending(x => x.Tick)
            .ToPagedListAsyncMapster<ClassRoomCall, ClassRoomCallList>(search.PageNum, search.PageSize);
        return list;
    }

    public async Task<List<ClassRoomCallList>> GetNoPageList(ClassRoomCallSearch search)
    {
        var query = Context.Queryable<ClassRoomCall>()
            .Includes(x => x.CameraInfoItem)
            .WhereIF(!string.IsNullOrEmpty(search.AlarmType), x => x.AlarmType == search.AlarmType)
            .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonSetId), x => x.PersonSetId == search.PersonSetId)
            .WhereIF(!string.IsNullOrEmpty(search.TaskId), x => x.TaskId == search.TaskId)
            .WhereIF(!string.IsNullOrEmpty(search.TrackId), x => x.TrackId == search.TrackId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonId), x => x.PersonId == search.PersonId)
            .WhereIF(search.StartTick.HasValue, x => x.Tick >= search.StartTick)
            .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick);

        var list = await query.OrderByDescending(x => x.Tick)
            .ToListAsync();
        return list.Adapt<List<ClassRoomCallList>>();
    }
}