1
HuBingJie
8 小时以前 4ac3f08f416685db0e218df55750d94556030330
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core;
using WIDESEA_Model.Models;
using WIDESEA_Core.Helper;
using Microsoft.Extensions.Logging;
using MailKit.Search;
using System.Reflection.Metadata;
using static WIDESEA_ITaskInfoService.ITaskService;
using WIDESEA_Common;
using WIDESEA_Core.LogHelper;
using WIDESEA_DTO.Task;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.CommonEnum;
using WIDESEA_DTO.ToMes;
using System.Diagnostics;
using WIDESEA_Common.OtherEnum;
using WIDESEA_Core.Const;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
 
        /// <summary>
        /// 仅申请任务,让WCS根据路由确定下一地址
        /// </summary>
        /// <param name="stationCode"></param>
        /// <param name="palletCode"></param>
        /// <returns></returns>
        public WebResponseContent DeviceRequestInboundTaskSimple(string stationCode, string palletCode)
        {
            try
            {
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
                if (task != null)
                {
                    PushTasksToWCS(new List<Dt_Task> { task });
                    return WebResponseContent.Instance.OK($"该托盘已生成任务", _mapper.Map<WMSTaskDTO>(task));
                }
               /* if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == OutTaskStatusEnum.New.ObjToInt()) != null)
                {
                    return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
                }*/
 
 
                //Dt_StockInfo stockInfo = _stockRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
 
                //if (stockInfo == null)
                //{
                //    return WebResponseContent.Instance.Error($"未找到组盘信息");
                //}
                //var details = stockInfo.Details.FirstOrDefault();
                //if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                //{
                //    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                //}
                Dt_RoadwayInfo roadwayInfo = _roadwayInforepository.QueryFirst(x => x.InStationCode == stationCode);
                if (roadwayInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到刚入库站台地址");
                }
                Dt_Warehouse warehouse = _warehouseRepository.QueryFirst(x => x.WarehouseCode == roadwayInfo.RoadwayNo);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error("未找到该仓库");
                }
                Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayInfo.RoadwayNo, warehouse.WarehouseId, "");//
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                }
                var dt_Stock = new Dt_StockInfo
                {
                    PalletCode = palletCode,
                    PalletType = 1,
                    LocationCode = locationInfo.LocationCode,
                    StockStatus = (int)StockStatusEmun.组盘暂存,
                    Creater = "WMS",
                    CreateDate = DateTime.Now,
                    WarehouseId = warehouse.WarehouseId,
                };
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = stationCode,
                    Grade = 0,
                    NextAddress = stationCode,//堆垛机
                    PalletCode = palletCode,
                    OrderNo = "1",
                    Roadway = "1",
                    SourceAddress = "",
                    TargetAddress = locationInfo.LocationCode,
                    TaskType = TaskInboundTypeEnum.Inbound.ObjToInt(),
                    TaskStatus = TaskInStatusEnum.Line_InExecuting.ObjToInt(),
                    WarehouseId = warehouse.WarehouseId,
                    //PalletType = GetPalletType(warehouse, palletCode),//GetPalletType(warehouse, palletCode)
                    Creater = "WCS",
                    CreateDate = DateTime.Now,
                    Depth = locationInfo.Depth,
                };
 
                _unitOfWorkManage.BeginTran();
                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                _stockRepository.AddData(dt_Stock);
                //_stockRepository.UpdateData(stockInfo);
                _locationInfoRepository.UpdateData(locationInfo);
                _unitOfWorkManage.CommitTran();
                WMSTaskDTO wMSTaskDTO = _mapper.Map<WMSTaskDTO>(newTask);
 
                PushTasksToWCS(new List<Dt_Task> { newTask });
                return WebResponseContent.Instance.OK(data: wMSTaskDTO);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 创建入库任务
        /// </summary>
        /// <param name="inboundTask"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResponse<object> sendEnTask(InOutboundTaskReceived inboundTask)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
 
            try
            {
                List<Dt_Task> AddtaskList = new List<Dt_Task>();
                List<Dt_LocationInfo> Adddt_Locations = new List<Dt_LocationInfo>();
                List<Dt_StockInfo> Adddtstockt = new List<Dt_StockInfo>();
 
                if (inboundTask.palletInfoList.Count > 0)
                {
                    List<Dt_Task> taskData = BaseDal.QueryData();
                    List<Dt_StockInfo> Stockdata = _stockRepository.QueryData();
 
                    /*Dt_StationManger dt_Station = _stationMangerService.QueryPlatform(inboundTask.NodeCode);
                    if (dt_Station == null) return MESresponse($"未找到站台信息,站台编号:{inboundTask.NodeCode}", false);*/
 
                    foreach (PalletInfo palletInfo in inboundTask.palletInfoList)
                    {
                        Dt_LocationInfo location = null;
                        if (palletInfo.locationCode == null || palletInfo.locationCode == "")
                        {
                            location = _basicService.LocationInfoService.GetLocationInfo();//获取到新库位
                        }
                        else
                        {
                            location = _locationInfoRepository.QueryFirst(x => x.LocationCode == palletInfo.locationCode);
                        }
                        if (location == null) return MESresponse($"未找到货位信息,条码:{palletInfo.palletCode}", false);
                        if (location.LocationStatus != (int)LocationStatusEnum.Free) return MESresponse($"托盘条码:{palletInfo.palletCode},查找到的货位:{location.LocationCode},不为空货位!", false);
 
                        Dt_StockInfo dt_ = Stockdata.Where(x => x.PalletCode == palletInfo.palletCode).FirstOrDefault();
                        if (dt_ == null)
                        {
                            //创建组盘信息
                            var dt_Stock = new Dt_StockInfo
                            {
                                PalletCode = palletInfo.palletCode,
                                PalletType = 1,
                                LocationCode = location.LocationCode,
                                StockStatus = (int)StockStatusEmun.组盘暂存,
                                Creater = "WMS",
                                CreateDate = DateTime.Now,
                                MESsubPalletCode = palletInfo.palletCode,
                            };
                            Adddtstockt.Add(dt_Stock);
                        }
                        else
                        {
                            if(dt_.StockStatus == (int)StockStatusEmun.已入库)
                            {
                                return MESresponse($"该库存条码已入库,不可重复入库,条码:{palletInfo.palletCode}", false);
                            }
                        }
 
                        string TaregtAddres = "";
                        if (inboundTask.NodeCode !="2011" && inboundTask.NodeCode != "2009")
                        {
                            int nextTaregt = int.Parse(inboundTask.NodeCode);
                            //确认目标站台(固定)
                             TaregtAddres = (nextTaregt + 1).ToString();
                        }
                        else
                        {
                            TaregtAddres = location.RoadwayNo == "SC01" ? "2021" : "2020";
                        }
                        
 
 
 
                        location.LocationStatus = (int)LocationStatusEnum.InStockLock;
 
                        //生成移动任务
                        Dt_Task dt_Task = new()
                        {
                            PalletCode = palletInfo.palletCode,
                            TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                            Roadway = location.RoadwayNo,
                            TaskType = TaskInboundTypeEnum.Inbound.ObjToInt(),
                            TaskStatus = TaskInStatusEnum.Line_InExecuting.ObjToInt(),
                            SourceAddress = TaregtAddres,
                            TargetAddress = location.LocationCode,
                            CurrentAddress = TaregtAddres,
                            NextAddress = location.LocationCode,
                            Grade = 1,
                            Creater = "MES",
                            Depth = location.Depth,
                            CreateDate = DateTime.Now,
                            MEStaskId = inboundTask.TaskId,
                            MESbusinessId = inboundTask.BusinessId,
                            MESsubPalletCode = palletInfo.subPalletCode,
                            OutboundPlatform = inboundTask.NodeCode
                        };
                       
                        Adddt_Locations.Add(location);
                        AddtaskList.Add(dt_Task);
                    }
                    if (Adddtstockt.Count > 0 && Adddt_Locations.Count > 0 && AddtaskList.Count > 0)
                    {
                        _unitOfWorkManage.BeginTran();
 
 
                        _stockRepository.AddData(Adddtstockt);
                        _locationInfoRepository.UpdateData(Adddt_Locations);
                        BaseDal.AddData(AddtaskList);
 
                        var respon = PushTasksToWCS(AddtaskList, "");
                        if (respon.Status)
                        {
                            _unitOfWorkManage.CommitTran();  //提交事务
                            return MESresponse("", true);
                        }
                        else
                        {
                            _unitOfWorkManage.RollbackTran();  //回滚事务
                            return MESresponse($"下发出库失败,原因:{respon.Message}!", false);
                        }
 
                    }
                    else
                    {
                        return MESresponse("任务生成失败!", false);
                    }
 
                }
                else
                {
                    return MESresponse("接收到MES托盘编码列表无数据!", false);
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();  //回滚事务
                return MESresponse($"下发出库失败,原因:{ex.Message}!", false);
                throw;
            }
        }
 
    }
}