2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data;
|
2022-05-22 21:18:43 +05:00
|
|
|
using AsbCloudApp.Services;
|
|
|
|
using AsbCloudDb.Model;
|
2022-05-25 20:19:08 +05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-05-22 21:18:43 +05:00
|
|
|
using System;
|
2023-11-03 18:55:49 +05:00
|
|
|
using System.Collections.Generic;
|
2022-05-22 21:18:43 +05:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2024-02-20 16:58:40 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2023-11-03 18:55:49 +05:00
|
|
|
using Mapster;
|
2022-05-22 21:18:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
|
|
|
|
|
|
public class ScheduleRepository : CrudWellRelatedRepositoryBase<ScheduleDto, Schedule>,
|
|
|
|
IScheduleRepository
|
2022-05-22 21:18:43 +05:00
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
private readonly IWellService wellService;
|
2023-04-18 16:22:53 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public ScheduleRepository(IAsbCloudDbContext context, IWellService wellService)
|
|
|
|
: base(context, dbSet => dbSet.Include(s => s.Driller))
|
2022-05-22 21:18:43 +05:00
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
this.wellService = wellService;
|
|
|
|
}
|
2022-05-22 21:18:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public async Task<IEnumerable<ScheduleDto>> GetAsync(int idWell, DateTimeOffset workTime, CancellationToken token)
|
|
|
|
{
|
|
|
|
var entities = await BuildQuery(idWell, workTime)
|
|
|
|
.AsNoTracking()
|
|
|
|
.ToArrayAsync(token);
|
2022-05-22 21:18:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
return entities.Select(Convert);
|
|
|
|
}
|
2022-05-22 21:18:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public async Task<DrillerDto?> GetOrDefaultDrillerAsync(int idWell, DateTimeOffset workTime, CancellationToken token)
|
|
|
|
{
|
|
|
|
var entities = await BuildQuery(idWell, workTime)
|
|
|
|
.AsNoTracking()
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
if (!entities.Any())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var hoursOffset = wellService.GetTimezone(idWell).Hours;
|
|
|
|
var time = new TimeOnly(workTime.Hour, workTime.Minute, workTime.Second);
|
|
|
|
|
|
|
|
var entity = entities.FirstOrDefault(s =>
|
|
|
|
s.ShiftStart > s.ShiftEnd ^
|
|
|
|
(time >= s.ShiftStart && time < s.ShiftEnd)
|
|
|
|
);
|
|
|
|
|
|
|
|
return entity?.Driller.Adapt<DrillerDto>();
|
|
|
|
}
|
2023-11-03 18:55:49 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
public async Task<IEnumerable<ScheduleDto>> GetPageAsync(GetStatRequest request, CancellationToken token)
|
|
|
|
{
|
|
|
|
var idWell = request.IdsWells;
|
|
|
|
var idsDrillers = request.IdsDrillers;
|
|
|
|
var query = GetQuery().Where(s => request.IdsWells.Contains(s.IdWell));
|
|
|
|
if (idsDrillers.Any())
|
2023-11-03 18:55:49 +05:00
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
query = query.Where(s => idsDrillers.Contains(s.IdDriller));
|
2022-05-22 21:18:43 +05:00
|
|
|
}
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
var result = await query.ToArrayAsync(token);
|
|
|
|
return result.Select(Convert);
|
2024-02-20 16:58:40 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
}
|
2024-02-20 16:58:40 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
private IQueryable<Schedule> BuildQuery(int idWell, DateTimeOffset workTime)
|
|
|
|
{
|
|
|
|
var hoursOffset = wellService.GetTimezone(idWell).Hours;
|
2023-11-09 15:01:29 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
var workTimeDateTime = workTime.ToUniversalTime();
|
|
|
|
|
|
|
|
return GetQuery().Where(s => s.IdWell == idWell
|
|
|
|
&& s.DrillStart <= workTimeDateTime
|
|
|
|
&& s.DrillEnd >= workTimeDateTime);
|
|
|
|
}
|
2023-11-03 18:55:49 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
protected override Schedule Convert(ScheduleDto dto)
|
|
|
|
{
|
|
|
|
var entity = base.Convert(dto);
|
|
|
|
entity.DrillStart = dto.DrillStart.ToUniversalTime();
|
|
|
|
entity.DrillEnd = dto.DrillEnd.ToUniversalTime();
|
|
|
|
return entity;
|
|
|
|
}
|
2022-05-22 21:18:43 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
protected override ScheduleDto Convert(Schedule entity)
|
|
|
|
{
|
|
|
|
var hoursOffset = wellService.GetTimezone(entity.IdWell).Hours;
|
|
|
|
var timeSpan = TimeSpan.FromHours(hoursOffset);
|
2024-03-25 16:08:59 +05:00
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
var dto = base.Convert(entity);
|
|
|
|
dto.DrillStart = entity.DrillStart.ToOffset(timeSpan);
|
|
|
|
dto.DrillEnd = entity.DrillEnd.ToOffset(timeSpan);
|
|
|
|
return dto;
|
2022-05-22 21:18:43 +05:00
|
|
|
}
|
|
|
|
}
|