feat(bll, dal): WeekendDayService + БД. CalendarService - получение рабочих дней за период

This commit is contained in:
Mikhail Kuznetsov
2024-07-01 11:32:19 +10:00
parent 9632b0c560
commit ff37e6c10f
14 changed files with 2981 additions and 43 deletions

View File

@@ -2,68 +2,92 @@
namespace PARR.BLL.Services.Implementations
{
public delegate Task<List<DateOnly>> GetWeekendsDelegate(DateOnly start, DateOnly end);
internal class CalendarService : ICalendarService
{
private DateOnly GetEndPeriodDate(DateOnly startPeriod, TimeSpan distributionPeriod)
{
return startPeriod.AddDays(distributionPeriod.Days).AddDays(-1);
}
public async Task<List<DateOnly>> GetWorkDatesForPeriodAsync(DateOnly startPeriod, TimeSpan distributionPeriod, GetWeekendsDelegate getWeekends)
public List<DateOnly> GetWorkDatesForPeriod(DateOnly startPeriod, TimeSpan distributionPeriod, GetWeekendsDelegate getWeekends)
{
var endPeriod = GetEndPeriodDate(startPeriod, distributionPeriod);
//не получилось сделать асинхронным
var weekends = getWeekends.Invoke(startPeriod, endPeriod).ToList(); //.ToListAsync();
var weekends = await getWeekends.Invoke(startPeriod, endPeriod);
//TODO: logic!!!
//TODO: переделать метод получения рабочих дней, получать актуальные рабочие дни за период
//не стали реализовывать сразу, так как повысится нагрузка на робота
// на следующий месяц будет много изменений
//временно сделаем список руками рабочих дней
var workDates = new List<DateOnly>();
var workDays = GetMockWorkDays(startPeriod, endPeriod);
workDays = workDays.Where(t => weekends.Any(x => x != t)).ToList();
return workDates;
}
return workDays;
}
public class Test
/// <summary>
/// Временные данные пока не реализован сервис и логика
/// </summary>
/// <param name="startPeriod"></param>
/// <param name="EndPeriod"></param>
/// <returns></returns>
private List<DateOnly> GetMockWorkDays(DateOnly startPeriod, DateOnly EndPeriod)
{
private readonly ICalendarService calendarService;
var workDaysList = new List<DateOnly>();
public Test(ICalendarService calendarService)
var currentDay = startPeriod;
while (currentDay <= EndPeriod)
{
this.calendarService = calendarService;
// 29 дней, потому что в феврале 28, чтоб не париться, сделали так
if (currentDay.Day < 29)
workDaysList.Add(currentDay);
currentDay = currentDay.AddDays(1);
}
public async void Ttt()
{
var tService = new TestService();
// var endPeriod = calendarService.GetEndPeriodDate(new DateOnly(2024, 1, 1), TimeSpan.Parse("30:00:00:00"));
var workDays = await calendarService.GetWorkDatesForPeriodAsync(new DateOnly(2024, 1, 1), TimeSpan.Parse("30:00:00:00"), tService.GetWeekendsAsync);
}
}
public class TestService
{
public async Task<List<DateOnly>> GetWeekendsAsync(DateOnly start, DateOnly end)
{
await Task.Delay(1000);
return new List<DateOnly> { new DateOnly(2024, 1, 12) };
return workDaysList.OrderBy(t => t).ToList();
}
}
//public class Test
//{
// private readonly ICalendarService calendarService;
// public Test(ICalendarService calendarService)
// {
// this.calendarService = calendarService;
// }
// public async void Ttt()
// {
// var tService = new TestService();
// // var endPeriod = calendarService.GetEndPeriodDate(new DateOnly(2024, 1, 1), TimeSpan.Parse("30:00:00:00"));
// var workDays = await calendarService.GetWorkDatesForPeriodAsync(new DateOnly(2024, 1, 1), TimeSpan.Parse("30:00:00:00"), tService.GetWeekendsAsync);
// }
//}
//public class TestService
//{
// public async Task<List<DateOnly>> GetWeekendsAsync(DateOnly start, DateOnly end)
// {
// await Task.Delay(1000);
// return new List<DateOnly> { new DateOnly(2024, 1, 12) };
// }
//}
}

View File

@@ -1,13 +1,16 @@

using PARR.BLL.Services.Implementations;
namespace PARR.BLL.Services.Interfaces
namespace PARR.BLL.Services.Interfaces
{
public delegate IQueryable<DateOnly> GetWeekendsDelegate(DateOnly start, DateOnly end);
public interface ICalendarService
{
Task<List<DateOnly>> GetWorkDatesForPeriodAsync(DateOnly startPeriod, TimeSpan distributionPeriod, GetWeekendsDelegate getWeekends);
//Task<List<DateOnly>> GetWorkDatesForPeriodAsync(DateOnly startPeriod, TimeSpan distributionPeriod, Action<List<DateOnly>> getWeekends);
//Task<List<DateOnly>> GetWorkDatesForPeriodAsync(DateOnly startPeriod, TimeSpan distributionPeriod, Action getWeekends);
/// <summary>
/// Получить только рабочие дни за период
/// </summary>
/// <param name="startPeriod"></param>
/// <param name="distributionPeriod"></param>
/// <param name="getWeekends"></param>
/// <returns></returns>
List<DateOnly> GetWorkDatesForPeriod(DateOnly startPeriod, TimeSpan distributionPeriod, GetWeekendsDelegate getWeekends);
}
}

