2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-11-17 16:09:51 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
using System;
|
2022-12-01 15:56:11 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Linq;
|
2024-04-18 15:53:05 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-06-16 12:33:05 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public class SetpointsRequestRepository : CrudWellRelatedCacheRepositoryBase<SetpointsRequestDto, SetpointsRequest>
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellService wellService;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService)
|
|
|
|
|
: base(dbContext, memoryCache, q => q.Include(s => s.Author)
|
|
|
|
|
.Include(s => s.Well))
|
|
|
|
|
{
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
2022-12-01 15:56:11 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
//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), "Все записи должны существовать в БД");
|
2022-12-01 15:56:11 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var entities = dtos.Select(Convert);
|
|
|
|
|
var entries = entities.Select(entity => dbContext.Set<SetpointsRequest>().Update(entity)).ToList();
|
2024-04-18 15:53:05 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var affected = await dbContext.SaveChangesAsync(token);
|
|
|
|
|
|
|
|
|
|
entries.ForEach(entry => entry.State = EntityState.Detached);
|
2022-12-01 15:56:11 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if(affected > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
|
|
|
|
|
return affected;
|
|
|
|
|
}
|
2022-06-16 12:33:05 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
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;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
protected override SetpointsRequest Convert(SetpointsRequestDto src)
|
|
|
|
|
{
|
|
|
|
|
var result = base.Convert(src);
|
|
|
|
|
result.UploadDate = src.UploadDate.ToUniversalTime();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-06-16 12:33:05 +05:00
|
|
|
|
}
|