Skip to content

Commit 65cf418

Browse files
committed
refactor: rename LabelType to DocumentType
1 parent f428a54 commit 65cf418

13 files changed

Lines changed: 98 additions & 62 deletions

File tree

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Newtonsoft.Json;
2-
3-
namespace EasyInk.Engine.Models;
1+
namespace EasyInk.Engine.Models;
42

53
/// <summary>
64
/// 用户数据参数
@@ -13,22 +11,7 @@ public class UserDataParams
1311
public string? UserId { get; set; }
1412

1513
/// <summary>
16-
/// 标签类型
14+
/// 文档类型
1715
/// </summary>
18-
public string? LabelType { get; set; }
19-
20-
/// <summary>
21-
/// 文档类型。兼容前端 SDK 使用的 documentType 字段,内部仍映射到审计日志的 LabelType。
22-
/// </summary>
23-
[JsonProperty("documentType")]
24-
public string? DocumentType
25-
{
26-
get => LabelType;
27-
set => LabelType = value;
28-
}
29-
30-
public bool ShouldSerializeDocumentType()
31-
{
32-
return false;
33-
}
16+
public string? DocumentType { get; set; }
3417
}

lib/EasyInk.Net/EasyInk.Engine/tests/ModelTests.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using EasyInk.Engine;
22
using EasyInk.Engine.Models;
33
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Linq;
54
using Xunit;
65

76
namespace EasyInk.Engine.Tests;
@@ -103,40 +102,27 @@ public void Error_SetsErrorInfo()
103102
public class UserDataParamsTests
104103
{
105104
[Fact]
106-
public void Deserialize_DocumentType_MapsToLabelType()
105+
public void Deserialize_DocumentType_SetsDocumentType()
107106
{
108107
var userData = JsonConvert.DeserializeObject<UserDataParams>(
109108
@"{""userId"":""demo-user"",""documentType"":""receipt""}",
110109
JsonConfig.CamelCase);
111110

112111
Assert.Equal("demo-user", userData!.UserId);
113-
Assert.Equal("receipt", userData.LabelType);
114112
Assert.Equal("receipt", userData.DocumentType);
115113
}
116114

117115
[Fact]
118-
public void Deserialize_LabelType_RemainsSupported()
119-
{
120-
var userData = JsonConvert.DeserializeObject<UserDataParams>(
121-
@"{""userId"":""demo-user"",""labelType"":""shipping""}",
122-
JsonConfig.CamelCase);
123-
124-
Assert.Equal("shipping", userData!.LabelType);
125-
Assert.Equal("shipping", userData.DocumentType);
126-
}
127-
128-
[Fact]
129-
public void Serialize_OmitsDocumentTypeAlias()
116+
public void Serialize_UsesDocumentType()
130117
{
131118
var json = JsonConvert.SerializeObject(new UserDataParams
132119
{
133120
UserId = "demo-user",
134-
LabelType = "receipt"
121+
DocumentType = "receipt"
135122
}, JsonConfig.CamelCase);
136-
var token = JObject.Parse(json);
137123

138-
Assert.Equal("demo-user", token["userId"]!.ToString());
139-
Assert.Equal("receipt", token["labelType"]!.ToString());
140-
Assert.Null(token["documentType"]);
124+
Assert.Contains(@"""userId"":""demo-user""", json);
125+
Assert.Contains(@"""documentType"":""receipt""", json);
126+
Assert.DoesNotContain("labelType", json);
141127
}
142128
}

lib/EasyInk.Net/EasyInk.Printer/src/Models/PrintAuditLog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public class PrintAuditLog
5353
public string? UserId { get; set; }
5454

5555
/// <summary>
56-
/// 标签类型
56+
/// 文档类型
5757
/// </summary>
58-
public string? LabelType { get; set; }
58+
public string? DocumentType { get; set; }
5959

6060
/// <summary>
6161
/// 打印状态

lib/EasyInk.Net/EasyInk.Printer/src/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static void Run()
136136
Copies = request.Copies,
137137
Dpi = request.Dpi,
138138
UserId = request.UserData?.UserId,
139-
LabelType = request.UserData?.LabelType,
139+
DocumentType = request.UserData?.DocumentType,
140140
Status = result.Success ? JobStatus.Completed.ToString() : JobStatus.Failed.ToString(),
141141
ErrorMessage = result.ErrorInfo?.Message,
142142
JobId = requestId

