CycleCountTaskService.java
18.4 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
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.domain.Container;
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.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetailChild;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailChildService;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.cycleCountHeader.domain.CycleCountHeader;
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
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.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.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 盘点服务层
* */
@Service
public class CycleCountTaskService {
@Resource
private CycleCountDetailService cycleCountDetailService;
@Resource
private CycleCountHeaderService cycleCountHeaderService;
@Resource
private CycleCountDetailChildService cycleCountDetailChildService;
@Resource
private TaskDetailService taskDetailService;
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private LocationService locationService;
@Resource
private ContainerService containerService;
@Resource
private InventoryHeaderService inventoryHeaderService;
@Resource
private InventoryDetailService inventoryDetailService;
/**
* 盘点任务取消
*/
@Transactional(rollbackFor = Exception.class)
public AjaxResult cycleCountCancelTask(Integer[] taskIds) {
for (int taskId : taskIds) {
TaskHeader taskHeader = taskHeaderService.getById(taskId);
if (taskHeader == null) {
return AjaxResult.error("任务" + taskId + "未找到,操作中止");
}
if (taskHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
return AjaxResult.error("存在任务" + taskHeader.getId() + "已下发或执行,操作中止");
}
//查出任务明细
TaskDetail taskDetail1 = new TaskDetail();
taskDetail1.setTaskId(taskHeader.getId());
taskDetail1.setWarehouseCode(taskHeader.getWarehouseCode());
taskDetail1.setCompanyCode(taskHeader.getCompanyCode());
LambdaQueryWrapper<TaskDetail> td = Wrappers.lambdaQuery(taskDetail1);
List<TaskDetail> taskDetailList = taskDetailService.list(td);
if (taskDetailList.size() < 1) {
throw new ServiceException("任务明细条目错误");
}
TaskDetail taskDetail = taskDetailList.get(0);
//删除子任务
LambdaQueryWrapper<TaskDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(TaskDetail::getTaskId, taskHeader.getId());
taskDetailService.remove(lambdaQueryWrapper);
//删除主任务
taskHeaderService.removeById(taskHeader.getId());
//盘点取消任务,恢复明细状态为1
if (taskHeader.getInternalTaskType().equals(QuantityConstant.TASK_INTENERTYPE_CYCLECOUNT)) {
CycleCountDetail cycleCountDetail = new CycleCountDetail();
cycleCountDetail.setCompanyCode(taskDetail.getCompanyCode());
cycleCountDetail.setWarehouseCode(taskDetail.getWarehouseCode());
cycleCountDetail.setLocationCode(taskDetail.getFromLocation());
cycleCountDetail.setContainerCode(taskDetail.getContainerCode());
cycleCountDetail.setCycleCountHeadCode(taskDetail.getBillCode());//盘点单Code
cycleCountDetail.setId(taskDetail.getBillDetailId());//盘点细单ID
LambdaQueryWrapper<CycleCountDetail> lam = Wrappers.lambdaQuery(cycleCountDetail);
List<CycleCountDetail> cycleCountDetailList = cycleCountDetailService.list(lam);//
for (CycleCountDetail item : cycleCountDetailList) {
item.setTaskHeaderId(null);
item.setTaskDetailId(null);
item.setLastUpdated(new Date());
item.setLastUpdatedBy(ShiroUtils.getLoginName());
item.setEnableStatus(1);
cycleCountDetailService.saveOrUpdate(item);
}
}
if (taskHeader.getToLocation() != null) {
//更新托盘、库位状态
locationService.updateStatus(taskHeader.getToLocation(), QuantityConstant.STATUS_LOCATION_EMPTY);
}
LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, taskHeader.getContainerCode());
InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
Container container = new Container();
container.setStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
if(inventoryHeader != null) {
if(inventoryHeader.getContainerStatus().equals(QuantityConstant.STATUS_CONTAINER_SOME)) {
container.setStatus(QuantityConstant.STATUS_CONTAINER_SOME);
}
}
LambdaUpdateWrapper<Container> containerUpdateWrapper = Wrappers.lambdaUpdate();
containerUpdateWrapper.eq(Container::getCode, taskHeader.getContainerCode());
containerService.update(container, containerUpdateWrapper);
}
return AjaxResult.success("取消任务成功!");
}
/**
* 生成单条盘点任务
* @param cycleCoutDetailId
* @return
*/
@Transactional(rollbackFor = Exception.class)
public AjaxResult createCycleCoutTaskByDetailId(Integer cycleCoutDetailId) {
/*任务主表中存在库位,在盘点明细中生成任务时,不同的容器生成不同的主任务*/
//校验有没有相同容器正在执行任务.
CycleCountDetail cycleCountDetail = cycleCountDetailService.getById(cycleCoutDetailId);
if(StringUtils.isNull(cycleCountDetail)){
return AjaxResult.error("盘点明细ID错误,没有该条明细");
}
//查出明细的子单
CycleCountDetailChild child = new CycleCountDetailChild();
child.setCycleCountHeadCode(cycleCountDetail.getCycleCountHeadCode());
child.setCycleCountDetailId(cycleCountDetail.getId());
LambdaQueryWrapper<CycleCountDetailChild> childLambdaQueryWrapper = Wrappers.lambdaQuery(child);
List<CycleCountDetailChild> childList = cycleCountDetailChildService.list(childLambdaQueryWrapper);
if(childList.size() < 1){
return AjaxResult.error("盘点明细没有子单");
}
Location location = locationService.getLocationByCode(
cycleCountDetail.getLocationCode(), ShiroUtils.getWarehouseCode());
/*if(!loc.getStatus().equals(QuantityConstant.STATUS_EMPTY)){
return AjaxResult.error(cycleCountDetail.getLocationCode() + "库位不在空闲状态,请先完成其他任务,操作失败!");
}*/
if(StringUtils.isEmpty(location.getContainerCode())){
return AjaxResult.error(cycleCountDetail.getLocationCode() + "库位中没有容器,操作失败!");
}
//生成任务同时锁定库位
locationService.updateStatus(location.getCode(), QuantityConstant.STATUS_LOCATION_LOCK);
//每个明细单生成一张主任务,子单就是任务明细。
TaskHeader task = new TaskHeader();
task.setWarehouseCode(ShiroUtils.getWarehouseCode());
task.setCompanyCode(cycleCountDetail.getCompanyCode());
task.setAllocationHeadId(cycleCountDetail.getId());//明细ID写入主任务
if(location != null) {
task.setZoneCode(location.getZoneCode());
}
task.setReferenceCode(cycleCountDetail.getCycleCountHeadCode()); //写入盘点主单号
task.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_CYCLECOUNT);
task.setTaskType(QuantityConstant.TASK_TYPE_CYCLECOUNT);
task.setContainerCode(cycleCountDetail.getContainerCode());
task.setStatus(QuantityConstant.TASK_STATUS_BUILD);
task.setFromLocation(cycleCountDetail.getLocationCode());
task.setToLocation(cycleCountDetail.getLocationCode());
task.setCreated(new Date());
task.setCreatedBy(ShiroUtils.getLoginName());
task.setLastUpdatedBy(ShiroUtils.getLoginName());
task.setLastUpdated(new Date());
if(taskHeaderService.save(task)){
//锁定库位状态
locationService.updateStatus(location.getContainerCode(), QuantityConstant.STATUS_LOCATION_LOCK);
cycleCountDetail.setTaskHeaderId(task.getId()); //盘点明细修改状态task数据源
}else{
throw new ServiceException("盘点任务主表生成失败!");
}
//写入任务细表
TaskDetail taskDetail = new TaskDetail();
for(CycleCountDetailChild item: childList){
taskDetail.setTaskId(task.getId());//主单ID
taskDetail.setTaskType(task.getTaskType());
taskDetail.setFromLocation(task.getFromLocation());
taskDetail.setToLocation(task.getToLocation());
taskDetail.setContainerCode(task.getContainerCode());
taskDetail.setInternalTaskType(task.getTaskType());
taskDetail.setWarehouseCode(task.getWarehouseCode());
taskDetail.setBillCode(item.getCycleCountHeadCode());
taskDetail.setBillDetailId(item.getCycleCountDetailId());
taskDetail.setMaterialCode(item.getMaterialCode());
taskDetail.setMaterialName(item.getMaterialName());
taskDetail.setMaterialSpec(item.getMaterialSpec());
taskDetail.setMaterialUnit(item.getMaterialUnit());
taskDetail.setQty(item.getSystemQty());
//taskDetail.setBatch(cycleCountDetail.getBatch());
//taskDetail.setFromInventoryId(cycleCountDetail.getInventoryDetailId());
//taskDetail.setLot(cycleCountDetail.getLot());
//taskDetail.setProjectNo(cycleCountDetail.getProjectNo());
taskDetail.setCompanyCode(cycleCountDetail.getCompanyCode());
taskDetail.setCreated(new Date());
taskDetail.setCreatedBy(ShiroUtils.getLoginName());
taskDetail.setLastUpdated(new Date());
taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
if(taskDetailService.save(taskDetail) == false){
throw new ServiceException("盘点任务明细生成失败!");
}
// 修改子单状态
item.setChildStatus(QuantityConstant.CYCLECOUNT_STATUS_BUILDTASK);
cycleCountDetailChildService.updateById(item);
}
//修改细单状态
cycleCountDetail.setTaskHeaderId(task.getId()); //盘点明细修改状态task数据源
cycleCountDetail.setLastUpdated(new Date());
cycleCountDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_BUILDTASK);
cycleCountDetail.setTaskHeaderId(task.getId());
//cycleCountDetail.setTaskDetailId(taskDetail.getId());
cycleCountDetailService.updateById(cycleCountDetail);
//修改主单状态
CycleCountHeader cycleCountHeader = new CycleCountHeader();
cycleCountHeader.setCode(cycleCountDetail.getCycleCountHeadCode());
cycleCountHeader.setWarehouseCode(cycleCountDetail.getWarehouseCode());
//cycleCountHeader.setCompanyCode(cycleCountDetail.getCompanyCode());
LambdaQueryWrapper<CycleCountHeader> lamb = Wrappers.lambdaQuery(cycleCountHeader);
cycleCountHeader = cycleCountHeaderService.getOne(lamb);
cycleCountHeader.setStatusCyc(QuantityConstant.CYCLECOUNT_STATUS_BUILDTASK);
cycleCountHeaderService.updateById(cycleCountHeader);
return AjaxResult.success("盘点任务生成成功");
}
/**
* 盘点完成
*盘点有差异完成时状态为105
* @param taskHeader
* @return
*/
@Transactional(rollbackFor = Exception.class)
public AjaxResult completeCycleCountTask(TaskHeader taskHeader) {
/*盘点完成,传入任务主单,查出任务明细,通过任务明细查找盘点的明细单,
完成任务同时,修改盘点细单和主单的状态,完成后库存锁复位*/
//修改任务主单状态
taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
taskHeader.setLastUpdatedBy(ShiroUtils.getLoginName()); //更新用户
taskHeader.setLastUpdated(new Date()); //更新时间
//task更新明细单状态
TaskDetail taskDetail = new TaskDetail();
taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
taskDetail.setTaskType(taskHeader.getTaskType());
taskDetail.setTaskId(taskHeader.getId());
LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(taskDetail);
List<TaskDetail> taskDetailList = taskDetailService.list(lambdaQueryWrapper);//查询子单
//盘点明细
CycleCountDetail cycleCountDetail = cycleCountDetailService.getById(taskHeader.getAllocationHeadId());
List<TaskDetail> list = new CopyOnWriteArrayList<>();
List<CycleCountDetailChild> cycChildList = new ArrayList<>();
boolean difference = false;
//修改任务明细状态的同时查找到盘点明细的条目并修改状态,最后修改主单状态
for (TaskDetail item : taskDetailList) {
item.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
item.setLastUpdatedBy(ShiroUtils.getLoginName()); //更新用户
item.setLastUpdated(new Date()); //更新时间
list.add(item);
//盘点子单状态
CycleCountDetailChild cycleCountDetailChild = new CycleCountDetailChild();
cycleCountDetailChild.setCycleCountHeadCode(cycleCountDetail.getCycleCountHeadCode());
cycleCountDetailChild.setCycleCountDetailId(cycleCountDetail.getId());
LambdaQueryWrapper<CycleCountDetailChild> childLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountDetailChild);
List<CycleCountDetailChild> childList = cycleCountDetailChildService.list(childLambdaQueryWrapper);
if(childList.size() > 0){
for (CycleCountDetailChild child : childList){
//判断是否差异状态
if(child.getGapQty().compareTo(BigDecimal.ZERO) != 0){
difference = true;
//有差异
child.setChildStatus(QuantityConstant.CYCLECOUNT_STATUS_DIFFERENCE);
}else{
child.setChildStatus(QuantityConstant.CYCLECOUNT_STATUS_COMPLETED);
}
cycChildList.add(child);
}
}
//取消库存盘点锁
InventoryHeader inventoryHeader = inventoryHeaderService.getById(cycleCountDetail.getInventoryDetailId());
if(StringUtils.isNull(inventoryHeader)){
throw new ServiceException("库存头错误!");
}
inventoryHeader.setLocking(1);
inventoryHeader.setLockRemark("");
inventoryHeaderService.updateById(inventoryHeader);
//库存明细最后盘点时间
LambdaQueryWrapper<InventoryDetail> inventoryDetail = Wrappers.lambdaQuery();
inventoryDetail.eq(InventoryDetail::getWarehouseCode,inventoryHeader.getWarehouseCode())
.eq(InventoryDetail::getInventoryHeaderId,inventoryHeader.getId());
List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetail);
if(inventoryDetailList.size() < 1 ){
throw new ServiceException("盘点的库存明细错误!");
}
inventoryDetailList.stream().forEach(i -> i.setLastCycleCountDate(new Date()));
inventoryDetailService.updateBatchById(inventoryDetailList);
}
if (!taskHeaderService.saveOrUpdate(taskHeader) ||
!taskDetailService.saveOrUpdateBatch(list) ||
!cycleCountDetailChildService.saveOrUpdateBatch(cycChildList)
) {
throw new ServiceException("盘点任务单据状态更新失败!");
}
//盘点明细状态
if(difference){
//有差异
cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_DIFFERENCE);
}else {
cycleCountDetail.setEnableStatus(QuantityConstant.CYCLECOUNT_STATUS_COMPLETED);
}
cycleCountDetail.setCompletedBy(taskHeader.getCreatedBy());
cycleCountDetail.setCompletedAt(new Date());
cycleCountDetailService.updateById(cycleCountDetail);
//更新主单状态
cycleCountHeaderService.updataHeaderStatus(cycleCountDetail.getCycleCountHeadCode());
//释放库位
locationService.updateStatus(cycleCountDetail.getLocationCode(), QuantityConstant.STATUS_LOCATION_EMPTY);
return AjaxResult.success("完成盘点任务成功!");
}
}