IShipmentApp.cs
27.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Linq;
using Infrastructure;
using WebRepository;
namespace WebApp
{
/// <summary>
/// 出库单下发接口App
/// </summary>
public partial class IShipmentApp : ApiApp
{
public IRepository<ShipmentHeader> _app;
public IShipmentApp(IUnitWork unitWork, IAuth auth, BaseDBContext context, IRepository<ShipmentHeader> shipmentHeader) : base(unitWork, auth, context)
{
_app = shipmentHeader;
}
public Response InsertShipment(IShipmentModel shipments)
{
Response Response = new Response();
if (!CheckLogin())
{
Response.Code = 500;
Response.Status = false;
Response.Message = "请先登录!";
return Response;
}
using (var tran = _context.Database.BeginTransaction())
{
try
{
#region 保存出库单接口主表
IshipmentHeader headdata = _unitWork.FindSingle<IshipmentHeader>(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode));
shipments.shipmentHeader.ReceiveTime = DateTime.Now;
shipments.shipmentHeader.Id = headdata?.Id;
if (shipments.shipmentHeader.Id == null)
{
if (shipments.shipmentHeader.CreateBy == null)
{
shipments.shipmentHeader.CreateBy = "wms";
shipments.shipmentHeader.CreateTime = DateTime.Now;
}
_unitWork.Add(shipments.shipmentHeader);
}
else
{
if (shipments.shipmentHeader.UpdateBy == null)
{
shipments.shipmentHeader.UpdateBy = "wms";
shipments.shipmentHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(shipments.shipmentHeader);
}
#endregion
#region 保存出库单主表
ShipmentHeader shipmentHeader = _unitWork.FindSingle<ShipmentHeader>(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode));
if (shipmentHeader != null)
{
if (shipmentHeader.FirstStatus != ShipmentHeaderStatus.新建)
{
throw new Exception("单据进入订单池后,不允许修改!");
}
}
else
{
shipmentHeader = new ShipmentHeader();
}
shipmentHeader.Type = shipments.shipmentHeader.Type;
shipmentHeader.SourceCode = shipments.shipmentHeader.SourceCode;
shipmentHeader.WarehouseType = shipments.shipmentHeader.WarehouseType;
shipmentHeader.Priority = (shipments.shipmentHeader.Priority == null ? 0 : shipments.shipmentHeader.Priority);
shipmentHeader.Station = shipments.shipmentHeader.Station;
shipmentHeader.CreateTime = shipments.shipmentHeader.CreateTime;
shipmentHeader.CreateBy = shipments.shipmentHeader.CreateBy;
shipmentHeader.UpdateTime = shipments.shipmentHeader.UpdateTime;
shipmentHeader.UpdateBy = shipments.shipmentHeader.UpdateBy;
shipmentHeader.Code = string.IsNullOrEmpty(shipmentHeader.Code) ? _unitWork.GetTaskNo(shipments.shipmentHeader.Type) : shipmentHeader.Code;
shipmentHeader.UploadStatus = 0;
shipmentHeader.FirstStatus = ShipmentHeaderStatus.新建;
shipmentHeader.LastStatus = ShipmentHeaderStatus.新建;
shipmentHeader.TotalLines = 0;
shipmentHeader.TotalQty = 0;
if (shipmentHeader.Id == null)
{
if (shipmentHeader.CreateBy == null)
{
shipmentHeader.CreateBy = "wms";
shipmentHeader.CreateTime = DateTime.Now;
}
_unitWork.Add(shipmentHeader);
}
else
{
if (shipmentHeader.UpdateBy == null)
{
shipmentHeader.UpdateBy = "wms";
shipmentHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(shipmentHeader);
}
#endregion
short? totalLines = 0;
decimal? qty = 0;
foreach (var item in shipments.shipmentDetails)
{
try
{
#region 保存出库单接口子表
IshipmentDetail data = _context.Set<IshipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
item.ReceiveTime = DateTime.Now;
item.Id = data?.Id;
if (item.Id == null)
{
if (item.CreateBy == null)
{
item.CreateBy = "wms";
item.CreateTime = DateTime.Now;
}
_unitWork.Add(item);
}
else
{
if (item.UpdateBy == null)
{
item.UpdateBy = "wms";
item.UpdateTime = DateTime.Now;
}
_unitWork.Update(item);
}
#endregion
#region 保存出库单子表
ShipmentDetail shipmentDetail = _context.Set<ShipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
if (shipmentDetail == null)
{
shipmentDetail = new ShipmentDetail();
}
shipmentDetail.SourceCode = item.SourceCode;
shipmentDetail.MaterialCode = item.MaterialCode;
shipmentDetail.Lot = item.Lot;
shipmentDetail.InventoryStatus = item.InventoryStatus;
shipmentDetail.Qty = item.Qty;
shipmentDetail.CreateTime = item.CreateTime;
shipmentDetail.CreateBy = item.CreateBy;
shipmentDetail.UpdateTime = item.UpdateTime;
shipmentDetail.UpdateBy = item.UpdateBy;
shipmentDetail.ShipmentId = shipmentHeader.Id;
shipmentDetail.ShipmentCode = shipmentHeader.Code;
shipmentDetail.QtyDivided = 0;
shipmentDetail.QtyCompleted = 0;
shipmentDetail.Price = 0;
shipmentDetail.Status = ShipmentHeaderStatus.新建;
//成品库逻辑
var con = (from c in _unitWork.Find<Container>(n => n.IsLock == 0)
join i in _unitWork.Find<Inventory>(n => n.MaterialCode == item.MaterialCode && n.TaskStatus == InventoryTaskType.无任务)
on c.Code equals i.ContainerCode
select c).FirstOrDefault();
if (con != null)
{
if (con.Type == ContainerType.木栈板母板_1 || con.Type == ContainerType.木栈板母板_2)
{
Station st = _unitWork.Find<Station>(n => n.Type == StationType.木板补给终点站台).FirstOrDefault();
Station stdel = _unitWork.Find<Station>(n => n.Type == (con.Type == ContainerType.木栈板母板_1 ? StationType.木栈板母板补给站台s_1 : StationType.木栈板母板补给站台s_2)).FirstOrDefault();
//判断“木板补给终点站台”是否有容器 如果有则判断“木栈板母板补给站台”是否有容器
if (st.Containercode == "")
{
//如果没有容器则直接出库
if (stdel.Containercode == "")
{
//建立容器出库任务
if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
{
int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == con.Code && n.TaskType == TaskType.容器出库).Count();
if (ptdcount < 1)
{
Task ptask = new Task();
TaskDetail ptaskDetail = new TaskDetail();
var taskNo = _app.GetTaskNo(TaskNo.空板补充);
ptask.TaskNo = taskNo;
ptask.OrderCode = taskNo;
ptask.BusinessType = BusinessType.出库_其他出库单;
ptask.FirstStatus = TaskStatus.待下发任务;
ptask.LastStatus = TaskStatus.待下发任务;
ptask.CreateTime = DateTime.Now;
_unitWork.Add(ptask);
ptaskDetail.TaskNo = taskNo;
ptaskDetail.OrderCode = taskNo;
ptaskDetail.TaskType = TaskType.空容器出库;
ptaskDetail.ContainerCode = con.Code;
ptaskDetail.SourceLocation = con.LocationCode;
if (con.Type == ContainerType.木栈板母板_1)
{
ptaskDetail.DestinationLocation = _unitWork.Find<Station>(n => n.Type == StationType.木栈板母板补给站台s_1).FirstOrDefault().Code;
}
else
{
ptaskDetail.DestinationLocation = _unitWork.Find<Station>(n => n.Type == StationType.木栈板母板补给站台s_2).FirstOrDefault().Code;
}
ptaskDetail.Station = ptaskDetail.DestinationLocation;
ptaskDetail.OderQty = 0;
ptaskDetail.ContainerQty = 0;
ptaskDetail.HadQty = 0;
ptaskDetail.Roadway = _unitWork.Find<Location>(n => n.ContainerCode == con.Code).FirstOrDefault().Roadway;
ptaskDetail.Status = TaskStatus.待下发任务;
ptaskDetail.Priority = 0;
ptaskDetail.CreateTime = DateTime.Now;
_unitWork.Add(ptaskDetail);
Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == con.Code && n.MaterialCode == item.MaterialCode).FirstOrDefault();
inventory.Qty -= 1;
_unitWork.Update(inventory);
st.Containercode = con.Code;
_unitWork.Update(st);
Location Location = _unitWork.Find<Location>(n => n.ContainerCode == con.Code).FirstOrDefault();
_unitWork.Update<Location>(n => n.Id == Location.Id && n.Version == Location.Version, n => new Location
{
IsStop = true
});
Container Container = _unitWork.Find<Container>(n => n.Code == con.Code).FirstOrDefault();
Container.IsLock = 1;
_unitWork.Update(Container);
stdel.Containercode = con.Code;
_unitWork.Update(stdel);
shipmentDetail.Status = ShipmentHeaderStatus.回传;
}
}
else
{
Response.Code = 500;
Response.Status = false;
Response.Message = con.Code + "托盘已有任务!";
return Response;
}
}
else
{
Inventory inv = _unitWork.Find<Inventory>(n => n.ContainerCode == stdel.Containercode && n.MaterialCode == item.MaterialCode).FirstOrDefault();
//如果有容器则判断该容器上的木栈板是否符合 符合则直接点对点出 不符合则回库 等回库完成再出
if (inv != null)
{
//建立容器出库任务
if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
{
int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == con.Code && n.TaskType == TaskType.容器出库).Count();
if (ptdcount < 1)
{
Task ptask = new Task();
TaskDetail ptaskDetail = new TaskDetail();
var taskNo = _app.GetTaskNo(TaskNo.空板补充);
ptask.TaskNo = taskNo;
ptask.OrderCode = taskNo;
ptask.BusinessType = BusinessType.出库_其他出库单;
ptask.FirstStatus = TaskStatus.待下发任务;
ptask.LastStatus = TaskStatus.待下发任务;
ptask.CreateTime = DateTime.Now;
_unitWork.Add(ptask);
ptaskDetail.TaskNo = taskNo;
ptaskDetail.OrderCode = taskNo;
ptaskDetail.TaskType = TaskType.空容器出库;
ptaskDetail.ContainerCode = stdel.Containercode;
ptaskDetail.SourceLocation = stdel.Code;
ptaskDetail.DestinationLocation = _unitWork.Find<Station>(n => n.Type == StationType.木板补给终点站台).FirstOrDefault().Code;
ptaskDetail.Station = ptaskDetail.DestinationLocation;
ptaskDetail.OderQty = 0;
ptaskDetail.ContainerQty = 0;
ptaskDetail.HadQty = 0;
ptaskDetail.Roadway = 0;
ptaskDetail.Status = TaskStatus.待下发任务;
ptaskDetail.Priority = 0;
ptaskDetail.CreateTime = DateTime.Now;
_unitWork.Add(ptaskDetail);
inv.Qty -= 1;
_unitWork.Update(inv);
st.Containercode = con.Code;
_unitWork.Update(st);
shipmentDetail.Status = ShipmentHeaderStatus.回传;
}
}
else
{
Response.Code = 500;
Response.Status = false;
Response.Message = con.Code + "托盘已有任务!";
return Response;
}
}
else
{
//空栈板回库逻辑待优化
Task ptask = new Task();
TaskDetail ptaskDetail = new TaskDetail();
var taskNo = _app.GetTaskNo(TaskNo.容器回库);
ptask.TaskNo = taskNo;
ptask.OrderCode = taskNo;
ptask.BusinessType = BusinessType.入库_其他入库单;
ptask.FirstStatus = TaskStatus.待下发任务;
ptask.LastStatus = TaskStatus.待下发任务;
ptask.CreateTime = DateTime.Now;
_unitWork.Add(ptask);
ptaskDetail.TaskNo = taskNo;
ptaskDetail.OrderCode = shipmentDetail.ShipmentCode;
ptaskDetail.TaskType = TaskType.空容器入库;
ptaskDetail.ContainerCode = stdel.Containercode;
ptaskDetail.SourceLocation = stdel.Code;
ptaskDetail.DestinationLocation = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位).FirstOrDefault().Code;
ptaskDetail.Station = stdel.Code;
ptaskDetail.OderQty = 0;
ptaskDetail.ContainerQty = 0;
ptaskDetail.HadQty = 0;
ptaskDetail.Roadway = 0;
ptaskDetail.Status = TaskStatus.待下发任务;
ptaskDetail.Priority = 0;
ptaskDetail.CreateTime = DateTime.Now;
_unitWork.Add(ptaskDetail);
shipmentDetail.Status = ShipmentHeaderStatus.分配完成;
}
}
}
else
{
Response.Code = 500;
Response.Status = false;
Response.Message = "请将" + st.Containercode + "托盘入库!";
return Response;
}
}
}
//else
//{
// Response.Code = 500;
// Response.Status = false;
// Response.Message = "无可用托盘!";
// return Response;
//}
if (shipmentDetail.Id == null)
{
if (shipmentDetail.CreateBy == "")
{
shipmentDetail.CreateBy = "wms";
shipmentDetail.CreateTime = DateTime.Now;
}
_unitWork.Add(shipmentDetail);
}
else
{
if (shipmentDetail.UpdateBy == "")
{
shipmentDetail.UpdateBy = "wms";
shipmentDetail.UpdateTime = DateTime.Now;
}
_unitWork.Update(shipmentDetail);
}
#endregion
totalLines += 1;
qty += item.Qty;
}
catch (Exception ex)
{
#region 记录出库单接口子表错误信息
IshipmentDetail data = _context.Set<IshipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
if (data != null)
{
data.ErrorMessage = ex.Message;
if (data.UpdateBy == null)
{
data.UpdateBy = "wms";
data.UpdateTime = DateTime.Now;
}
_unitWork.Update(data);
}
#endregion
Response.Code = 500;
Response.Status = false;
Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "SourceCode:" + item.SourceCode + ",MaterialCode:" + item.MaterialCode + "同步失败:" + ex.Message;
}
}
#region 更新出库单主表汇总信息
shipmentHeader.TotalLines += (short)_unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentHeader.Code).ToList().Count; ;
shipmentHeader.TotalQty += _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentHeader.Code).Sum(x => x.Qty); ;
if (shipmentHeader.UpdateBy == null)
{
shipmentHeader.UpdateBy = "wms";
shipmentHeader.UpdateTime = DateTime.Now;
}
_unitWork.Update(shipmentHeader);
#endregion
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
#region 记录出库单接口子表错误信息
try
{
IshipmentHeader data = _context.Set<IshipmentHeader>().AsQueryable().Where(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode)).SingleOrDefault();
if (data != null)
{
data.ErrorMessage = ex.Message;
if (data.UpdateBy == null)
{
data.UpdateBy = "wms";
data.UpdateTime = DateTime.Now;
}
_unitWork.Update(data);
}
}
catch (Exception)
{
}
#endregion
Response.Code = 500;
Response.Status = false;
Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "同步失败:" + ex.Message;
}
}
return Response;
}
}
}