DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/SetpointsRequestRepository.cs
2024-08-19 10:01:07 +05:00

79 lines
2.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using AsbCloudApp.Exceptions;
namespace AsbCloudInfrastructure.Repository;
public class SetpointsRequestRepository : CrudWellRelatedCacheRepositoryBase<SetpointsRequestDto, SetpointsRequest>
{
private readonly IWellService wellService;
public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService)
: base(dbContext, memoryCache, q => q.Include(s => s.Author)
.Include(s => s.Well))
{
this.wellService = wellService;
}
//TODO: заметка для рефакторинга. Использовать метод из базового репозитория
public virtual async Task<int> UpdateRangeAsync(IEnumerable<SetpointsRequestDto> dtos, CancellationToken token)
{
if (!dtos.Any())
return 0;
var ids = dtos
.Select(o => o.Id)
.Distinct()
.ToArray();
if (ids.Any(id => id == default))
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны иметь Id");
if (ids.Length != dtos.Count())
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны иметь уникальные Id");
var existingEntitiesCount = await dbContext.Set<SetpointsRequest>()
.Where(o => ids.Contains(o.Id))
.CountAsync(token);
if (ids.Length != existingEntitiesCount)
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны существовать в БД");
var entities = dtos.Select(Convert);
var entries = entities.Select(entity => dbContext.Set<SetpointsRequest>().Update(entity)).ToList();
var affected = await dbContext.SaveChangesAsync(token);
entries.ForEach(entry => entry.State = EntityState.Detached);
if(affected > 0)
DropCache();
return affected;
}
protected override SetpointsRequestDto Convert(SetpointsRequest src)
{
var result = base.Convert(src);
var timezoneOffsetHours = wellService.GetTimezone(src.IdWell).Hours;
result.UploadDate = src.UploadDate.ToOffset(TimeSpan.FromHours(timezoneOffsetHours));
return result;
}
protected override SetpointsRequest Convert(SetpointsRequestDto src)
{
var result = base.Convert(src);
result.UploadDate = src.UploadDate.ToUniversalTime();
return result;
}
}