IWorkOrderApi.cs
15.3 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace IndustrialControl.Services
{
// ===================== 接口定义 =====================
public interface IWorkOrderApi
{
// ① 工单分页列表
Task<WorkOrderPageResult> GetWorkOrdersAsync(WorkOrderQuery q, CancellationToken ct = default);
Task<DictBundle> GetWorkOrderDictsAsync(CancellationToken ct = default);
Task<WorkflowResp?> GetWorkOrderWorkflowAsync(string id, CancellationToken ct = default);
Task<PageResp<ProcessTask>?> PageWorkProcessTasksAsync(string workOrderNo, int pageNo = 1, int pageSize = 50, CancellationToken ct = default);
}
// ===================== 实现 =====================
public class WorkOrderApi : IWorkOrderApi
{
private readonly HttpClient _http;
// 统一由 appconfig.json 管理的端点路径
private readonly string _pageEndpoint;
private readonly string _workflowEndpoint;
private readonly string _processTasksEndpoint;
private readonly string _dictEndpoint;
public WorkOrderApi(HttpClient http, IConfigLoader configLoader)
{
_http = http;
// 读取 appconfig.json(AppData 下的生效配置)
JsonNode cfg = configLoader.Load();
// 优先新结构 apiEndpoints.workOrder.*;其次兼容旧键名;最后兜底硬编码
_pageEndpoint =
(string?)cfg?["apiEndpoints"]?["workOrder"]?["page"]
?? (string?)cfg?["apiEndpoints"]?["pageWorkOrders"]
?? "/normalService/pda/pmsWorkOrder/pageWorkOrders";
_workflowEndpoint =
(string?)cfg?["apiEndpoints"]?["workOrder"]?["workflow"]
?? (string?)cfg?["apiEndpoints"]?["getWorkOrderWorkflow"]
?? "/normalService/pda/pmsWorkOrder/getWorkOrderWorkflow";
_processTasksEndpoint =
(string?)cfg?["apiEndpoints"]?["workOrder"]?["processTasks"]
?? (string?)cfg?["apiEndpoints"]?["pageWorkProcessTasks"]
?? "/normalService/pda/pmsWorkOrder/pageWorkProcessTasks";
_dictEndpoint =
(string?)cfg?["apiEndpoints"]?["workOrder"]?["dictList"]
?? "/normalService/pda/pmsWorkOrder/getWorkOrderDictList";
}
private static string BuildQuery(IDictionary<string, string> p)
=> string.Join("&", p.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
// using System.Text.Json;
// using System.Text;
public async Task<WorkOrderPageResult> GetWorkOrdersAsync(WorkOrderQuery q, CancellationToken ct = default)
{
// 1) 先把所有要传的参数放进字典(只加有值的)
var p = new Dictionary<string, string>
{
["pageNo"] = q.PageNo.ToString(),
["pageSize"] = q.PageSize.ToString()
};
if (q.CreatedTimeStart.HasValue) p["createdTimeStart"] = q.CreatedTimeStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
if (q.CreatedTimeEnd.HasValue) p["createdTimeEnd"] = q.CreatedTimeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");
if (!string.IsNullOrWhiteSpace(q.WorkOrderNo)) p["workOrderNo"] = q.WorkOrderNo!.Trim();
if (!string.IsNullOrWhiteSpace(q.MaterialName)) p["materialName"] = q.MaterialName!.Trim();
// 2) 逐项进行 Uri.EscapeDataString 编码,避免出现“空格没编码”的情况
string qs = string.Join("&", p.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
var url = _pageEndpoint + "?" + qs;
using var req = new HttpRequestMessage(HttpMethod.Get, url);
System.Diagnostics.Debug.WriteLine("[WorkOrderApi] GET " + (_http.BaseAddress?.ToString() ?? "") + url);
using var httpResp = await _http.SendAsync(req, ct);
var json = await httpResp.Content.ReadAsStringAsync(ct);
System.Diagnostics.Debug.WriteLine("[WorkOrderApi] Resp: " + json[..Math.Min(300, json.Length)] + "...");
if (!httpResp.IsSuccessStatusCode)
return new WorkOrderPageResult { success = false, message = $"HTTP {(int)httpResp.StatusCode}" };
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var resp = JsonSerializer.Deserialize<WorkOrderPageResult>(json, options) ?? new WorkOrderPageResult();
// 兼容 result.records
var nested = resp.result?.records;
if (nested is not null && resp.result is not null)
{
if (resp.result.records is null || resp.result.records.Count == 0)
resp.result.records = nested;
if (resp.result.pageNo == 0) resp.result.pageNo = resp.result.list.pageNo;
if (resp.result.pageSize == 0) resp.result.pageSize = resp.result.list.pageSize;
if (resp.result.total == 0) resp.result.total = resp.result.list.total;
}
return resp;
}
/// <summary>
/// 工单流程:/getWorkOrderWorkflow?id=...
/// 返回 result 为数组(statusValue/statusName/statusTime)
/// </summary>
public async Task<WorkflowResp?> GetWorkOrderWorkflowAsync(string id, CancellationToken ct = default)
{
var p = new Dictionary<string, string> { ["id"] = id?.Trim() ?? "" };
var url = _workflowEndpoint + "?" + BuildQuery(p);
using var req = new HttpRequestMessage(HttpMethod.Get, url);
System.Diagnostics.Debug.WriteLine("[WorkOrderApi] GET " + url);
using var httpResp = await _http.SendAsync(req, ct);
var json = await httpResp.Content.ReadAsStringAsync(ct);
System.Diagnostics.Debug.WriteLine("[WorkOrderApi] Resp(getWorkOrderWorkflow): " + json[..Math.Min(300, json.Length)] + "...");
if (!httpResp.IsSuccessStatusCode)
return new WorkflowResp { success = false, message = $"HTTP {(int)httpResp.StatusCode}" };
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var resp = JsonSerializer.Deserialize<WorkflowResp>(json, options) ?? new WorkflowResp();
return resp;
}
/// <summary>
/// 工序分页:/pageWorkProcessTasks?pageNo=&pageSize=&workOrderNo=
/// 返回分页结构,数据在 result.records[]
/// </summary>
public async Task<PageResp<ProcessTask>?> PageWorkProcessTasksAsync(
string workOrderNo, int pageNo = 1, int pageSize = 50, CancellationToken ct = default)
{
var p = new Dictionary<string, string>
{
["pageNo"] = pageNo.ToString(),
["pageSize"] = pageSize.ToString()
};
if (!string.IsNullOrWhiteSpace(workOrderNo)) p["workOrderNo"] = workOrderNo.Trim();
var url = _processTasksEndpoint + "?" + BuildQuery(p);
using var req = new HttpRequestMessage(HttpMethod.Get, url);
System.Diagnostics.Debug.WriteLine("[WorkOrderApi] GET " + url);
using var httpResp = await _http.SendAsync(req, ct);
var json = await httpResp.Content.ReadAsStringAsync(ct);
System.Diagnostics.Debug.WriteLine("[WorkOrderApi] Resp(pageWorkProcessTasks): " + json[..Math.Min(300, json.Length)] + "...");
if (!httpResp.IsSuccessStatusCode)
return new PageResp<ProcessTask> { success = false, message = $"HTTP {(int)httpResp.StatusCode}" };
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var resp = JsonSerializer.Deserialize<PageResp<ProcessTask>>(json, options) ?? new PageResp<ProcessTask>();
// 兼容 result.records(你的实际返回就是 records,结构示例如你发的 JSON)
// 如果后端某些场景包在 result.list.records,也一并兼容
var nested = resp.result?.records ?? resp.result?.records;
if (nested is not null && resp.result is not null)
{
if (resp.result.records is null || resp.result.records.Count == 0)
resp.result.records = nested;
if (resp.result.pageNo == 0 && resp.result is not null) resp.result.pageNo = resp.result.pageNo;
if (resp.result.pageSize == 0 && resp.result is not null) resp.result.pageSize = resp.result.pageSize;
if (resp.result.total == 0 && resp.result is not null) resp.result.total = resp.result.total;
}
return resp;
}
public async Task<DictBundle> GetWorkOrderDictsAsync(CancellationToken ct = default)
{
using var req = new HttpRequestMessage(HttpMethod.Get, _dictEndpoint);
using var res = await _http.SendAsync(req, ct);
var json = await res.Content.ReadAsStringAsync(ct);
var opt = new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = System.Text.Json.JsonSerializer.Deserialize<DictResponse>(json, opt);
var all = dto?.result ?? new List<DictField>();
var audit = all.FirstOrDefault(f => string.Equals(f.field, "auditStatus", StringComparison.OrdinalIgnoreCase))
?.dictItems ?? new List<DictItem>();
var urgent = all.FirstOrDefault(f => string.Equals(f.field, "urgent", StringComparison.OrdinalIgnoreCase))
?.dictItems ?? new List<DictItem>();
return new DictBundle { AuditStatus = audit, Urgent = urgent };
}
}
// ===================== 请求模型 =====================
public class WorkOrderQuery
{
public int PageNo { get; set; } = 1;
public int PageSize { get; set; } = 50;
// 0 待执行;1 执行中;2 入库中;3 已完成
public string? AuditStatus { get; set; }
public DateTime? CreatedTimeStart { get; set; }
public DateTime? CreatedTimeEnd { get; set; }
public string? WorkOrderNo { get; set; }
public string? MaterialName { get; set; }
}
// ===================== 返回模型:分页 =====================
// 顶层:{ code, message, success, result: {...}, costTime }
public class WorkOrderPageResult
{
public int code { get; set; }
public string? message { get; set; }
public bool success { get; set; }
public WorkOrderPageData? result { get; set; }
public long costTime { get; set; }
}
// result 可能是 {pageNo,pageSize,records} 或 {list:{pageNo,pageSize,records}}
public class WorkOrderPageData
{
public WorkOrderPageList? list { get; set; }
public int pageNo { get; set; }
public int pageSize { get; set; }
public long total { get; set; }
public List<WorkOrderRecord> records { get; set; } = new();
}
public class WorkOrderPageList
{
public int pageNo { get; set; }
public int pageSize { get; set; }
public long total { get; set; }
public List<WorkOrderRecord> records { get; set; } = new();
}
// 只列页面需要字段;后续按实际补
public class WorkOrderRecord
{
public string? id { get; set; }
public string? workOrderNo { get; set; }
public string? workOrderName { get; set; }
public string? auditStatus { get; set; } // ★ "1" 这样的字符串
public decimal? curQty { get; set; }
public string? materialCode { get; set; }
public string? materialName { get; set; }
public string? line { get; set; }
public string? lineName { get; set; }
public string? workShop { get; set; }
public string? workShopName { get; set; }
public string? urgent { get; set; }
// ★ 这些时间都是 "yyyy-MM-dd HH:mm:ss" 字符串
public string? schemeStartDate { get; set; }
public string? schemeEndDate { get; set; }
public string? createdTime { get; set; }
public string? modifiedTime { get; set; }
public string? commitedTime { get; set; }
public string? bomCode { get; set; }
public string? routeName { get; set; }
}
// ===================== 返回模型:工作流 =====================
public class WorkOrderWorkflowResp
{
public int code { get; set; }
public long costTime { get; set; }
public string? message { get; set; }
public bool success { get; set; }
public WorkOrderWorkflow? result { get; set; }
}
public class WorkOrderWorkflow
{
public string? statusName { get; set; } // 待执行/执行中/入库中/已完成
public string? statusTime { get; set; } // 时间字符串
public int? statusValue { get; set; } // 0/1/2/3
}
// ===================== 返回模型:工序节点 =====================
public class ProcessTasksPageResult
{
public int code { get; set; }
public string? message { get; set; }
public bool success { get; set; }
public long costTime { get; set; }
public ProcessTasksList? result { get; set; }
}
public class ProcessTasksList
{
public int pageNo { get; set; }
public int pageSize { get; set; }
public long total { get; set; }
public List<ProcessTaskRecord> records { get; set; } = new();
}
public class ProcessTaskRecord
{
public string? processName { get; set; }
public string? startDate { get; set; }
public string? endDate { get; set; }
public int? sortNumber { get; set; }
}
public class DictResponse
{
public bool success { get; set; }
public string? message { get; set; }
public int code { get; set; }
public List<DictField>? result { get; set; }
public long costTime { get; set; }
}
public class DictField
{
public string? field { get; set; }
public List<DictItem> dictItems { get; set; } = new();
}
public class DictItem
{
public string? dictItemValue { get; set; } // 参数值("0"/"1"/"2"/"3"/"4"...)
public string? dictItemName { get; set; } // 显示名("待执行"/"执行中"...)
}
public class DictBundle
{
public List<DictItem> AuditStatus { get; set; } = new();
public List<DictItem> Urgent { get; set; } = new();
}
public sealed class WorkflowResp { public bool success { get; set; } public string? message { get; set; } public int code { get; set; } public List<WorkflowItem>? result { get; set; } }
public sealed class WorkflowItem { public string? statusValue { get; set; } public string? statusName { get; set; } public string? statusTime { get; set; } }
public sealed class PageResp<T> { public bool success { get; set; } public string? message { get; set; } public int code { get; set; } public PageResult<T>? result { get; set; } }
public sealed class PageResult<T> { public int pageNo { get; set; } public int pageSize { get; set; } public int total { get; set; } public List<T>? records { get; set; } }
public sealed class ProcessTask
{
public string? id { get; set; }
public string? processCode { get; set; }
public string? processName { get; set; }
public decimal? scheQty { get; set; }
public decimal? completedQty { get; set; }
public string? startDate { get; set; }
public string? endDate { get; set; }
public int? sortNumber { get; set; }
public string? auditStatus { get; set; }
}
}