|
1
2
|
package com.huaheng.api.general.service;
|
|
3
4
5
6
7
8
9
10
11
12
13
|
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
|
|
14
15
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
16
|
import com.huaheng.api.general.domain.ShipmentDomain;
|
|
17
|
import com.huaheng.common.constant.QuantityConstant;
|
|
18
19
20
|
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
|
|
21
|
import com.huaheng.framework.web.domain.AjaxResult;
|
|
22
23
24
25
26
27
28
29
30
31
|
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
|
|
32
|
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
|
|
33
34
|
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
|
|
35
36
|
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
|
|
37
38
|
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
|
|
39
40
|
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
|
|
41
42
43
44
45
46
|
/**
* @author ricard
* @time 19/11/11
*/
|
|
47
48
49
50
|
@Component
@Transactional
public class ShipmentApiService {
|
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
@Autowired
private ShipmentHeaderService shipmentHeaderService;
@Autowired
private ShipmentDetailService shipmentDetailService;
@Autowired
private WarehouseService warehouseService;
@Autowired
private CompanyService companyService;
@Autowired
private CustomerServiceImpl customerService;
@Autowired
private ShipmentTypeService shipmentTypeService;
@Autowired
private MaterialService materialService;
|
|
72
73
74
75
76
77
|
@Resource
private ShipmentHeaderHistoryService shipmentHeaderHistoryService;
@Resource
private ShipmentDetailHistoryService shipmentDetailHistoryService;
|
|
78
79
80
|
@Resource
private InventoryDetailService inventoryDetailService;
|
|
81
|
/**
|
|
82
83
|
* 出库单下发
* @param shipmentDomain
|
|
84
85
|
* @return
*/
|
|
86
|
@Transactional(rollbackFor = Exception.class)
|
|
87
|
public AjaxResult shipment(ShipmentDomain shipmentDomain) {
|
|
88
|
|
|
89
|
// 1、判断出库主单和子单列是否为空
|
|
90
91
|
ShipmentHeader shipmentHeader = shipmentDomain.getShipmentHeader();
List<ShipmentDetail> shipmentDetails = shipmentDomain.getShipmentDetails();
|
|
92
|
if (shipmentHeader == null) {
|
|
93
94
|
return AjaxResult.error("出库主单为空");
}
|
|
95
|
if (shipmentDetails.size() < 1 || shipmentDetails == null) {
|
|
96
97
98
|
return AjaxResult.error("出库子单为空");
}
|
|
99
|
// 2、检查出库主单的合法性
|
|
100
101
|
AjaxResult ajaxResult = this.checkShipmentHeader(shipmentHeader);
|
|
102
103
|
// 3、检查出库子单的合法性
shipmentDetails = this.checkShipmentDetail(shipmentDetails, shipmentHeader);
|
|
104
|
|
|
105
|
// 4、保存出库主表
|
|
106
|
BigDecimal totalQty = new BigDecimal(0);
|
|
107
108
|
for (ShipmentDetail item : shipmentDetails) {
totalQty = totalQty.add(item.getQty());
|
|
109
|
}
|
|
110
|
shipmentHeader.setWarehouseCode(QuantityConstant.WAREHOUSECODE);
|
|
111
|
shipmentHeader.setCompanyCode(shipmentHeader.getCompanyCode() == null ? QuantityConstant.COMPANYCODE : shipmentHeader.getCompanyCode());
|
|
112
|
shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_ERP);
|
|
113
114
|
shipmentHeader.setTotalLines(shipmentDetails.size());
shipmentHeader.setTotalQty(totalQty);
|
|
115
116
117
118
|
String shipmentCode = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
shipmentHeader.setCode(shipmentCode);
shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
|
|
119
|
|
|
120
|
if (!shipmentHeaderService.save(shipmentHeader)) {
|
|
121
122
123
|
throw new ServiceException("保存出库主表失败");
}
|
|
124
125
|
// 5、保存出库子表
for (ShipmentDetail shipmentDetail : shipmentDetails) {
|
|
126
|
shipmentDetail.setInventorySts(QuantityConstant.GOOD);
|
|
127
|
shipmentDetail.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
|
|
128
129
|
shipmentDetail.setReferCode(shipmentHeader.getReferCode());
shipmentDetail.setReferId(shipmentHeader.getReferId());
|
|
130
131
|
shipmentDetail.setShipmentId(shipmentHeader.getId());
shipmentDetail.setShipmentCode(shipmentHeader.getCode());
|
|
132
133
134
135
136
137
138
139
|
shipmentDetail.setInventorySts(shipmentDetail.getInventorySts() == null ? QuantityConstant.GOOD : shipmentDetail.getInventorySts());
if (StringUtils.isNotEmpty(shipmentDetail.getMaterialCode())) {
Material material = materialService.getMaterialByCode(shipmentDetail.getMaterialCode(), shipmentHeader.getWarehouseCode());
shipmentDetail.setMaterialName(material.getName());
shipmentDetail.setMaterialSpec(material.getSpec());
shipmentDetail.setMaterialUnit(material.getUnit());
}
shipmentDetail.setCreated(new Date());
|
|
140
|
shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
|
|
141
|
shipmentDetail.setZoneCode(shipmentHeader.getZoneCode());
|
|
142
143
144
|
}
int num = 0;
|
|
145
|
List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
|
|
146
147
148
149
150
151
|
// 检查卷号是否存在库存表
for (ShipmentDetail item : shipmentDetails) {
if (StringUtils.isNotEmpty(item.getRollNumber())) {
boolean tag = inventoryDetailService.isRollNumber(item, null);
if (!tag) {
throw new ServiceException(item.getRollNumber() + ",卷号不存在");
|
|
152
153
154
|
}
}
}
|
|
155
156
|
if (shipmentDetails.size() > 500) {
for (ShipmentDetail item : shipmentDetails) {
|
|
157
158
|
num++;
shipmentDetailList.add(item);
|
|
159
160
|
if (num % 500 == 0 || num == shipmentDetails.size()) {
if (!shipmentDetailService.insertDetails(shipmentDetailList)) {
|
|
161
162
|
throw new ServiceException("保存出库子表失败");
}
|
|
163
|
shipmentDetailList = new ArrayList<>();
|
|
164
165
|
}
}
|
|
166
167
|
} else {
if (!shipmentDetailService.insertDetails(shipmentDetails)) {
|
|
168
169
170
|
throw new ServiceException("保存出库子表失败");
}
}
|
|
171
|
return AjaxResult.success("出库单下发成功").setData(shipmentCode);
|
|
172
173
174
175
|
}
/**
* 检查出库主单的合法性
|
|
176
|
* @param shipmentHeader
|
|
177
178
|
* @return
*/
|
|
179
180
181
|
public AjaxResult checkShipmentHeader(ShipmentHeader shipmentHeader) {
// 1、判断仓库和货主
if (StringUtils.isEmpty(shipmentHeader.getWarehouseCode())) {
|
|
182
183
|
return AjaxResult.error("仓库为空");
}
|
|
184
|
if (StringUtils.isEmpty(shipmentHeader.getCompanyCode())) {
|
|
185
186
187
188
|
return AjaxResult.error("货主为空");
}
LambdaQueryWrapper<Warehouse> warehouseLamb = Wrappers.lambdaQuery();
|
|
189
190
191
|
warehouseLamb.eq(Warehouse::getCode, shipmentHeader.getWarehouseCode());
Warehouse warehouse = warehouseService.getOne(warehouseLamb);
if (warehouse == null) {
|
|
192
193
194
195
|
return AjaxResult.error("wms没有此仓库");
}
LambdaQueryWrapper<Company> companyLamb = Wrappers.lambdaQuery();
|
|
196
197
198
|
companyLamb.eq(Company::getCode, shipmentHeader.getCompanyCode());
Company company = companyService.getOne(companyLamb);
if (company == null) {
|
|
199
200
201
|
return AjaxResult.error("wms没有此货主");
}
|
|
202
|
// 2、判断出库类型
|
|
203
|
LambdaQueryWrapper<ShipmentType> shipmentTypeLamb = Wrappers.lambdaQuery();
|
|
204
205
206
|
shipmentTypeLamb.eq(ShipmentType::getCode, shipmentHeader.getShipmentType()).eq(ShipmentType::getWarehouseCode, shipmentHeader.getWarehouseCode());
ShipmentType shipmentType = shipmentTypeService.getOne(shipmentTypeLamb);
if (shipmentType == null) {
|
|
207
208
209
|
return AjaxResult.error("wms没有此出库类型");
}
|
|
210
|
// 3、检查上游单号是否存在
|
|
211
|
LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLamb = Wrappers.lambdaQuery();
|
|
212
|
shipmentHeaderLamb.eq(ShipmentHeader::getWarehouseCode, shipmentHeader.getWarehouseCode()).eq(ShipmentHeader::getReferCode, shipmentHeader.getReferCode());
|
|
213
|
List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLamb);
|
|
214
|
if (shipmentHeaders.size() > 0 || shipmentHeaders != null) {
|
|
215
216
217
|
return AjaxResult.success("出库单已经下发");
}
|
|
218
219
|
// 4、检查客户
if (StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())) {
|
|
220
|
LambdaQueryWrapper<Customer> customerLamb = Wrappers.lambdaQuery();
|
|
221
|
customerLamb.eq(Customer::getWarehouseCode, shipmentHeader.getWarehouseCode()).eq(Customer::getCode, shipmentHeader.getCustomerCode());
|
|
222
|
Customer customer = customerService.getOne(customerLamb);
|
|
223
|
if (customer == null) {
|
|
224
225
226
227
228
229
230
231
|
return AjaxResult.error("wms没有此客户");
}
}
return AjaxResult.success(shipmentHeader);
}
/**
* 检查出库子单的合法性
|
|
232
|
* @param shipmentDetails
|
|
233
234
|
* @return
*/
|
|
235
|
public List<ShipmentDetail> checkShipmentDetail(List<ShipmentDetail> shipmentDetails, ShipmentHeader shipmentHeader) {
|
|
236
|
|
|
237
238
239
240
|
for (ShipmentDetail shipmentDetail : shipmentDetails) {
// 1、检查物料
if (StringUtils.isEmpty(shipmentDetail.getMaterialCode())) {
throw new ServiceException("物料为空");
|
|
241
242
|
}
LambdaQueryWrapper<Material> materialLamb = Wrappers.lambdaQuery();
|
|
243
244
|
materialLamb.eq(Material::getCode, shipmentDetail.getMaterialCode()).eq(StringUtils.isNotEmpty(shipmentHeader.getWarehouseCode()),
Material::getWarehouseCode, shipmentHeader.getWarehouseCode());
|
|
245
|
Material material = materialService.getOne(materialLamb);
|
|
246
|
if (material == null) {
|
|
247
248
249
|
throw new ServiceException("wms没有此物料");
}
|
|
250
|
// 2、赋值物料属性
|
|
251
252
253
254
255
|
shipmentDetail.setMaterialName(material.getName());
shipmentDetail.setMaterialSpec(material.getSpec());
shipmentDetail.setMaterialUnit(material.getUnit());
}
return shipmentDetails;
|
|
256
|
}
|
|
257
258
259
260
261
262
|
@Transactional
public AjaxResult remove(List<String> shipmentCodeList) {
LambdaQueryWrapper<ShipmentHeader> headerQueryWrapper;
for (String shipmentCode : shipmentCodeList) {
headerQueryWrapper = Wrappers.lambdaQuery();
|
|
263
|
headerQueryWrapper.eq(ShipmentHeader::getCode, shipmentCode);
|
|
264
265
266
267
|
ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerQueryWrapper);
if (shipmentHeader == null) {
return AjaxResult.success("");
}
|
|
268
269
270
271
|
if ((shipmentHeader.getFirstStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN
&& shipmentHeader.getLastStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN)
|| (shipmentHeader.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_POOL
& shipmentHeader.getLastStatus() < QuantityConstant.SHIPMENT_HEADER_POOL)) {
|
|
272
273
|
ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
List<ShipmentDetailHistory> shipmentDetailHistoryList = new ArrayList<>();
|
|
274
|
// 查询入库单明细
|
|
275
276
277
278
|
LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode);
List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);
|
|
279
|
// 复制到入库历史实体
|
|
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentHeaderHistory, shipmentHeader);
for (ShipmentDetail shipmentDetail : list) {
ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentDetailHistory, shipmentDetail);
shipmentDetailHistoryList.add(shipmentDetailHistory);
}
shipmentHeaderHistory.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!shipmentHeaderService.removeById(shipmentHeader.getId())) {
throw new ServiceException("删除头表失败");
}
if (!shipmentHeaderHistoryService.save(shipmentHeaderHistory)) {
throw new ServiceException("新增历史出库单失败");
}
// 当存在明细时删除
if (list.size() != 0) {
|
|
296
|
// 删除入库明细
|
|
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
List<Integer> shipmentDetailIds = new ArrayList<>();
for (int i = 0; i < shipmentDetailHistoryList.size(); i++) {
shipmentDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getLoginName());
shipmentDetailHistoryList.get(i).setShipmentId(shipmentHeaderHistory.getId());
shipmentDetailIds.add(shipmentDetailHistoryList.get(i).getId());
}
if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
throw new ServiceException("删除明细表失败");
}
if (!shipmentDetailHistoryService.saveBatch(shipmentDetailHistoryList)) {
throw new ServiceException("新增明细失败");
}
}
} else {
return AjaxResult.success("出库单没有完成,无法删除");
}
}
return AjaxResult.success("删除成功");
}
|
|
317
318
|
public AjaxResult search(String shipmentCode, String companyCode, String warehouseCode) {
if (companyCode == null) {
|
|
319
320
|
return AjaxResult.error("货主编码不能为空");
}
|
|
321
|
if (warehouseCode == null) {
|
|
322
323
324
325
326
|
return AjaxResult.error("仓库编码不能为空");
}
ShipmentDomain shipmentDomain = new ShipmentDomain();
LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
|
|
327
328
329
330
|
headerLambdaQuery.eq(ShipmentHeader::getWarehouseCode, warehouseCode).eq(ShipmentHeader::getCompanyCode, companyCode).eq(ShipmentHeader::getCode,
shipmentCode);
detailLambdaQuery.eq(ShipmentDetail::getWarehouseCode, warehouseCode).eq(ShipmentDetail::getCompanyCode, companyCode).eq(ShipmentDetail::getShipmentCode,
shipmentCode);
|
|
331
332
333
334
|
ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
shipmentDomain.setShipmentHeader(shipmentHeader);
shipmentDomain.setShipmentDetails(shipmentDetailList);
|
|
335
|
return AjaxResult.success("查询成功", shipmentDomain);
|
|
336
|
}
|
|
337
|
|
|
338
|
public AjaxResult confirmShipment(String shipmentCode) {
|
|
339
340
341
|
ShipmentDomain shipmentDomain = new ShipmentDomain();
LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
|
|
342
343
|
headerLambdaQuery.eq(ShipmentHeader::getCode, shipmentCode);
detailLambdaQuery.eq(ShipmentDetail::getShipmentCode, shipmentCode);
|
|
344
345
346
347
|
ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
shipmentDomain.setShipmentHeader(shipmentHeader);
shipmentDomain.setShipmentDetails(shipmentDetailList);
|
|
348
|
return AjaxResult.success("查询成功", shipmentDomain);
|
|
349
|
}
|
|
350
|
}
|