using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mapster; using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; using System.Threading; using AsbCloudInfrastructure.Services.Cache; namespace AsbCloudInfrastructure.Services { public class WellSectionService: IWellSectionService { private readonly IAsbCloudDbContext context; private readonly DbSet dbSet; private readonly CacheTable cachedSectionsTypes; public WellSectionService(IAsbCloudDbContext context, Cache.CacheDb cache) { this.context = context; dbSet = context.Set(); cachedSectionsTypes = cache.GetCachedTable((DbContext)context); } public async Task> GetAllByWellIdAsync(int idWell, int skip, int take, CancellationToken token = default) { var query = dbSet .Include(s => s.WellSectionType) .Where(s => s.IdWell == idWell) .AsNoTracking(); var result = new PaginationContainer { Skip = skip, Take = take, Count = await query.CountAsync(token).ConfigureAwait(false), }; query = query .OrderBy(e => e.WellDepthPlan); if (skip > 0) query = query.Skip(skip); query = query.Take(take); var entities = await query.Take(take).ToListAsync(token).ConfigureAwait(false); foreach (var item in entities) { var dto = item.Adapt(); dto.SectionType = item.WellSectionType.Caption; result.Items.Add(dto); } return result; } public async Task GetAsync(int id, CancellationToken token = default) { var entity = await dbSet .Include(s => s.WellSectionType) .FirstOrDefaultAsync(e => e.Id == id, token) .ConfigureAwait(false); var dto = entity.Adapt(); dto.SectionType = entity.WellSectionType.Caption; return dto; } public async Task InsertAsync(WellSectionDto item, int idWell, CancellationToken token = default) { if(string.IsNullOrEmpty(item.SectionType)) throw new ArgumentException("Тип секции должен быть указан", nameof(WellSectionDto.SectionType)); var sectionType = await cachedSectionsTypes .FirstOrDefaultAsync(s => s.Caption == item.SectionType, token) .ConfigureAwait(false); if (sectionType is null) { throw new ArgumentException("Тип секции отсутствует в справочнике", nameof(WellSectionDto.SectionType)); //sectionType = await cachedSectionsTypes.InsertAsync(new WellSectionType { Caption = item.SectionType}, token); } var entity = item.Adapt(); entity.Id = default; entity.IdWell = idWell; entity.IdWellSectionType = sectionType.Id; var dbEntity = dbSet.Add(entity); await context.SaveChangesAsync(token).ConfigureAwait(false); return dbEntity.Entity.Adapt(); } public Task> InsertRangeAsync(IEnumerable newItems, int idWell, CancellationToken token = default) { throw new NotImplementedException(); } public async Task UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default) { if (string.IsNullOrEmpty(item.SectionType)) throw new ArgumentException("Тип секции должен быть указан", nameof(WellSectionDto.SectionType)); var sectionType = await cachedSectionsTypes .FirstOrDefaultAsync(s => s.Caption == item.SectionType, token) .ConfigureAwait(false); if (sectionType is null) { throw new ArgumentException("Тип секции отсутствует в справочнике", nameof(WellSectionDto.SectionType)); //sectionType = await cachedSectionsTypes.InsertAsync(new WellSectionType { Caption = item.SectionType}, token); } var entity = item.Adapt(); entity.Id = idSection; entity.IdWell = idWell; entity.IdWellSectionType = sectionType.Id; var dbEntity = dbSet.Update(entity); await context.SaveChangesAsync(token).ConfigureAwait(false); return dbEntity.Entity.Adapt(); } public Task DeleteAsync(int id, CancellationToken token = default) { var entity = dbSet.FirstOrDefault(e => e.Id == id); if (entity == default) return Task.FromResult(0); dbSet.Remove(entity); return context.SaveChangesAsync(token); } public Task DeleteAsync(IEnumerable ids, CancellationToken token = default) { throw new NotImplementedException(); } } }