Blame view

src/main/java/com/huaheng/pc/shipment/wave/service/WaveService.java 14.6 KB
pengcheng authored
1
package com.huaheng.pc.shipment.wave.service;
2
pengcheng authored
3
4
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
5
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
pengcheng authored
6
import com.huaheng.common.constant.QuantityConstant;
pengcheng authored
7
import com.huaheng.common.exception.service.ServiceException;
pengcheng authored
8
9
10
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
pengcheng authored
11
12
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
pengcheng authored
13
14
15
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
pengcheng authored
16
17
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
pengcheng authored
18
import com.huaheng.pc.shipment.shippingCombination.service.ShippingCombinationService;
pengcheng authored
19
20
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.mapper.WaveMapper;
pengcheng authored
21
22
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
pengcheng authored
23
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
pengcheng authored
24
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
pengcheng authored
25
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.stereotype.Service;
pengcheng authored
27
28
import org.springframework.transaction.annotation.Transactional;
pengcheng authored
29
import java.util.*;
30
31
32
33

@Service
public class WaveService extends ServiceImpl<WaveMapper, Wave> {
pengcheng authored
34
35
36
37
38

    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
pengcheng authored
39
40
41
42
43
44
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private TaskHeaderService taskHeaderService;
    @Autowired
    private ShipmentHeaderService shipmentHeaderService;
pengcheng authored
45
46
    @Autowired
    private ShippingCombinationService shippingCombinationService;
pengcheng authored
47
pengcheng authored
48
49
50
51
52
53
54
55
    /**
     * 开始波次,对带有此波次号的单据进行后续的后台操作
     * 1、查看波次是否符合开始波次的条件
     * 2、整合波次的所有子单单据
     * 3、对每个单据自动组盘,失败则回退
     * 4、生成任务
     * 5、修改波次的状态
     */
pengcheng authored
56
57
58

    @Transactional
    public AjaxResult startWave(String ids) {
pengcheng authored
59
        List<Wave> waves = new ArrayList<>();
pengcheng authored
60
61
        List<ShipmentDetail> list = new ArrayList<>();
        for (Integer id : Convert.toIntArray(ids)) {
pengcheng authored
62
pengcheng authored
63
            //1、查看此波次的状态,状态不为o时,无法开始波次
pengcheng authored
64
            Wave wave = this.getById(id);
pengcheng authored
65
66
            if(wave == null || (wave.getCurrentWaveStep()> QuantityConstant.WAVE_STEP_BUILD &&
                    wave.getCurrentWaveStep() < QuantityConstant.WAVE_STEP_ERROR)){
pengcheng authored
67
68
69
70
                return AjaxResult.error("id为"+id+"的波次找不到,或者状态不能做开始操作");
            }
            waves.add(wave);
pengcheng authored
71
            //2、找到此波次的单据
pengcheng authored
72
73
74
75
76
77
78
79
80
81
82
            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);
            }
        }
pengcheng authored
83
        //3、自动组盘
pengcheng authored
84
        AjaxResult ajaxResult=shipmentContainerHeaderService.autoCombination(list);
pengcheng authored
85
86
87
88
89
90
91
92
        if(ajaxResult.getCode() != 200){
            for(Wave wave : waves) {
                wave.setStatus(QuantityConstant.WAVE_STATUS_ERROR);
                wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_ERROR);
                wave.setLastWaveStep(QuantityConstant.WAVE_STEP_BUILD);
                this.updateById(wave);
            }
            throw new ServiceException("波次执行失败,组盘失败");
pengcheng authored
93
        }
pengcheng authored
94
pengcheng authored
95
pengcheng authored
96
        //5、修改波次的状态
pengcheng authored
97
        for(Wave wave : waves){
pengcheng authored
98
99
100
            wave.setStatus(QuantityConstant.WAVE_STATUS_START);
            wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_START);
            wave.setLastWaveStep(QuantityConstant.WAVE_STEP_BUILD);
pengcheng authored
101
102
103
104
105
        }
        Boolean flag = this.updateBatchById(waves);
        if(flag == false){
            throw new ServiceException("波次运行失败,修改波次状态时报错");
        }
pengcheng authored
106
107
        return AjaxResult.success("波次运行成功");
    }