lib/EasyInk.Net/EasyInk.Printer/src/Resources/en-US.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"Logs_ColPrinter": "Printer",
115115
"Logs_ColStatus": "Status",
116116
"Logs_ColUser": "User",
117-
"Logs_ColLabelType": "Label Type",
117+
"Logs_ColDocumentType": "Document Type",
118118
"Logs_ColJobId": "Job ID",
119119
"Logs_ColError": "Error",
120120

lib/EasyInk.Net/EasyInk.Printer/src/Resources/zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"Logs_ColPrinter": "打印机",
115115
"Logs_ColStatus": "状态",
116116
"Logs_ColUser": "用户",
117-
"Logs_ColLabelType": "标签类型",
117+
"Logs_ColDocumentType": "文档类型",
118118
"Logs_ColJobId": "任务ID",
119119
"Logs_ColError": "错误",
120120

lib/EasyInk.Net/EasyInk.Printer/src/Services/AuditService.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ CREATE TABLE IF NOT EXISTS PrintAuditLog (
5858
Copies INTEGER DEFAULT 1,
5959
Dpi INTEGER,
6060
UserId TEXT,
61-
LabelType TEXT,
61+
DocumentType TEXT,
6262
Status TEXT NOT NULL,
6363
ErrorMessage TEXT,
6464
JobId TEXT,
6565
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP
6666
)
6767
");
6868

69+
EnsureDocumentTypeColumn(connection);
6970
connection.Execute("CREATE INDEX IF NOT EXISTS idx_audit_timestamp ON PrintAuditLog(Timestamp)");
7071
connection.Execute("CREATE INDEX IF NOT EXISTS idx_audit_printer ON PrintAuditLog(PrinterName)");
7172
connection.Execute("CREATE INDEX IF NOT EXISTS idx_audit_user ON PrintAuditLog(UserId)");
@@ -81,10 +82,10 @@ public void LogPrint(PrintAuditLog log)
8182
connection.Execute(@"
8283
INSERT INTO PrintAuditLog
8384
(Timestamp, PrinterName, PaperWidth, PaperHeight, PaperUnit,
84-
Copies, Dpi, UserId, LabelType, Status, ErrorMessage, JobId)
85+
Copies, Dpi, UserId, DocumentType, Status, ErrorMessage, JobId)
8586
VALUES
8687
(@Timestamp, @PrinterName, @PaperWidth, @PaperHeight, @PaperUnit,
87-
@Copies, @Dpi, @UserId, @LabelType, @Status, @ErrorMessage, @JobId)
88+
@Copies, @Dpi, @UserId, @DocumentType, @Status, @ErrorMessage, @JobId)
8889
", log);
8990
}
9091

@@ -175,6 +176,24 @@ private static string BuildLogsQuery(
175176
return sql;
176177
}
177178

179+
private static void EnsureDocumentTypeColumn(SQLiteConnection connection)
180+
{
181+
var columns = connection.Query<string>("SELECT name FROM pragma_table_info('PrintAuditLog')").ToList();
182+
if (!columns.Contains("DocumentType", StringComparer.OrdinalIgnoreCase))
183+
connection.Execute("ALTER TABLE PrintAuditLog ADD COLUMN DocumentType TEXT");
184+
185+
if (columns.Contains("LabelType", StringComparer.OrdinalIgnoreCase))
186+
{
187+
connection.Execute(@"
188+
UPDATE PrintAuditLog
189+
SET DocumentType = LabelType
190+
WHERE (DocumentType IS NULL OR DocumentType = '')
191+
AND LabelType IS NOT NULL
192+
AND LabelType <> ''
193+
");
194+
}
195+
}
196+
178197
public int CleanupOldLogs(DateTime? now = null)
179198
{
180199
var cutoff = (now ?? DateTime.Now).AddDays(-_retentionDays);

lib/EasyInk.Net/EasyInk.Printer/src/UI/Presenters/LogsPresenter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private IReadOnlyList<ListViewRow> LoadRows(DateTime from, DateTime to)
122122
log.PrinterName,
123123
log.Status,
124124
log.UserId ?? string.Empty,
125-
log.LabelType ?? string.Empty,
125+
log.DocumentType ?? string.Empty,
126126
log.JobId ?? string.Empty,
127127
log.ErrorMessage ?? string.Empty));
128128
}
@@ -145,7 +145,7 @@ private int ExportRows(string filePath, DateTime from, DateTime to)
145145
LangManager.Get("Logs_ColPrinter"),
146146
LangManager.Get("Logs_ColStatus"),
147147
LangManager.Get("Logs_ColUser"),
148-
LangManager.Get("Logs_ColLabelType"),
148+
LangManager.Get("Logs_ColDocumentType"),
149149
LangManager.Get("Logs_ColJobId"),
150150
LangManager.Get("Logs_ColError"));
151151

