2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
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
package cn.iocoder.yudao.module.wms.service.order.shipment;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.order.shipment.vo.order.WmsShipmentOrderSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.order.shipment.WmsShipmentOrderDetailDO;
import cn.iocoder.yudao.module.wms.dal.mysql.order.shipment.WmsShipmentOrderDetailMapper;
import cn.iocoder.yudao.module.wms.service.md.item.WmsItemSkuService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;
 
/**
 * WMS 出库单明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WmsShipmentOrderDetailServiceImpl implements WmsShipmentOrderDetailService {
 
    @Resource
    private WmsShipmentOrderDetailMapper shipmentOrderDetailMapper;
    @Resource
    private WmsItemSkuService itemSkuService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createShipmentOrderDetailList(Long orderId, WmsShipmentOrderSaveReqVO reqVO) {
        List<WmsShipmentOrderDetailDO> list = buildShipmentOrderDetailList(reqVO);
        if (CollUtil.isEmpty(list)) {
            return;
        }
        list.forEach(detail -> detail.setId(null).setOrderId(orderId));
        shipmentOrderDetailMapper.insertBatch(list);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShipmentOrderDetailList(Long orderId, WmsShipmentOrderSaveReqVO reqVO) {
        // 第一步,对比新老数据,获得添加、修改、删除的列表
        List<WmsShipmentOrderDetailDO> oldList = shipmentOrderDetailMapper.selectListByOrderId(orderId);
        List<WmsShipmentOrderDetailDO> list = buildShipmentOrderDetailList(reqVO);
        List<WmsShipmentOrderDetailDO> newList = CollUtil.isEmpty(list) ? ListUtil.of() : list;
        List<List<WmsShipmentOrderDetailDO>> diffList = diffList(oldList, newList, // id 不同,就认为是不同的记录
                (oldVal, newVal) -> ObjectUtil.equal(oldVal.getId(), newVal.getId()));
 
        // 第二步,批量添加、修改、删除
        if (CollUtil.isNotEmpty(diffList.get(0))) {
            if (CollUtil.isNotEmpty(convertList(diffList.get(0), WmsShipmentOrderDetailDO::getId))) {
                throw exception(SHIPMENT_ORDER_DETAIL_NOT_EXISTS);
            }
            diffList.get(0).forEach(detail -> detail.setOrderId(orderId));
            shipmentOrderDetailMapper.insertBatch(diffList.get(0));
        }
        if (CollUtil.isNotEmpty(diffList.get(1))) {
            diffList.get(1).forEach(detail -> detail.setOrderId(orderId));
            shipmentOrderDetailMapper.updateBatch(diffList.get(1));
        }
        if (CollUtil.isNotEmpty(diffList.get(2))) {
            shipmentOrderDetailMapper.deleteByIds(convertList(diffList.get(2), WmsShipmentOrderDetailDO::getId));
        }
    }
 
    @Override
    public void deleteShipmentOrderDetailListByOrderId(Long orderId) {
        shipmentOrderDetailMapper.deleteByOrderId(orderId);
    }
 
    @Override
    public List<WmsShipmentOrderDetailDO> getShipmentOrderDetailList(Long orderId) {
        return shipmentOrderDetailMapper.selectListByOrderId(orderId);
    }
 
    @Override
    public List<WmsShipmentOrderDetailDO> getShipmentOrderDetailList(Collection<Long> orderIds) {
        if (CollUtil.isEmpty(orderIds)) {
            return ListUtil.of();
        }
        return shipmentOrderDetailMapper.selectListByOrderIds(orderIds);
    }
 
    @Override
    public List<WmsShipmentOrderDetailDO> validateShipmentOrderDetailListExists(Long orderId) {
        List<WmsShipmentOrderDetailDO> details = shipmentOrderDetailMapper.selectListByOrderId(orderId);
        if (CollUtil.isEmpty(details)) {
            throw exception(SHIPMENT_ORDER_DETAIL_REQUIRED);
        }
        return details;
    }
 
    @Override
    public long getShipmentOrderDetailCountBySkuId(Long skuId) {
        return shipmentOrderDetailMapper.selectCountBySkuId(skuId);
    }
 
    private List<WmsShipmentOrderDetailDO> buildShipmentOrderDetailList(WmsShipmentOrderSaveReqVO reqVO) {
        if (CollUtil.isEmpty(reqVO.getDetails())) {
            return ListUtil.of();
        }
        return convertList(reqVO.getDetails(), detail -> {
            // 校验 SKU 存在
            itemSkuService.validateItemSkuExists(detail.getSkuId());
            // 构建对象
            WmsShipmentOrderDetailDO detailDO = BeanUtils.toBean(detail, WmsShipmentOrderDetailDO.class)
                    .setWarehouseId(reqVO.getWarehouseId());
            fillDetailTotalPrice(detailDO);
            return detailDO;
        });
    }
 
    private static void fillDetailTotalPrice(WmsShipmentOrderDetailDO detail) {
        if (detail.getTotalPrice() != null || detail.getQuantity() == null || detail.getPrice() == null) {
            return;
        }
        detail.setTotalPrice(MoneyUtils.priceMultiply(detail.getPrice(), detail.getQuantity()));
    }
 
}