DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/MeasureService.cs
ngfrolov 4db67113b4 CrudService очищен от неиспользуемого кода.
CrudCacheService Адаптировано для новой схемы кеширования.
Убраны extention методы для mapster.
2022-06-06 15:43:47 +05:00

148 lines
5.6 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class MeasureService : IMeasureService
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
private readonly CacheTable<MeasureCategory> cacheCategories;
public MeasureService(IAsbCloudDbContext db, Cache.CacheDb cacheDb, IWellService wellService)
{
this.db = db;
this.wellService = wellService;
cacheCategories = cacheDb.GetCachedTable<MeasureCategory>((DbContext)db);
}
public async Task<Dictionary<int, string>> GetCategoriesAsync(CancellationToken token)
{
var entities = await cacheCategories.WhereAsync(token).ConfigureAwait(false);
var dto = entities.ToDictionary(e => e.Id, e => e.Name);
return dto;
}
public async Task<MeasureDto> GetLastAsync(int idWell, int idCategory, CancellationToken token)
{
var query = db.Measures
.Include(m => m.Category)
.Where(m => m.IdWell == idWell && m.IdCategory == idCategory && !m.IsDeleted)
.OrderByDescending(m => m.Timestamp)
.Take(1);
var entity = await query
.AsNoTracking()
.FirstOrDefaultAsync(token)
.ConfigureAwait(false);
var timezone = wellService.GetTimezone(idWell);
var dto = Convert(entity, timezone.Hours);
return dto;
}
public async Task<IEnumerable<MeasureDto>> GetHisoryAsync(int idWell, int? idCategory = null, CancellationToken token = default)
{
var query = db.Measures.Include(m => m.Category)
.Where(m => m.IdWell == idWell && !m.IsDeleted);
if (idCategory is not null)
query = query.Where(m => m.IdCategory == idCategory);
var entities = await query
.OrderBy(m => m.Timestamp)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
var timezone = wellService.GetTimezone(idWell);
var dtos = entities.Select(e => Convert(e, timezone.Hours));
return dtos;
}
public Task<int> InsertAsync(int idWell, MeasureDto dto, CancellationToken token)
{
if (dto.IdCategory < 1)
throw new ArgumentInvalidException("wrong idCategory", nameof(dto));
if (dto.Data is null)
throw new ArgumentInvalidException("data.data is not optional", nameof(dto));
var timezone = wellService.GetTimezone(idWell);
var entity = Convert(dto, timezone.Hours);
entity.IdWell = idWell;
db.Measures.Add(entity);
return db.SaveChangesAsync(token);
}
public async Task<int> UpdateAsync(int idWell, MeasureDto dto, CancellationToken token)
{
if (dto.Id < 1)
throw new ArgumentInvalidException("wrong id", nameof(dto));
if (dto.IdCategory < 1)
throw new ArgumentInvalidException("wrong idCategory", nameof(dto));
if (dto.Data is null)
throw new ArgumentInvalidException("data.data is not optional", nameof(dto));
var entity = await db.Measures
.Where(m => m.Id == dto.Id && !m.IsDeleted)
.FirstOrDefaultAsync(token)
.ConfigureAwait(false);
if (entity is null)
throw new ArgumentInvalidException("id doesn't exist", nameof(dto));
var timezone = wellService.GetTimezone(idWell);
entity.IdWell = idWell;
entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(timezone.Hours);
entity.Data = (RawData)dto.Data;
return await db.SaveChangesAsync(token).ConfigureAwait(false);
}
public async Task<int> MarkAsDeleteAsync(int idWell, int idData, CancellationToken token)
{
if (idData < 1)
throw new ArgumentInvalidException("wrong id", nameof(idData));
var entity = await db.Measures.Where(m => m.IdWell == idWell && m.Id == idData)
.FirstOrDefaultAsync(token)
.ConfigureAwait(false);
entity.IsDeleted = true;
return await db.SaveChangesAsync(token).ConfigureAwait(false);
}
public Task<int> DeleteAsync(int idWell, int idData, CancellationToken token)
{
if (idData < 1)
throw new ArgumentInvalidException("wrong id", nameof(idData));
db.Measures.RemoveRange(db.Measures.Where(m => m.IdWell == idWell && m.Id == idData));
return db.SaveChangesAsync(token);
}
private MeasureDto Convert(Measure entity, double hours)
{
var dto = entity.Adapt<MeasureDto>();
dto.CategoryName = entity.Category?.Name;
dto.Timestamp = entity.Timestamp.ToRemoteDateTime(hours);
return dto;
}
private Measure Convert(MeasureDto dto, double hours)
{
var entity = dto.Adapt<Measure>();
entity.Category = null;
entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(hours);
return entity;
}
}
}