TransferTaskService.java
13.8 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
package com.huaheng.pc.task.taskHeader.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
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.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 移库任务
*/
@Service
public class TransferTaskService {
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private TaskDetailService taskDetailService;
@Resource
private ContainerService containerService;
@Resource
private LocationService locationService;
@Resource
private InventoryDetailService inventoryDetailService;
@Resource
private InventoryHeaderService inventoryHeaderService;
@Resource
private InventoryTransactionService inventoryTransactionService;
/**
* 创建立库移库任务
* @param sourceLocationCode 源库位
* @param desLocationCode 目的库位
* @return
*/
@Transactional(rollbackFor = Exception.class)
public AjaxResult createTransferTask(String sourceLocationCode, String desLocationCode) {
Location sourceLocation = locationService.findLocationByCode(sourceLocationCode);
Location desLocation = locationService.findLocationByCode(desLocationCode);
Integer preTaskNo = 0;
if (StringUtils.isNull(sourceLocation)) {
return AjaxResult.error("源库位:" + sourceLocation + "未找到");
}
if (!"empty".equals(sourceLocation.getStatus())) {
return AjaxResult.error("源库位:" + sourceLocation + "状态非空闲");
}
if (StringUtils.isEmpty(sourceLocation.getContainerCode())) {
return AjaxResult.error("源库位:" + sourceLocation + "不存在托盘");
}
//这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
//校验入库组盘
if (inventoryHeaderService.getUncompleteReceiptContainer(sourceLocationCode) > 0) {
return AjaxResult.error("源库位:" + sourceLocation + "存在入库组盘,不能移库");
}
if (inventoryHeaderService.getUncompleteShipmentContainer(sourceLocationCode) > 0) {
return AjaxResult.error("源库位:" + sourceLocation + "存在出库组盘,不能移库");
}
if (StringUtils.isNull(desLocation)) {
return AjaxResult.error("目标库位:" + desLocationCode + "未找到");
}
if (!"empty".equals(desLocation.getStatus())) {
return AjaxResult.error("目标库位:" + desLocationCode + "状态非空闲");
}
if (StringUtils.isNotEmpty(desLocation.getContainerCode())) {
return AjaxResult.error("目标库位:" + desLocationCode + "存在托盘");
}
if (inventoryHeaderService.getUncompleteReceiptContainer(desLocationCode) > 0) {
return AjaxResult.error("目标库位:" + desLocationCode + "存在入库组盘,不能移库");
}
if (taskHeaderService.getUncompleteTaskInNear(desLocation) > 0) {
return AjaxResult.error("目标库位:" + desLocationCode + "旁边存在任务,请完成任务以后再分配");
}
if(sourceLocation.getRowFlag() == 1) {
Location location1 = locationService.getNear(sourceLocation); //内侧库位
String locationCode = location1.getCode();
if(StringUtils.isNotEmpty(location1.getContainerCode())) {
LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, locationCode)
.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
if(taskHeader != null) {
preTaskNo = taskHeader.getId();
} else {
return AjaxResult.error("源库位:" + sourceLocationCode + "旁边库位有托盘无法移库");
}
} else {
LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
taskHeaderLambdaQueryWrapper.eq(TaskHeader::getToLocation, locationCode)
.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
if(taskHeader != null) {
return AjaxResult.error("源库位:" + sourceLocationCode + "旁边库位有任务无法移库");
}
}
}
TaskHeader taskHeader = new TaskHeader();
taskHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
taskHeader.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_TRANSFER);
taskHeader.setContainerCode(sourceLocation.getContainerCode());
taskHeader.setFromLocation(sourceLocationCode);
taskHeader.setToLocation(desLocationCode);
taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
taskHeader.setPreTaskNo(preTaskNo);
if (taskHeaderService.save(taskHeader)) {
locationService.updateStatus(sourceLocationCode, "lock");
locationService.updateStatus(desLocationCode, "lock");
containerService.updateLocationCodeAndStatus(sourceLocation.getContainerCode(), sourceLocationCode, "lock");
} else {
throw new ServiceException("创建任务失败");
}
LambdaUpdateWrapper<InventoryDetail> detailLambdaUpdateWrapper = Wrappers.lambdaUpdate();
detailLambdaUpdateWrapper.eq(InventoryDetail::getLocationCode, sourceLocationCode);
List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(detailLambdaUpdateWrapper);
for (InventoryDetail inventoryDetail : inventoryDetailList) {
TaskDetail taskDetail = new TaskDetail();
taskDetail.setTaskId(taskHeader.getId());
taskDetail.setTaskType(taskHeader.getTaskType());
taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());;
taskDetail.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
taskDetail.setMaterialName(inventoryDetail.getMaterialName());
taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
taskDetail.setQty(inventoryDetail.getQty());
taskDetail.setFromLocation(inventoryDetail.getLocationCode());
taskDetail.setToLocation(desLocationCode);
taskDetail.setContainerCode(inventoryDetail.getContainerCode());
taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
taskDetail.setReferenceCode(inventoryDetail.getReferCode());
taskDetail.setAttribute1(inventoryDetail.getAttribute1());
taskDetail.setAttribute2(inventoryDetail.getAttribute2());
taskDetail.setAttribute3(inventoryDetail.getAttribute3());
taskDetail.setBatch(inventoryDetail.getBatch());
taskDetail.setLot(inventoryDetail.getLot());
taskDetail.setProjectNo(inventoryDetail.getProjectNo());
taskDetail.setManufactureDate(inventoryDetail.getManufactureDate());
taskDetail.setExpirationDate(inventoryDetail.getExpirationDate());
taskDetail.setAgingDate(inventoryDetail.getAgingDate());
taskDetail.setInventorySts(inventoryDetail.getInventorySts());
taskDetail.setFromInventoryId(inventoryDetail.getId());
taskDetail.setToInventoryId(inventoryDetail.getId());
inventoryDetail.setLockedQty(inventoryDetail.getQty());
if (!taskDetailService.save(taskDetail) || !inventoryDetailService.updateById(inventoryDetail)) {
throw new ServiceException("创建任务失败");
}
}
return AjaxResult.success(taskHeader.getId());
}
/**
* 完成移库任务
* @param taskHeader
*/
public void completeTransferTask(TaskHeader taskHeader) {
taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
List<TaskDetail> taskDetailList = taskDetailService.findByTaskId(taskHeader.getId());
/* 库存头表id*/
List<Integer> inventoryHeadIdList = new ArrayList<>();
for (TaskDetail taskDetail : taskDetailList) {
taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
InventoryDetail inventoryDetail = inventoryDetailService.getById(taskDetail.getToInventoryId());
inventoryHeadIdList.add(inventoryDetail.getInventoryHeaderId());
InventoryTransaction inventoryTransaction = new InventoryTransaction();
inventoryTransaction.setWarehouseCode(taskDetail.getWarehouseCode());
inventoryTransaction.setCompanyCode(taskDetail.getCompanyCode());
inventoryTransaction.setLocationCode(taskDetail.getToLocation());
inventoryTransaction.setContainerCode(taskDetail.getContainerCode());
inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFERINTO);
inventoryTransaction.setMaterialCode(taskDetail.getMaterialCode());
inventoryTransaction.setMaterialName(taskDetail.getMaterialName());
inventoryTransaction.setMaterialSpec(taskDetail.getMaterialSpec());
inventoryTransaction.setMaterialUnit(taskDetail.getMaterialUnit());
inventoryTransaction.setTaskQty(taskDetail.getQty());
inventoryTransaction.setInventorySts(taskDetail.getInventorySts());
inventoryTransaction.setReferCode(taskDetail.getReferenceCode());
inventoryTransaction.setBatch(taskDetail.getBatch());
inventoryTransaction.setLot(taskDetail.getLot());
inventoryTransaction.setProjectNo(taskDetail.getProjectNo());
inventoryTransaction.setQcCheck(taskDetail.getQcCheck());
inventoryTransaction.setManufactureDate(taskDetail.getManufactureDate());
inventoryTransaction.setExpirationDate(taskDetail.getExpirationDate());
inventoryTransaction.setAgingDate(taskDetail.getAgingDate());
inventoryTransaction.setAttributeId(String.valueOf(taskDetail.getAttributeId()));
inventoryTransaction.setAttribute1(taskDetail.getAttribute1());
inventoryTransaction.setAttribute2(taskDetail.getAttribute2());
inventoryTransaction.setAttribute3(taskDetail.getAttribute3());
inventoryTransactionService.save(inventoryTransaction);
inventoryTransaction.setLocationCode(taskDetail.getFromLocation());
inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFEROUT);
inventoryTransactionService.save(inventoryTransaction);
}
if (!taskDetailService.saveOrUpdateBatch(taskDetailList) ||
!taskHeaderService.saveOrUpdate(taskHeader)) {
throw new ServiceException("任务单据状态更新失败!");
}
locationService.updateContainerCodeAndStatus(taskHeader.getFromLocation(), "", "empty");
locationService.updateContainerCodeAndStatus(taskHeader.getToLocation(), taskHeader.getContainerCode(), "empty");
containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(), taskHeader.getToLocation(), "some");
inventoryHeadIdList = inventoryHeadIdList.stream().distinct().collect(Collectors.toList());
LambdaUpdateWrapper<InventoryHeader> headerUpdateWrapper = Wrappers.lambdaUpdate();
headerUpdateWrapper.set(InventoryHeader::getLocationCode, taskHeader.getToLocation())
.in(InventoryHeader::getId, inventoryHeadIdList);
LambdaUpdateWrapper<InventoryDetail> detailUpdateWrapper = Wrappers.lambdaUpdate();
detailUpdateWrapper.set(InventoryDetail::getLocationCode, taskHeader.getToLocation())
.set(InventoryDetail::getLockedQty, BigDecimal.ZERO)
.in(InventoryDetail::getInventoryHeaderId, inventoryHeadIdList);
if (!inventoryHeaderService.update(headerUpdateWrapper) ||
!inventoryDetailService.update(detailUpdateWrapper)) {
throw new ServiceException("完成任务失败");
}
}
}