WaveService.java
7.67 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
package com.huaheng.pc.shipment.wave.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.support.Convert;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.mapper.WaveMapper;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
@Service
public class WaveService extends ServiceImpl<WaveMapper, Wave> {
@Autowired
private ShipmentDetailService shipmentDetailService;
@Autowired
private ShipmentContainerHeaderService shipmentContainerHeaderService;
@Autowired
private TaskDetailService taskDetailService;
@Autowired
private TaskHeaderService taskHeaderService;
@Autowired
private ShipmentHeaderService shipmentHeaderService;
/**
* 开始波次,对带有此波次号的单据进行后续的后台操作
* 1、查看波次是否符合开始波次的条件
* 2、整合波次的所有子单单据
* 3、对每个单据自动组盘,失败则回退
* 4、生成任务
* 5、修改波次的状态
*/
@Transactional
public AjaxResult startWave(String ids) {
List<Wave> waves = new ArrayList<>();
List<ShipmentDetail> list = new ArrayList<>();
for (Integer id : Convert.toIntArray(ids)) {
//1、查看此波次的状态,状态不为o时,无法开始波次
Wave wave = this.getById(id);
if(wave == null || wave.getStatus() != 0){
return AjaxResult.error("id为"+id+"的波次找不到,或者状态不能做开始操作");
}
waves.add(wave);
//2、找到此波次的单据
LambdaQueryWrapper<ShipmentDetail> lam = Wrappers.lambdaQuery();
lam.eq(ShipmentDetail::getWaveId,id)
.eq(ShipmentDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
List<ShipmentDetail> shipmentDetails=shipmentDetailService.list(lam);
//整合所有加入波次的单据
if(shipmentDetails != null){
list.addAll(shipmentDetails);
}
}
//3、自动组盘
AjaxResult ajaxResult=shipmentContainerHeaderService.autoCombination(list);
if(ajaxResult.getData() != null){
//4、生成任务
shipmentContainerHeaderService.createTask(Arrays.asList(Convert.toIntArray(ajaxResult.getData().toString())));
}
//5、修改波次的状态
for(Wave wave : waves){
wave.setStatus(300);
wave.setCurrentWaveStep("生成任务");
}
Boolean flag = this.updateBatchById(waves);
if(flag == false){
throw new ServiceException("波次运行失败,修改波次状态时报错");
}
return AjaxResult.success("波次运行成功");
}
/**
* 释放波次,执行任务
* 1、查看此波次的状态,是否可以释放波次
* 2、查看波次的状态,是成功释放还是失败释放
* 成功释放:1、找到此波次的所有子任务
* 2、根据子任务选出主任务
* 3、将这些主任务执行
* 失败释放:1.找到次波次的所有子单据
* 2.修改子单据的波次为0
* 3.修改主子单的状态
* @param ids
* @return
*/
@Transactional
public AjaxResult freed(String ids) {
Boolean flag = false;
List<TaskDetail> taskDetailList = new ArrayList<>();
for (Integer id : Convert.toIntArray(ids)) {
//1、查看此波次是否可以释放
Wave wave = this.getById(id);
if(wave == null || (wave.getStatus()!=300 && wave.getStatus()!=999)){
return AjaxResult.error(id+"波次不可释放");
}
//2、查看此波次的状态,看是成功释放还是失败释放
if(wave.getStatus() == 300) {
//成功释放——找到此波次的未执行的子任务列表
LambdaQueryWrapper<TaskDetail> lam = Wrappers.lambdaQuery();
lam.eq(TaskDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
.eq(TaskDetail::getStatus, 0)
.eq(TaskDetail::getWaveId, id);
List<TaskDetail> taskDetails = taskDetailService.list(lam);
if (taskDetails != null) {
taskDetailList.addAll(taskDetails);
}
}else {
//失败释放——找到次波次的所有子单据
LambdaQueryWrapper<ShipmentDetail> detailLam = Wrappers.lambdaQuery();
detailLam.eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
.eq(ShipmentDetail::getWaveId,wave.getId());
List<ShipmentDetail> shipmentDetails =shipmentDetailService.list(detailLam);
//2.修改子单据的波次为0
//3.修改主子单的状态
HashSet<Integer> set = new HashSet<>();
for(ShipmentDetail item : shipmentDetails){
if(item.getShipQty().compareTo(item.getRequestQty())!=0) {
item.setStatus(0);
}
item.setWaveId(0);
set.add(item.getShipmentId());
}
flag = shipmentDetailService.updateBatchById(shipmentDetails);
if(flag == false){
throw new ServiceException("修改出库子单状态失败");
}
for(Integer shipmentId :set){
ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentId);
if(shipmentHeader == null){
throw new ServiceException("没有对应的出库主单");
}
if(shipmentHeader.getFirstStatus()<= 200) {
shipmentHeader.setFirstStatus(0);
flag = shipmentHeaderService.updateById(shipmentHeader);
if (flag == false) {
throw new ServiceException("修改出库主单失败");
}
}
}
}
}
HashSet<Integer> idSet = new HashSet();
//成功释放——遍历子任务列表,找出主单(distinct)id
for(TaskDetail item : taskDetailList){
idSet.add(item.getTaskId());
}
//成功释放——将这些主任务执行
taskHeaderService.sendTaskToWcs(idSet.toArray(new Integer[idSet.size()]));
return AjaxResult.success("波次释放成功");
}
}