wanshenmean
9 小时以前 ad64840cc04dac2278ca02f22ddc02b1a218e9cf
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics.CodeAnalysis;
using WIDESEAWCS_Common.Constants;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 堆垛机命令构建器 - 封装任务到命令对象的转换与地址解析
    /// </summary>
    /// <remarks>
    /// 核心职责:
    /// 1. 根据巷道类型选择命令格式(标准命令/成型命令)
    /// 2. 构建入库、出库、移库任务的命令对象
    /// 3. 解析任务地址(行-列-层格式)到命令坐标
    /// 4. 查询路由信息,确定堆垛机的取货/放货站台
    ///
    /// 地址格式约定:地址字符串格式为 "行-列-层",例如 "1-2-3" 表示第1行、第2列、第3层。
    /// </remarks>
    public class StackerCraneCommandBuilder
    {
        /// <summary>
        /// 任务服务
        /// </summary>
        private readonly ITaskService _taskService;
 
        /// <summary>
        /// 路由服务
        /// </summary>
        private readonly IRouterService _routerService;
 
        /// <summary>
        /// 堆垛机命令配置
        /// </summary>
        private readonly StackerCraneCommandConfig _config;
 
        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger _logger;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="taskService">任务服务</param>
        /// <param name="routerService">路由服务</param>
        /// <param name="config">命令配置</param>
        /// <param name="logger">日志记录器</param>
        public StackerCraneCommandBuilder(
            ITaskService taskService,
            IRouterService routerService,
            StackerCraneCommandConfig config,
            ILogger logger)
        {
            _taskService = taskService;
            _routerService = routerService;
            _config = config;
            _logger = logger;
        }
 
        /// <summary>
        /// 将任务转换为堆垛机命令
        /// </summary>
        /// <remarks>
        /// 根据巷道类型选择不同的命令构建策略。
        /// </remarks>
        /// <param name="task">任务对象</param>
        /// <returns>堆垛机命令对象,转换失败返回 null</returns>
        public object? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            return  BuildCommand(task, CreateStandardCommand(task));
            // 根据巷道获取命令类型
            //string commandType = GetCommandType(task.Roadway);
 
            //_logger.LogInformation("ConvertToStackerCraneTaskCommand:构建命令,任务号: {TaskNum},巷道: {Roadway},命令类型: {CommandType}", task.TaskNum, task.Roadway, commandType);
            //QuartzLogger.Info($"构建命令,任务号: {task.TaskNum},巷道: {task.Roadway},命令类型: {commandType}", task.Roadway);
 
            //// 根据命令类型调用相应的构建方法
            //return commandType switch
            //{
            //    "Formation" => BuildCommand(task, CreateFormationCommand(task)),  // 成型命令
            //    _ => BuildCommand(task, CreateStandardCommand(task))              // 标准命令
            //};
        }
 
        /// <summary>
        /// 根据巷道获取命令类型
        /// </summary>
        /// <remarks>
        /// 遍历配置中的映射关系,找到匹配的命令类型。
        /// 如果不匹配任何映射,返回默认命令类型。
        /// </remarks>
        /// <param name="roadway">巷道编码</param>
        /// <returns>命令类型(Standard 或 Formation)</returns>
        private string GetCommandType(string roadway)
        {
            foreach (var mapping in _config.RoadwayCommandMapping)
            {
                if (roadway.Contains(mapping.Key))
                {
                    QuartzLogHelper.LogDebug(_logger, "GetCommandType:匹配巷道 {Roadway},命令类型: {CommandType}", $"GetCommandType:匹配巷道 {roadway},命令类型: {mapping.Value}", roadway, roadway, mapping.Value);
                    return mapping.Value;
                }
            }
 
            QuartzLogHelper.LogDebug(_logger, "GetCommandType:巷道 {Roadway} 未匹配,使用默认命令类型: {DefaultType}", $"GetCommandType:巷道 {roadway} 未匹配,使用默认命令类型: {_config.DefaultCommandType}", roadway, roadway, _config.DefaultCommandType);
            return _config.DefaultCommandType;
        }
 
        /// <summary>
        /// 创建标准命令
        /// </summary>
        /// <remarks>
        /// 用于标准堆垛机(GW、CW 开头巷道)。
        /// </remarks>
        /// <param name="task">任务对象</param>
        /// <returns>标准命令对象</returns>
        private static StackerCraneTaskCommand CreateStandardCommand(Dt_Task task)
        {
            return new StackerCraneTaskCommand
            {
                TaskNum = task.TaskNum,   // 任务号
                WorkType = StackerCraneConst.WorkTypeInbound,  // 作业类型:入库
                //WorkAction = StackerCraneWorkActionEnum.StartTask  // 作业指令:开始执行
            };
        }
 
        /// <summary>
        /// 创建成型命令
        /// </summary>
        /// <remarks>
        /// 用于成型堆垛机(HC 开头巷道)。
        /// 包含条码字段,用于电池追溯。
        /// </remarks>
        /// <param name="task">任务对象</param>
        /// <returns>成型命令对象</returns>
        private static FormationStackerCraneTaskCommand CreateFormationCommand(Dt_Task task)
        {
            return new FormationStackerCraneTaskCommand
            {
                Barcode = task.PalletCode,   // 托盘条码
                TaskNum = task.TaskNum,      // 任务号
                WorkType = StackerCraneConst.WorkTypeInbound,  // 作业类型:入库
                WorkAction = (short)StackerCraneWorkActionEnum.StartTask,  // 作业指令:开始执行
                FireAlarm = StackerCraneConst.FireAlarmNormal,  // 火警:正常
                HeartBeat = StackerCraneConst.HeartBeatInitial,  // 心跳
                FieldName = string.Empty     // 保留字段
            };
        }
 
        /// <summary>
        /// 构建命令(通用)
        /// </summary>
        /// <remarks>
        /// 根据任务类型(入库/出库/移库)调用相应的构建方法。
        /// </remarks>
        /// <typeparam name="T">命令类型</typeparam>
        /// <param name="task">任务对象</param>
        /// <param name="command">初始命令对象</param>
        /// <returns>填充好的命令对象</returns>
        private T? BuildCommand<T>(Dt_Task task, T command) where T : class
        {
            // 获取任务类型分组
            TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup();
 
            QuartzLogHelper.LogDebug(_logger, "BuildCommand:任务号: {TaskNum},任务类型分组: {TaskTypeGroup}", $"BuildCommand:任务号: {task.TaskNum},任务类型分组: {taskTypeGroup}", task.Roadway, task.TaskNum, taskTypeGroup);
 
            // 根据任务类型分发构建
            return taskTypeGroup switch
            {
                TaskTypeGroup.InboundGroup => BuildInboundCommand(task, command),    // 入库
                TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, command),  // 出库
                TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, command),  // 移库
                _ => command  // 未知类型,返回原命令
            };
        }
 
        /// <summary>
        /// 构建入库命令
        /// </summary>
        /// <remarks>
        /// 入库任务需要:
        /// 1. 查询堆垛机取货站台(根据当前地址和任务类型)
        /// 2. 设置起始坐标(来自站台)
        /// 3. 解析目标地址,设置终点坐标
        /// </remarks>
        /// <typeparam name="T">命令类型</typeparam>
        /// <param name="task">任务对象</param>
        /// <param name="command">命令对象</param>
        /// <returns>填充好的命令对象</returns>
        private T? BuildInboundCommand<T>(Dt_Task task, T command) where T : class
        {
            QuartzLogHelper.LogInfo(_logger, "BuildInboundCommand:构建入库命令,任务号: {TaskNum}", $"BuildInboundCommand:构建入库命令,任务号: {task.TaskNum}", task.Roadway, task.TaskNum);
 
            // 确定任务类型(空托盘用特殊类型)
            int taskType = 0;
            if (task.TaskType == (int)TaskOutboundTypeEnum.OutEmpty)
            {
                taskType = StackerCraneConst.EmptyPalletTaskType;
            }
            else
                taskType = task.TaskType;
 
            // 查询堆垛机取货站台路由
            Dt_Router? router = _routerService.QueryNextRoute(task.CurrentAddress, task.Roadway, taskType);
            if (router == null)
            {
                // 未找到站台,更新异常信息
                QuartzLogHelper.LogError(_logger, "BuildInboundCommand:未找到站台【{CurrentAddress}】信息,任务号: {TaskNum}", $"BuildInboundCommand:未找到站台【{task.CurrentAddress}】信息", task.Roadway, task.CurrentAddress, task.TaskNum);
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.CurrentAddress}】信息,无法获取对应的堆垛机取货站台信息");
                return null;
            }
 
            // 设置起始坐标(来自路由配置)
            SetCommandProperty(command, "StartRow", Convert.ToInt16(router.SrmRow));
            SetCommandProperty(command, "StartColumn", Convert.ToInt16(router.SrmColumn));
            SetCommandProperty(command, "StartLayer", Convert.ToInt16(router.SrmLayer));
 
            // 解析目标地址(库位地址)
            if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer))
            {
                QuartzLogHelper.LogError(_logger, "BuildInboundCommand:入库任务终点地址解析失败,终点: {NextAddress},任务号: {TaskNum}", $"BuildInboundCommand:入库任务终点地址解析失败,终点: {task.NextAddress}", task.Roadway, task.NextAddress, task.TaskNum);
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,终点:【{task.NextAddress}】");
                return null;
            }
 
            // 设置终点坐标
            SetCommandProperty(command, "EndRow", endRow);
            SetCommandProperty(command, "EndColumn", endColumn);
            SetCommandProperty(command, "EndLayer", endLayer);
 
            QuartzLogHelper.LogInfo(_logger, "BuildInboundCommand:入库命令构建成功,起点: {StartRow}-{StartColumn}-{StartLayer},终点: {EndRow}-{EndColumn}-{EndLayer},任务号: {TaskNum}", $"BuildInboundCommand:入库命令构建成功,起点: {router.SrmRow}-{router.SrmColumn}-{router.SrmLayer},终点: {endRow}-{endColumn}-{endLayer}", task.Roadway, router.SrmRow, router.SrmColumn, router.SrmLayer, endRow, endColumn, endLayer, task.TaskNum);
 
            return command;
        }
 
        /// <summary>
        /// 构建出库命令
        /// </summary>
        /// <remarks>
        /// 出库任务需要:
        /// 1. 查询堆垛机放货站台(根据目标地址和任务类型)
        /// 2. 设置终点坐标(来自站台)
        /// 3. 解析起始地址,设置起点坐标
        /// </remarks>
        /// <typeparam name="T">命令类型</typeparam>
        /// <param name="task">任务对象</param>
        /// <param name="command">命令对象</param>
        /// <returns>填充好的命令对象</returns>
        private T? BuildOutboundCommand<T>(Dt_Task task, T command) where T : class
        {
            QuartzLogHelper.LogInfo(_logger, "BuildOutboundCommand:构建出库命令,任务号: {TaskNum}", $"BuildOutboundCommand:构建出库命令,任务号: {task.TaskNum}", task.Roadway, task.TaskNum);
 
            // 确定任务类型
            int taskType = 0;
            if (task.TaskType == (int)TaskOutboundTypeEnum.OutEmpty)
            {
                taskType = StackerCraneConst.EmptyPalletTaskType;
            }
            else
                taskType = task.TaskType;
 
            // 查询堆垛机放货站台路由
            Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.TargetAddress, taskType);
            if (router == null)
            {
                QuartzLogHelper.LogError(_logger, "BuildOutboundCommand:未找到站台【{TargetAddress}】信息,任务号: {TaskNum}", $"BuildOutboundCommand:未找到站台【{task.TargetAddress}】信息", task.Roadway, task.TargetAddress, task.TaskNum);
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.TargetAddress}】信息,无法获取对应的堆垛机放货站台信息");
                return null;
            }
 
            // 设置终点坐标(来自路由配置)
            SetCommandProperty(command, "EndRow", Convert.ToInt16(router.SrmRow));
            SetCommandProperty(command, "EndColumn", Convert.ToInt16(router.SrmColumn));
            SetCommandProperty(command, "EndLayer", Convert.ToInt16(router.SrmLayer));
 
            // 解析起始地址(库位地址)
            if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer))
            {
                QuartzLogHelper.LogError(_logger, "BuildOutboundCommand:出库任务起点地址解析失败,起点: {CurrentAddress},任务号: {TaskNum}", $"BuildOutboundCommand:出库任务起点地址解析失败,起点: {task.CurrentAddress}", task.Roadway, task.CurrentAddress, task.TaskNum);
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                return null;
            }
 
            // 设置起点坐标
            SetCommandProperty(command, "StartRow", startRow);
            SetCommandProperty(command, "StartColumn", startColumn);
            SetCommandProperty(command, "StartLayer", startLayer);
 
            QuartzLogHelper.LogInfo(_logger, "BuildOutboundCommand:出库命令构建成功,起点: {StartRow}-{StartColumn}-{StartLayer},终点: {EndRow}-{EndColumn}-{EndLayer},任务号: {TaskNum}", $"BuildOutboundCommand:出库命令构建成功,起点: {startRow}-{startColumn}-{startLayer},终点: {router.SrmRow}-{router.SrmColumn}-{router.SrmLayer}", task.Roadway, startRow, startColumn, startLayer, router.SrmRow, router.SrmColumn, router.SrmLayer, task.TaskNum);
 
            return command;
        }
 
        /// <summary>
        /// 构建移库命令
        /// </summary>
        /// <remarks>
        /// 移库任务需要:
        /// 1. 解析目标地址(新的库位)
        /// 2. 解析起始地址(原来的库位)
        /// 3. 设置起止坐标
        /// </remarks>
        /// <typeparam name="T">命令类型</typeparam>
        /// <param name="task">任务对象</param>
        /// <param name="command">命令对象</param>
        /// <returns>填充好的命令对象</returns>
        private T? BuildRelocationCommand<T>(Dt_Task task, T command) where T : class
        {
            QuartzLogHelper.LogInfo(_logger, "BuildRelocationCommand:构建移库命令,任务号: {TaskNum}", $"BuildRelocationCommand:构建移库命令,任务号: {task.TaskNum}", task.Roadway, task.TaskNum);
 
            // 解析目标地址
            if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer))
            {
                QuartzLogHelper.LogError(_logger, "BuildRelocationCommand:移库任务终点地址解析失败,终点: {NextAddress},任务号: {TaskNum}", $"BuildRelocationCommand:移库任务终点地址解析失败,终点: {task.NextAddress}", task.Roadway, task.NextAddress, task.TaskNum);
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,终点:【{task.NextAddress}】");
                return null;
            }
 
            // 设置终点坐标
            SetCommandProperty(command, "EndRow", endRow);
            SetCommandProperty(command, "EndColumn", endColumn);
            SetCommandProperty(command, "EndLayer", endLayer);
 
            // 解析起始地址
            if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer))
            {
                QuartzLogHelper.LogError(_logger, "BuildRelocationCommand:移库任务起点地址解析失败,起点: {CurrentAddress},任务号: {TaskNum}", $"BuildRelocationCommand:移库任务起点地址解析失败,起点: {task.CurrentAddress}", task.Roadway, task.CurrentAddress, task.TaskNum);
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
                return null;
            }
 
            // 设置起点坐标
            SetCommandProperty(command, "StartRow", startRow);
            SetCommandProperty(command, "StartColumn", startColumn);
            SetCommandProperty(command, "StartLayer", startLayer);
 
            QuartzLogHelper.LogInfo(_logger, "BuildRelocationCommand:移库命令构建成功,起点: {StartRow}-{StartColumn}-{StartLayer},终点: {EndRow}-{EndColumn}-{EndLayer},任务号: {TaskNum}", $"BuildRelocationCommand:移库命令构建成功,起点: {startRow}-{startColumn}-{startLayer},终点: {endRow}-{endColumn}-{endLayer}", task.Roadway, startRow, startColumn, startLayer, endRow, endColumn, endLayer, task.TaskNum);
 
            return command;
        }
 
        /// <summary>
        /// 设置命令属性值(通过反射)
        /// </summary>
        /// <remarks>
        /// 使用反射动态设置命令对象的属性值。
        /// </remarks>
        /// <typeparam name="T">命令类型</typeparam>
        /// <param name="command">命令对象</param>
        /// <param name="propertyName">属性名称</param>
        /// <param name="value">属性值</param>
        private static void SetCommandProperty<T>(T command, string propertyName, object value) where T : class
        {
            var property = typeof(T).GetProperty(propertyName);
            property?.SetValue(command, value);
        }
 
        /// <summary>
        /// 解析地址字符串
        /// </summary>
        /// <remarks>
        /// 地址格式:行-列-层,例如 "1-2-3" 表示第1行、第2列、第3层。
        /// </remarks>
        /// <param name="address">地址字符串</param>
        /// <param name="row">解析出的行坐标</param>
        /// <param name="column">解析出的列坐标</param>
        /// <param name="layer">解析出的层坐标</param>
        /// <returns>解析成功返回 true</returns>
        private static bool TryParseAddress(string address, out short row, out short column, out short layer)
        {
            row = column = layer = 0;
 
            // 按 "-" 分隔地址
            string[] parts = address.Split('-');
            if (parts.Length != 3)
            {
                return false;
            }
 
            // 解析各部分为 short 类型
            return short.TryParse(parts[0], out row)
                && short.TryParse(parts[1], out column)
                && short.TryParse(parts[2], out layer);
        }
    }
}