fix:永丰售后修复BUG,
1,物料库存获取不到ERP:重新调整触发查询逻辑 2,工序变更导致物料无法出库:配置旧工序 3,测试出库订单ERP推送,任务下发
This commit is contained in:
1 parent
b2a9a38f38
commit
c76e7bb681
69 files changed
+5510
-157
No files matched your search
@@ -0,0 +1,237 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using HHWMS.Web.Models;
|
||||
using HHWMS.Web.Models.Agv;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
|
||||
namespace HHWMS.Web.Services.Agv
|
||||
{
|
||||
/// <summary>
|
||||
/// AGV 工具类:封装与 AGV 调度系统的全部 HTTP 通信(方案 11.5)
|
||||
/// 职责:显式超时、报文留痕(AgvTaskLog)、响应统一解析、失败抛业务异常
|
||||
/// </summary>
|
||||
public class AgvApiClient
|
||||
{
|
||||
private readonly AgvOption _option;
|
||||
private readonly WTMContext _wtm;
|
||||
private readonly IUnitWork _unitWork;
|
||||
private readonly ILogger<AgvApiClient> _logger;
|
||||
|
||||
public AgvApiClient(IOptions<AgvOption> option, WTMContext wtm, IUnitWork unitWork, ILogger<AgvApiClient> logger)
|
||||
{
|
||||
_option = option.Value;
|
||||
_wtm = wtm;
|
||||
_unitWork = unitWork;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下发:创建运单(方案 11.2)。成功返回 orderId(=TaskNo);失败抛异常
|
||||
/// </summary>
|
||||
public async Task<AgvCreateOrderResult> CreateOrderAsync(AgvCreateOrderRequest request, string taskNo)
|
||||
{
|
||||
// Mock 模式:不联网,直接返回约定成功报文
|
||||
if (_option.MockEnabled)
|
||||
{
|
||||
WriteLog(taskNo, "down", "create", request, new AgvApiResponse<AgvCreateOrderResult>
|
||||
{
|
||||
Code = "0",
|
||||
Msg = "mock success",
|
||||
Data = new AgvCreateOrderResult { OrderId = taskNo, ExternalId = request.ExternalId }
|
||||
}, 0, null);
|
||||
return new AgvCreateOrderResult { OrderId = taskNo, ExternalId = request.ExternalId };
|
||||
}
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
var result = await _wtm.CallAPI<AgvApiResponse<AgvCreateOrderResult>>(
|
||||
"AGV", "api/fleet/orders/create", HttpMethodEnum.POST, request, _option.CallTimeoutSeconds);
|
||||
sw.Stop();
|
||||
WriteLog(taskNo, "down", "create", request, result, sw.ElapsedMilliseconds, null);
|
||||
if (result?.Data == null || result.Data.Code != "0" || result.Data.Data == null)
|
||||
{
|
||||
throw new Exception($"AGV下发失败: {result?.Data?.Msg ?? result?.ErrorMsg ?? "无响应"}");
|
||||
}
|
||||
return result.Data.Data;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "AGV下发任务[{taskNo}]异常", taskNo);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消运单(方案 11.6)。sceneId 与 orderId 均必填,orderId 即 TaskNo;厂商侧幂等
|
||||
/// </summary>
|
||||
public async Task CancelOrderAsync(string sceneId, string orderId)
|
||||
{
|
||||
var request = new AgvCancelOrderRequest { SceneId = sceneId, OrderId = orderId };
|
||||
if (_option.MockEnabled)
|
||||
{
|
||||
WriteLog(orderId, "down", "cancel", request, new AgvApiResponse<object>
|
||||
{
|
||||
Code = "0",
|
||||
Msg = "mock success"
|
||||
}, 0, null);
|
||||
return;
|
||||
}
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
var result = await _wtm.CallAPI<AgvApiResponse<object>>(
|
||||
"AGV", "api/fleet/orders/cancel", HttpMethodEnum.POST, request, _option.CallTimeoutSeconds);
|
||||
sw.Stop();
|
||||
WriteLog(orderId, "down", "cancel", request, result, sw.ElapsedMilliseconds, null);
|
||||
if (result?.Data == null || result.Data.Code != "0")
|
||||
{
|
||||
throw new Exception($"AGV取消失败: {result?.Data?.Msg ?? result?.ErrorMsg ?? "无响应"}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "AGV取消任务[{orderId}]异常", orderId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询运单状态(11.3 查询式兜底用;接口待厂商确认,Mock 直接返回执行中)
|
||||
/// </summary>
|
||||
public async Task<AgvOrderStatusResult> QueryOrderAsync(string sceneId, string orderId)
|
||||
{
|
||||
if (_option.MockEnabled)
|
||||
{
|
||||
return new AgvOrderStatusResult { OrderId = orderId, Status = "Running" };
|
||||
}
|
||||
|
||||
var request = new AgvCancelOrderRequest { SceneId = sceneId, OrderId = orderId };
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
var result = await _wtm.CallAPI<AgvApiResponse<AgvOrderStatusResult>>(
|
||||
"AGV", "api/fleet/orders/status", HttpMethodEnum.POST, request, _option.CallTimeoutSeconds);
|
||||
sw.Stop();
|
||||
WriteLog(orderId, "down", "query", request, result, sw.ElapsedMilliseconds, null);
|
||||
if (result?.Data == null || result.Data.Code != "0" || result.Data.Data == null)
|
||||
{
|
||||
throw new Exception($"AGV查询失败: {result?.Data?.Msg ?? result?.ErrorMsg ?? "无响应"}");
|
||||
}
|
||||
return result.Data.Data;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "AGV查询任务[{orderId}]异常", orderId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 报文留痕:原始请求/响应 JSON + 耗时统一写入 AgvTaskLog
|
||||
/// </summary>
|
||||
private void WriteLog(string taskNo, string direction, string @event,
|
||||
object request, object response, long durationMs, string remark)
|
||||
{
|
||||
try
|
||||
{
|
||||
var log = new AgvTaskLog
|
||||
{
|
||||
TaskNo = taskNo,
|
||||
OrderId = taskNo,
|
||||
Direction = direction,
|
||||
Event = @event,
|
||||
DurationMs = (int)durationMs,
|
||||
RequestJson = request == null ? null : JsonConvert.SerializeObject(request),
|
||||
ResponseJson = response == null ? null : JsonConvert.SerializeObject(response),
|
||||
Operator = "AGV",
|
||||
Remark = remark,
|
||||
CreateTime = DateTime.Now,
|
||||
CreateBy = "AGV"
|
||||
};
|
||||
_unitWork.Add(log);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 留痕失败不能阻断主流程,仅记录日志
|
||||
_logger.LogWarning(ex, "AGV报文留痕失败 taskNo={taskNo}", taskNo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AGV 接口统一响应包装(code 非 "0" 即失败,msg 为失败原因)
|
||||
/// </summary>
|
||||
public class AgvApiResponse<T>
|
||||
{
|
||||
public string Code { get; set; }
|
||||
public string Msg { get; set; }
|
||||
public T Data { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下发创建运单请求(方案 11.2 报文)
|
||||
/// </summary>
|
||||
public class AgvCreateOrderRequest
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string SceneId { get; set; }
|
||||
public string SceneName { get; set; }
|
||||
public string ExternalId { get; set; }
|
||||
public int? Priority { get; set; }
|
||||
public string ContainerId { get; set; }
|
||||
public bool StepFixed { get; set; } = true;
|
||||
public System.Collections.Generic.List<AgvOrderStep> Steps { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下发步骤(起点取货 Load / 终点放货 Unload)
|
||||
/// </summary>
|
||||
public class AgvOrderStep
|
||||
{
|
||||
public string Location { get; set; }
|
||||
public AgvStepRbkArgs RbkArgs { get; set; }
|
||||
public bool ForLoad { get; set; }
|
||||
public bool ForUnload { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 步骤动作参数(binTask 取货/放货动作名)
|
||||
/// </summary>
|
||||
public class AgvStepRbkArgs
|
||||
{
|
||||
public string BinTask { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下发成功响应(orderId = TaskNo,无需落库,仅日志留痕)
|
||||
/// </summary>
|
||||
public class AgvCreateOrderResult
|
||||
{
|
||||
public string OrderId { get; set; }
|
||||
public string ExternalId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消/查询请求(sceneId 与 orderId 均必填)
|
||||
/// </summary>
|
||||
public class AgvCancelOrderRequest
|
||||
{
|
||||
public string SceneId { get; set; }
|
||||
public string OrderId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 运单状态查询结果(11.3 查询式兜底用;字段待厂商确认)
|
||||
/// </summary>
|
||||
public class AgvOrderStatusResult
|
||||
{
|
||||
public string OrderId { get; set; }
|
||||
public string Status { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace HHWMS.Web.Services.Agv
|
||||
{
|
||||
/// <summary>
|
||||
/// AGV 接单请求 DTO(方案 11.1;与手动下发页面参数保持一致)
|
||||
/// </summary>
|
||||
public class AgvReceiveRequest
|
||||
{
|
||||
/// <summary>上游任务号(可选;非空则作为 TaskNo,且按 SourceCode+SourceSystem 幂等)</summary>
|
||||
public string SourceCode { get; set; }
|
||||
|
||||
/// <summary>上游来源系统标识(必填)</summary>
|
||||
public string SourceSystem { get; set; }
|
||||
|
||||
/// <summary>上游任务类型(必填;字符串类型按场景配置 SourceTaskType 匹配,数字枚举 1~6 按 AgvTaskType 兼容)</summary>
|
||||
public string TaskType { get; set; }
|
||||
|
||||
/// <summary>起点库位编码(必填,AGV 点位)</summary>
|
||||
public string FromLocation { get; set; }
|
||||
|
||||
/// <summary>终点库位编码(必填,AGV 点位)</summary>
|
||||
public string ToLocation { get; set; }
|
||||
|
||||
/// <summary>容器号(可选;不给则自动生成 LS 临时容器)</summary>
|
||||
public string ContainerCode { get; set; }
|
||||
|
||||
/// <summary>优先级(可选;默认 0,越大越优先)</summary>
|
||||
public int? Priority { get; set; }
|
||||
|
||||
/// <summary>起点楼层(可选;WMS 不做楼层管控,仅接收不校验)</summary>
|
||||
public int? FromFloor { get; set; }
|
||||
|
||||
/// <summary>终点楼层(可选;WMS 不做楼层管控,仅接收不校验)</summary>
|
||||
public int? ToFloor { get; set; }
|
||||
|
||||
/// <summary>物料明细(可选;缺 SKU 按空容器业务处理)</summary>
|
||||
public List<AgvReceiveDetail> Details { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接单物料明细(SKU,可不用但不能没有)
|
||||
/// </summary>
|
||||
public class AgvReceiveDetail
|
||||
{
|
||||
/// <summary>物料编码(本地不存在时调 ERP LoadInventory 补建)</summary>
|
||||
public string MaterialCode { get; set; }
|
||||
|
||||
/// <summary>数量</summary>
|
||||
public decimal? Qty { get; set; }
|
||||
|
||||
/// <summary>批次(入库报工 FinishIn 的 StepCode 来源,可空)</summary>
|
||||
public string Batch { get; set; }
|
||||
|
||||
/// <summary>ERP 生产订单明细 ID(可空;入库报工 FinishIn 的 MoDetailId 来源)</summary>
|
||||
public string ErpDetailId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AGV 回调请求 DTO(方案 11.3)
|
||||
/// </summary>
|
||||
public class AgvCallbackRequest
|
||||
{
|
||||
/// <summary>WMS 任务号(=AGV 侧 orderId,必填)</summary>
|
||||
public string TaskNo { get; set; }
|
||||
|
||||
/// <summary>事件(Accepted/ArrivedLoad/Loaded/ArrivedUnload/Unloaded/Error/Cancel,必填)</summary>
|
||||
public string Event { get; set; }
|
||||
|
||||
/// <summary>事件发生时间</summary>
|
||||
public DateTime? EventTime { get; set; }
|
||||
|
||||
/// <summary>容器号(与任务比对,不一致告警)</summary>
|
||||
public string ContainerCode { get; set; }
|
||||
|
||||
/// <summary>实际卸货库位(Unloaded 必填,完成联动以该值落位)</summary>
|
||||
public string ActualLocation { get; set; }
|
||||
|
||||
/// <summary>AGV 小车编号(可选)</summary>
|
||||
public string AgvCarNo { get; set; }
|
||||
|
||||
/// <summary>错误码(异常/取消时)</summary>
|
||||
public string ErrorCode { get; set; }
|
||||
|
||||
/// <summary>错误信息(异常/取消时)</summary>
|
||||
public string ErrorMsg { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AGV 业务异常(带错误码,Controller 层转 ApiResultError)
|
||||
/// </summary>
|
||||
public class AgvBizException : Exception
|
||||
{
|
||||
/// <summary>错误码:param/notfound/status/lock/error</summary>
|
||||
public string ErrorCode { get; }
|
||||
|
||||
public AgvBizException(string errorCode, string message) : base(message)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,179 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
using WalkingTec.Mvvm.Core.Extensions;
|
||||
using HHWMS.Web.Models;
|
||||
|
||||
namespace HHWMS.Web.Services.Erp
|
||||
{
|
||||
/// <summary>
|
||||
/// 物料查询服务:本地物料表优先,查不到再调用 ERP LoadInventory,并把 ERP 结果回写本地
|
||||
/// 物料编码对应 ERP 字段 InvCode,图号对应 ERP 字段 InvDefine1
|
||||
/// </summary>
|
||||
public class ErpMaterialService
|
||||
{
|
||||
private const string ErpCode = "003";
|
||||
private const int ErpSize = 50;
|
||||
private const int ErpTimeoutSeconds = 15;
|
||||
|
||||
private readonly IUnitWork _unitWork;
|
||||
private readonly WTMContext _wtm;
|
||||
|
||||
public ErpMaterialService(IUnitWork unitWork, WTMContext wtm)
|
||||
{
|
||||
_unitWork = unitWork;
|
||||
_wtm = wtm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地是否有可用物料:物料编码命中且物料编码、图号都不为空/空格(否则视为没查到,需要调 ERP 补全)
|
||||
/// </summary>
|
||||
public bool ExistsLocalByCode(string keyword)
|
||||
{
|
||||
keyword = keyword?.Trim();
|
||||
return !string.IsNullOrWhiteSpace(keyword)
|
||||
&& _unitWork.Find<Material>(x => x.MaterialCode.Contains(keyword)
|
||||
&& x.MaterialCode != null && x.MaterialCode.Trim() != ""
|
||||
&& x.MaterialType != null && x.MaterialType.Trim() != "")
|
||||
.Any();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地是否有可用物料:图号命中且图号、物料编码都不为空/空格(否则视为没查到,需要调 ERP 补全)
|
||||
/// </summary>
|
||||
public bool ExistsLocalByType(string keyword)
|
||||
{
|
||||
keyword = keyword?.Trim();
|
||||
return !string.IsNullOrWhiteSpace(keyword)
|
||||
&& _unitWork.Find<Material>(x => x.MaterialType.Contains(keyword)
|
||||
&& x.MaterialType != null && x.MaterialType.Trim() != ""
|
||||
&& x.MaterialCode != null && x.MaterialCode.Trim() != "")
|
||||
.Any();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列表搜索前置同步:物料编码本地查不到时才调 ERP(InvCode)并回写,不返回数据,列表随后统一查询
|
||||
/// </summary>
|
||||
public async Task EnsureByCodeAsync(string keyword)
|
||||
{
|
||||
keyword = keyword?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(keyword) || ExistsLocalByCode(keyword))
|
||||
{
|
||||
return;
|
||||
}
|
||||
SaveItems(await QueryErpByCodeAsync(keyword));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列表搜索前置同步:图号本地查不到时才调 ERP(InvDefine1)并回写
|
||||
/// </summary>
|
||||
public async Task EnsureByTypeAsync(string keyword)
|
||||
{
|
||||
keyword = keyword?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(keyword) || ExistsLocalByType(keyword))
|
||||
{
|
||||
return;
|
||||
}
|
||||
SaveItems(await QueryErpByTypeAsync(keyword));
|
||||
}
|
||||
|
||||
/// <summary>按物料编码调 ERP(InvCode)</summary>
|
||||
private Task<List<InventoryItem>> QueryErpByCodeAsync(string keyword)
|
||||
{
|
||||
return QueryErpAsync(new LoadInventoryRequest { InvCode = keyword });
|
||||
}
|
||||
|
||||
/// <summary>按图号调 ERP(InvDefine1)</summary>
|
||||
private Task<List<InventoryItem>> QueryErpByTypeAsync(string keyword)
|
||||
{
|
||||
return QueryErpAsync(new LoadInventoryRequest { InvDefine1 = keyword });
|
||||
}
|
||||
|
||||
/// <summary>调用 ERP LoadInventory;异常或返回非成功时返回空列表,不影响页面搜索</summary>
|
||||
private async Task<List<InventoryItem>> QueryErpAsync(LoadInventoryRequest request)
|
||||
{
|
||||
request.ErpCode = ErpCode;
|
||||
request.Size = ErpSize;
|
||||
request.Page = 1;
|
||||
try
|
||||
{
|
||||
var apiResult = await _wtm.CallAPI<LoadInventoryResponse>("ERP", "LoadInventory",
|
||||
HttpMethodEnum.POST, request, ErpTimeoutSeconds);
|
||||
var data = apiResult?.Data;
|
||||
if (data == null || data.Code != 0)
|
||||
{
|
||||
_wtm.DoLog("ERP物料查询: 返回异常", ActionLogTypesEnum.Normal, "物料",
|
||||
request: $"InvCode={request.InvCode}, InvDefine1={request.InvDefine1}",
|
||||
result: $"Code={data?.Code}, Msg={data?.Msg ?? apiResult?.ErrorMsg}");
|
||||
return new List<InventoryItem>();
|
||||
}
|
||||
|
||||
return data.Data?.Where(x => !string.IsNullOrWhiteSpace(x.InvCode)).ToList()
|
||||
?? new List<InventoryItem>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_wtm.DoLog("ERP物料查询: 调用失败", ActionLogTypesEnum.Normal, "物料",
|
||||
request: $"InvCode={request.InvCode}, InvDefine1={request.InvDefine1}",
|
||||
result: ex.Message);
|
||||
return new List<InventoryItem>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>将 ERP 物料回写本地:按物料编码更新,没有则新增</summary>
|
||||
private void SaveItems(List<InventoryItem> items)
|
||||
{
|
||||
if (items == null || items.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var codes = items.Select(x => x.InvCode).ToList();
|
||||
var existing = _unitWork.Find<Material>(x => codes.Contains(x.MaterialCode)).ToList();
|
||||
var adds = new List<Material>();
|
||||
var updates = new List<Material>();
|
||||
var operatorCode = _wtm.LoginUserInfo?.ITCode ?? "System";
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var local = existing.FirstOrDefault(x => x.MaterialCode == item.InvCode);
|
||||
if (local == null)
|
||||
{
|
||||
adds.Add(new Material
|
||||
{
|
||||
MaterialCode = item.InvCode,
|
||||
MaterialName = item.InvName,
|
||||
MaterialSpec = item.InvStd,
|
||||
MaterialUnit = item.UnitName,
|
||||
MaterialType = item.InvDefine1,
|
||||
SupplierCode = item.InvDefine4,
|
||||
Qty = 0,
|
||||
CreateTime = DateTime.Now,
|
||||
CreateBy = operatorCode
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
local.MaterialName = item.InvName;
|
||||
local.MaterialSpec = item.InvStd;
|
||||
local.MaterialUnit = item.UnitName;
|
||||
local.MaterialType = item.InvDefine1;
|
||||
local.SupplierCode = item.InvDefine4;
|
||||
local.UpdateTime = DateTime.Now;
|
||||
local.UpdateBy = operatorCode;
|
||||
updates.Add(local);
|
||||
}
|
||||
|
||||
if (adds.Count > 0)
|
||||
{
|
||||
_unitWork.BatchAdd(adds);
|
||||
}
|
||||
if (updates.Count > 0)
|
||||
{
|
||||
_unitWork.BatchUpdate(updates);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user