DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/ProcessMapPlanBaseRepository.cs
2024-05-28 00:05:21 +05:00

67 lines
2.0 KiB
C#

using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.ProcessMapPlan;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace AsbCloudInfrastructure.Repository;
public class ProcessMapPlanBaseRepository<TDto, TEntity> : ChangeLogRepositoryAbstract<TDto, TEntity, ProcessMapPlanBaseRequestWithWell>
where TDto : AsbCloudApp.Data.IId
where TEntity : ProcessMapPlanBase
{
private readonly IWellService wellService;
public ProcessMapPlanBaseRepository(IAsbCloudDbContext context, IWellService wellService)
: base(context)
{
this.wellService = wellService;
}
protected override IQueryable<TEntity> BuildQuery(ProcessMapPlanBaseRequestWithWell request)
{
var query = db
.Set<TEntity>()
.Include(e => e.Author)
.Include(e => e.Editor)
.Include(e => e.Well)
.Include(e => e.WellSectionType)
.Where(e => e.IdWell == request.IdWell);
if (request.UpdateFrom.HasValue)
{
var from = request.UpdateFrom.Value.ToUniversalTime();
query = query.Where(e => e.Creation >= from || e.Obsolete >= from);
}
if (request.Moment.HasValue)
{
var moment = request.Moment.Value.ToUniversalTime();
query = query
.Where(e => e.Creation <= moment)
.Where(e => e.Obsolete == null || e.Obsolete >= moment);
}
return query;
}
protected override TimeSpan GetTimezoneOffset(ProcessMapPlanBaseRequestWithWell request)
{
var timezone = wellService.GetTimezone(request.IdWell);
var offset = TimeSpan.FromHours(timezone.Hours);
return offset;
}
protected override TEntity Convert(TDto dto)
{
var entity = base.Convert(dto);
entity.Author = null;
entity.Editor = null;
return entity;
}
}