IShipmentApp.cs
10.9 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
using System;
using System.Linq;
using Infrastructure;
using WebRepository;
namespace WebApp
{
/// <summary>
/// 出库单下发接口App
/// </summary>
public partial class IShipmentApp : ApiApp
{
public IShipmentApp(IUnitWork unitWork, IAuth auth, BaseDBContext context) : base(unitWork, auth, context)
{
}
public string InsertShipment(IShipmentModel shipments)
{
Response Response = new Response();
if (!CheckLogin())
{
Response.Code = 500;
Response.Status = false;
Response.Message = "请先登录!";
return JsonHelper.Instance.Serialize(Response);
}
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.新建;
if (shipmentDetail.Id == null)
{
if (shipmentDetail.CreateBy == null)
{
shipmentDetail.CreateBy = "wms";
shipmentDetail.CreateTime = DateTime.Now;
}
_unitWork.Add(shipmentDetail);
}
else
{
if (shipmentDetail.UpdateBy == null)
{
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
}
catch (Exception ex)
{
#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 JsonHelper.Instance.Serialize(Response);
}
}
}