Убрал ненужные переопределения.

Добавил учет часового пояса скважины.
This commit is contained in:
ngfrolov 2022-05-26 13:33:59 +05:00
parent 339921b968
commit 23cf8d2108
3 changed files with 41 additions and 69 deletions

View File

@ -137,7 +137,7 @@ namespace AsbCloudInfrastructure.Services
public virtual TModel Convert(TDto src) => src.Adapt<TModel>(); public virtual TModel Convert(TDto src) => src.Adapt<TModel>();
private IQueryable<TModel> GetQueryWithIncludes() protected IQueryable<TModel> GetQueryWithIncludes()
{ {
IQueryable<TModel> query = dbSet; IQueryable<TModel> query = dbSet;
foreach (var include in Includes) foreach (var include in Includes)

View File

@ -14,33 +14,5 @@ namespace AsbCloudInfrastructure.Services
public DrillerService(IAsbCloudDbContext context) : base(context) public DrillerService(IAsbCloudDbContext context) : base(context)
{ {
} }
public async Task<int> DeleteAsync(int id, CancellationToken dto)
{
return await base.DeleteAsync(id, dto);
}
public async Task<IEnumerable<DrillerDto>> GetAllAsync(CancellationToken token)
{
return await base.GetAllAsync(token);
}
public async Task<DrillerDto> GetAsync(int id, CancellationToken token)
{
return await base.GetAsync(id,token)
.ConfigureAwait(false);
}
public async Task<int> InsertAsync(DrillerDto dto, CancellationToken token)
{
return await base.InsertAsync(dto, token);
}
public async Task<int> UpdateAsync(int id, DrillerDto dto, CancellationToken token)
{
return await base.UpdateAsync(id,dto, token);
}
} }
} }

View File

@ -1,68 +1,68 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services namespace AsbCloudInfrastructure.Services
{ {
public class ScheduleService : CrudServiceBase<ScheduleDto, ScheduleItem>, IScheduleService #nullable enable
public class ScheduleService : CrudServiceBase<ScheduleDto, Schedule>, IScheduleService
{ {
public ScheduleService(IAsbCloudDbContext context) : base(context) private readonly IWellService wellService;
public ScheduleService(IAsbCloudDbContext context, IWellService wellService) : base(context)
{ {
Includes.Add(nameof(ScheduleItem.Driller)); Includes.Add(nameof(Schedule.Driller));
this.wellService = wellService;
} }
public async Task<int> DeleteAsync(int id, CancellationToken dto) public async Task<DrillerDto?> GetDrillerAsync(int idWell, DateTime workTime, CancellationToken token = default)
{
return await base.DeleteAsync(id,dto);
}
public async Task<IEnumerable<ScheduleDto>> GetAllAsync(CancellationToken token)
{ {
return await base.GetAllAsync(token); var hoursOffset = wellService.GetTimezone(idWell).Hours;
} var date = workTime.ToUtcDateTimeOffset(hoursOffset);
public async Task<ScheduleDto> GetAsync(int id, CancellationToken token) var entities = await GetQueryWithIncludes()
{ .Where(s => s.IdWell==idWell
var res= await base.GetAsync(id,token) && s.DrillStart <= date
.ConfigureAwait(false); && s.DrillEnd >= date)
return res; .ToListAsync(token);
}
public async Task<DrillerDto> GetSchedule(int idWell, DateTimeOffset workTime, CancellationToken token = default) if (!entities.Any())
{
IQueryable<ScheduleItem> query = context.Set<ScheduleItem>();
foreach (var include in Includes)
query = query.Include(include);
var entires = query.Where(e => e.IdWell==idWell
&& e.DrillStart <= workTime
&& e.DrillEnd >= workTime)
.FirstOrDefault();
if (entires is null)
return null; return null;
return entires.Driller.Adapt<DrillerDto>(); var remoteDate = date.ToRemoteDateTime(hoursOffset);
var time = new TimeOnly(remoteDate.Hour, remoteDate.Minute, remoteDate.Second);
var entity = entities.FirstOrDefault(s =>
(s.ShiftStart > s.ShiftEnd) ^
(time >= s.ShiftStart && time < s.ShiftEnd)
);
return entity?.Driller.Adapt<DrillerDto>();
} }
public async Task<int> InsertAsync(ScheduleDto dto, CancellationToken token) public override Schedule Convert(ScheduleDto dto)
{
return await base.InsertAsync(dto,token);
}
public async Task<int> UpdateAsync(int id, ScheduleDto dto, CancellationToken token)
{ {
return await base.UpdateAsync(id,dto,token); var hoursOffset = wellService.GetTimezone(dto.IdWell).Hours;
var entity = base.Convert(dto);
entity.DrillStart = dto.DrillStart.ToUtcDateTimeOffset(hoursOffset);
entity.DrillEnd = dto.DrillEnd.ToUtcDateTimeOffset(hoursOffset);
return entity;
} }
public override ScheduleDto Convert(Schedule entity)
{
var hoursOffset = wellService.GetTimezone(entity.IdWell).Hours;
var dto = base.Convert(entity);
dto.DrillStart = entity.DrillStart.ToRemoteDateTime(hoursOffset);
dto.DrillEnd = entity.DrillEnd.ToRemoteDateTime(hoursOffset);
return dto;
}
} }
#nullable disable
} }