skyouc
7 天以前 b9d414bc2d61b4824ce6a019b1c10f526f71ced1
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.controller.params.PakinItem;
import com.vincent.rsf.server.manager.controller.params.WaitPakinParam;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.PakinIOStatus;
import com.vincent.rsf.server.manager.mapper.WaitPakinMapper;
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service("waitPakinService")
public class WaitPakinServiceImpl extends ServiceImpl<WaitPakinMapper, WaitPakin> implements WaitPakinService {
 
 
    @Autowired
    private AsnOrderService asnOrderService;
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private WaitPakinItemService waitPakinItemService;
    @Autowired
    private WarehouseAreasItemService warehouseAreasItemService;
    @Autowired
    private LocService locService;
 
    /**
     * @param
     * @param userId
     * @return
     * @author Ryan
     * @description 组拖
     * @time 2025/3/29 14:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaitPakin mergeItems(WaitPakinParam waitPakin, Long userId) {
        if (Objects.isNull(waitPakin.getItems()) || waitPakin.getItems().isEmpty()) {
            throw new CoolException("参数错误:物料跟踪码为空!");
        }
        if (StringUtils.isBlank(waitPakin.getBarcode())) {
            throw new CoolException("参数错误:托盘码为空!!");
        }
        List<Short> asList = Arrays.asList(Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val), Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val));
        WaitPakin list = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                .notIn(WaitPakin::getIoStatus, asList)
                .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
        if (!Objects.isNull(list)) {
            throw new CoolException("拖盘码:" + waitPakin.getBarcode() + "已被组拖单:" + list.getCode() + "使用!!");
        }
        List<Loc> locs = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getBarcode, waitPakin.getBarcode()));
        if (!locs.isEmpty()) {
            List<String> locCodes = locs.stream().map(Loc::getCode).collect(Collectors.toList());
            String join = StringUtils.join(locCodes, ",");
            throw new CoolException("拖盘码:" + waitPakin.getBarcode() + "已被库位:" + join + "使用!!");
        }
        double sum = waitPakin.getItems().stream().mapToDouble(PakinItem::getReceiptQty).sum();
 
        WaitPakin waitPakin1 = new WaitPakin();
        WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                .in(WaitPakin::getIoStatus, asList)
                .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
        if (Objects.isNull(pakin)) {
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAIT_PAKIN_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码规则错误: 编码规则「SYS_WAIT_PAKIN_CODE」规则是不存在");
            }
            waitPakin1.setCode(ruleCode)
                    //状态修改为入库中
                    .setIoStatus(Short.parseShort(PakinIOStatus.PAKIN_IO_STATUS_DONE.val))
                    .setAnfme(sum)
                    .setUpdateBy(userId)
                    .setCreateBy(userId)
                    .setBarcode(waitPakin.getBarcode());
            if (!this.save(waitPakin1)) {
                throw new CoolException("主单保存失败!!");
            }
        } else {
            BeanUtils.copyProperties(pakin, waitPakin1);
            waitPakin1.setAnfme(sum);
            if (!this.saveOrUpdate(waitPakin1)) {
                throw new CoolException("主单修改失败!!");
            }
        }
        /**物料跟踪码*/
        List<String> tracks = waitPakin.getItems().stream().map(PakinItem::getTrackCode).collect(Collectors.toList());
        List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>()
                .eq(WaitPakinItem::getPakinId, waitPakin1.getId())
                .in(WaitPakinItem::getTrackCode, tracks));
        if (!pakinItems.isEmpty()) {
            if (!waitPakinItemService.remove(new LambdaQueryWrapper<WaitPakinItem>()
                    .eq(WaitPakinItem::getPakinId, waitPakin1.getId())
                    .in(WaitPakinItem::getTrackCode, tracks))) {
                throw new CoolException("原单据清除失败!!");
            }
        }
        List<WaitPakinItem> items = new ArrayList<>();
        if (!Objects.isNull(waitPakin.getType()) && waitPakin.getType().equals("defective")) {
            List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().in(AsnOrderItem::getTrackCode, tracks));
            if (Objects.isNull(orderItems) || orderItems.isEmpty()) {
                throw new CoolException("单据不存在!!");
            }
            for (AsnOrderItem item : orderItems) {
                WaitPakinItem pakinItem = new WaitPakinItem();
                pakinItem.setAnfme(item.getAnfme())
                        .setPakinId(waitPakin1.getId())
                        .setAsnId(item.getAsnId())
                        .setAsnCode(item.getAsnCode())
                        .setAsnItemId(item.getId())
                        .setBatch(item.getSplrBatch())
                        .setUnit(item.getStockUnit())
                        .setFieldsIndex(item.getFieldsIndex())
                        .setMatnrId(item.getMatnrId())
                        .setMaktx(item.getMaktx())
                        .setUpdateBy(userId)
                        .setCreateBy(userId)
                        .setMatnrCode(item.getMatnrCode());
                AsnOrder order = asnOrderService.getById(item.getAsnId());
                if (!Objects.isNull(order)) {
                    pakinItem.setType(null == order.getType() ?  null :order.getType())
                            .setWkType(null == order.getWkType() ? null : Short.parseShort(order.getWkType()) );
                }
                for (PakinItem waitPakinItem : waitPakin.getItems()) {
                    if (waitPakinItem.getTrackCode().equals(item.getTrackCode())) {
                        if (waitPakinItem.getReceiptQty() > item.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) >= 0) {
                            throw new CoolException("组拖数量不能大于收货数量!!");
                        }
                        pakinItem.setAnfme(waitPakinItem.getReceiptQty()).setTrackCode(waitPakinItem.getTrackCode());
                    }
                }
                items.add(pakinItem);
            }
        } else {
            LambdaQueryWrapper<WarehouseAreasItem> queryWrapper = new QueryWrapper<WarehouseAreasItem>()
                    .select("SUM(anfme) as anfme, track_code, asn_code, id, splr_batch, ispt_result, plat_item_id, batch, qty, work_qty, matnr_code, matnr_id, maktx")
                    .lambda()
                    .in(WarehouseAreasItem::getTrackCode, tracks)
                    .groupBy(WarehouseAreasItem::getSplrBatch,
                            WarehouseAreasItem::getTrackCode);
            List<WarehouseAreasItem> warehouseAreasItems = warehouseAreasItemService.list(queryWrapper);
            if (Objects.isNull(warehouseAreasItems) || warehouseAreasItems.isEmpty()) {
                throw new CoolException("物料未送至收货区!!");
            }
            for (WarehouseAreasItem item : warehouseAreasItems) {
                WaitPakinItem pakinItem = new WaitPakinItem();
                pakinItem.setAnfme(item.getAnfme())
                        .setPakinId(waitPakin1.getId())
                        .setAsnId(item.getAsnId())
                        .setAsnCode(item.getAsnCode())
                        .setAsnItemId(item.getId())
                        .setBatch(item.getSplrBatch())
                        .setUnit(item.getStockUnit())
                        .setFieldsIndex(item.getFieldsIndex())
                        .setMatnrId(item.getMatnrId())
                        .setMaktx(item.getMaktx())
                        .setUpdateBy(userId)
                        .setCreateBy(userId)
                        .setMatnrCode(item.getMatnrCode());
                AsnOrder order = asnOrderService.getById(item.getAsnId());
                if (!Objects.isNull(order)) {
                    pakinItem.setType(null == order.getType() ?  null :order.getType())
                            .setWkType(null == order.getWkType() ? null : Short.parseShort(order.getWkType()) );
                }
 
                for (PakinItem waitPakinItem : waitPakin.getItems()) {
                    if (waitPakinItem.getTrackCode().equals(item.getTrackCode())) {
                        if (waitPakinItem.getReceiptQty() > item.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) {
                            throw new CoolException("组拖数量不能大于收货数量且不能小于零!!");
                        }
                        pakinItem.setAnfme(waitPakinItem.getReceiptQty()).setTrackCode(waitPakinItem.getTrackCode());
                    }
                }
                items.add(pakinItem);
            }
        }
        double sum1 = items.stream().mapToDouble(WaitPakinItem::getAnfme).sum();
        if (!waitPakinItemService.saveBatch(items)) {
            throw new CoolException("组拖明细保存失败!!");
        }
        waitPakin1.setAnfme(sum1);
        if (!this.updateById(waitPakin1)) {
            throw new CoolException("组拖数量修改失败!!");
        }
 
        //TODO 组拖完成后,扣减收货区库存
 
        return pakin;
    }
 
    /**
     * @author Ryan
     * @description 组拖解绑
     * @param
     * @return
     * @time 2025/3/29 14:42
     */
    @Override
    public WaitPakin unBind(WaitPakinParam param) {
        String barcode = param.getBarcode();
        if (StringUtils.isNotBlank(barcode)) {
            WaitPakin waitPakins = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, barcode), false);
            if (Objects.isNull(waitPakins)) {
                throw new CoolException("组拖不存在!!");
            }
            List<PakinItem> paramItems = param.getItems();
            if (Objects.isNull(paramItems) || paramItems.isEmpty()) {
                throw new CoolException("解绑物料不能为空!!");
            }
            List<Long> list = paramItems.stream().map(PakinItem::getMatnrId).collect(Collectors.toList());
            List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>()
                    .eq(WaitPakinItem::getPakinId, waitPakins.getId())
                    .in(WaitPakinItem::getMatnrId, list));
            if (pakinItems.isEmpty()) {
                throw new CoolException("数据错误:组拖明细不存在!!");
            }
            List<Long> ids = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toList());
            if (!waitPakinItemService.removeByIds(ids)) {
                throw new CoolException("组拖明细解绑失败!!");
            }
            return waitPakins;
        }
        return new WaitPakin();
    }
}