DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellSectionService.cs

160 lines
6.1 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
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<WellSection> dbSet;
private readonly CacheTable<WellSectionType> cachedSectionsTypes;
public WellSectionService(IAsbCloudDbContext context, Cache.CacheDb cache)
{
this.context = context;
dbSet = context.Set<WellSection>();
cachedSectionsTypes = cache.GetCachedTable<WellSectionType>((DbContext)context);
}
public async Task<PaginationContainer<WellSectionDto>> 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<WellSectionDto>
{
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<WellSectionDto>();
dto.SectionType = item.WellSectionType.Caption;
result.Items.Add(dto);
}
return result;
}
public async Task<WellSectionDto> GetAsync(int id, CancellationToken token = default)
{
var entity = await dbSet
.Include(s => s.WellSectionType)
.FirstOrDefaultAsync(e => e.Id == id, token)
.ConfigureAwait(false);
if (entity is null)
return null;
var dto = entity.Adapt<WellSectionDto>();
dto.SectionType = entity.WellSectionType.Caption;
return dto;
}
public async Task<WellSectionDto> InsertAsync(WellSectionDto item, int idWell, CancellationToken token = default)
{
var sectionType = await GetWellSectionTypeFromCacheAndAssert(item.SectionType);
var entity = item.Adapt<WellSection>();
entity.Id = default;
entity.IdWell = idWell;
entity.IdWellSectionType = sectionType.Id;
var dbEntity = dbSet.Add(entity);
await context.SaveChangesAsync(token).ConfigureAwait(false);
var dto = dbEntity.Entity.Adapt<WellSectionDto>();
dto.SectionType = sectionType.Caption;
return dto;
}
public async Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> items, int idWell, CancellationToken token = default)
{
var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<WellSection>[items.Count()];
for (int i = 0; i < dbEntities.Length; i++)
{
var sectionType = await GetWellSectionTypeFromCacheAndAssert(items.ElementAt(i).SectionType);
var item = items.ElementAt(i).Adapt<WellSection>();
item.IdWell = idWell;
item.IdWellSectionType = sectionType.Id;
dbEntities[i] = dbSet.Add(item);
}
await context.SaveChangesAsync(token).ConfigureAwait(false);
var dtos = dbEntities.Select((e) => {
var dto = e.Entity.Adapt<WellSectionDto>();
var sectionType = cachedSectionsTypes.FirstOrDefault(s => s.Id == e.Entity.IdWellSectionType);
dto.SectionType = sectionType.Caption;
return dto;
});
return dtos;
}
public async Task<WellSectionDto> UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default)
{
var sectionType = await GetWellSectionTypeFromCacheAndAssert(item.SectionType);
var entity = item.Adapt<WellSection>();
entity.Id = idSection;
entity.IdWell = idWell;
entity.IdWellSectionType = sectionType.Id;
var dbEntity = dbSet.Update(entity);
await context.SaveChangesAsync(token).ConfigureAwait(false);
var dto = dbEntity.Entity.Adapt<WellSectionDto>();
dto.SectionType = sectionType.Caption;
return dto;
}
public Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
{
var entities = dbSet.Where(e => ids.Contains(e.Id));
dbSet.RemoveRange(entities);
return context.SaveChangesAsync(token);
}
private async Task<WellSectionType> GetWellSectionTypeFromCacheAndAssert(string wellSectionType, CancellationToken token = default)
{
if (string.IsNullOrEmpty(wellSectionType))
throw new ArgumentException("Тип секции должен быть указан", nameof(WellSectionDto.SectionType));
var sectionType = await cachedSectionsTypes
.FirstOrDefaultAsync(s => s.Caption.Equals(wellSectionType, StringComparison.OrdinalIgnoreCase), token)
.ConfigureAwait(false);
if (sectionType is null)
{
throw new ArgumentException($"Тип секции '{wellSectionType}' отсутствует в справочнике", nameof(WellSectionDto.SectionType)) ;
//sectionType = await cachedSectionsTypes.InsertAsync(new WellSectionType { Caption = item.SectionType}, token);
}
return sectionType;
}
}
}