@@ -158,7 +158,7 @@ private int ExportRows(string filePath, DateTime from, DateTime to)
158158
log.PrinterName,
159159
log.Status,
160160
log.UserId ?? string.Empty,
161-
log.LabelType ?? string.Empty,
161+
log.DocumentType ?? string.Empty,
162162
log.JobId ?? string.Empty,
163163
log.ErrorMessage ?? string.Empty);
164164
count++;

lib/EasyInk.Net/EasyInk.Printer/src/UI/Views/LogsView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public LogsView(LogsPresenter presenter)
9999
_listView.Columns.Add(LangManager.Get("Logs_ColPrinter"), 150);
100100
_listView.Columns.Add(LangManager.Get("Logs_ColStatus"), 80);
101101
_listView.Columns.Add(LangManager.Get("Logs_ColUser"), 100);
102-
_listView.Columns.Add(LangManager.Get("Logs_ColLabelType"), 120);
102+
_listView.Columns.Add(LangManager.Get("Logs_ColDocumentType"), 120);
103103
_listView.Columns.Add(LangManager.Get("Logs_ColJobId"), 200);
104104
_listView.Columns.Add(LangManager.Get("Logs_ColError"), 200);
105105

lib/EasyInk.Net/EasyInk.Printer/tests/AuditServiceTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Data.SQLite;
23
using System.IO;
34
using System.Linq;
45
using EasyInk.Printer.Models;
@@ -32,13 +33,15 @@ public void LogPrint_InsertsRecord()
3233
{
3334
Timestamp = DateTime.UtcNow,
3435
PrinterName = "TestPrinter",
36+
DocumentType = "receipt",
3537
Status = "Success",
3638
JobId = "job-1"
3739
});
3840

3941
var logs = _service.QueryLogs(limit: 10);
4042
Assert.Single(logs);
4143
Assert.Equal("TestPrinter", logs[0].PrinterName);
44+
Assert.Equal("receipt", logs[0].DocumentType);
4245
Assert.Equal("Success", logs[0].Status);
4346
Assert.Equal("job-1", logs[0].JobId);
4447
}
@@ -173,4 +176,53 @@ public void CleanupOldLogs_DeletesRowsOlderThanRetention()
173176
File.Delete(dbPath);
174177
}
175178
}
179+
180+
[Fact]
181+
public void Constructor_MigratesLegacyLabelTypeToDocumentType()
182+
{
183+
var dbPath = Path.Combine(Path.GetTempPath(), $"audit_legacy_{Guid.NewGuid():N}.db");
184+
try
185+
{
186+
using (var connection = new SQLiteConnection($"Data Source={dbPath}"))
187+
{
188+
connection.Open();
189+
using var command = connection.CreateCommand();
190+
command.CommandText = @"
191+
CREATE TABLE PrintAuditLog (
192+
Id INTEGER PRIMARY KEY AUTOINCREMENT,
193+
Timestamp DATETIME NOT NULL,
194+
PrinterName TEXT NOT NULL,
195+
PaperWidth REAL,
196+
PaperHeight REAL,
197+
PaperUnit TEXT DEFAULT 'mm',
198+
Copies INTEGER DEFAULT 1,
199+
Dpi INTEGER,
200+
UserId TEXT,
201+
LabelType TEXT,
202+
Status TEXT NOT NULL,
203+
ErrorMessage TEXT,
204+
JobId TEXT,
205+
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP
206+
);
207+
INSERT INTO PrintAuditLog
208+
(Timestamp, PrinterName, UserId, LabelType, Status, JobId)
209+
VALUES
210+
(@Timestamp, 'LegacyPrinter', 'user-1', 'receipt', 'Success', 'legacy-job');
211+
";
212+
command.Parameters.AddWithValue("@Timestamp", DateTime.UtcNow);
213+
command.ExecuteNonQuery();
214+
}
215+
216+
using var service = new AuditService(dbPath, startCleanupTimer: false);
217+
var logs = service.QueryLogs(limit: 10);
218+
219+
Assert.Single(logs);
220+
Assert.Equal("receipt", logs[0].DocumentType);
221+
}
222+
finally
223+
{
224+
if (File.Exists(dbPath))
225+
File.Delete(dbPath);
226+
}
227+
}
176228
}

0 commit comments

Comments
 (0)