using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; using Quartz; using WIDESEAWCS_Common; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO.BasicInfo; using WIDESEAWCS_IBasicInfoRepository; using WIDESEAWCS_IBasicInfoService; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_Tasks.ConveyorLineJob; namespace WIDESEAWCS_Tasks { /// /// 输送线 /// /// /// 1. 实现IJob接口,作为Quartz.NET作业
/// 2. 使用[DisallowConcurrentExecution]特性防止并发执行
/// 3. 主要功能:
/// - 处理PLC请求信号
/// - 根据条码查询或生成任务
/// - 设置WCS响应参数
/// - 异常处理 ///
[DisallowConcurrentExecution] public class ConveyorLineOutJob : JobBase, IJob { /// /// 任务服务接口实例,用于处理与任务相关的操作 /// private readonly ITaskService _taskService; /// /// AutoMapper 对象映射器实例 /// private readonly IMapper _mapper; /// /// 订单明细服务接口 /// private readonly IOrderDetailsService _orderDetailsService; /// /// 任务仓储接口实例,用于操作任务数据 /// private readonly ITaskRepository _taskRepository; /// /// 容器仓储接口实例,用于容器相关数据操作 /// private readonly IContainerRepository _containerRepository; /// /// 静态容器实例,用于存放排出容器信息 /// private static Dt_Container DischargeContainer; /// /// 排出站编号 /// private static int dischargeStation = 8; /// /// 构造函数 /// /// 任务服务 /// 对象映射器 /// 订单详情服务 /// 任务仓储 /// 容器仓储 public ConveyorLineOutJob(ITaskService taskService, IMapper mapper, IOrderDetailsService orderDetailsService, ITaskRepository taskRepository, IContainerRepository containerRepository) { _taskService = taskService; _mapper = mapper; _orderDetailsService = orderDetailsService; _taskRepository = taskRepository; _containerRepository = containerRepository; } public Task Execute(IJobExecutionContext context) { if (DischargeContainer == null) { DischargeContainer = _containerRepository.QueryFirst(x => x.ContainerEnable && x.ContainerType == ContainerTypeEnum.DischargeContainer.ObjToInt()); if (DischargeContainer != null) { dischargeStation = DischargeContainer.ContainerNo; } } bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); if (flag && value != null && value is OtherDevice otherDevice) { try { bool request = otherDevice.GetValue(ConveyorLineStationDBName.PLCStationRequest); //申请 bool response = otherDevice.GetValue(ConveyorLineStationDBName.PLCStationResponse); //应答 bool wcsResponse = otherDevice.GetValue(ConveyorLineStationDBName.WCSStationResponse); //应答 if (request && !response && !wcsResponse) { DeviceProDTO? devicePro = otherDevice.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == ConveyorLineStationDBName.PLCStationBarcode.ToString()); if (devicePro == null) { WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", $"通讯协议数据未找到{ConveyorLineStationDBName.PLCStationBarcode.ToString()}信息"); return Task.CompletedTask; } string barcode = otherDevice.GetValue(ConveyorLineStationDBName.PLCStationBarcode); if (string.IsNullOrEmpty(barcode)) { return Task.CompletedTask; } Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == barcode); if (task == null) { OrderInfo orderInfo = _orderDetailsService.GetOrderInfoByBarcode(barcode); Task.Run(() => { _orderDetailsService.ToMes(barcode, 3); }); int minWidth = AppSettings.Get("MinWidth").ObjToInt(); int maxWidth = AppSettings.Get("MaxWidth").ObjToInt(); int minLength = AppSettings.Get("MinLength").ObjToInt(); int maxLength = AppSettings.Get("MaxLength").ObjToInt(); bool isNormalOrientation = orderInfo.Width >= minWidth && orderInfo.Width <= maxWidth && orderInfo.Length >= minLength && orderInfo.Length <= maxLength; bool isSwappedOrientation = orderInfo.Length >= minWidth && orderInfo.Length <= maxWidth && orderInfo.Width >= minLength && orderInfo.Width <= maxLength; if (isNormalOrientation || isSwappedOrientation) { var (taskFlag, gTask, message) = _taskService.GenerateTask(orderInfo); if (taskFlag && gTask != null) { task = gTask; } else { WriteDebug($"码垛任务生成", $"生成任务失败: 【{barcode}】{message}"); (taskFlag, gTask, message) = _taskService.GenerateExceptionTask(orderInfo); if (taskFlag && gTask != null) { task = gTask; } } } else { var (taskFlag, gTask, message) = _taskService.GenerateExceptionTask(orderInfo); if (taskFlag && gTask != null) { task = gTask; } } } if (task != null) { if (!string.IsNullOrEmpty(task.ItemInfo)) { string[] itemInfos = task.ItemInfo.Split("*"); if (itemInfos.Length == 3) { otherDevice.SetValue(ConveyorLineStationDBName.WCSStationLength, Convert.ToInt32(itemInfos[0])); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationWidth, Convert.ToInt32(itemInfos[1])); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationHeight, Convert.ToInt32(itemInfos[2])); } } if (task.TargetAddress == dischargeStation.ToString()) { List containers = _containerRepository.Db.Queryable().Where(x => x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt() && x.ContainerEnable).Includes(x => x.Items).ToList(); List orderContainers = _taskRepository.Db.Queryable().ToList(); List tasks = _taskRepository.Db.Queryable().ToList(); WriteDebug($"异常工位任务生成", $"【{barcode}】生成任务成功: 去向【{task.TargetAddress}】{Environment.NewLine}{Environment.NewLine}{containers.Serialize()}{Environment.NewLine}{Environment.NewLine}{orderContainers.Serialize()}{Environment.NewLine}{Environment.NewLine}{tasks.Serialize()}"); } else { WriteDebug($"码垛任务生成", $"【{barcode}】生成任务成功: 去向【{task.TargetAddress}】"); } otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, Convert.ToInt32(task.TargetAddress)); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTaskNum, task.TaskNum); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true); } else { WriteDebug($"码垛任务生成", $"【{barcode}】生成任务失败: 排到8号工位"); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, dischargeStation); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTaskNum, 998); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true); } } } catch (Exception ex) { otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, dischargeStation); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTaskNum, 999); otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true); WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", ex.Message); } } else { WriteError(nameof(CommonConveyorLineOutJob), "参数错误,未传递设备参数或设备类型错误"); } return Task.CompletedTask; } } }