wanshenmean
2026-03-27 bf2aa9dd56432a74940ca1bb08fb4d7eaee37045
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
using Microsoft.Extensions.Logging;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 输送线任务过滤器 - 统一封装任务查询与 WMS 请求
    /// </summary>
    /// <remarks>
    /// 核心职责:
    /// 1. 查询输送线的待处理任务
    /// 2. 查询正在执行的任务
    /// 3. 向 WMS 请求新任务
    ///
    /// 该类作为业务层与任务服务之间的中间层,
    /// 封装了常用的任务查询操作,提供简洁的接口给调用方。
    /// </remarks>
    public class ConveyorLineTaskFilter
    {
        /// <summary>
        /// 任务服务实例
        /// </summary>
        /// <remarks>
        /// 用于访问数据库中的任务数据,以及与 WMS 系统交互。
        /// </remarks>
        private readonly ITaskService _taskService;
 
        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger _logger;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="taskService">任务服务实例</param>
        /// <param name="logger">日志记录器</param>
        public ConveyorLineTaskFilter(ITaskService taskService, ILogger logger)
        {
            _taskService = taskService;
            _logger = logger;
        }
 
        /// <summary>
        /// 查询待处理的输送线任务
        /// </summary>
        /// <remarks>
        /// 根据设备编码和子设备编码查询状态为"待处理"的任务。
        /// 用于当 PLC 请求任务时,WCS 向数据库查询可下发的任务。
        /// </remarks>
        /// <param name="deviceCode">设备编码(主设备)</param>
        /// <param name="childDeviceCode">子设备编码(位置编码)</param>
        /// <returns>待处理的任务对象,如果没有则返回 null</returns>
        public Dt_Task? QueryPendingTask(string deviceCode, string childDeviceCode)
        {
            var task = _taskService.QueryConveyorLineTask(deviceCode, childDeviceCode);
            _logger.LogDebug("QueryPendingTask:设备 {DeviceCode},子设备 {ChildDeviceCode},查询结果: {TaskNum}", deviceCode, childDeviceCode, task?.TaskNum);
            QuartzLogger.Debug($"QueryPendingTask:设备 {deviceCode},子设备 {childDeviceCode},查询结果: {task?.TaskNum}", deviceCode);
            return task;
        }
 
        /// <summary>
        /// 查询正在执行的输送线任务
        /// </summary>
        /// <remarks>
        /// 根据任务号和子设备编码查询状态为"执行中"的任务。
        /// 用于跟踪任务的执行进度。
        /// </remarks>
        /// <param name="taskNo">任务号</param>
        /// <param name="childDeviceCode">子设备编码</param>
        /// <returns>执行中的任务对象,如果没有则返回 null</returns>
        public Dt_Task? QueryExecutingTask(int taskNo, string childDeviceCode)
        {
            var task = _taskService.QueryExecutingConveyorLineTask(taskNo, childDeviceCode);
            _logger.LogDebug("QueryExecutingTask:任务号 {TaskNo},子设备 {ChildDeviceCode},查询结果: {Found}", taskNo, childDeviceCode, task != null);
            QuartzLogger.Debug($"QueryExecutingTask:任务号 {taskNo},子设备 {childDeviceCode},查询结果: {(task != null)}", childDeviceCode);
            return task;
        }
 
        /// <summary>
        /// 向 WMS 请求新任务
        /// </summary>
        /// <remarks>
        /// 当输送线有货物到达时,向 WMS 请求新的任务。
        /// WMS 会根据仓库情况和调度策略返回合适的任务。
        /// </remarks>
        /// <param name="barcode">货物/托盘条码</param>
        /// <param name="childDeviceCode">请求的子设备编码</param>
        /// <returns>请求是否成功</returns>
        public bool RequestWmsTask(string barcode, string childDeviceCode)
        {
            _logger.LogInformation("RequestWmsTask:向WMS请求任务,条码: {Barcode},子设备: {ChildDeviceCode}", barcode, childDeviceCode);
            QuartzLogger.Info($"向WMS请求任务,条码: {barcode}", childDeviceCode);
            var result = _taskService.RequestWMSTask(barcode, childDeviceCode);
            return result.Status;
        }
    }
}