OutboundMaterialService.cs
16.9 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// 文件:Services/WarehouseDataApiService.cs
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using IndustrialControl.ViewModels;
namespace IndustrialControl.Services;
/// <summary>
/// 真实接口实现,风格对齐 WorkOrderApi
/// </summary>
public sealed class OutboundMaterialService : IOutboundMaterialService
{
private readonly HttpClient _http;
private readonly string _outboundListEndpoint;
private readonly string _detailEndpoint;
private readonly string _scanDetailEndpoint;
// 新增:扫码入库端点
private readonly string _scanByBarcodeEndpoint;
private readonly string _scanConfirmEndpoint;
private readonly string _cancelScanEndpoint;
private readonly string _confirmOutstockEndpoint;
private readonly string _judgeScanAllEndpoint;
public OutboundMaterialService(HttpClient http, IConfigLoader configLoader)
{
_http = http;
// 和 WorkOrderApi 一样从 appconfig.json 读取端点,留兼容键名 + 兜底硬编码
JsonNode cfg = configLoader.Load();
// ⭐ 新增:读取 baseUrl 或 ip+port
var baseUrl =
(string?)cfg?["server"]?["baseUrl"]
?? BuildBaseUrl(cfg?["server"]?["ipAddress"], cfg?["server"]?["port"]);
if (string.IsNullOrWhiteSpace(baseUrl))
throw new InvalidOperationException("后端基础地址未配置:请在 appconfig.json 配置 server.baseUrl 或 server.ipAddress + server.port");
if (_http.BaseAddress is null)
_http.BaseAddress = new Uri(baseUrl, UriKind.Absolute);
_outboundListEndpoint =
(string?)cfg?["apiEndpoints"]?["outbound"]?["list"] ??
(string?)cfg?["apiEndpoints"]?["getOutStock"] ??
"/normalService/pda/wmsMaterialOutstock/getOutStock";
_detailEndpoint = (string?)cfg?["apiEndpoints"]?["outbound"]?["detail"]
?? "/normalService/pda/wmsMaterialOutstock/getOutStockDetail";
_scanDetailEndpoint = (string?)cfg?["apiEndpoints"]?["outbound"]?["scanDetail"]
?? "/normalService/pda/wmsMaterialOutstock/getOutStockScanDetail";
_scanByBarcodeEndpoint =
(string?)cfg?["apiEndpoints"]?["outbound"]?["scanByBarcode"]
?? "/normalService/pda/wmsMaterialOutstock/getOutStockByBarcode";
_scanConfirmEndpoint =
(string?)cfg?["apiEndpoints"]?["outbound"]?["scanConfirm"]
?? "/normalService/pda/wmsMaterialOutstock/scanConfirm";
_cancelScanEndpoint =
(string?)cfg?["apiEndpoints"]?["outbound"]?["cancelScan"]
?? "/normalService/pda/wmsMaterialOutstock/cancelScan";
_confirmOutstockEndpoint =
(string?)cfg?["apiEndpoints"]?["outbound"]?["confirm"]
?? "/normalService/pda/wmsMaterialOutstock/confirm";
_judgeScanAllEndpoint =
(string?)cfg?["apiEndpoints"]?["outbound"]?["judgeScanAll"]
?? "/normalService/pda/wmsMaterialOutstock/judgeOutstockDetailScanAll";
}
// ====== 你当前页面会调用的方法 ======
private static string? BuildBaseUrl(JsonNode? ipNode, JsonNode? portNode)
{
string? ip = ipNode?.ToString().Trim();
string? port = portNode?.ToString().Trim();
if (string.IsNullOrWhiteSpace(ip)) return null;
// 如果没带 http:// 或 https://,默认 http://
var hasScheme = ip.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| ip.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
var host = hasScheme ? ip : $"http://{ip}";
return string.IsNullOrEmpty(port) ? host : $"{host}:{port}";
}
public async Task<IEnumerable<OutboundOrderSummary>> ListOutboundOrdersAsync(
string? orderNoOrBarcode,
DateTime startDate,
DateTime endDate,
string orderType,
string[] orderTypeList,
CancellationToken ct = default)
{
// 结束时间扩到当天 23:59:59,避免把当日数据排除
var begin = startDate.ToString("yyyy-MM-dd 00:00:00");
var end = endDate.ToString("yyyy-MM-dd 23:59:59");
// 用 KVP 列表(不要 Dictionary)→ 规避 WinRT generic + AOT 警告
var pairs = new List<KeyValuePair<string, string>>
{
new("createdTimeBegin", begin),
new("createdTimeEnd", end),
new("pageNo", "1"),
new("pageSize","10")
// 如需统计总数:new("searchCount", "true")
};
if (!string.IsNullOrWhiteSpace(orderNoOrBarcode))
pairs.Add(new("outstockNo", orderNoOrBarcode.Trim()));
if (!string.IsNullOrWhiteSpace(orderType))
pairs.Add(new("orderType", orderType));
if (orderTypeList is { Length: > 0 })
pairs.Add(new("orderTypeList", string.Join(",", orderTypeList)));
// 交给 BCL 编码(比手写 Escape 安全)
using var form = new FormUrlEncodedContent(pairs);
var qs = await form.ReadAsStringAsync(ct);
var url = _outboundListEndpoint + "?" + qs;
using var req = new HttpRequestMessage(HttpMethod.Get, url);
using var resp = await _http.SendAsync(req, ct);
var json = await resp.Content.ReadAsStringAsync(ct);
if (!resp.IsSuccessStatusCode)
return Enumerable.Empty<OutboundOrderSummary>();
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<GetOutStockPageResp>(json, opt);
var records = dto?.result?.records;
if (dto?.success != true || records is null || records.Count == 0)
return Enumerable.Empty<OutboundOrderSummary>();
return records.Select(x => new OutboundOrderSummary(
outstockId: x.id ?? "",
orderType: x.orderType ?? "",
orderTypeName: x.orderTypeName ?? "",
purchaseNo: x.purchaseNo ?? "",
supplierName: x.supplierName ?? "",
arrivalNo: x.arrivalNo ?? "",
createdTime: x.createdTime ?? "",
deliveryNo: x.deliveryNo ?? "",
requisitionMaterialNo:x.requisitionMaterialNo ?? "",
returnNo: x.returnNo ?? "",
workOrderNo:x.workOrderNo ?? ""
));
}
public async Task<IReadOnlyList<OutboundPendingRow>> GetOutStockDetailAsync(
string outstockId, CancellationToken ct = default)
{
var url = $"{_detailEndpoint}?outstockId={Uri.EscapeDataString(outstockId)}";
using var req = new HttpRequestMessage(HttpMethod.Get, url);
using var res = await _http.SendAsync(req, ct).ConfigureAwait(false);
res.EnsureSuccessStatusCode();
var json = await res.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<GetOutStockDetailResp>(json, opt);
if (dto?.success != true || dto.result is null || dto.result.Count == 0)
return Array.Empty<OutboundPendingRow>();
static int ToIntSafe(string? s)
{
if (string.IsNullOrWhiteSpace(s)) return 0;
s = s.Trim().Replace(",", "");
return int.TryParse(s, out var v) ? v : 0;
}
// ⚠️ 接口没有 barcode,这里先用空串;如需展示可以改成 x.materialCode 或 x.stockBatch
var list = dto.result.Select(x => new OutboundPendingRow(
Barcode: string.Empty, // 或 $"{x.materialCode}" / $"{x.stockBatch}"
DetailId: x.id ?? string.Empty, // ← 改为接口的 id
Location: x.location ?? string.Empty,
MaterialName: x.materialName ?? string.Empty,
PendingQty: ToIntSafe(x.outstockQty), // ← 预计数量
ScannedQty: ToIntSafe(x.qty), // ← 已扫描量
Spec: x.spec ?? string.Empty
)).ToList();
return list;
}
public async Task<IReadOnlyList<OutboundScannedRow>> GetOutStockScanDetailAsync(
string outstockId,
CancellationToken ct = default)
{
// 文档为 GET + x-www-form-urlencoded,这里用 query 传递(关键在大小写常为 OutstockId)
var url = $"{_scanDetailEndpoint}?OutstockId={Uri.EscapeDataString(outstockId)}";
using var req = new HttpRequestMessage(HttpMethod.Get, url);
using var res = await _http.SendAsync(req, ct).ConfigureAwait(false);
res.EnsureSuccessStatusCode();
var json = await res.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<GetOutStockScanDetailResp>(json, opt);
if (dto?.success != true || dto.result is null || dto.result.Count == 0)
return Array.Empty<OutboundScannedRow>();
static int ToIntSafe(string? s)
{
if (string.IsNullOrWhiteSpace(s)) return 0;
// 去除千分位、空格
s = s.Trim().Replace(",", "");
return int.TryParse(s, out var v) ? v : 0;
}
// 映射:OutstockId <- id(截图注释“入库单明细主键id”)
var list = dto.result.Select(x => new OutboundScannedRow(
Barcode: (x.barcode ?? string.Empty).Trim(),
DetailId: (x.id ?? string.Empty).Trim(),
Location: (x.location ?? string.Empty).Trim(),
MaterialName: (x.materialName ?? string.Empty).Trim(),
Qty: ToIntSafe(x.qty),
Spec: (x.spec ?? string.Empty).Trim(),
ScanStatus: x.scanStatus ?? false,
WarehouseCode: x.warehouseCode?.Trim()
)).ToList();
return list;
}
// ========= 扫码入库实现 =========
public async Task<SimpleOk> OutStockByBarcodeAsync(string outstockId, string barcode, CancellationToken ct = default)
{
var body = JsonSerializer.Serialize(new { barcode, outstockId });
using var req = new HttpRequestMessage(HttpMethod.Post, _scanByBarcodeEndpoint)
{
Content = new StringContent(body, Encoding.UTF8, "application/json")
};
using var res = await _http.SendAsync(req, ct);
var json = await res.Content.ReadAsStringAsync(ct);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<ScanByBarcodeResp>(json, opt);
// 按文档:以 success 判断;message 作为失败提示
var ok = dto?.success == true;
return new SimpleOk(ok, dto?.message);
}
public async Task<SimpleOk> ScanConfirmAsync(string outstockId, CancellationToken ct = default)
{
var bodyJson = JsonSerializer.Serialize(new { outstockId });
using var req = new HttpRequestMessage(HttpMethod.Post, _scanConfirmEndpoint)
{
Content = new StringContent(bodyJson, Encoding.UTF8, "application/json")
};
using var res = await _http.SendAsync(req, ct);
var json = await res.Content.ReadAsStringAsync(ct);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<ScanConfirmResp>(json, opt);
var ok = dto?.success == true;
return new SimpleOk(ok, dto?.message);
}
public async Task<SimpleOk> CancelScanAsync(string outstockId, CancellationToken ct = default)
{
var bodyJson = JsonSerializer.Serialize(new { outstockId });
using var req = new HttpRequestMessage(HttpMethod.Post, _cancelScanEndpoint)
{
Content = new StringContent(bodyJson, Encoding.UTF8, "application/json")
};
using var res = await _http.SendAsync(req, ct);
var json = await res.Content.ReadAsStringAsync(ct);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<CancelScanResp>(json, opt);
var ok = dto?.success == true;
return new SimpleOk(ok, dto?.message);
}
public async Task<SimpleOk> ConfirmOutstockAsync(string outstockId, CancellationToken ct = default)
{
var bodyJson = JsonSerializer.Serialize(new { outstockId });
using var req = new HttpRequestMessage(HttpMethod.Post, _confirmOutstockEndpoint)
{
Content = new StringContent(bodyJson, Encoding.UTF8, "application/json")
};
using var res = await _http.SendAsync(req, ct);
var json = await res.Content.ReadAsStringAsync(ct);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<ConfirmResp>(json, opt);
var ok = dto?.success == true;
return new SimpleOk(ok, dto?.message);
}
public async Task<bool> JudgeOutstockDetailScanAllAsync(string outstockId, CancellationToken ct = default)
{
var url = $"{_judgeScanAllEndpoint}?id={Uri.EscapeDataString(outstockId)}";
using var req = new HttpRequestMessage(HttpMethod.Get, url);
using var res = await _http.SendAsync(req, ct);
var json = await res.Content.ReadAsStringAsync(ct);
var opt = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var dto = JsonSerializer.Deserialize<JudgeScanAllResp>(json, opt);
// 按文档:看 result(true/false);若接口异常或无 result,则返回 false 让前端提示/二次确认
return dto?.result == true;
}
}
public class GetOutStockReq
{
public string? createdTime { get; set; }
public string? endTime { get; set; }
public string? outstockNo { get; set; }
public string? orderType { get; set; }
public string? startTime { get; set; }
}
public class GetOutStockResp
{
public int code { get; set; }
public long costTime { get; set; }
public string? message { get; set; }
public bool success { get; set; }
public List<GetOutStockItem>? result { get; set; }
}
public class GetOutStockItem
{
public string? arrivalNo { get; set; }
public string? createdTime { get; set; }
public string? outstockId { get; set; }
public string? outstockNo { get; set; }
public string? orderType { get; set; }
public string? purchaseNo { get; set; }
public string? supplierName { get; set; }
}
public sealed class GetOutStockDetailResp
{
public bool success { get; set; }
public string? message { get; set; }
public int? code { get; set; }
public List<GetOutStockDetailItem>? result { get; set; }
public int? costTime { get; set; }
}
public sealed class GetOutStockDetailItem
{
public string? id { get; set; } // 入库单明细主键id
public string? outstockNo { get; set; } // 入库单号
public string? materialCode { get; set; }
public string? materialName { get; set; }
public string? spec { get; set; }
public string? stockBatch { get; set; }
public string? outstockQty { get; set; } // 预计数量(字符串/可能为空)
public string? outstockWarehouseCode { get; set; } // 入库仓库编码
public string? location { get; set; } // 内点库位
public string? qty { get; set; } // 已扫描量(字符串/可能为空)
}
public class GetOutStockPageResp
{
public int code { get; set; }
public long costTime { get; set; }
public string? message { get; set; }
public bool success { get; set; }
public GetOutStockPageData? result { get; set; }
}
public class GetOutStockPageData
{
public int pageNo { get; set; }
public int pageSize { get; set; }
public long total { get; set; }
public List<GetOutStockRecord> records { get; set; } = new();
}
public class GetOutStockRecord
{
public string? id { get; set; }
public string? outstockNo { get; set; }
public string? orderType { get; set; }
public string? orderTypeName { get; set; }
public string? supplierName { get; set; }
public string? arrivalNo { get; set; }
public string? purchaseNo { get; set; }
public string? createdTime { get; set; }
public string? deliveryNo { get; set; }
public string? requisitionMaterialNo { get; set; }
public string? returnNo { get; set; }
public string? workOrderNo { get; set; }
}
public sealed class GetOutStockScanDetailResp
{
public bool success { get; set; }
public string? message { get; set; }
public int? code { get; set; }
public List<GetOutStockScanDetailItem>? result { get; set; }
public int? costTime { get; set; }
}
public sealed class GetOutStockScanDetailItem
{
public string? id { get; set; } // 入库单明细主键 id
public string? barcode { get; set; }
public string? materialName { get; set; }
public string? spec { get; set; }
public string? qty { get; set; } // 可能是 null 或 “数字字符串”
public string? warehouseCode { get; set; }
public string? location { get; set; }
public bool? scanStatus { get; set; } // 可能为 null,按 false 处理
}