Add TryGetTimezone methods.

EnshureTimezonesIsSet() sets default timezone if can't find correct one.
Edit WellDto to fix exception by constr: FK_t_well_t_well_type_id_well_type. Make IdWellType optional.
This commit is contained in:
Фролов 2022-01-10 17:39:33 +05:00
parent 0687efa5af
commit 90a0aa9e5b
2 changed files with 45 additions and 4 deletions

View File

@ -10,7 +10,7 @@ namespace AsbCloudApp.Data
public double? Longitude { get; set; }
public SimpleTimezoneDto Timezone { get; set; }
public string WellType { get; set; }
public int IdWellType { get; set; }
public int? IdWellType { get; set; }
public int? IdCluster { get; set; }
/// <summary>

View File

@ -184,7 +184,8 @@ namespace AsbCloudInfrastructure.Services
entity.IdTelemetry = entity.IdTelemetry ?? dto.IdTelemetry ?? dto.Telemetry?.Id;
if (dto.Timezone is null)
entity.Timezone = GetTimezone(dto.Id).Adapt<SimpleTimezone>();
if (TryGetTimezone(dto.Id, out var timezoneDto))
entity.Timezone = timezoneDto.Adapt<SimpleTimezone>();
return entity;
}
@ -197,7 +198,8 @@ namespace AsbCloudInfrastructure.Services
var dto = base.Convert(entity);
if (entity.Timezone is null)
dto.Timezone = GetTimezone(entity);
if(TryGetTimezone(entity, out var timezone))
dto.Timezone = timezone;
dto.WellType = entity.WellType?.Caption;
dto.Cluster = entity.Cluster?.Caption;
@ -219,7 +221,18 @@ namespace AsbCloudInfrastructure.Services
{
var wells = Cache.Where(w => w.Timezone is null).ToList();
foreach (var well in wells)
well.Timezone = GetTimezone(well).Adapt<SimpleTimezone>();
{
if (TryGetTimezone(well, out var timezone))
well.Timezone = timezone.Adapt<SimpleTimezone>();
else
well.Timezone = new SimpleTimezone
{
Hours = 5,
IsOverride = false,
TimeZoneId = "Assumed",
};
}
var wellsWithTz = wells.Where(w => w.Timezone is not null);
if (wellsWithTz.Any())
{
@ -228,6 +241,20 @@ namespace AsbCloudInfrastructure.Services
}
}
private bool TryGetTimezone(int idWell, out SimpleTimezoneDto timezone)
{
timezone = null;
try
{
timezone = GetTimezone(idWell);
return timezone is not null;
}
catch
{
return false;
}
}
public SimpleTimezoneDto GetTimezone(int idWell)
{
var well = Cache.FirstOrDefault(c => c.Id == idWell);
@ -236,6 +263,20 @@ namespace AsbCloudInfrastructure.Services
return GetTimezone(well);
}
private bool TryGetTimezone(Well well, out SimpleTimezoneDto timezone)
{
timezone = null;
try
{
timezone = GetTimezone(well);
return timezone is not null;
}
catch
{
return false;
}
}
private SimpleTimezoneDto GetTimezone(Well well)
{
if (well == null)