using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; using Mapster; namespace AsbCloudInfrastructure.Services { public class WellOperationService : IWellOperationService { private readonly IAsbCloudDbContext context; private readonly CacheTable cachedOperationCategories; public WellOperationService(IAsbCloudDbContext context, Cache.CacheDb cache) { this.context = context; cachedOperationCategories = cache.GetCachedTable((DbContext)context); } public IEnumerable GetCategories() { var operationTypes = cachedOperationCategories.Where().Distinct(); var result = operationTypes.Adapt(); return result; } public async Task> GetAllByWellIdAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default) { var query = context.WellOperations .Include(s => s.WellSectionType) .Include(s => s.OperationCategory) .Where(s => s.IdWell == idWell); var result = new PaginationContainer { Skip = skip, Take = take, Count = await query.CountAsync(token).ConfigureAwait(false), }; query = query .OrderBy(e => e.WellDepth); if (skip > 0) query = query.Skip(skip); var entities = await query.Take(take).AsNoTracking() .ToListAsync(token).ConfigureAwait(false); foreach (var item in entities) { var dto = item.Adapt(); dto.WellSectionTypeName = item.WellSectionType.Caption; dto.CategoryName = item.OperationCategory.Name; result.Items.Add(dto); } return result; } public async Task GetAsync(int id, CancellationToken token = default) { var entity = await context.WellOperations .Include(s => s.WellSectionType) .Include(s => s.OperationCategory) .FirstOrDefaultAsync(e => e.Id == id, token) .ConfigureAwait(false); if (entity is null) return null; var dto = entity.Adapt(); dto.WellSectionTypeName = entity.WellSectionType.Caption; dto.CategoryName = entity.OperationCategory.Name; return dto; } public async Task InsertAsync(WellOperationDto wellOperationDto, int idWell, CancellationToken token = default) { var entity = wellOperationDto.Adapt(); context.WellOperations.Add(entity); return await context.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task InsertRangeAsync(int idWell, IEnumerable wellOperationDtos, CancellationToken token = default) { foreach(var operationDto in wellOperationDtos) { var entity = operationDto.Adapt(); context.WellOperations.Add(entity); } return await context.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task UpdateAsync(int idWell, int idSection, WellOperationDto item, CancellationToken token = default) { var entity = item.Adapt(); context.WellOperations.Update(entity); return await context.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task DeleteAsync(IEnumerable ids, CancellationToken token = default) { var query = context.WellOperations.Where(e => ids.Contains(e.Id)); context.WellOperations.RemoveRange(query); return await context.SaveChangesAsync(token) .ConfigureAwait(false); } } }