DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/ScheduleService.cs

69 lines
2.3 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
2022-05-25 20:19:08 +05:00
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
#nullable enable
public class ScheduleService : CrudWellRelatedServiceBase<ScheduleDto, Schedule>, IScheduleService
{
private readonly IWellService wellService;
2022-06-15 14:57:37 +05:00
public ScheduleService(IAsbCloudDbContext context, IWellService wellService)
: base(context, dbSet => dbSet.Include(s => s.Driller))
{
this.wellService = wellService;
}
public async Task<DrillerDto?> GetDrillerAsync(int idWell, DateTime workTime, CancellationToken token)
{
var hoursOffset = wellService.GetTimezone(idWell).Hours;
var date = workTime.ToUtcDateTimeOffset(hoursOffset);
var entities = await GetQuery()
2022-06-15 14:57:37 +05:00
.Where(s => s.IdWell == idWell
&& s.DrillStart <= date
&& s.DrillEnd >= date)
.ToListAsync(token);
2022-05-25 20:19:08 +05:00
if (!entities.Any())
2022-05-25 20:19:08 +05:00
return null;
var remoteDate = date.ToRemoteDateTime(hoursOffset);
var time = new TimeOnly(remoteDate.Hour, remoteDate.Minute, remoteDate.Second);
2022-06-15 14:57:37 +05:00
var entity = entities.FirstOrDefault(s =>
(s.ShiftStart > s.ShiftEnd) ^
(time >= s.ShiftStart && time < s.ShiftEnd)
);
return entity?.Driller.Adapt<DrillerDto>();
}
protected override Schedule Convert(ScheduleDto dto)
{
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;
}
protected 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
}