namespace SafeCampus.Application.Services.Business.CameraGroupService;

public class CameraGroupService :DbRepository<CameraGroup>, ICameraGroupService
{
    public async Task<bool> Add(CameraGroupAddInput input)
    {
        var model = input.Adapt<CameraGroup>();
        await InsertAsync(model);
        return true;
    }

    public async Task<bool> Update(CameraGroupInput 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 cameraInfo = ChangeRepository<DbRepository<CameraInfo>>();//切换仓储
        var model = await GetFirstAsync(x => x.Id == id);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }

        var isOK = await cameraInfo.IsAnyAsync(x => x.GroupId == model.Id);
        if (isOK)
        {
            throw Oops.Oh("分组下存在摄像头，请转移至其他分组后在删除！");
        }
        await DeleteAsync(model);
        return true;
    }

    public async Task<List<CameraGroup>> GetNoPageList()
    {
        var list = await Context.Queryable<CameraGroup>()
            .ToTreeAsync(x => x.Children, x => x.ParentId, null);
        //var list = await GetListAsync();
        return list;
    }

    public async Task<List<CameraGroup>> GetBIList()
    {
        var list = await Context.Queryable<CameraGroup>().Includes(x => x.CameraInfos).ToListAsync();
        return list;
    }
}