pengcheng authored
108
109
110


    /**
pengcheng authored
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
     * 剔除单据,剔除无法组盘的单据
     * 1、找到无法组盘的单据
     * 2、剔除单据
     * 3、修改波次的状态
     */

    @Transactional
    public AjaxResult cullWave(String ids) {
        //判断波次是否可以完成
        for (Integer id : Convert.toIntArray(ids)) {
            Wave wave =this.getById(id);
            if(wave == null){
                return AjaxResult.error("id为"+id+"的波次不存在");
            }
xqs authored
126
127
            if(!wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STEP_BUILD) &&
                    !wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STEP_ERROR)){
pengcheng authored
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
                return AjaxResult.error("id为"+id+"的波次已执行,无法剔除单据");
            }

            LambdaQueryWrapper<ShipmentDetail> detailLamb = Wrappers.lambdaQuery();
            detailLamb.eq(ShipmentDetail::getWaveId,id)
                    .eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
            List<ShipmentDetail> shipmentDetailList =shipmentDetailService.list(detailLamb);
            for(ShipmentDetail item :shipmentDetailList){
                List<InventoryDetail> list =shippingCombinationService.getInventorys(item);
                if(list.isEmpty()){
                    item.setWaveId(0);
                    shipmentDetailService.updateById(item);
                }
            }

            //修改波次状态
            wave.setStatus(QuantityConstant.WAVE_STATUS_BUILD);
            wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_CULL);
            wave.setLastWaveStep(QuantityConstant.WAVE_STEP_START);
            wave.setLastUpdatedBy(ShiroUtils.getLoginName());
            wave.setLastUpdated(new Date());
            this.updateById(wave);
        }
        return AjaxResult.success("波次运行成功");
    }



    /**
     * 完成波次,对带有此波次号的组盘进行后续的后台操作
     * 1、查看波次是否符合完成波次的条件
     * 2、生成任务
     * 3、修改波次的状态
     */

    @Transactional
    public AjaxResult endWave(String ids) {
        //判断波次是否可以完成
        for (Integer id : Convert.toIntArray(ids)) {
            Wave wave =this.getById(id);
            if(wave == null){
                return AjaxResult.error("id为"+id+"的波次不存在");
            }
xqs authored
172
            if(!wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STEP_START)){
pengcheng authored
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
                return AjaxResult.error("id为"+id+"的波次未执行,无法完成");
            }

            //2、找到此波次的所有阻盘头,生成任务
            LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLamb = Wrappers.lambdaQuery();
            shipmentContainerHeaderLamb.eq(ShipmentContainerHeader::getWarehouseCode,ShiroUtils.getWarehouseCode())
                    .eq(ShipmentContainerHeader::getWaveId,id);
            List<ShipmentContainerHeader> shipmentContainerHeaders =shipmentContainerHeaderService.list(shipmentContainerHeaderLamb);

            if(shipmentContainerHeaders.isEmpty()){
                return AjaxResult.error("id为"+id+"的波次的出库组盘头未找到");
            }

            //找到未生成出库任务的组盘头
            List<Integer> idList =new ArrayList<>();
            for(ShipmentContainerHeader item : shipmentContainerHeaders){
xqs authored
189
                if(item.getStatus().equals(QuantityConstant.SHIPMENT_CONTAINER_BUILD)){
pengcheng authored
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
                    idList.add(item.getId());
                }
            }
            if(idList.isEmpty()){
                return AjaxResult.error("id为"+id+"的波次的出库组盘头都已生成任务");
            }

            AjaxResult ajaxResult =shipmentContainerHeaderService.createTask(idList);
            if(ajaxResult.hasErr()){
                throw new ServiceException("id为"+id+"的波次的出库组盘头生成任务失败");
            }

            //修改波次状态
            wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_END);
            wave.setLastWaveStep(QuantityConstant.WAVE_STEP_START);
            wave.setLastUpdatedBy(ShiroUtils.getLoginName());
            wave.setLastUpdated(new Date());
            this.updateById(wave);
        }
        return AjaxResult.success("波次运行成功");
    }



    /**
pengcheng authored
215
     * 释放波次,执行任务
pengcheng authored
216
217
218
219
220
221
222
223
     * 1、查看此波次的状态,是否可以释放波次
     * 2、查看波次的状态,是成功释放还是失败释放
     * 成功释放:1、找到此波次的所有子任务
     *          2、根据子任务选出主任务
     *          3、将这些主任务执行
     *  失败释放:1.找到次波次的所有子单据
     *             2.修改子单据的波次为0
     *             3.修改主子单的状态
pengcheng authored
224
225
226
227
228
     * @param ids
     * @return
     */
    @Transactional
    public AjaxResult freed(String ids) {
pengcheng authored
229
230
            Boolean flag = false;
            for (Integer id : Convert.toIntArray(ids)) {
pengcheng authored
231
pengcheng authored
232
233
                //1、查看此波次是否可以释放
                Wave wave = this.getById(id);
xqs authored
234
                 if(wave == null || wave.getStatus().equals(QuantityConstant.WAVE_STATUS_START)){
pengcheng authored
235
236
                    return AjaxResult.error(id+"波次不可释放,正在执行中");
                }
pengcheng authored
237
238

            //2、查看此波次的状态,看是成功释放还是失败释放
pengcheng authored
239
                //失败释放——找到次波次的所有子单据和主单
pengcheng authored
240
241
242
243
244
                LambdaQueryWrapper<ShipmentDetail> detailLam = Wrappers.lambdaQuery();
                detailLam.eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
                        .eq(ShipmentDetail::getWaveId,wave.getId());
                List<ShipmentDetail> shipmentDetails =shipmentDetailService.list(detailLam);
pengcheng authored
245
246
247
                LambdaQueryWrapper<ShipmentHeader> headerLam =Wrappers.lambdaQuery();
                headerLam.eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode())
                        .eq(ShipmentHeader::getWaveId,wave.getId());
pengcheng authored
248
                List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(headerLam);
pengcheng authored
249
250
251
252
253

                if(shipmentDetails.isEmpty()) {
                    throw new ServiceException("该波次"+id+"的子单未查到");
                }
pengcheng authored
254
                if(shipmentHeaders.isEmpty()) {
pengcheng authored
255
256
257
258
                    throw new ServiceException("该波次"+id+"的主单未查到");
                }

                //2.修改主子单据的波次为0
pengcheng authored
259
260
261
                //3.修改主子单的状态
                HashSet<Integer> set = new HashSet<>();
                for(ShipmentDetail item : shipmentDetails){
pengcheng authored
262
263
264
                    if(item.getShipQty().compareTo(item.getRequestQty())==0) {
                        item.setStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
                    }else {
pengcheng authored
265
                        item.setStatus(QuantityConstant.SHIPMENT_HEADER_POOL);
pengcheng authored
266
267
268
269
270
271
272
273
                    }
                    item.setWaveId(0);
                    set.add(item.getShipmentId());
                }
                flag = shipmentDetailService.updateBatchById(shipmentDetails);
                if(flag == false){
                    throw new ServiceException("修改出库子单状态失败");
                }
pengcheng authored
274
pengcheng authored
275
276
277
278
279
280
281
282
283
284
285
                for(ShipmentHeader shipmentHeader : shipmentHeaders) {
                    if (shipmentHeader.getFirstStatus() <= QuantityConstant.SHIPMENT_HEADER_WAVE) {
                        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_POOL);
                    }
                    if (shipmentHeader.getLastStatus() <= QuantityConstant.SHIPMENT_HEADER_WAVE) {
                        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_POOL);
                    }
                    flag = shipmentHeaderService.updateById(shipmentHeader);
                    if (flag == false) {
                        throw new ServiceException("修改出库主单失败");
                    }
