Blame view

WebApp/Apps/api/IShipmentApp.cs 26.4 KB
霍尔 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Linq;
using Infrastructure;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 出库单下发接口App
    /// </summary>

    public partial class IShipmentApp : ApiApp
    {
gy.huang authored
14
15
        public IRepository<ShipmentHeader> _app;
        public IShipmentApp(IUnitWork unitWork, IAuth auth, BaseDBContext context, IRepository<ShipmentHeader> shipmentHeader) : base(unitWork, auth, context)
霍尔 authored
16
        {
gy.huang authored
17
            _app = shipmentHeader;
霍尔 authored
18
19
        }
gy.huang authored
20
        public Response InsertShipment(IShipmentModel shipments)
霍尔 authored
21
22
23
24
25
26
27
28
        {
            Response Response = new Response();

            if (!CheckLogin())
            {
                Response.Code = 500;
                Response.Status = false;
                Response.Message = "请先登录!";
gy.huang authored
29
                return Response;
霍尔 authored
30
            }
霍尔 authored
31
            using (var tran = _context.Database.BeginTransaction())
霍尔 authored
32
            {
霍尔 authored
33
                try
霍尔 authored
34
                {
霍尔 authored
35
36
37
38
39
                    #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)
霍尔 authored
40
                    {
霍尔 authored
41
42
43
44
45
46
                        if (shipments.shipmentHeader.CreateBy == null)
                        {
                            shipments.shipmentHeader.CreateBy = "wms";
                            shipments.shipmentHeader.CreateTime = DateTime.Now;
                        }
                        _unitWork.Add(shipments.shipmentHeader);
霍尔 authored
47
                    }
霍尔 authored
48
                    else
霍尔 authored
49
                    {
霍尔 authored
50
51
52
53
54
55
                        if (shipments.shipmentHeader.UpdateBy == null)
                        {
                            shipments.shipmentHeader.UpdateBy = "wms";
                            shipments.shipmentHeader.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(shipments.shipmentHeader);
霍尔 authored
56
                    }
霍尔 authored
57
                    #endregion
霍尔 authored
58
霍尔 authored
59
60
61
                    #region 保存出库单主表
                    ShipmentHeader shipmentHeader = _unitWork.FindSingle<ShipmentHeader>(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode));
                    if (shipmentHeader != null)
霍尔 authored
62
                    {
霍尔 authored
63
64
65
66
67
68
69
70
                        if (shipmentHeader.FirstStatus != ShipmentHeaderStatus.新建)
                        {
                            throw new Exception("单据进入订单池后,不允许修改!");
                        }
                    }
                    else
                    {
                        shipmentHeader = new ShipmentHeader();
霍尔 authored
71
72
                    }
霍尔 authored
73
74
75
                    shipmentHeader.Type = shipments.shipmentHeader.Type;
                    shipmentHeader.SourceCode = shipments.shipmentHeader.SourceCode;
                    shipmentHeader.WarehouseType = shipments.shipmentHeader.WarehouseType;
霍尔 authored
76
霍尔 authored
77
78
79
80
81
82
                    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;
霍尔 authored
83
霍尔 authored
84
85
86
87
88
89
                    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;
霍尔 authored
90
霍尔 authored
91
                    if (shipmentHeader.Id == null)
霍尔 authored
92
                    {
霍尔 authored
93
94
95
96
97
98
                        if (shipmentHeader.CreateBy == null)
                        {
                            shipmentHeader.CreateBy = "wms";
                            shipmentHeader.CreateTime = DateTime.Now;
                        }
                        _unitWork.Add(shipmentHeader);
霍尔 authored
99
                    }
霍尔 authored
100
                    else
霍尔 authored
101
                    {
霍尔 authored
102
103
104
105
106
107
                        if (shipmentHeader.UpdateBy == null)
                        {
                            shipmentHeader.UpdateBy = "wms";
                            shipmentHeader.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(shipmentHeader);
霍尔 authored
108
                    }
霍尔 authored
109
                    #endregion
霍尔 authored
110
霍尔 authored
111
112
113
                    short? totalLines = 0;
                    decimal? qty = 0;
                    foreach (var item in shipments.shipmentDetails)
霍尔 authored
114
                    {
霍尔 authored
115
                        try
霍尔 authored
116
                        {
霍尔 authored
117
118
119
120
121
122
                            #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)
霍尔 authored
123
                            {
霍尔 authored
124
125
126
127
128
129
                                if (item.CreateBy == null)
                                {
                                    item.CreateBy = "wms";
                                    item.CreateTime = DateTime.Now;
                                }
                                _unitWork.Add(item);
霍尔 authored
130
                            }
霍尔 authored
131
                            else
霍尔 authored
132
                            {
霍尔 authored
133
134
135
136
137
138
                                if (item.UpdateBy == null)
                                {
                                    item.UpdateBy = "wms";
                                    item.UpdateTime = DateTime.Now;
                                }
                                _unitWork.Update(item);
霍尔 authored
139
                            }
霍尔 authored
140
                            #endregion
霍尔 authored
141
霍尔 authored
142
143
144
145
146
147
                            #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();
                            }
霍尔 authored
148
霍尔 authored
149
150
151
152
153
154
155
156
157
                            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;
霍尔 authored
158
霍尔 authored
159
160
161
162
163
164
                            shipmentDetail.ShipmentId = shipmentHeader.Id;
                            shipmentDetail.ShipmentCode = shipmentHeader.Code;
                            shipmentDetail.QtyDivided = 0;
                            shipmentDetail.QtyCompleted = 0;
                            shipmentDetail.Price = 0;
                            shipmentDetail.Status = ShipmentHeaderStatus.新建;
霍尔 authored
165
166
                            //成品库逻辑
霍尔 authored
167
168
169
170
171
                            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)
gy.huang authored
172
                            {
霍尔 authored
173
                                if (con.Type == ContainerType.木栈板母板_1 || con.Type == ContainerType.木栈板母板_2)
gy.huang authored
174
                                {
霍尔 authored
175
176
177
178
                                    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 == "")
gy.huang authored
179
                                    {
霍尔 authored
180
181
                                        //如果没有容器则直接出库
                                        if (stdel.Containercode == "")
gy.huang authored
182
                                        {
霍尔 authored
183
184
                                            //建立容器出库任务
                                            if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
gy.huang authored
185
                                            {
霍尔 authored
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
                                                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.待下发任务;
                                                    _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;
                                                    _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);
霍尔 authored
225
226
227
                                                    Location Location = _unitWork.Find<Location>(n => n.ContainerCode == con.Code).FirstOrDefault();
                                                    Location.IsStop = true;
                                                    _unitWork.Update(Location);
228
229
230
                                                    Container Container = _unitWork.Find<Container>(n => n.Code == con.Code).FirstOrDefault();
                                                    Container.IsLock = 1;
                                                    _unitWork.Update(Container);
霍尔 authored
231
232
233
234
                                                    stdel.Containercode = con.Code;
                                                    _unitWork.Update(stdel);
                                                    shipmentDetail.Status = ShipmentHeaderStatus.回传;
                                                }
gy.huang authored
235
236
237
                                            }
                                            else
                                            {
霍尔 authored
238
239
240
                                                Response.Code = 500;
                                                Response.Status = false;
                                                Response.Message = con.Code + "托盘已有任务!";
gy.huang authored
241
                                                return Response;
gy.huang authored
242
243
                                            }
                                        }
霍尔 authored
244
                                        else
gy.huang authored
245
                                        {
霍尔 authored
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
                                            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.待下发任务;
                                                        _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;
                                                        _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 + "托盘已有任务!";
gy.huang authored
292
                                                    return Response;
霍尔 authored
293
294
295
                                                }
                                            }
                                            else
gy.huang authored
296
                                            {
霍尔 authored
297
                                                //空栈板回库逻辑待优化
gy.huang authored
298
299
                                                Task ptask = new Task();
                                                TaskDetail ptaskDetail = new TaskDetail();
霍尔 authored
300
                                                var taskNo = _app.GetTaskNo(TaskNo.容器回库);
gy.huang authored
301
302
                                                ptask.TaskNo = taskNo;
                                                ptask.OrderCode = taskNo;
霍尔 authored
303
                                                ptask.BusinessType = BusinessType.入库_其他入库单;
gy.huang authored
304
305
306
307
308
                                                ptask.FirstStatus = TaskStatus.待下发任务;
                                                ptask.LastStatus = TaskStatus.待下发任务;
                                                _unitWork.Add(ptask);

                                                ptaskDetail.TaskNo = taskNo;
霍尔 authored
309
310
                                                ptaskDetail.OrderCode = shipmentDetail.ShipmentCode;
                                                ptaskDetail.TaskType = TaskType.空容器入库;
gy.huang authored
311
312
                                                ptaskDetail.ContainerCode = stdel.Containercode;
                                                ptaskDetail.SourceLocation = stdel.Code;
霍尔 authored
313
314
                                                ptaskDetail.DestinationLocation = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位).FirstOrDefault().Code;
                                                ptaskDetail.Station = stdel.Code;
gy.huang authored
315
316
317
318
319
320
321
                                                ptaskDetail.OderQty = 0;
                                                ptaskDetail.ContainerQty = 0;
                                                ptaskDetail.HadQty = 0;
                                                ptaskDetail.Roadway = 0;
                                                ptaskDetail.Status = TaskStatus.待下发任务;
                                                ptaskDetail.Priority = 0;
                                                _unitWork.Add(ptaskDetail);
霍尔 authored
322
                                                shipmentDetail.Status = ShipmentHeaderStatus.分配完成;
gy.huang authored
323
324
325
326
327
                                            }
                                        }
                                    }
                                    else
                                    {
霍尔 authored
328
329
330
                                        Response.Code = 500;
                                        Response.Status = false;
                                        Response.Message = "请将" + st.Containercode + "托盘入库!";
gy.huang authored
331
                                        return Response;
gy.huang authored
332
333
334
335
336
337
338
                                    }
                                }
                            }
                            else
                            {
                                Response.Code = 500;
                                Response.Status = false;
霍尔 authored
339
                                Response.Message = "无可用托盘!";
gy.huang authored
340
                                return Response;
gy.huang authored
341
342
                            }
