forked from ddrilling/AsbCloudServer
164 lines
6.4 KiB
C#
164 lines
6.4 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 Task<string[]> GetTypesAsync(CancellationToken token) =>
|
|
context.WellSectionTypes.Select(e => e.Caption).Distinct().AsNoTracking().ToArrayAsync(token);
|
|
|
|
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 GetWellSectionTypeFromCacheAndAssertAsync(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(int idWell, IEnumerable<WellSectionDto> items, CancellationToken token = default)
|
|
{
|
|
var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<WellSection>[items.Count()];
|
|
|
|
for (int i = 0; i < dbEntities.Length; i++)
|
|
{
|
|
var sectionType = await GetWellSectionTypeFromCacheAndAssertAsync(items.ElementAt(i).SectionType, token);
|
|
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(int idWell, int idSection, WellSectionDto item, CancellationToken token = default)
|
|
{
|
|
var sectionType = await GetWellSectionTypeFromCacheAndAssertAsync(item.SectionType, token)
|
|
.ConfigureAwait(false);
|
|
|
|
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> GetWellSectionTypeFromCacheAndAssertAsync(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;
|
|
}
|
|
}
|
|
}
|