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,168 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using HHWMS.Web.Models;
|
||||
using HHWMS.Web.Models.Agv;
|
||||
using Quartz;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
using WalkingTec.Mvvm.Core.Extensions;
|
||||
using WalkingTec.Mvvm.Core.Support.Quartz;
|
||||
|
||||
namespace HHWMS.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// AGV 任务归档 Job(方案 5.5 / 9.6,每日执行):
|
||||
/// 1) AgvTask 超 1 个月终态任务(99/95/100)先镜像备份到 AgvTaskHistory,再从主表物理清除;
|
||||
/// 2) AgvTaskLog 超 6 个月日志镜像备份到 AgvTaskLogHistory 后清除。
|
||||
/// 事务包裹防重复归档;每批限量处理,失败回滚下次重跑天然重试。
|
||||
/// </summary>
|
||||
[PersistJobDataAfterExecution]
|
||||
[DisallowConcurrentExecution]
|
||||
public class AgvTaskArchiveJob : WtmJob
|
||||
{
|
||||
public override async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var unitWork = new UnitWork(Wtm);
|
||||
var now = DateTime.Now;
|
||||
const int batchSize = 200;
|
||||
|
||||
await ArchiveTasksAsync(unitWork, now, batchSize);
|
||||
await ArchiveLogsAsync(unitWork, now, batchSize);
|
||||
|
||||
// 更新 SysJob 触发时间
|
||||
var job = unitWork.FindSingle<SysJob>(x => x.JobName == context.JobDetail.Key.Name);
|
||||
if (job != null)
|
||||
{
|
||||
job.LastFireTime = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
context.FireTimeUtc.DateTime,
|
||||
TimeZoneInfo.Local);
|
||||
if (context.NextFireTimeUtc != null)
|
||||
{
|
||||
job.NextFireTime = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
context.NextFireTimeUtc.Value.DateTime,
|
||||
TimeZoneInfo.Local);
|
||||
}
|
||||
unitWork.Update(job);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Wtm.DoLog("AGV任务归档失败", ActionLogTypesEnum.Job,
|
||||
"AgvTaskArchiveJob", request: "error", result: ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 任务归档:超 1 个月终态任务镜像到 AgvTaskHistory 后物理删除主表记录
|
||||
/// </summary>
|
||||
private async Task ArchiveTasksAsync(UnitWork unitWork, DateTime now, int batchSize)
|
||||
{
|
||||
var tasks = unitWork.Find<AgvTask>(x =>
|
||||
x.CreateTime != null
|
||||
&& x.CreateTime < now.AddMonths(-1)
|
||||
&& (x.AgvStatus == AgvTaskStatusEnum.完成
|
||||
|| x.AgvStatus == AgvTaskStatusEnum.取消
|
||||
|| x.AgvStatus == AgvTaskStatusEnum.完成联动失败))
|
||||
.OrderBy(x => x.CreateTime)
|
||||
.Take(batchSize)
|
||||
.ToList();
|
||||
if (tasks.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (var transaction = Wtm.DC.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var histories = tasks.Select(t => new AgvTaskHistory
|
||||
{
|
||||
TaskNo = t.TaskNo,
|
||||
SourceCode = t.SourceCode,
|
||||
SourceSystem = t.SourceSystem,
|
||||
AgvTaskType = t.AgvTaskType,
|
||||
FromLocation = t.FromLocation,
|
||||
ToLocation = t.ToLocation,
|
||||
FromFloor = t.FromFloor,
|
||||
ToFloor = t.ToFloor,
|
||||
ContainerCode = t.ContainerCode,
|
||||
Priority = t.Priority,
|
||||
AgvStatus = t.AgvStatus,
|
||||
AgvCarNo = t.AgvCarNo,
|
||||
ErrorMsg = t.ErrorMsg,
|
||||
BillType = t.BillType,
|
||||
Details = t.Details,
|
||||
ArchivedAt = now,
|
||||
CreateTime = t.CreateTime,
|
||||
CreateBy = t.CreateBy,
|
||||
UpdateTime = t.UpdateTime,
|
||||
UpdateBy = t.UpdateBy
|
||||
}).ToList();
|
||||
unitWork.BatchAdd(histories);
|
||||
unitWork.BatchDelete(tasks);
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日志归档:超 6 个月日志镜像到 AgvTaskLogHistory 后物理删除
|
||||
/// </summary>
|
||||
private async Task ArchiveLogsAsync(UnitWork unitWork, DateTime now, int batchSize)
|
||||
{
|
||||
var logs = unitWork.Find<AgvTaskLog>(x =>
|
||||
x.CreateTime != null
|
||||
&& x.CreateTime < now.AddMonths(-6))
|
||||
.OrderBy(x => x.CreateTime)
|
||||
.Take(batchSize)
|
||||
.ToList();
|
||||
if (logs.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (var transaction = Wtm.DC.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var histories = logs.Select(l => new AgvTaskLogHistory
|
||||
{
|
||||
TaskNo = l.TaskNo,
|
||||
OrderId = l.OrderId,
|
||||
FromStatus = l.FromStatus,
|
||||
ToStatus = l.ToStatus,
|
||||
Event = l.Event,
|
||||
Direction = l.Direction,
|
||||
EventTime = l.EventTime,
|
||||
SourceIp = l.SourceIp,
|
||||
DurationMs = l.DurationMs,
|
||||
RequestJson = l.RequestJson,
|
||||
ResponseJson = l.ResponseJson,
|
||||
Operator = l.Operator,
|
||||
Remark = l.Remark,
|
||||
ArchivedAt = now,
|
||||
CreateTime = l.CreateTime,
|
||||
CreateBy = l.CreateBy,
|
||||
UpdateTime = l.UpdateTime,
|
||||
UpdateBy = l.UpdateBy
|
||||
}).ToList();
|
||||
unitWork.BatchAdd(histories);
|
||||
unitWork.BatchDelete(logs);
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using HHWMS.Web.Models;
|
||||
using HHWMS.Web.Models.Agv;
|
||||
using HHWMS.Web.Services.Agv;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Quartz;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
using WalkingTec.Mvvm.Core.Extensions;
|
||||
using WalkingTec.Mvvm.Core.Support.Quartz;
|
||||
namespace HHWMS.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// AGV 定时下发 Job:周期扫描 新建(0)/异常(90) 任务自动下发。
|
||||
/// 启停方式:在【系统管理→定时任务】页面新增本条记录,Cron 建议 0 */2 * * * ?(每 2 分钟)。
|
||||
/// 依赖 DispatchTaskAsync 内部的 FOR UPDATE 行锁 + 状态校验,天然防并发/防重复下发。
|
||||
/// 日志双通道:Console 直写控制台(现场直跑 exe 可直接观察是否触发),Wtm.DoLog 写 ActionLogs 表。
|
||||
/// </summary>
|
||||
[PersistJobDataAfterExecution]
|
||||
[DisallowConcurrentExecution]
|
||||
public class AgvTaskDispatchJob : WtmJob
|
||||
{
|
||||
// 无参构造 —— WtmJob 反射创建 Job 实例必须有无参构造
|
||||
public AgvTaskDispatchJob() { }
|
||||
|
||||
public override async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
// 每次触发必输出心跳行:用于区分「调度没触发」和「触发了但没任务」
|
||||
WriteLog($"Job触发,计划时间 {Fmt(context.ScheduledFireTimeUtc)},下次触发 {Fmt(context.NextFireTimeUtc)}");
|
||||
try
|
||||
{
|
||||
var sp = context.JobDetail.JobDataMap.Get("Sp") as IServiceProvider;
|
||||
if (sp == null)
|
||||
{
|
||||
WriteLog("错误:未能从 JobDataMap 获取 IServiceProvider");
|
||||
DoJobLog("AGV定时下发Job异常", "error", "未能从 JobDataMap 获取 IServiceProvider");
|
||||
return;
|
||||
}
|
||||
// AgvTaskService 为 Scoped 服务,root provider 直接解析会抛
|
||||
// Cannot resolve scoped service(ValidateScopes),必须创建独立作用域
|
||||
using (var scope = sp.CreateScope())
|
||||
{
|
||||
var service = scope.ServiceProvider.GetService(typeof(AgvTaskService)) as AgvTaskService;
|
||||
if (service == null)
|
||||
{
|
||||
WriteLog("错误:DI 容器中未注册 AgvTaskService");
|
||||
DoJobLog("AGV定时下发Job异常", "error", "DI 容器中未注册 AgvTaskService");
|
||||
return;
|
||||
}
|
||||
WriteLog("依赖解析成功:IServiceScope / AgvTaskService 均可用");
|
||||
await DispatchBatch(context, service);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 输出完整异常堆栈到控制台,避免只有 Message 无法定位
|
||||
WriteLog($"Job执行异常:{ex}");
|
||||
DoJobLog("AGV定时下发Job异常", "error", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描待下发任务并逐条下发,输出每条结果与汇总
|
||||
/// </summary>
|
||||
private async Task DispatchBatch(IJobExecutionContext context, AgvTaskService service)
|
||||
{
|
||||
var unitWork = new UnitWork(Wtm);
|
||||
const int batchSize = 20;
|
||||
|
||||
var tasks = unitWork.Find<AgvTask>(x =>
|
||||
x.AgvStatus == AgvTaskStatusEnum.新建
|
||||
|| x.AgvStatus == AgvTaskStatusEnum.异常)
|
||||
.OrderByDescending(x => x.Priority)
|
||||
.ThenBy(x => x.CreateTime)
|
||||
.Take(batchSize)
|
||||
.ToList();
|
||||
|
||||
WriteLog($"扫描完成:待下发任务 {tasks.Count} 条(状态=新建/异常,批量上限 {batchSize})"
|
||||
+ (tasks.Count > 0 ? ",任务号:" + string.Join(",", tasks.Select(t => t.TaskNo)) : ""));
|
||||
|
||||
if (tasks.Count == 0)
|
||||
{
|
||||
UpdateJobFireTime(unitWork, context);
|
||||
return;
|
||||
}
|
||||
|
||||
int success = 0, fail = 0;
|
||||
foreach (var task in tasks)
|
||||
{
|
||||
try
|
||||
{
|
||||
await service.DispatchTaskAsync(task.TaskNo);
|
||||
success++;
|
||||
WriteLog($"下发成功:{task.TaskNo}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
fail++;
|
||||
WriteLog($"下发失败:{task.TaskNo},原因:{ex.Message}");
|
||||
DoJobLog($"AGV定时下发失败:{task.TaskNo}", task.TaskNo, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
WriteLog($"本轮执行结束:共 {tasks.Count} 条,成功 {success},失败 {fail}");
|
||||
if (success > 0 || fail > 0)
|
||||
{
|
||||
DoJobLog("AGV定时下发", $"batch={tasks.Count}", $"ok={success},fail={fail}");
|
||||
}
|
||||
|
||||
UpdateJobFireTime(unitWork, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 SysJob 上 LastFireTime / NextFireTime;内部异常仅记日志,不影响下发主流程
|
||||
/// </summary>
|
||||
private static void UpdateJobFireTime(UnitWork unitWork, IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var job = unitWork.FindSingle<SysJob>(x => x.JobName == context.JobDetail.Key.Name);
|
||||
if (job == null)
|
||||
{
|
||||
WriteLog($"提示:SysJob 表中未找到 JobName={context.JobDetail.Key.Name} 的记录,LastFireTime 未更新");
|
||||
return;
|
||||
}
|
||||
job.LastFireTime = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
context.FireTimeUtc.DateTime, TimeZoneInfo.Local);
|
||||
if (context.NextFireTimeUtc != null)
|
||||
{
|
||||
job.NextFireTime = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local);
|
||||
}
|
||||
unitWork.Update(job);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteLog($"更新 LastFireTime 失败(不影响下发):{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写 ActionLogs 日志;Wtm 不可用时静默跳过(控制台日志已兜底)
|
||||
/// </summary>
|
||||
private void DoJobLog(string title, string request, string result)
|
||||
{
|
||||
try
|
||||
{
|
||||
Wtm.DoLog(title, ActionLogTypesEnum.Job, "AgvTaskDispatchJob", request: request, result: result);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Wtm 为 null 等场景忽略,控制台日志已兜底
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 控制台直出日志(带时间戳),用于确认 Job 是否真实触发
|
||||
/// </summary>
|
||||
private static void WriteLog(string message)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [AgvTaskDispatchJob] {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可空时间格式化(本地时间),空值显示 -
|
||||
/// </summary>
|
||||
private static string Fmt(DateTimeOffset? time)
|
||||
{
|
||||
return time.HasValue ? time.Value.LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using HHWMS.Web.Models;
|
||||
using HHWMS.Web.Models.Agv;
|
||||
using HHWMS.Web.Services.Agv;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Quartz;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
using WalkingTec.Mvvm.Core.Extensions;
|
||||
using WalkingTec.Mvvm.Core.Support.Quartz;
|
||||
|
||||
namespace HHWMS.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// AGV 完成联动失败自动重试 Job:周期扫描 99(完成联动失败)任务自动重试完成联动。
|
||||
/// 启停方式:在【系统管理→定时任务】页面新增本条记录,Cron 建议 0 */5 * * * ?(每 5 分钟)。
|
||||
/// 注意:完成联动失败时库存/流水/容器/库位已回滚,ERP 报工可能已成功,重试时 ERP 侧需保证幂等性。
|
||||
/// 日志双通道:Console 直写控制台,Wtm.DoLog 写 ActionLogs 表。
|
||||
/// </summary>
|
||||
[PersistJobDataAfterExecution]
|
||||
[DisallowConcurrentExecution]
|
||||
public class AgvTaskRetryFinishJob : WtmJob
|
||||
{
|
||||
// 无参构造 —— WtmJob 反射创建 Job 实例必须有无参构造
|
||||
public AgvTaskRetryFinishJob() { }
|
||||
|
||||
public override async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
WriteLog($"Job触发,计划时间 {Fmt(context.ScheduledFireTimeUtc)},下次触发 {Fmt(context.NextFireTimeUtc)}");
|
||||
try
|
||||
{
|
||||
var sp = context.JobDetail.JobDataMap.Get("Sp") as IServiceProvider;
|
||||
if (sp == null)
|
||||
{
|
||||
WriteLog("错误:未能从 JobDataMap 获取 IServiceProvider");
|
||||
DoJobLog("AGV完成联动重试Job异常", "error", "未能从 JobDataMap 获取 IServiceProvider");
|
||||
return;
|
||||
}
|
||||
using (var scope = sp.CreateScope())
|
||||
{
|
||||
var service = scope.ServiceProvider.GetService(typeof(AgvTaskService)) as AgvTaskService;
|
||||
if (service == null)
|
||||
{
|
||||
WriteLog("错误:DI 容器中未注册 AgvTaskService");
|
||||
DoJobLog("AGV完成联动重试Job异常", "error", "DI 容器中未注册 AgvTaskService");
|
||||
return;
|
||||
}
|
||||
WriteLog("依赖解析成功:IServiceScope / AgvTaskService 均可用");
|
||||
await RetryBatch(context, service);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteLog($"Job执行异常:{ex}");
|
||||
DoJobLog("AGV完成联动重试Job异常", "error", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描完成联动失败任务并逐条重试完成联动,输出每条结果与汇总
|
||||
/// </summary>
|
||||
private async Task RetryBatch(IJobExecutionContext context, AgvTaskService service)
|
||||
{
|
||||
var unitWork = new UnitWork(Wtm);
|
||||
const int batchSize = 20;
|
||||
|
||||
var tasks = unitWork.Find<AgvTask>(x => x.AgvStatus == AgvTaskStatusEnum.完成联动失败)
|
||||
.OrderByDescending(x => x.Priority)
|
||||
.ThenBy(x => x.CreateTime)
|
||||
.Take(batchSize)
|
||||
.ToList();
|
||||
|
||||
WriteLog($"扫描完成:待重试完成联动任务 {tasks.Count} 条(状态=完成联动失败99,批量上限 {batchSize})"
|
||||
+ (tasks.Count > 0 ? ",任务号:" + string.Join(",", tasks.Select(t => t.TaskNo)) : ""));
|
||||
|
||||
if (tasks.Count == 0)
|
||||
{
|
||||
UpdateJobFireTime(unitWork, context);
|
||||
return;
|
||||
}
|
||||
|
||||
int success = 0, fail = 0;
|
||||
foreach (var task in tasks)
|
||||
{
|
||||
try
|
||||
{
|
||||
await service.RetryFinishLinkAsync(task.TaskNo);
|
||||
success++;
|
||||
WriteLog($"重试完成联动成功:{task.TaskNo}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
fail++;
|
||||
WriteLog($"重试完成联动失败:{task.TaskNo},原因:{ex.Message}");
|
||||
DoJobLog($"AGV重试完成联动失败:{task.TaskNo}", task.TaskNo, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
WriteLog($"本轮执行结束:共 {tasks.Count} 条,成功 {success},失败 {fail}");
|
||||
if (success > 0 || fail > 0)
|
||||
{
|
||||
DoJobLog("AGV完成联动重试", $"batch={tasks.Count}", $"ok={success},fail={fail}");
|
||||
}
|
||||
|
||||
UpdateJobFireTime(unitWork, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 SysJob 上 LastFireTime / NextFireTime;内部异常仅记日志,不影响主流程
|
||||
/// </summary>
|
||||
private static void UpdateJobFireTime(UnitWork unitWork, IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var job = unitWork.FindSingle<SysJob>(x => x.JobName == context.JobDetail.Key.Name);
|
||||
if (job == null)
|
||||
{
|
||||
WriteLog($"提示:SysJob 表中未找到 JobName={context.JobDetail.Key.Name} 的记录,LastFireTime 未更新");
|
||||
return;
|
||||
}
|
||||
job.LastFireTime = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
context.FireTimeUtc.DateTime, TimeZoneInfo.Local);
|
||||
if (context.NextFireTimeUtc != null)
|
||||
{
|
||||
job.NextFireTime = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local);
|
||||
}
|
||||
unitWork.Update(job);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteLog($"更新 LastFireTime 失败(不影响重试):{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写 ActionLogs 日志;Wtm 不可用时静默跳过
|
||||
/// </summary>
|
||||
private void DoJobLog(string title, string request, string result)
|
||||
{
|
||||
try
|
||||
{
|
||||
Wtm.DoLog(title, ActionLogTypesEnum.Job, "AgvTaskRetryFinishJob", request: request, result: result);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Wtm 为 null 等场景忽略,控制台日志已兜底
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 控制台直出日志(带时间戳)
|
||||
/// </summary>
|
||||
private static void WriteLog(string message)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [AgvTaskRetryFinishJob] {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可空时间格式化(本地时间),空值显示 -
|
||||
/// </summary>
|
||||
private static string Fmt(DateTimeOffset? time)
|
||||
{
|
||||
return time.HasValue ? time.Value.LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user