霍尔 authored
343
344
345
346
347
348
349
350
351
352
353

                            if (shipmentDetail.Id == null)
                            {
                                if (shipmentDetail.CreateBy == "")
                                {
                                    shipmentDetail.CreateBy = "wms";
                                    shipmentDetail.CreateTime = DateTime.Now;
                                }
                                _unitWork.Add(shipmentDetail);
                            }
                            else
霍尔 authored
354
                            {
霍尔 authored
355
356
357
358
359
360
                                if (shipmentDetail.UpdateBy == "")
                                {
                                    shipmentDetail.UpdateBy = "wms";
                                    shipmentDetail.UpdateTime = DateTime.Now;
                                }
                                _unitWork.Update(shipmentDetail);
霍尔 authored
361
                            }
霍尔 authored
362
363
364
365
                            #endregion

                            totalLines += 1;
                            qty += item.Qty;
霍尔 authored
366
                        }
霍尔 authored
367
                        catch (Exception ex)
霍尔 authored
368
                        {
霍尔 authored
369
370
371
                            #region 记录出库单接口子表错误信息
                            IshipmentDetail data = _context.Set<IshipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
                            if (data != null)
霍尔 authored
372
                            {
霍尔 authored
373
374
375
376
377
378
379
                                data.ErrorMessage = ex.Message;
                                if (data.UpdateBy == null)
                                {
                                    data.UpdateBy = "wms";
                                    data.UpdateTime = DateTime.Now;
                                }
                                _unitWork.Update(data);
霍尔 authored
380
                            }
霍尔 authored
381
382
383
384
385
                            #endregion

                            Response.Code = 500;
                            Response.Status = false;
                            Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "SourceCode:" + item.SourceCode + ",MaterialCode:" + item.MaterialCode + "同步失败:" + ex.Message;
霍尔 authored
386
                        }
