ReportsPrintLogDailyLabelIdHelper.cs
4.61 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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;
}
/// <summary>
/// 预览/即将打印:取门店在指定自然日的下一个打印序号(不落库)。
/// </summary>
public static async Task<string> ResolveNextDailyLabelIdAsync(
ISqlSugarClient db,
string locationId,
DateTime referenceTime)
{
var locId = locationId?.Trim();
if (string.IsNullOrWhiteSpace(locId))
{
return "无";
}
var dayStart = referenceTime.Date;
var dayEnd = dayStart.AddDays(1);
var count = 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)
.CountAsync();
return Format(dayStart, count + 1);
}
public static string Format(DateTime printDay, int sequence) =>
$"{printDay:yyyyMMdd}-{sequence}";
/// <summary>
/// 为门店当日后续打印任务预留连续序号(不含已存在任务之外的并发锁,与列表展示规则一致)。
/// </summary>
public static async Task<List<string>> ResolveNextDailyLabelIdsAsync(
ISqlSugarClient db,
string locationId,
DateTime day,
int count)
{
var result = new List<string>();
if (count <= 0 || string.IsNullOrWhiteSpace(locationId))
{
return result;
}
var locId = locationId.Trim();
var dayStart = day.Date;
var dayEnd = dayStart.AddDays(1);
var existingCount = 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)
.CountAsync();
for (var i = 1; i <= count; i++)
{
result.Add(Format(dayStart, existingCount + i));
}
return result;
}
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; }
}
}