This commit is contained in:
@@ -72,6 +72,21 @@ namespace Elwig.Services {
|
||||
vm.ManualWeighingReason = p.WeighingReason;
|
||||
}
|
||||
|
||||
public static (DateTime From, DateTime To)? GetFilterToday(DateTime today, IEnumerable<DateTime> timestamps) {
|
||||
var filtered = timestamps.Where(t => t >= today.AddDays(-1) && t < today.AddDays(1)).Order().ToArray();
|
||||
if (filtered.Length == 0) return null;
|
||||
|
||||
var latest = filtered.Last();
|
||||
var since = filtered.Cast<DateTime>().Reverse().Aggregate(latest, (s, c) => s.Subtract(c).TotalHours <= 6 ? c : s);
|
||||
latest += new TimeSpan(0, 1, -latest.Minute, -latest.Second, -latest.Millisecond, -latest.Microsecond);
|
||||
since += new TimeSpan(0, 0, -since.Minute, -since.Second, -since.Millisecond, -since.Microsecond);
|
||||
|
||||
if (since.Subtract(today) <= new TimeSpan(-6, 0, 0) && latest.Subtract(today) < new TimeSpan(0, 0, 0))
|
||||
return null;
|
||||
|
||||
return (since, latest);
|
||||
}
|
||||
|
||||
private static async Task<(Expression<Func<Delivery, bool>>, string)> GetFilterToday(AppDbContext ctx) {
|
||||
var today = DateTime.Today;
|
||||
var timestamps = (await ctx.Deliveries
|
||||
@@ -83,17 +98,16 @@ namespace Elwig.Services {
|
||||
.Where(t => t.HasValue)
|
||||
.Cast<DateTime>()
|
||||
.ToArray();
|
||||
if (timestamps.Length > 0) {
|
||||
var latest = timestamps.Last();
|
||||
var since = timestamps.Cast<DateTime>().Reverse().Aggregate(latest, (s, c) => s.Subtract(c).TotalHours <= 6 ? c : s);
|
||||
latest += new TimeSpan(0, 1, -latest.Minute, -latest.Second, -latest.Millisecond, -latest.Microsecond);
|
||||
since += new TimeSpan(0, 0, -since.Minute, -since.Second, -since.Millisecond, -since.Microsecond);
|
||||
|
||||
return (d => (d.DateString == since.ToString("yyyy-MM-dd") && (d.TimeString == null || d.TimeString.CompareTo(since.ToString("HH:mm:ss")) >= 0)) ||
|
||||
(d.DateString.CompareTo(since.ToString("yyyy-MM-dd")) > 0),
|
||||
since.Date == latest.Date ? since.ToString("dd.MM.yyyy") : $"{since:dd.MM.yyyy \\a\\b HH:mm} / {latest:dd.MM.yyyy \\b\\i\\s HH:mm}");
|
||||
} else {
|
||||
var res = GetFilterToday(today, timestamps);
|
||||
if (!res.HasValue) {
|
||||
return (_ => false, today.ToString("dd.MM.yyyy"));
|
||||
} else {
|
||||
var from = res.Value.From;
|
||||
var to = res.Value.To;
|
||||
return (d => (d.DateString == from.ToString("yyyy-MM-dd") && (d.TimeString == null || d.TimeString.CompareTo(from.ToString("HH:mm:ss")) >= 0)) ||
|
||||
(d.DateString.CompareTo(from.ToString("yyyy-MM-dd")) > 0),
|
||||
from.Date == to.Date ? from.ToString("dd.MM.yyyy") : $"{from:dd.MM.yyyy \\a\\b HH:mm} / {to:dd.MM.yyyy \\b\\i\\s HH:mm}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -842,5 +842,151 @@ namespace Tests.UnitTests.ServiceTests {
|
||||
Assert.That(await ctx.Deliveries.FindAsync(2022, 2), Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly DateTime[] DeliveryDates = [
|
||||
new(2020, 10, 1, 17, 45, 0),
|
||||
new(2020, 10, 1, 18, 30, 0),
|
||||
|
||||
new(2020, 10, 2, 8, 34, 0),
|
||||
new(2020, 10, 2, 9, 45, 0),
|
||||
new(2020, 10, 2, 10, 19, 0),
|
||||
new(2020, 10, 2, 11, 22, 0),
|
||||
new(2020, 10, 2, 13, 39, 0),
|
||||
new(2020, 10, 2, 14, 2, 0),
|
||||
new(2020, 10, 2, 15, 55, 0),
|
||||
new(2020, 10, 2, 16, 28, 0),
|
||||
new(2020, 10, 2, 17, 13, 0),
|
||||
new(2020, 10, 2, 18, 10, 0),
|
||||
|
||||
new(2020, 10, 3, 8, 13, 0),
|
||||
new(2020, 10, 3, 9, 39, 0),
|
||||
new(2020, 10, 3, 10, 20, 0),
|
||||
new(2020, 10, 3, 11, 46, 0),
|
||||
new(2020, 10, 3, 13, 56, 0),
|
||||
new(2020, 10, 3, 14, 4, 0),
|
||||
new(2020, 10, 3, 15, 33, 0),
|
||||
new(2020, 10, 3, 16, 23, 0),
|
||||
new(2020, 10, 3, 17, 18, 0),
|
||||
new(2020, 10, 3, 18, 19, 0),
|
||||
new(2020, 10, 3, 19, 10, 0),
|
||||
new(2020, 10, 3, 20, 25, 0),
|
||||
|
||||
new(2020, 10, 4, 5, 57, 0),
|
||||
new(2020, 10, 4, 6, 13, 0),
|
||||
new(2020, 10, 4, 7, 42, 0),
|
||||
new(2020, 10, 4, 8, 34, 0),
|
||||
new(2020, 10, 4, 9, 45, 0),
|
||||
new(2020, 10, 4, 10, 19, 0),
|
||||
new(2020, 10, 4, 11, 22, 0),
|
||||
new(2020, 10, 4, 13, 39, 0),
|
||||
new(2020, 10, 4, 14, 2, 0),
|
||||
new(2020, 10, 4, 15, 55, 0),
|
||||
new(2020, 10, 4, 16, 28, 0),
|
||||
new(2020, 10, 4, 17, 13, 0),
|
||||
new(2020, 10, 4, 18, 10, 0),
|
||||
|
||||
new(2020, 10, 5, 5, 57, 0),
|
||||
new(2020, 10, 5, 6, 13, 0),
|
||||
new(2020, 10, 5, 7, 42, 0),
|
||||
new(2020, 10, 5, 8, 34, 0),
|
||||
new(2020, 10, 5, 9, 45, 0),
|
||||
new(2020, 10, 5, 10, 19, 0),
|
||||
new(2020, 10, 5, 11, 22, 0),
|
||||
new(2020, 10, 5, 13, 39, 0),
|
||||
new(2020, 10, 5, 14, 2, 0),
|
||||
new(2020, 10, 5, 15, 55, 0),
|
||||
new(2020, 10, 5, 16, 28, 0),
|
||||
new(2020, 10, 5, 17, 13, 0),
|
||||
new(2020, 10, 5, 18, 10, 0),
|
||||
new(2020, 10, 5, 19, 10, 0),
|
||||
new(2020, 10, 5, 20, 25, 0),
|
||||
|
||||
new(2020, 10, 6, 16, 23, 0),
|
||||
new(2020, 10, 6, 22, 24, 0),
|
||||
new(2020, 10, 6, 23, 45, 0),
|
||||
new(2020, 10, 7, 0, 32, 0),
|
||||
new(2020, 10, 7, 1, 12, 0),
|
||||
new(2020, 10, 7, 2, 16, 0),
|
||||
new(2020, 10, 7, 3, 24, 0),
|
||||
new(2020, 10, 7, 4, 9, 0),
|
||||
new(2020, 10, 7, 5, 57, 0),
|
||||
new(2020, 10, 7, 6, 41, 0),
|
||||
new(2020, 10, 7, 7, 10, 0),
|
||||
|
||||
new(2020, 10, 7, 17, 23, 0),
|
||||
new(2020, 10, 7, 18, 16, 0),
|
||||
new(2020, 10, 7, 19, 39, 0),
|
||||
new(2020, 10, 7, 20, 31, 0),
|
||||
new(2020, 10, 7, 21, 23, 0),
|
||||
new(2020, 10, 7, 22, 24, 0),
|
||||
new(2020, 10, 7, 23, 45, 0),
|
||||
new(2020, 10, 8, 0, 32, 0),
|
||||
new(2020, 10, 8, 1, 12, 0),
|
||||
new(2020, 10, 8, 2, 16, 0),
|
||||
new(2020, 10, 8, 3, 24, 0),
|
||||
new(2020, 10, 8, 4, 9, 0),
|
||||
new(2020, 10, 8, 5, 57, 0),
|
||||
new(2020, 10, 8, 6, 41, 0),
|
||||
new(2020, 10, 8, 7, 10, 0),
|
||||
|
||||
new(2020, 10, 9, 7, 53, 0),
|
||||
new(2020, 10, 9, 12, 36, 0),
|
||||
new(2020, 10, 9, 16, 26, 0),
|
||||
new(2020, 10, 9, 20, 19, 0),
|
||||
];
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_01_Empty() {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 10), DeliveryDates),
|
||||
Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_02_Normal() {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 2), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 2, 8, 0, 0), new DateTime(2020, 10, 2, 19, 0, 0))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_03_Overtime() {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 3), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 3, 8, 0, 0), new DateTime(2020, 10, 3, 21, 0, 0))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_04_EarlyStart() {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 4), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 4, 5, 0, 0), new DateTime(2020, 10, 4, 19, 0, 0))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_05_EarlyStartAndOvertime() {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 5), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 5, 5, 0, 0), new DateTime(2020, 10, 5, 21, 0, 0))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_06_NightShift() {
|
||||
using (Assert.EnterMultipleScope()) {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 6), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 6, 22, 0, 0), new DateTime(2020, 10, 7, 0, 0, 0))));
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 7), DeliveryDates.Where(d => d <= new DateTime(2020, 10, 7, 12, 0, 0))),
|
||||
Is.EqualTo((new DateTime(2020, 10, 6, 22, 0, 0), new DateTime(2020, 10, 7, 8, 0, 0))));
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 7), DeliveryDates.Where(d => d <= new DateTime(2020, 10, 7, 12, 0, 0)).Append(new(2020, 10, 7, 15, 10, 0))),
|
||||
Is.EqualTo((new DateTime(2020, 10, 7, 15, 0, 0), new DateTime(2020, 10, 7, 16, 0, 0))));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_GetFilterToday_07_EarlyStartNightShift() {
|
||||
using (Assert.EnterMultipleScope()) {
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 7), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 7, 17, 0, 0), new DateTime(2020, 10, 8, 0, 0, 0))));
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 8), DeliveryDates),
|
||||
Is.EqualTo((new DateTime(2020, 10, 7, 17, 0, 0), new DateTime(2020, 10, 8, 8, 0, 0))));
|
||||
Assert.That(DeliveryService.GetFilterToday(new(2020, 10, 8), DeliveryDates.Append(new(2020, 10, 8, 15, 10, 0))),
|
||||
Is.EqualTo((new DateTime(2020, 10, 8, 15, 0, 0), new DateTime(2020, 10, 8, 16, 0, 0))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user