1
HuBingJie
2025-11-13 d58196721475e968769d708d9c14f60dd8d5671f
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
using SqlSugar;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.OtherEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_DTO.ToMes;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
        /// <summary>
        /// MES下发库位调拨任务
        /// </summary>
        /// <param name="allocationTask">库位调拨任务信息</param>
        /// <returns></returns>
        public ApiResponse<object> sendAllocationTask(AllocationTaskReceived allocationTask)
        {
            try
            {
                // 参数验证
                if (allocationTask == null)
                    return MESresponse("调拨任务信息不能为空", false);
 
                if (string.IsNullOrEmpty(allocationTask.palletCode))
                    return MESresponse("托盘编码不能为空", false);
 
                if (string.IsNullOrEmpty(allocationTask.sourceLocationCode))
                    return MESresponse("源货位编码不能为空", false);
 
                if (string.IsNullOrEmpty(allocationTask.locationCode))
                    return MESresponse("目标货位编码不能为空", false);
 
                _unitOfWorkManage.BeginTran();
 
                // 验证托盘是否存在
                var stockInfo = _stockRepository.QueryFirst(x => x.PalletCode == allocationTask.palletCode);
                if (stockInfo == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"未找到托盘信息,托盘编码:{allocationTask.palletCode}", false);
                }
 
                // 验证源货位
                var sourceLocation = _basicService.LocationInfoService.Repository
                    .QueryFirst(x => x.LocationCode == allocationTask.sourceLocationCode);
                if (sourceLocation == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"未找到源货位信息,货位编码:{allocationTask.sourceLocationCode}", false);
                }
 
                // 验证目标货位
                var targetLocation = _basicService.LocationInfoService.Repository
                    .QueryFirst(x => x.LocationCode == allocationTask.locationCode && x.LocationStatus == (int)LocationStatusEnum.Free);
                if (targetLocation == null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"未找到目标货位信息,货位编码:{allocationTask.locationCode}或货位为其他状态", false);
                }
 
                // 验证托盘当前位置是否与源货位一致
                if (stockInfo.LocationCode != allocationTask.sourceLocationCode)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"托盘当前位置与源货位不一致,托盘编码:{allocationTask.palletCode},当前位置:{stockInfo.LocationCode},源货位:{allocationTask.sourceLocationCode}", false);
                }
 
                // 验证源货位状态
                if (sourceLocation.LocationStatus != (int)LocationStatusEnum.InStock)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"源货位状态异常,货位编码:{allocationTask.sourceLocationCode},当前状态:{sourceLocation.LocationStatus}", false);
                }
 
                // 验证目标货位状态
                if (targetLocation.LocationStatus != (int)LocationStatusEnum.Free)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"目标货位不为空闲状态,货位编码:{allocationTask.locationCode},当前状态:{targetLocation.LocationStatus}", false);
                }
 
                // 检查是否已存在相同的调拨任务
                var existingTask = BaseDal.QueryFirst(x =>
                    x.PalletCode == allocationTask.palletCode &&
                    x.SourceAddress == allocationTask.sourceLocationCode &&
                    x.TargetAddress == allocationTask.locationCode &&
                    x.TaskType == (int)TaskRelocationTypeEnum.Relocation &&
                    x.TaskStatus < (int)TaskRelocationStatusEnum.RelocationFinish);
 
                if (existingTask != null)
                {
                    _unitOfWorkManage.RollbackTran();
                    return MESresponse($"已存在相同的调拨任务,托盘编码:{allocationTask.palletCode},任务号:{existingTask.TaskNum}", false);
                }
 
                // 锁定货位状态
                sourceLocation.LocationStatus = (int)LocationStatusEnum.InStockLock;
                targetLocation.LocationStatus = (int)LocationStatusEnum.InStockLock;
 
                // 更新库存状态
                stockInfo.StockStatus = (int)StockStatusEmun.调拨中;
                List<Dt_Task> dt_Tasks = new List<Dt_Task>();
 
                // 创建调拨任务
                Dt_Task allocationTaskEntity = new Dt_Task
                {
                    PalletCode = allocationTask.palletCode,
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Roadway = sourceLocation.RoadwayNo,
                    TaskType = (int)TaskRelocationTypeEnum.Relocation,
                    TaskStatus = (int)TaskRelocationStatusEnum.RelocationNew,
                    SourceAddress = allocationTask.sourceLocationCode,
                    TargetAddress = allocationTask.locationCode,
                    CurrentAddress = allocationTask.sourceLocationCode,
                    NextAddress = allocationTask.locationCode,
                    Grade = 2, // 默认优先级
                    Creater = "MES",
                    Depth = targetLocation.Depth,
                    CreateDate = DateTime.Now,
                    MEStaskId = allocationTask.taskId,
                    MESbusinessId = allocationTask.businessId,
                    MESsubPalletCode = "", // 新结构中没有此字段
                    Remark = "MES库位调拨任务"
                };
 
                dt_Tasks.Add(allocationTaskEntity);
 
                // 批量更新数据
                _basicService.LocationInfoService.Repository.UpdateData(sourceLocation);
                _basicService.LocationInfoService.Repository.UpdateData(targetLocation);
                _stockRepository.UpdateData(stockInfo);
                BaseDal.AddData(allocationTaskEntity);
 
 
                var respon = PushTasksToWCS(dt_Tasks, "");
                if (respon.Status)
                {
                    _unitOfWorkManage.CommitTran();  //提交事务
                    return MESresponse("", true);
                }
                else
                {
                    _unitOfWorkManage.RollbackTran();  //回滚事务
                    return MESresponse($"下发库位调拨失败,原因:{respon.Message}!", false);
                }
 
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                // 记录错误日志
                Console.WriteLine($"MES库位调拨任务创建失败: {ex.Message}");
                return MESresponse($"调拨任务创建失败:{ex.Message}", false);
            }
        }
 
        /// <summary>
        /// MES响应格式化
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="success">是否成功</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        private ApiResponse<object> MESresponse(string message, bool success, object data = null)
        {
            if (success)
            {
                return ApiResponse<object>.SuccessResponse(message, data);
            }
            else
            {
                return ApiResponse<object>.ErrorResponse(message);
            }
        }
    }
}