SendWcsTaskAction.cs
20.1 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;
using System.Linq;
namespace WebMvc
{
/// <summary>
/// 单据结果回传
/// </summary>
/// <summary>
/// 入库单结果回传
/// </summary>
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class SendWcsTaskAction
{
private string ConnString { set; get; }
IJobExecutionContext Context { set; get; }
public SendWcsTaskAction(string _ConnString, IJobExecutionContext _Context)
{
ConnString = _ConnString;
Context = _Context;
}
public void Execute(JobContainer jobContainer)
{
UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
#region 任务下发逻辑
try
{
List<TaskDetail> taskDetails = (from td in _unitWork.Find<TaskDetail>(n => n.Status == TaskStatus.待下发任务)
join con in _unitWork.Find<Container>(n => n.Code != null)
on td.ContainerCode equals con.Code
where (new string[] { TaskType.容器出库, TaskType.直接出库 }.Contains(td.TaskType) && con.IsLock == 0) || (new string[] { TaskType.空容器入库, TaskType.容器回库, TaskType.站台到站台, TaskType.空容器出库, TaskType.盘点 }.Contains(td.TaskType))
select td).ToList();
foreach (TaskDetail taskDetail in taskDetails)
{
//成品库入库空板补给成功触发下一个入库
if (taskDetail.TaskType == TaskType.容器回库 && taskDetail.Roadway == 5 && _unitWork.IsExist<Station>(n => n.Code == taskDetail.ContainerCode))
{
int num = _unitWork.Find<StoreStation>(n => n.StationCode == "BufferStationE05").Count();
if (num < 3)
{
if (!_unitWork.IsExist<TaskDetail>(n => n.TaskType == TaskType.空容器入库 && n.DestinationLocation == "BufferStationE05"))
{
string type = "";
if (taskDetail.MaterialCode == "1" || taskDetail.MaterialCode == "2" || taskDetail.MaterialCode == "3")
{
type = ContainerType.木栈板母板_1;
}
else
{
type = ContainerType.木栈板母板_2;
}
var emptyContainer = (from con in _unitWork.Find<Container>(n => n.Type == type && n.Status == ContainerStatus.空 && n.IsLock == 0)
join loc in _unitWork.Find<Location>(n => n.Roadway == taskDetail.Roadway && n.Status == LocationStatus.空容器)
on con.LocationCode equals loc.Code
orderby loc.MaxHeight
select con).FirstOrDefault();
if (emptyContainer == null)
{
taskDetail.Error = "暂无空母板无法入库:" + DateTime.Now.ToString("HH:mm:ss");
_unitWork.Update(taskDetail);
continue;
}
//建立容器出库任务
if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == emptyContainer.Code))
{
int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.DestinationLocation == taskDetail.DestinationLocation).Count();
if (ptdcount < 1)
{
Task ptask = new Task();
TaskDetail ptaskDetail = new TaskDetail();
var taskNo = _unitWork.GetTaskNo(TaskNo.空板补充);
ptask.TaskNo = taskNo;
ptask.OrderCode = taskDetail.TaskNo;
ptask.BusinessType = BusinessType.出库_其他出库单;
ptask.FirstStatus = TaskStatus.待下发任务;
ptask.LastStatus = TaskStatus.待下发任务;
ptask.CreateTime = DateTime.Now;
_unitWork.Add(ptask);
ptaskDetail.TaskNo = taskNo;
ptaskDetail.OrderCode = taskDetail.TaskNo;
ptaskDetail.TaskType = TaskType.空容器出库;
ptaskDetail.ContainerCode = emptyContainer.Code;
ptaskDetail.SourceLocation = emptyContainer.LocationCode;
ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
ptaskDetail.OderQty = 0;
ptaskDetail.ContainerQty = 0;
ptaskDetail.HadQty = 0;
ptaskDetail.Roadway = taskDetail.Roadway;
ptaskDetail.Station = taskDetail.Station;
ptaskDetail.Status = TaskStatus.待下发任务;
ptaskDetail.Priority = 0;
ptaskDetail.CreateTime = DateTime.Now;
_unitWork.Add(ptaskDetail);
emptyContainer.IsLock = 1;
_unitWork.Update(emptyContainer);
Location loc = _unitWork.Find<Location>(n => n.Code == emptyContainer.LocationCode).FirstOrDefault();
_unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
{
Status = LocationStatus.任务锁定中
});
List<Inventory> list = _unitWork.Find<Inventory>(n => n.ContainerCode == taskDetail.ContainerCode).ToList();
foreach (Inventory inv in list)
{
inv.SourceCode = ptask.TaskNo;
_unitWork.Update(inv);
}
}
}
}
else
{
continue;
}
}
else
{
continue;
}
}
if (_unitWork.IsExist<Station>(n => n.Containercode == taskDetail.ContainerCode && n.Code != taskDetail.Station) && taskDetail.TaskType == TaskType.容器出库)
{
taskDetail.Error = "托盘:" + taskDetail.ContainerCode + "已占用别的站台" + ":" + DateTime.Now.ToString("HH:mm:ss");
_unitWork.Update(taskDetail);
continue;
}
//暂存站台判断
if (_unitWork.IsExist<Station>(n => n.Type == StationType.暂存区站台 && n.Code == taskDetail.Station))
{
Station bufferStation = null;
if (taskDetail.Roadway == 4)
{
bufferStation = (from st in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "")
join sr in _unitWork.Find<StationRoadway>(n => n.RoadWay == taskDetail.Roadway)
on st.Code equals sr.StationCode
orderby st.Code descending
select st).FirstOrDefault();
}
else
{
bufferStation = (from st in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "")
join sr in _unitWork.Find<StationRoadway>(n => n.RoadWay == taskDetail.Roadway)
on st.Code equals sr.StationCode
orderby st.Code
select st).FirstOrDefault();
}
if (bufferStation == null)
{
taskDetail.Error = "暂无暂存台,托盘:" + taskDetail.ContainerCode + ":" + DateTime.Now.ToString("HH:mm:ss");
_unitWork.Update(taskDetail);
continue;
}
else
{
taskDetail.Station = bufferStation.Code;
taskDetail.DestinationLocation = bufferStation.Code;
}
}
Station station = null;
Location location = null;
Container container = _unitWork.Find<Container>(n => n.Code == taskDetail.ContainerCode).FirstOrDefault(); ;
if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.站台到站台 || taskDetail.TaskType == TaskType.盘点)
{
station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && (n.Containercode == "" || n.Containercode == taskDetail.ContainerCode) && n.IsOut == 1 && n.IsStop == 0).FirstOrDefault();
}
else if (taskDetail.TaskType == TaskType.空容器出库)
{
station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && (n.Containercode == "" || n.Containercode == taskDetail.ContainerCode) && n.IsOut == 1 && n.IsStop == 0 && n.IsEmpty == 1).FirstOrDefault();
}
else
{
station = _unitWork.Find<Station>(n => n.IsStop == 0).FirstOrDefault();
}
if (station != null)
{
if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.盘点)
{
if (!_unitWork.IsExist<Location>(n => n.ContainerCode == taskDetail.ContainerCode && n.Code == taskDetail.SourceLocation))
{
taskDetail.Error = "WMS:容器出库起始仓位与实际仓位不符 托盘:" + taskDetail.ContainerCode + ":" + DateTime.Now.ToString("HH:mm:ss");
_unitWork.Update(taskDetail);
continue;
}
}
}
else
{
taskDetail.Error = "站台:" + taskDetail.Station + "异常" + ":" + DateTime.Now.ToString("HH:mm:ss");
_unitWork.Update(taskDetail);
continue;
}
ApiRequest apiRequest = new ApiRequest("WCS");
//WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetail), "WcsWebApi/TaskAssign", "任务下发");
WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetail), "IWcsTaskTest/CreateWcsTask", "任务下发");
if (_WCSResponse.Code == 200)
{
if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.空容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.盘点)
{
taskDetail.Status = TaskStatus.下达任务;
taskDetail.DestinationLocation = station.Code;
location = _unitWork.Find<Location>(n => n.Code == taskDetail.SourceLocation).FirstOrDefault();
location.Status = LocationStatus.任务锁定中;
station.Containercode = taskDetail.ContainerCode;
if (taskDetail.TaskType == TaskType.盘点)
{
container.IsLock = ContainerLock.盘点锁;
}
else
{
container.IsLock = ContainerLock.任务锁;
}
}
else if(taskDetail.TaskType == TaskType.空容器入库 && taskDetail.SourceLocation != "PP_SpecialPoint")
{
taskDetail.Status = TaskStatus.下达任务;
location = _unitWork.Find<Location>(n => n.Code == taskDetail.DestinationLocation).FirstOrDefault();
location.Status = LocationStatus.任务锁定中;
station.Containercode = taskDetail.ContainerCode;
container.IsLock = ContainerLock.任务锁;
}
else if (taskDetail.TaskType == TaskType.站台到站台)
{
taskDetail.Status = TaskStatus.下达任务;
taskDetail.DestinationLocation = station.Code;
if (taskDetail.ContainerCode != "woodPallet")
{
station.Containercode = taskDetail.ContainerCode;
if (taskDetail.TaskType == TaskType.盘点)
{
container.IsLock = ContainerLock.盘点锁;
}
else
{
container.IsLock = ContainerLock.任务锁;
}
}
}
else
{
taskDetail.Status = TaskStatus.下达任务;
}
if (station != null)
{
_unitWork.Update(station);
}
if (location != null)
{
_unitWork.Update<Location>(n => n.Id == location.Id && n.Version == location.Version, n => new Location
{
Status = location.Status,
UpdateBy = "wms_wcs",
UpdateTime = DateTime.Now
});
}
if (container != null)
{
_unitWork.Update(container);
}
taskDetail.Error = "";
_unitWork.Update(taskDetail);
Task task = null;
if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == taskDetail.TaskNo && n.Status > taskDetail.Status))
{
task = _unitWork.Find<Task>(n => n.TaskNo == taskDetail.TaskNo).FirstOrDefault();
if (task != null)
{
task.FirstStatus = taskDetail.Status;
}
_unitWork.Update(task);
}
if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == taskDetail.TaskNo && n.Status < taskDetail.Status))
{
task = _unitWork.Find<Task>(n => n.TaskNo == taskDetail.TaskNo).FirstOrDefault();
if (task != null)
{
task.LastStatus = taskDetail.Status;
}
_unitWork.Update(task);
}
List<TaskDetail> list = new List<TaskDetail>();
if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库)
{
list = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDetail.ContainerCode && (taskDetail.TaskType == TaskType.整盘出库 || taskDetail.TaskType == TaskType.分拣出库)).ToList();
}
else if (taskDetail.TaskType == TaskType.容器回库)
{
list = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDetail.ContainerCode && (taskDetail.TaskType == TaskType.整盘入库)).ToList();
}
if (list.Count > 0)
{
foreach (TaskDetail td in list)
{
td.Status = TaskStatus.下达任务;
if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == td.TaskNo && n.Status > taskDetail.Status))
{
task = _unitWork.Find<Task>(n => n.TaskNo == td.TaskNo).FirstOrDefault();
if (task != null)
{
task.FirstStatus = td.Status;
}
_unitWork.Update(task);
}
if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == td.TaskNo && n.Status < taskDetail.Status))
{
task = _unitWork.Find<Task>(n => n.TaskNo == td.TaskNo).FirstOrDefault();
if (task != null)
{
task.LastStatus = td.Status;
}
_unitWork.Update(task);
}
_unitWork.Update(td);
}
}
}
else
{
taskDetail.Error = "WCS报错:" + _WCSResponse.Message + ":" + DateTime.Now.ToString("HH:mm:ss");
_unitWork.Update(taskDetail);
continue;
}
}
}
catch (Exception ex)
{
throw ex;
}
#endregion
}
}
}