ReceiptContainerHeaderServiceImpl.java
20.2 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
package com.huaheng.pc.receipt.receiptContainerHeader.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.domain.ContainerStatus;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.mapper.ReceiptContainerHeaderMapper;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.PushbackInputStream;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class ReceiptContainerHeaderServiceImpl extends ServiceImpl<ReceiptContainerHeaderMapper, ReceiptContainerHeader> implements ReceiptContainerHeaderService{
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private LocationService locationService;
@Resource
private ReceiptDetailService receiptDetailService;
@Resource
private ReceiptContainerDetailService receiptContainerDetailService;
@Resource
private MaterialService materialService;
@Resource
private ContainerService containerService;
@Resource
private ReceiptHeaderService receiptHeaderService;
/**
* 保存入库组盘
* @param receiptCode 入库单编码
* @param containerCode 容器编码
* @param receiptDetailId 入库单详情id
* @param locationCode 库位编码
* @param qty 收货数量
* @param locatingRule 定位规则
* @return 是否保存成功
*/
@Override
@Transactional
public AjaxResult saveCountain(String receiptCode, String containerCode, Integer receiptDetailId,
String locationCode, Integer qty, String locatingRule) {
ReceiptDetail detail = receiptDetailService.getById(receiptDetailId);
//检查容器编码合法性
Integer taskType = checkContainer(containerCode, detail.getMaterialCode());
if (taskType == 1){
//创建临时容器
containerCode = createContainer();
taskType = 100;
}else {
if (taskType == 0){ throw new ServiceException("容器状态未知"); }
}
//检查库位编码合法性
if (checkLocationCode(locationCode, containerCode, taskType) == true) {
locationService.updateStatus(locationCode, "lock");
}
/* 新建保存组盘头表记录*/
//根据容器编码查询组盘表头记录
LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
lambda.eq(ReceiptContainerHeader::getContainerCode, containerCode)
.eq(ReceiptContainerHeader::getStatus, 0);
List<ReceiptContainerHeader> list = this.list(lambda);
ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
//如果不存在记录则新建记录
if (list.size() < 1){
ReceiptDetail receiptDetail = receiptDetailService.getById(receiptDetailId);
receiptContainerHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
receiptContainerHeader.setCompanyCode(receiptDetail.getCompanyCode());
receiptContainerHeader.setContainerCode(containerCode);
Container container = containerService.findAllByCode(containerCode);
receiptContainerHeader.setContainerType(container.getContainerType());
receiptContainerHeader.setTaskType(String.valueOf(taskType));
receiptContainerHeader.setProjectNo(receiptDetail.getProjectNo());
receiptContainerHeader.setToLocation(locationCode);
receiptContainerHeader.setCreatedBy(ShiroUtils.getLoginName());
receiptContainerHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!this.save(receiptContainerHeader)){
throw new ServiceException("入库组盘头表保存失败");
}
} else {
receiptContainerHeader = list.get(0);
if (receiptContainerHeader.getStatus() > 9 && receiptContainerHeader.getStatus() < 20){
throw new ServiceException("容器已经生成任务,不能放物料了!");
}
}
List<ReceiptContainerHeader> receiptContainerHeaders = this.list(lambda);
/* 更新入库单详情的收货数量*/
//根据入库单详情id查询入库详情
ReceiptDetail receiptDetail = receiptDetailService.getById(receiptDetailId);
receiptDetail.setId(receiptDetailId);
receiptDetail.setOpenQty(new BigDecimal(qty).add(receiptDetail.getOpenQty()));
//更新入库单详情的收货数量
if (!receiptDetailService.updateById(receiptDetail)){
throw new ServiceException("更新入库单详情失败");
}
receiptDetail = receiptDetailService.getById(receiptDetailId);
receiptContainerDetailAdd(receiptContainerHeaders.get(0).getId(), receiptDetail, qty, containerCode, locationCode);
//如果单据数量等于已收数量,更新入库详情状态和入库单状态
if (receiptDetail.getTotalQty() == receiptDetail.getOpenQty()){
receiptDetail.setProcessStamp("200");
if (!receiptDetailService.updateById(receiptDetail)){
throw new ServiceException("更新入库详情处理标记失败");
}
ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
if (!receiptDetailService.updateById(receiptDetail1)){
throw new ServiceException("更新入库详情下一流程失败");
}
}
receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
return AjaxResult.success("success");
}
/**
* 批量撤销入库组盘
* @param ids
* @return
*/
@Override
@Transactional
public Boolean cancelByIds(List<Integer> ids) {
for (Integer id : ids) {
//如果已生成任务则不允许取消组盘
if (this.getById(id).getStatus() < 10){
//根据组盘头表id查询组盘明细表
LambdaQueryWrapper<ReceiptContainerDetail> containerDetailLambda = Wrappers.lambdaQuery();
containerDetailLambda.eq(ReceiptContainerDetail::getReceiptContainerId, id);
List<ReceiptContainerDetail> receiptContainerDetails = receiptContainerDetailService.list(containerDetailLambda);
//查询入库单明细,减去已收数量,更新单据
for (ReceiptContainerDetail receiptContainerDetail: receiptContainerDetails){
ReceiptDetail receiptDetail = receiptDetailService.getById(receiptContainerDetail.getReceiptDetailId());
receiptDetail.setOpenQty(receiptDetail.getOpenQty().subtract(receiptContainerDetail.getQty()));
if (!receiptDetailService.updateById(receiptDetail)){throw new ServiceException("回滚入库明细失败"); }
containerDetailLambda = Wrappers.lambdaQuery();
containerDetailLambda.eq(ReceiptContainerDetail::getReceiptId, receiptContainerDetail.getReceiptId());
List<ReceiptContainerDetail> containerDetailList = receiptContainerDetailService.list(containerDetailLambda);
//如果是临时容器,取消组盘时删除容器表
containerService.removeContainer(receiptContainerDetail.getContainerType(), receiptContainerDetail.getContainerCode());
//如果入库组盘没有该入库单的组盘信息,回滚入库单状态
if (containerDetailList == null){
ReceiptHeader receiptHeader = new ReceiptHeader();
receiptHeader.setId(receiptContainerDetail.getReceiptId());
receiptHeader.setFirstStatus(200);
receiptHeader.setLastStatus(200);
receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!receiptHeaderService.updateById(receiptHeader)){ throw new ServiceException("回滚头表状态失败"); }
}
if (!receiptContainerDetailService.removeById(receiptContainerDetail.getId())){
throw new ServiceException("删除入库组盘明细表失败,id是"+receiptContainerDetail.getId());
}
}
//删除入库组盘头表
if (!this.removeById(id)){
throw new ServiceException("删除入库组盘头表失败");
}
} else {
throw new ServiceException("已生成任务不允许取消");
}
}
return true;
}
/**
* 检查容器编码合法性
* @param containerCode
* @param materialCode
* @return 返回容器状态,如果需自动生成容器编码返回1
*/
@Transactional
public Integer checkContainer(String containerCode, String materialCode) {
if (StringUtils.isEmpty(containerCode)){
LambdaQueryWrapper<Material> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(Material::getCode, materialCode);
Material material = materialService.getOne(lambdaQueryWrapper);
if (material.getAutoGenSerialNum() == 0){
return 1; //需自动生成容器编码
} else {
throw new ServiceException("容器不能为空");
}
}
LambdaQueryWrapper<Container> lambda = Wrappers.lambdaQuery();
lambda.eq(Container::getCode, containerCode);
Container container = containerService.getOne(lambda);
if (container == null){
throw new ServiceException("该容器编号不存在");
}
//检查该容器编码是否已存任务
LambdaQueryWrapper<TaskHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(TaskHeader::getContainerCode, containerCode)
.lt(TaskHeader::getStatus, 100)
.eq(TaskHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
if (taskHeaderService.count(lambdaQueryWrapper) > 0)
throw new ServiceException("容器已经存在任务,请更换容器");
if ("empty".equals(container.getStatus())){
return 100;
}else if ("some".equals(container.getStatus())) {
return 200;
} else if ("full".equals(container.getStatus())) {
throw new ServiceException("该容器已满,请更换容器");
}
return 0;
}
/**
* 检查库位是否合法
* @param locationCode 库位编码
* @param containerCode 容器编码
* @param taskType 任务类型
* @return
*/
@Transactional
Boolean checkLocationCode(String locationCode, String containerCode, Integer taskType) {
//如果选了库位,就要校验库位和已经组盘的库位是否一致,避免重入库
if (StringUtils.isEmpty(locationCode)) {
if ("200".equals(locationCode))
throw new ServiceException("补充入库,必须填写库位");
else
return true;
}
//查询组盘头表
LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
lambda.eq(ReceiptContainerHeader::getContainerCode, containerCode)
.eq(ReceiptContainerHeader::getTaskType, taskType)
.eq(ReceiptContainerHeader::getStatus, 0)
.last("Limit 1");
ReceiptContainerHeader receiptContainerHeader = this.getOne(lambda);
if (receiptContainerHeader != null) {
if (receiptContainerHeader.getToLocation().equals(locationCode))
return true;
else
throw new ServiceException("容器(" + containerCode + ")对应的库位是:" + receiptContainerHeader.getToLocation());
}
LambdaQueryWrapper<Location> lambdaLocation = Wrappers.lambdaQuery();
lambdaLocation.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(Location::getDeleted, false)
.eq(Location::getCode, locationCode);
Location location = locationService.getOne(lambdaLocation);
if (location == null)
throw new ServiceException("库位不存在!");
if (location.getStatus().equals("empty") == false)
throw new ServiceException("库位不是空闲状态!");
if ("100".equals(taskType)) {
if (com.huaheng.common.utils.StringUtils.isNotEmpty(location.getContainerCode()))
throw new ServiceException("库位(" + containerCode + ")有容器(" + location.getContainerCode() + "),不能整盘入库!");
}
if("200".equals(taskType)) {
if (com.huaheng.common.utils.StringUtils.isEmpty(location.getContainerCode()))
throw new ServiceException("库位(" + locationCode + ")没有容器,不能补充入库!");
else
if (location.getContainerCode().equals(containerCode) == false)
throw new ServiceException("库位(" + containerCode + ")对应的容器是:" + location.getContainerCode()+ ",请重选库位!");
}
LambdaQueryWrapper<Location> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(Location::getStatus, "empty")
.ne(Location::getContainerCode, "")
.eq(Location::getIColumn, location.getIColumn())
.eq(Location::getILayer, location.getILayer())
.gt(Location::getIRow, location.getIRow())
.lt(Location::getIRow, location.getIRow());
int frontCount = locationService.count(lambdaQueryWrapper);
if (frontCount > 0) {
throw new ServiceException("库位前面有货物挡住,不能入库");
}
return true;
}
/**
* 增加组盘明细记录
* @param receiptContainerHeaderId 入库组盘头表id
* @param receiptDetail 入库详情
* @param qty 收货数量
* @param containerCode 容器编码
*/
@Transactional
public void receiptContainerDetailAdd(Integer receiptContainerHeaderId, ReceiptDetail receiptDetail, Integer qty, String containerCode, String locationCode){
LambdaQueryWrapper<ReceiptContainerDetail> lambda = Wrappers.lambdaQuery();
lambda.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerHeaderId)
.eq(ReceiptContainerDetail::getReceiptId, receiptDetail.getReceiptId())
.eq(ReceiptContainerDetail::getReceiptDetailId, receiptDetail.getId())
.last("Limit 1");
ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailService.getOne(lambda);
if (receiptContainerDetail == null){
//查询入库头表
LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambda = Wrappers.lambdaQuery();
receiptHeaderLambda.eq(ReceiptHeader::getId, receiptDetail.getReceiptId())
.eq(ReceiptHeader::getCode, receiptDetail.getReceiptCode());
ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambda);
//查询容器
LambdaQueryWrapper<Container> containerLambda = Wrappers.lambdaQuery();
containerLambda.eq(Container::getCode, containerCode);
Container container = containerService.getOne(containerLambda);
receiptContainerDetail = new ReceiptContainerDetail();
receiptContainerDetail.setReceiptContainerId(receiptContainerHeaderId);
receiptContainerDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
receiptContainerDetail.setReceiptId(receiptDetail.getReceiptId());
receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
receiptContainerDetail.setReceiptCode(receiptDetail.getReceiptCode());
receiptContainerDetail.setReceiptType(receiptHeader.getReceiptType());
receiptContainerDetail.setLocationCode(locationCode);
receiptContainerDetail.setContainerCode(container.getCode());
receiptContainerDetail.setContainerType(container.getContainerType());
receiptContainerDetail.setCompanyCode(receiptDetail.getCompanyCode());
receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
receiptContainerDetail.setQty(new BigDecimal(qty));
receiptContainerDetail.setSupplierCode(receiptDetail.getSupplierCode());
receiptContainerDetail.setBatch(receiptDetail.getBatch());
receiptContainerDetail.setLot(receiptDetail.getLot());
receiptContainerDetail.setProjectNo(receiptDetail.getProjectNo());
receiptContainerDetail.setManufactureDate(receiptDetail.getManufactureDate());
receiptContainerDetail.setExpirationDate(receiptDetail.getExpirationDate());
receiptContainerDetail.setAgingDate(receiptDetail.getAgingDate());
receiptContainerDetail.setInventorySts(receiptDetail.getInventorySts());
receiptContainerDetail.setCreatedBy(ShiroUtils.getLoginName());
receiptContainerDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!receiptContainerDetailService.save(receiptContainerDetail)){
throw new ServiceException("保存入库组盘详情失败");
}
} else {
receiptContainerDetail.setQty(receiptContainerDetail.getQty().add(BigDecimal.valueOf(qty)));
if (!receiptContainerDetailService.updateById(receiptContainerDetail)){
throw new ServiceException("更新入库组盘详情失败");
}
}
}
/**
* 自动生成容器编码
* @return 容器编码
*/
@Transactional
public String createContainer(){
String code = MessageFormat.format("{0}{1}",
"LS",
String.format("%d", Calendar.getInstance().getTimeInMillis()));
Container container = new Container();
container.setCode(code);
container.setWarehouseCode(ShiroUtils.getWarehouseCode());
container.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
container.setContainerType("LS");
container.setCreatedBy(ShiroUtils.getLoginName());
container.setLastUpdated(new Date());
container.setLastUpdatedBy(ShiroUtils.getLoginName());
container.setEnable(true);
container.setStatus("empty");
if (!containerService.save(container)){
throw new ServiceException("新增容器失败");
}
return code;
}
}