оптимизирован метод save

This commit is contained in:
eugeniy_ivanov 2023-04-11 13:27:42 +05:00
parent 27ee40816f
commit 44f1b08f90
2 changed files with 19 additions and 46 deletions

View File

@ -17,9 +17,9 @@ namespace AsbCloudDb.Model.GTR
[Column("date", TypeName = "timestamp with time zone")]
public DateTimeOffset DateTime { get; set; }
[Column("Value")]
public T Value { get; set; }
public T Value { get; set; } = default!;
[ForeignKey(nameof(IdTelemetry))]
public virtual Telemetry? Telemetry { get; set; }

View File

@ -3,6 +3,7 @@ using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.GTR;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
@ -156,25 +157,23 @@ namespace AsbCloudInfrastructure.Repository
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
foreach (var item in dto.Items)
{
var jsonValue = item.Value;
var type = jsonValue.Value.GetType().Name;
if (type == "String")
var jsonValue = item.Value;
if(jsonValue.Value is string valueString)
{
var entity = GetEntityStr(dto, jsonValue, item.Key, timezoneHours);
db.WitsItemString.Add(entity);
var entity = GetEntity(dto, valueString, item.Key, timezoneHours);
db.WitsItemString.Add(entity.Adapt<WitsItemString>());
}
else if (type == "Int32")
if (jsonValue.Value is float valueFloat)
{
var entity = GetEntityInt(dto, jsonValue, item.Key, timezoneHours);
db.WitsItemInt.Add(entity);
var entity = GetEntity(dto, valueFloat, item.Key, timezoneHours);
db.WitsItemFloat.Add(entity.Adapt<WitsItemFloat>());
}
else if (type == "Single")
if (jsonValue.Value is int valueInt)
{
var entity = GetEntityFloat(dto, jsonValue, item.Key, timezoneHours);
db.WitsItemFloat.Add(entity);
}
}
var entity = GetEntity(dto, valueInt, item.Key, timezoneHours);
db.WitsItemInt.Add(entity.Adapt<WitsItemInt>());
}
}
await db.SaveChangesAsync(token);
}
@ -261,42 +260,16 @@ namespace AsbCloudInfrastructure.Repository
.ConfigureAwait(false);
return entities;
}
private static WitsItemString GetEntityStr(WitsRecordDto record,JsonValue value,int idItems,double timezoneHours)
private static WitsItemBase<Tvalue> GetEntity<Tvalue>(WitsRecordDto record, Tvalue value, int idItems, double timezoneHours)
{
var entity = new WitsItemString
var entity = new WitsItemBase<Tvalue>
{
IdTelemetry = record.IdTelemetry,
DateTime = record.Date.ToUtcDateTimeOffset(timezoneHours),
IdRecord = record.Id,
IdItem = idItems,
Value = value.Value.ToString()!,
};
return entity;
}
private static WitsItemInt GetEntityInt(WitsRecordDto record, JsonValue value, int idItems, double timezoneHours)
{
var entity = new WitsItemInt
{
IdTelemetry = record.IdTelemetry,
DateTime = record.Date.ToUtcDateTimeOffset(timezoneHours),
IdRecord = record.Id,
IdItem = idItems,
Value = JsonSerializer.Deserialize<int>(value.ToString())!,
};
return entity;
}
private static WitsItemFloat GetEntityFloat(WitsRecordDto record, JsonValue value, int idItems, double timezoneHours)
{
var entity = new WitsItemFloat
{
IdTelemetry = record.IdTelemetry,
DateTime = record.Date.ToUtcDateTimeOffset(timezoneHours),
IdRecord = record.Id,
IdItem = idItems,
Value = (float)value.Value,
Value = value,
};
return entity;
}