namespace SafeCampus.Application.Services.Business.BuildingService;

public class BuildingService:DbRepository<BuildingInfo>,IBuildingService
{
    public async Task<bool> Add(BuildingInfoDto input)
    {
        var model = input.Adapt<BuildingInfo>();
        await InsertAsync(model);
        return true;
    }

    public async Task<bool> Update(BuildingInfoDto 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<BuildingInfoDto> GetInfo(long id)
    {
            var model = await Context.Queryable<BuildingInfo>()
                .FirstAsync(x => x.Id == id);
            if (model == null)
            {
                return null;
            }
            return model.Adapt<BuildingInfoDto>();
    }

    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<List<BuildingInfoDto>> GetNoPageList()
    {
        var list = await Context.Queryable<BuildingInfo>()
            .Includes(x => x.InsCameraInfoItem)
            .Includes(x => x.OutCameraInfoItem)
            .ToListAsync();
        //var list = await GetListAsync();
        return list.Adapt<List<BuildingInfoDto>>();
    }
}