1
z8018
2025-06-10 e46aa927d231af83724683c7286d9db503e24cf7
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
{
    /// <summary>
    /// 输送线
    /// </summary>
    /// <remarks>
    /// 1. 实现IJob接口,作为Quartz.NET作业 <br/>
    /// 2. 使用[DisallowConcurrentExecution]特性防止并发执行 <br/>
    /// 3. 主要功能: <br/>
    ///    - 处理PLC请求信号 <br/>
    ///    - 根据条码查询或生成任务 <br/>
    ///    - 设置WCS响应参数 <br/>
    ///    - 异常处理
    /// </remarks>
    [DisallowConcurrentExecution]
    public class ConveyorLineOutJob : JobBase, IJob
    {
        /// <summary>
        /// 任务服务接口实例,用于处理与任务相关的操作
        /// </summary>
        private readonly ITaskService _taskService;
 
        /// <summary>
        /// AutoMapper 对象映射器实例
        /// </summary>
        private readonly IMapper _mapper;
 
        /// <summary>
        /// 订单明细服务接口
        /// </summary>
        private readonly IOrderDetailsService _orderDetailsService;
 
        /// <summary>
        /// 任务仓储接口实例,用于操作任务数据
        /// </summary>
        private readonly ITaskRepository _taskRepository;
 
        /// <summary>
        /// 容器仓储接口实例,用于容器相关数据操作
        /// </summary>
        private readonly IContainerRepository _containerRepository;
 
        /// <summary>
        /// 静态容器实例,用于存放排出容器信息
        /// </summary>
        private static Dt_Container DischargeContainer;
 
        /// <summary>
        /// 排出站编号
        /// </summary>
        private static int dischargeStation = 8;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="taskService">任务服务</param>
        /// <param name="mapper">对象映射器</param>
        /// <param name="orderDetailsService">订单详情服务</param>
        /// <param name="taskRepository">任务仓储</param>
        /// <param name="containerRepository">容器仓储</param>
        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, bool>(ConveyorLineStationDBName.PLCStationRequest);   //申请
                    bool response = otherDevice.GetValue<ConveyorLineStationDBName, bool>(ConveyorLineStationDBName.PLCStationResponse);    //应答
                    bool wcsResponse = otherDevice.GetValue<ConveyorLineStationDBName, bool>(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, string>(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<Dt_Container> containers = _containerRepository.Db.Queryable<Dt_Container>().Where(x => x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt() && x.ContainerEnable).Includes(x => x.Items).ToList();
                                List<Dt_OrderContainer> orderContainers = _taskRepository.Db.Queryable<Dt_OrderContainer>().ToList();
                                List<Dt_Task> tasks = _taskRepository.Db.Queryable<Dt_Task>().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;
        }
    }
}