霍尔 authored
387
                    }
霍尔 authored
388
霍尔 authored
389
390
391
392
393
394
395
                    #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;
霍尔 authored
396
                    }
霍尔 authored
397
398
399
400
401
402
403
404
405
                    _unitWork.Update(shipmentHeader);
                    #endregion
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    #region 记录出库单接口子表错误信息
                    try
霍尔 authored
406
                    {
霍尔 authored
407
                        IshipmentHeader data = _context.Set<IshipmentHeader>().AsQueryable().Where(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode)).SingleOrDefault();
霍尔 authored
408
409
410
411
412
413
414
415
416
417
418
                        if (data != null)
                        {
                            data.ErrorMessage = ex.Message;
                            if (data.UpdateBy == null)
                            {
                                data.UpdateBy = "wms";
                                data.UpdateTime = DateTime.Now;
                            }
                            _unitWork.Update(data);
                        }
                    }
霍尔 authored
419
                    catch (Exception)
霍尔 authored
420
421
                    {
                    }
霍尔 authored
422
                    #endregion
霍尔 authored
423
霍尔 authored
424
425
426
427
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "同步失败:" + ex.Message;
                }
霍尔 authored
428
429
            }
gy.huang authored
430
            return Response;
霍尔 authored
431
432
433
434
        }
    }
}