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