ReportsPrintLogDailyLabelIdHelper.cs
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using FoodLabeling.Application.Services.DbModels;
using SqlSugar;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// Print Log「Label ID」:按门店 + 自然日对打印任务排序,格式 <c>yyyyMMdd-n</c>(非 fl_label.LabelCode)。
/// </summary>
public static class ReportsPrintLogDailyLabelIdHelper
{
/// <summary>
/// 为列表/导出行计算门店当日打印序号。
/// </summary>
public static async Task<Dictionary<string, string>> ResolveDailyLabelIdsAsync(
ISqlSugarClient db,
IReadOnlyList<PrintTaskScopeKey> tasks)
{
var result = new Dictionary<string, string>(StringComparer.Ordinal);
if (tasks.Count == 0)
{
return result;
}
foreach (var task in tasks)
{
if (string.IsNullOrWhiteSpace(task.TaskId))
{
continue;
}
result.TryAdd(task.TaskId.Trim(), "无");
}
var buckets = tasks
.Where(t =>
!string.IsNullOrWhiteSpace(t.TaskId) &&
!string.IsNullOrWhiteSpace(t.LocationId) &&
t.PrintedAt != default)
.GroupBy(t => (LocationId: t.LocationId!.Trim(), Day: t.PrintedAt.Date))
.ToList();
foreach (var bucket in buckets)
{
var locId = bucket.Key.LocationId;
var dayStart = bucket.Key.Day;
var dayEnd = dayStart.AddDays(1);
var dayTasks = await db.Queryable<FlLabelPrintTaskDbEntity>()
.Where(t => t.LocationId == locId)
.Where(t =>
SqlFunc.IsNull(t.PrintedAt, t.CreationTime) >= dayStart &&
SqlFunc.IsNull(t.PrintedAt, t.CreationTime) < dayEnd)
.OrderBy(t => SqlFunc.IsNull(t.PrintedAt, t.CreationTime), OrderByType.Asc)
.OrderBy(t => t.Id, OrderByType.Asc)
.Select(t => new { t.Id, PrintedAt = SqlFunc.IsNull(t.PrintedAt, t.CreationTime) })
.ToListAsync();
for (var i = 0; i < dayTasks.Count; i++)
{
result[dayTasks[i].Id] = Format(dayStart, i + 1);
}
}
return result;
}
public static string Format(DateTime printDay, int sequence) =>
$"{printDay:yyyyMMdd}-{sequence}";
public readonly struct PrintTaskScopeKey
{
public PrintTaskScopeKey(string taskId, string? locationId, DateTime printedAt)
{
TaskId = taskId;
LocationId = locationId;
PrintedAt = printedAt;
}
public string TaskId { get; }
public string? LocationId { get; }
public DateTime PrintedAt { get; }
}
}