pengcheng authored
286
287
                }
xqs authored
288
                if(wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STEP_END)) {
pengcheng authored
289
290
291
292
293
294
295
296
                    //成功释放——找到此波次的未执行的子任务列表
                    LambdaQueryWrapper<TaskHeader> lam = Wrappers.lambdaQuery();
                    lam.eq(TaskHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                            .eq(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD)
                            .eq(TaskHeader::getWaveId, id);
                    List<TaskHeader> taskHeaders = taskHeaderService.list(lam);
                    if (taskHeaders == null) {
                        throw new ServiceException("该波次的主任务找不到");
pengcheng authored
297
                    }
pengcheng authored
298
299
300
                    List<Integer> taskIds =new ArrayList<>();
                    for(TaskHeader item :taskHeaders){
                        taskIds.add(item.getId());
pengcheng authored
301
                    }
xqs authored
302
                    taskHeaderService.sendTaskToWcs(taskIds.toArray(new Integer[taskIds.size()]));
pengcheng authored
303
                }
pengcheng authored
304
305

                //修改波次状态
xqs authored
306
                if(wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STEP_END)) {
pengcheng authored
307
308
309
310
                    wave.setStatus(QuantityConstant.WAVE_STATUS_END);
                    wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_FREED);
                    wave.setLastWaveStep(QuantityConstant.WAVE_STEP_END);
                }
xqs authored
311
                if(wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STATUS_BUILD)){
pengcheng authored
312
313
314
                    wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_FREED);
                    wave.setLastWaveStep(QuantityConstant.WAVE_STEP_BUILD);
                }
xqs authored
315
                if(wave.getCurrentWaveStep().equals(QuantityConstant.WAVE_STATUS_ERROR)){
pengcheng authored
316
317
318
319
320
321
                    wave.setCurrentWaveStep(QuantityConstant.WAVE_STEP_FREED);
                    wave.setLastWaveStep(QuantityConstant.WAVE_STEP_ERROR);
                }
                wave.setLastUpdatedBy(ShiroUtils.getLoginName());
                wave.setLastUpdated(new Date());
                this.updateById(wave);
pengcheng authored
322
323
        }
        return AjaxResult.success("波次释放成功");
pengcheng authored
324
    }
325
}