View File

@@ -53,6 +53,8 @@ namespace PARR.DAL.Context
public DbSet<Models.Role> Roles { get; set; }
public DbSet<UsersInRole> UsersInRoles { get; set; }
public DbSet<WeekendDay> WeekendDays { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,34 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PARR.DAL.Migrations
{
/// <inheritdoc />
public partial class tblWeekendDaysCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "WeekendDays",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Date = table.Column<DateOnly>(type: "date", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_WeekendDays", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "WeekendDays");
}
}
}

View File

@@ -2204,6 +2204,20 @@ namespace PARR.DAL.Migrations
b.ToTable("UsersInRoles");
});
modelBuilder.Entity("PARR.DAL.Models.WeekendDay", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateOnly>("Date")
.HasColumnType("date");
b.HasKey("Id");
b.ToTable("WeekendDays");
});
modelBuilder.Entity("PARR.DAL.Models.Work", b =>
{
b.Property<Guid>("Id")

View File

@@ -0,0 +1,21 @@
using PARR.DAL.Models.Base;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PARR.DAL.Models
{
[Table("WeekendDays")]
public class WeekendDay : IBase
{
[Key]
public Guid Id { get; set; }
[NotMapped]
public DateTimeOffset DateCreated { get; set; }
[NotMapped]
public DateTimeOffset? DateModified { get; set; }
public DateOnly Date { get; set; }
}
}

View File

@@ -80,6 +80,7 @@ namespace PARR.DAL
services.AddTransient<ITaskStatusService, TaskStatusService>();
services.AddTransient<IEkStatusService, EkStatusService>();
services.AddTransient<IEsppSchTypeScheduleService, EsppSchTypeScheduleService>();
services.AddTransient<IWeekendDayService, WeekendDayService>();
// TransformServices
services.AddTransient<IEsppScheduleTransformService, EsppScheduleTransformService>();

View File

@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.DAL.Context;
using PARR.DAL.Models;
using PARR.DAL.Services.Abstracts;
using PARR.DAL.Services.Interfaces;
namespace PARR.DAL.Services.Implementations
{
internal class WeekendDayService : BaseService<WeekendDay>, IWeekendDayService
{
private readonly DataContext dataContext;
private readonly ILogger<WeekendDayService> logger;
protected override DbSet<WeekendDay> EntitySet => dataContext.WeekendDays;
protected override DataContext EntitiContext => dataContext;
public WeekendDayService(DataContext dataContext, ILogger<WeekendDayService> logger) : base(logger)
{
this.dataContext = dataContext;
this.logger = logger;
}
public IQueryable<DateOnly> GetWeekends(DateOnly start, DateOnly end)
{
return EntitySet.Where(t => t.Date >= start && t.Date <= end).Select(t => t.Date);
}
}
}

View File

@@ -0,0 +1,10 @@
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces.Base;
namespace PARR.DAL.Services.Interfaces
{
public interface IWeekendDayService : IBaseService<WeekendDay>
{
IQueryable<DateOnly> GetWeekends(DateOnly start, DateOnly end);
}
}

View File

@@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\PARR.BLL\PARR.BLL.csproj" />
<ProjectReference Include="..\PARR.DAL\PARR.DAL.csproj" />
<ProjectReference Include="..\PARR.EsppApi\PARR.EsppApi.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,5 +1,6 @@
using PARR.BLL;
using PARR.EsppApi;
using PARR.DAL;
using PARR.Test;
using Serilog;
@@ -8,6 +9,7 @@ IHost host = Host.CreateDefaultBuilder(args)
{
services.InstallEsppApiServices(hostContext.Configuration);
services.InstallBllServices(hostContext.Configuration);
services.InstallDalServices(hostContext.Configuration);
services.AddHostedService<Worker>();
})

View File

@@ -1,5 +1,6 @@
using PARR.BLL.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.Services.Interfaces;
using PARR.EsppApi;
using PARR.EsppApi.Constants;
using PARR.EsppApi.Models.Query;
@@ -12,18 +13,21 @@ namespace PARR.Test
private readonly IEsppApiService esppApiService;
private readonly IIntervalService intervalService;
private readonly ICalendarService calendarService;
private readonly IServiceProvider serviceProvider;
public Worker(
ILogger<Worker> logger,
IEsppApiService esppApiService,
IIntervalService intervalService,
ICalendarService calendarService
ICalendarService calendarService,
IServiceProvider serviceProvider
)
{
_logger = logger;
this.esppApiService = esppApiService;
this.intervalService = intervalService;
this.calendarService = calendarService;
this.serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -38,9 +42,18 @@ namespace PARR.Test
#region Calendar
var testCalendar = new BLL.Services.Implementations.Test(calendarService);
//var testCalendar = new BLL.Services.Implementations.Test(calendarService);
//testCalendar.Ttt() ;
//using (var scope = serviceProvider.CreateScope())
//{
// var weekendDayService = scope.ServiceProvider.GetService<IWeekendDayService>();
// var workDays = calendarService.GetWorkDatesForPeriod(new DateOnly(2024, 07, 01), TimeSpan.Parse("30:00:00:00"), weekendDayService!.GetWeekends);
//}
testCalendar.Ttt() ;
#endregion

View File

@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;"
},
"Logging": {
"LogLevel": {
"Default": "Information",