skyouc
9 小时以前 c772d3422a7e74878ffe9360d4bc4f83c5034dac
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.entity.AsnOrder;
import com.vincent.rsf.server.manager.entity.LocItem;
import com.vincent.rsf.server.manager.entity.WaveItem;
import com.vincent.rsf.server.manager.enums.AsnExceStatus;
import com.vincent.rsf.server.manager.enums.WaveExceStatus;
import com.vincent.rsf.server.manager.mapper.WaveMapper;
import com.vincent.rsf.server.manager.entity.Wave;
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.manager.utils.OptimalAlgorithmUtil;
import org.apache.commons.lang3.StringUtils;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service("waveService")
public class WaveServiceImpl extends ServiceImpl<WaveMapper, Wave> implements WaveService {
 
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    @Autowired
    private AsnOrderService asnOrderService;
    @Autowired
    private WaveItemService waveItemService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskItemService taskItemService;
    @Autowired
    private LocItemService locItemService;
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 波次任务下发
     * @time 2025/4/25 16:24
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R publicTask(Map<String, Object> map) {
        List<Long> ids = (List<Long>) map.get("ids");
        if (Objects.isNull(ids) || ids.isEmpty()) {
            throw new CoolException("参数不能为空!!");
        }
        List<Wave> waves = this.list(new LambdaQueryWrapper<Wave>().in(Wave::getId, ids));
        if (Objects.isNull(waves) || waves.isEmpty()) {
            throw new CoolException("波次数据不存在!!");
        }
        List<Long> list = waves.stream().map(Wave::getId).collect(Collectors.toList());
        List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().in(WaveItem::getWaveId, list));
        if (waveItems.isEmpty()) {
            throw new CoolException("波次明细不存在!!");
        }
        List<Long> orderIds = waveItems.stream().map(WaveItem::getOrderId).collect(Collectors.toList());
 
        /**查询每条明细匹配的库位*/
        try {
            List<WaveItem> items = getLocs(waveItems);
        } catch (Exception e) {
            throw new CoolException("库位获取失败!!!");
        }
        //TODO 1. 根据波次明细生成出库任务
        // 2. 根据物料SKU寻找符合物料库位  {1. 根据物料编码,批次,动态字段 查询符合的库位,再根据库位中物料的数量选择最适合的库位 2. 判断当前订单是全拖出库还是拣料入库}
        // 3. 修改主单、波次执行数量
        // 4. 判断全仓出库或拣料出库
        List<AsnOrder> orders = asnOrderService.list(new LambdaQueryWrapper<AsnOrder>().in(AsnOrder::getId, orderIds));
        /**修改原出库单状态*/
        if (!asnOrderService.update(new LambdaQueryWrapper<AsnOrder>()
                .eq(AsnOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val)
                .in(AsnOrder::getId, orders))) {
            throw new CoolException("出库单据状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Wave>().set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK).in(Wave::getId, ids))) {
            throw new CoolException("波次状态修改失败!!");
        }
        return R.ok();
    }
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 预览波次下发任务
     * @time 2025/4/27 11:09
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<WaveItem> mergeWavePreview(Long waveId) {
        Wave wave = this.getById(waveId);
        if (Objects.isNull(wave)) {
            throw new CoolException("波次不能存在!!");
        }
        List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, waveId));
        if (waveItems.isEmpty()) {
            throw new CoolException("波次明细不存在!!");
        }
        List<WaveItem> itemPreview = null;
        try {
            itemPreview = getLocs(waveItems);
        } catch (Exception e) {
            throw new CoolException("库位获取失败!!!!");
        }
        return itemPreview;
    }
 
    /**
     * @param
     * @param waveItems
     * @return
     * @author Ryan
     * @description 根据物料编码,批次,动态字段 查询符合的库位,再根据库位中物料的数量选择最适合的库位
     * @time 2025/4/27 09:26
     */
    private synchronized List<WaveItem> getLocs(List<WaveItem> waveItems) throws Exception {
        //TODO  根据物料编码,批次,动态字段 查询符合的库位,再根据库位中物料的数量选择最适合的库位
        waveItems.forEach(waveItem -> {
            List<LocItem> locItems = locItemService.list(new QueryWrapper<LocItem>()
                    .select("id", "loc_id", "loc_code", "order_id", "SUM(anfme) anfme", "SUM(work_qty) work_qty", "splr_batch", "fields_index", "matnr_code")
                    .lambda()
                    .eq(LocItem::getMatnrCode, waveItem.getMatnrCode())
                    .eq(LocItem::getSplrBatch, waveItem.getSplrBatch())
                    .eq(StringUtils.isNotBlank(waveItem.getFieldsIndex()), LocItem::getFieldsIndex, waveItem.getFieldsIndex())
                    .groupBy(LocItem::getMatnrCode, LocItem::getSplrBatch, LocItem::getFieldsIndex, LocItem::getId));
 
            Double[] doubles = locItems.stream().map(LocItem::getAnfme).toArray(Double[]::new);
            List<Double> result = OptimalAlgorithmUtil.findBestCombination(doubles, waveItem.getAnfme());
            String locs = JSONArray.toJSONString(new ArrayList<>());
            if (!locItems.isEmpty()) {
                List<String> codes = locItems.stream().map(LocItem::getLocCode).collect(Collectors.toList());
                locs = JSONArray.toJSONString(codes);
            }
            waveItem.setStockLocs(locs);
        });
        return waveItems;
    }
}