Переписан метод GetTimezone (метод больше не маппит в dto)

This commit is contained in:
Olga Nemt 2024-03-26 15:23:24 +05:00
parent e2cd8cbfd2
commit 626c3cb238

View File

@ -116,12 +116,12 @@ namespace AsbCloudInfrastructure.Services
if (well is null)
return null;
var wellInfo = wellInfoService.FirstOrDefault(well => well.Id == idWell);
if (wellInfo is null)
return well.Adapt<WellMapInfoWithTelemetryStat>();
wellInfo.IdState = well.IdState;
return wellInfo;
}
@ -153,7 +153,7 @@ namespace AsbCloudInfrastructure.Services
{
if (IsTelemetryAssignedToDifferentWell(dto))
throw new ArgumentInvalidException(nameof(dto), "Телеметрия уже была привязана к другой скважине.");
if (dto.Id != 0 && (await GetCacheAsync(token)).Any(w => w.Id == dto.Id))
throw new ArgumentInvalidException(nameof(dto), $"Нельзя повторно добавить скважину с id: {dto.Id}");
@ -177,12 +177,12 @@ namespace AsbCloudInfrastructure.Services
throw new NotImplementedException();
}
public override async Task<int> UpdateAsync(WellDto dto,
public override async Task<int> UpdateAsync(WellDto dto,
CancellationToken token)
{
if (IsTelemetryAssignedToDifferentWell(dto))
throw new ArgumentInvalidException(nameof(dto), "Телеметрия уже была привязана к другой скважине.");
var oldRelations = (await GetCacheRelationCompanyWellAsync(token))
.Where(r => r.IdWell == dto.Id).ToArray();
@ -192,16 +192,16 @@ namespace AsbCloudInfrastructure.Services
dbContext.RelationCompaniesWells
.RemoveRange(dbContext.RelationCompaniesWells
.Where(r => r.IdWell == dto.Id));
DropCacheRelationCompanyWell();
var newRelations = dto.Companies
.Select(c => new RelationCompanyWell
{
IdWell = dto.Id,
IdWell = dto.Id,
IdCompany = c.Id
});
dbContext.RelationCompaniesWells.AddRange(newRelations);
}
@ -215,7 +215,7 @@ namespace AsbCloudInfrastructure.Services
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{
var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
return entity!.Caption;
}
@ -270,10 +270,7 @@ namespace AsbCloudInfrastructure.Services
{
var dto = base.Convert(entity);
if (entity.Timezone is null)
dto.Timezone = GetTimezone(entity.Id);
dto.StartDate = wellOperationRepository.FirstOperationDate(entity.Id)?.ToOffset(TimeSpan.FromHours(dto.Timezone.Hours));
dto.StartDate = wellOperationRepository.FirstOperationDate(entity.Id);
dto.WellType = entity.WellType.Caption;
dto.Cluster = entity.Cluster.Caption;
dto.Deposit = entity.Cluster.Deposit.Caption;
@ -296,47 +293,10 @@ namespace AsbCloudInfrastructure.Services
public SimpleTimezoneDto GetTimezone(int idWell)
{
var well = GetOrDefault(idWell)
var cache = GetCache();
var cacheItem = cache.FirstOrDefault(d => d.Id == idWell)
?? throw new ArgumentInvalidException(nameof(idWell), $"idWell: {idWell} does not exist.");
return GetTimezone(well);
}
private SimpleTimezoneDto GetTimezone(WellDto wellDto)
{
if (wellDto.Timezone is not null)
return wellDto.Timezone;
if (wellDto.Telemetry is not null)
{
var timezone = telemetryService.GetTimezone(wellDto.Telemetry.Id);
if (timezone is not null)
return timezone;
}
var well = GetQuery().FirstOrDefault(w => w.Id == wellDto.Id);
if (well is not null)
{
var point = GetCoordinates(well);
if (point is not null)
{
if (point.Timezone is not null)
{
return point.Timezone.Adapt<SimpleTimezoneDto>();
}
if (point.Latitude is not null & point.Longitude is not null)
{
var timezone = timezoneService.GetOrDefaultByCoordinates(point.Latitude!.Value, point.Longitude!.Value);
if (timezone is not null)
{
return timezone;
}
}
}
}
throw new Exception($"Can't find timezone for well {wellDto.Caption} id: {wellDto.Id}");
return cacheItem.Timezone.Adapt<SimpleTimezoneDto>();
}
private bool IsTelemetryAssignedToDifferentWell(WellDto wellDto)
@ -349,7 +309,7 @@ namespace AsbCloudInfrastructure.Services
if (existingWellWithAssignedTelemetry is null)
return false;
return existingWellWithAssignedTelemetry.Id != wellDto.Id;
}