wanshenmean
2026-02-11 88724eca51a5863f20ee0a552af741aeebf4e8f2
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#region << 版 本 注 释 >>
 
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 *
 *----------------------------------------------------------------*/
 
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using Quartz;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonConveyorLineNewJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IMapper _mapper;
        ConveyorLineDispatchHandler _conveyorLineDispatch;
 
        public CommonConveyorLineNewJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
            _conveyorLineDispatch = new ConveyorLineDispatchHandler(_taskService, _taskExecuteDetailService, _routerService, _mapper);
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    if (childDeviceCodes == null || childDeviceCodes.Count == 0)
                    {
                        Console.WriteLine($"输送线 {conveyorLine.DeviceCode} 没有子设备");
                        return Task.CompletedTask;
                    }
 
                    // 创建并行选项
                    var parallelOptions = new ParallelOptions
                    {
                        MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2), // 合理限制并发数
                    };
                    Parallel.For(0, childDeviceCodes.Count, parallelOptions, i =>
                    {
                        string childDeviceCode = childDeviceCodes[i];
                        var correlationId = Guid.NewGuid().ToString("N");
                        try
                        {
                            ConveyorLineTaskCommandNew command = conveyorLine.ReadCustomer<ConveyorLineTaskCommandNew>(childDeviceCode);
                            if (command == null || command.PLC_STB == 0)
                            {
                                return;
                            }
 
                            if (command.Barcode.IsNullOrEmpty())
                            {
                                //无托盘号时
                                _conveyorLineDispatch.RequestOutbound(conveyorLine, command, childDeviceCode);
                                return;
                            }
 
                            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
                            if (task.IsNullOrEmpty())
                            {
                                _conveyorLineDispatch.RequestInbound(conveyorLine, command, childDeviceCode);
                                return;
                            }
 
                            // 处理任务状态
                            ProcessTaskState(conveyorLine, command, task, childDeviceCode);
                        }
                        catch (Exception innerEx)
                        {
                            Console.Error.WriteLine($"{DateTime.UtcNow:O} [{childDeviceCode}] CorrelationId={correlationId} {innerEx}");
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
            return Task.CompletedTask;
        }
 
 
        /// <summary>
        /// 处理任务状态
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="task">子设备编号</param>
        /// <param name="childDeviceCode"></param>
        private void ProcessTaskState(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, Dt_Task task, string childDeviceCode)
        {
            // 定义状态常量(如果类中已定义则可移除)
            const int InExecuting = (int)TaskInStatusEnum.Line_InExecuting;
            const int OutExecuting = (int)TaskOutStatusEnum.Line_OutExecuting;
            const int InFinish = (int)TaskInStatusEnum.InFinish;
            const int OutFinish = (int)TaskOutStatusEnum.OutFinish;
 
            int state = task.TaskState;
            bool isTargetAddress = task.TargetAddress == childDeviceCode;
 
            // 处理状态逻辑
            switch (state)
            {
                case InExecuting:
                    if (isTargetAddress)
                        _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
                    else
                        _conveyorLineDispatch.RequestInNextAddress(conveyorLine, command, childDeviceCode);
                    break;
 
                case OutExecuting:
                    if (isTargetAddress)
                        _conveyorLineDispatch.ConveyorLineOutFinish(conveyorLine, command, childDeviceCode);
                    else
                        _conveyorLineDispatch.RequestOutNextAddress(conveyorLine, command, childDeviceCode);
                    break;
 
                case InFinish:
                    _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
                    break;
 
                case OutFinish:
                    _conveyorLineDispatch.ConveyorLineOutFinish(conveyorLine, command, childDeviceCode);
                    break;
            }
        }
 
        
    }
}