2021-08-24 10:59:10 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
|
using Mapster;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-08-18 16:57:20 +05:00
|
|
|
|
using System;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2021-10-09 20:16:22 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.WellOperationService
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
|
|
|
|
public class WellOperationService : IWellOperationService
|
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-08-17 09:20:31 +05:00
|
|
|
|
private readonly CacheTable<WellOperationCategory> cachedOperationCategories;
|
2021-12-22 17:09:26 +05:00
|
|
|
|
private readonly CacheTable<WellSectionType> cachedSectionTypes;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
public WellOperationService(IAsbCloudDbContext db, CacheDb cache)
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
this.db = db;
|
|
|
|
|
cachedOperationCategories = cache.GetCachedTable<WellOperationCategory>((DbContext)db);
|
|
|
|
|
cachedSectionTypes = cache.GetCachedTable<WellSectionType>((DbContext)db);
|
2021-08-16 14:19:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
public IDictionary<int, string> GetSectionTypes()
|
|
|
|
|
=> cachedSectionTypes.ToDictionary(s => s.Id, s => s.Caption);
|
|
|
|
|
|
2021-08-17 09:20:31 +05:00
|
|
|
|
public IEnumerable<WellOperationCategoryDto> GetCategories()
|
2021-08-16 14:19:43 +05:00
|
|
|
|
{
|
2021-10-08 11:30:06 +05:00
|
|
|
|
var operationTypes = cachedOperationCategories
|
|
|
|
|
//.Where(oc => oc.Code > 999)
|
2021-09-23 17:34:27 +05:00
|
|
|
|
.Distinct().OrderBy(o => o.Name);
|
2021-08-17 09:20:31 +05:00
|
|
|
|
var result = operationTypes.Adapt<WellOperationCategoryDto>();
|
2021-08-16 14:19:43 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 16:57:20 +05:00
|
|
|
|
public async Task<PaginationContainer<WellOperationDto>> GetOperationsAsync(
|
|
|
|
|
int idWell,
|
2021-12-07 18:27:52 +05:00
|
|
|
|
int? operationType = default,
|
2021-08-18 16:57:20 +05:00
|
|
|
|
IEnumerable<int> sectionTypeIds = default,
|
|
|
|
|
IEnumerable<int> operationCategoryIds = default,
|
|
|
|
|
DateTime begin = default,
|
|
|
|
|
DateTime end = default,
|
2021-08-23 16:59:26 +05:00
|
|
|
|
double minDepth = double.MinValue,
|
|
|
|
|
double maxDepth = double.MaxValue,
|
2021-08-18 16:57:20 +05:00
|
|
|
|
int skip = 0,
|
|
|
|
|
int take = 32,
|
|
|
|
|
CancellationToken token = default)
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
var query = db.WellOperations
|
2021-08-16 10:21:46 +05:00
|
|
|
|
.Include(s => s.WellSectionType)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.Include(s => s.OperationCategory)
|
2021-08-17 09:20:31 +05:00
|
|
|
|
.Where(s => s.IdWell == idWell);
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
if (operationType != default)
|
|
|
|
|
query = query.Where(e => e.IdType == (int)operationType);
|
2021-08-18 16:57:20 +05:00
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
if (sectionTypeIds != default && sectionTypeIds.Any())
|
2021-08-18 16:57:20 +05:00
|
|
|
|
query = query.Where(e => sectionTypeIds.Contains(e.IdWellSectionType));
|
|
|
|
|
|
|
|
|
|
if (operationCategoryIds != default && operationCategoryIds.Any())
|
|
|
|
|
query = query.Where(e => operationCategoryIds.Contains(e.IdCategory));
|
|
|
|
|
|
2021-08-23 16:59:26 +05:00
|
|
|
|
if (minDepth != double.MinValue)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
query = query.Where(e => e.DepthEnd >= minDepth);
|
2021-08-23 16:59:26 +05:00
|
|
|
|
|
|
|
|
|
if (maxDepth != double.MaxValue)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
query = query.Where(e => e.DepthEnd <= maxDepth);
|
2021-08-23 16:59:26 +05:00
|
|
|
|
|
2021-08-18 16:57:20 +05:00
|
|
|
|
if (begin != default)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
query = query.Where(e => e.DateStart >= begin);
|
2021-08-18 16:57:20 +05:00
|
|
|
|
|
|
|
|
|
if (end != default)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
query = query.Where(e => e.DateStart <= end);
|
2021-08-18 16:57:20 +05:00
|
|
|
|
|
2021-08-13 17:25:06 +05:00
|
|
|
|
var result = new PaginationContainer<WellOperationDto>
|
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
|
|
|
|
Take = take,
|
|
|
|
|
Count = await query.CountAsync(token).ConfigureAwait(false),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
query = query
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(e => e.DateStart)
|
|
|
|
|
.ThenBy(e => e.DepthEnd)
|
2021-08-18 16:57:20 +05:00
|
|
|
|
.ThenBy(e => e.Id);
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
|
|
|
|
query = query.Skip(skip);
|
|
|
|
|
|
2021-08-17 09:20:31 +05:00
|
|
|
|
var entities = await query.Take(take).AsNoTracking()
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ToListAsync(token).ConfigureAwait(false);
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
|
|
|
|
foreach (var item in entities)
|
|
|
|
|
{
|
|
|
|
|
var dto = item.Adapt<WellOperationDto>();
|
2021-08-16 14:19:43 +05:00
|
|
|
|
dto.WellSectionTypeName = item.WellSectionType.Caption;
|
|
|
|
|
dto.CategoryName = item.OperationCategory.Name;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
result.Items.Add(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-08-16 14:19:43 +05:00
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<WellOperationDto> GetAsync(int id,
|
2021-08-16 14:19:43 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
var entity = await db.WellOperations
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.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<WellOperationDto>();
|
|
|
|
|
dto.WellSectionTypeName = entity.WellSectionType.Caption;
|
|
|
|
|
dto.CategoryName = entity.OperationCategory.Name;
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<int> InsertRangeAsync(int idWell,
|
|
|
|
|
IEnumerable<WellOperationDto> wellOperationDtos,
|
2021-08-16 14:19:43 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-08-24 10:59:10 +05:00
|
|
|
|
foreach (var operationDto in wellOperationDtos)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
{
|
|
|
|
|
var entity = operationDto.Adapt<WellOperation>();
|
2021-09-30 18:02:50 +05:00
|
|
|
|
entity.Id = default;
|
2021-08-18 16:57:20 +05:00
|
|
|
|
entity.IdWell = idWell;
|
2021-12-22 17:09:26 +05:00
|
|
|
|
db.WellOperations.Add(entity);
|
2021-08-16 14:19:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<int> UpdateAsync(int idWell, int idOperation,
|
2021-08-16 14:19:43 +05:00
|
|
|
|
WellOperationDto item, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var entity = item.Adapt<WellOperation>();
|
2021-08-18 16:57:20 +05:00
|
|
|
|
entity.Id = idOperation;
|
|
|
|
|
entity.IdWell = idWell;
|
2021-12-22 17:09:26 +05:00
|
|
|
|
db.WellOperations.Update(entity);
|
|
|
|
|
return await db.SaveChangesAsync(token)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteAsync(IEnumerable<int> ids,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
var query = db.WellOperations.Where(e => ids.Contains(e.Id));
|
|
|
|
|
db.WellOperations.RemoveRange(query);
|
|
|
|
|
return await db.SaveChangesAsync(token)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2021-08-13 17:25:06 +05:00
|
|
|
|
}
|
|
|
|
|
}
|