WMS/HHWMS.Web/Areas/System/Controllers/jobs/AgvTaskArchiveJob.cs
sunwang c76e7bb681 fix:永丰售后修复BUG,
1,物料库存获取不到ERP:重新调整触发查询逻辑
2,工序变更导致物料无法出库:配置旧工序
3,测试出库订单ERP推送,任务下发
2026-10-29 13:12:11 +08:00

169 lines
6.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}
}
}