云
7 天以前 87733eb3623593da247c051593d64b8ff9e1d3a4
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
package cn.iocoder.yudao.module.mes.service.wm.productsales;
 
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.detail.MesWmProductSalesDetailSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDetailDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesDetailMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.materialstockbag.MesWmMaterialStockBagMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesMapper;
import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductSalesStatusEnum;
import org.springframework.transaction.annotation.Transactional;
import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService;
import cn.iocoder.yudao.module.mes.service.wm.materialstock.MesWmMaterialStockService;
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 销售出库明细 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesWmProductSalesDetailServiceImpl implements MesWmProductSalesDetailService {
 
    @Resource
    private MesWmProductSalesDetailMapper productSalesDetailMapper;
 
    @Resource
    private MesMdmItemService mdmItemService;
    @Resource
    @Lazy
    private MesWmProductSalesLineService productSalesLineService;
    @Resource
    private MesWmMaterialStockService materialStockService;
    @Resource
    private MesWmWarehouseAreaService warehouseAreaService;
    @Resource
    private MesWmMaterialStockBagMapper stockBagMapper;
    @Resource
    private MesWmProductSalesMapper productSalesMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createProductSalesDetail(MesWmProductSalesDetailSaveReqVO createReqVO) {
        lockPickingOrder(createReqVO.getSalesId());
        if (createReqVO.getRemark() != null && createReqVO.getRemark().matches("移动端无袋码拣货 #[a-f0-9-]{36}")) {
            var existing = productSalesDetailMapper.selectListBySalesId(createReqVO.getSalesId()).stream()
                    .filter(d -> createReqVO.getRemark().equals(d.getRemark())).findFirst().orElse(null);
            if (existing != null) {
                if (ObjUtil.notEqual(existing.getLineId(), createReqVO.getLineId())
                        || ObjUtil.notEqual(existing.getMaterialStockId(), createReqVO.getMaterialStockId())
                        || existing.getQuantity().compareTo(createReqVO.getQuantity()) != 0)
                    throw new IllegalArgumentException("拣货请求标识已用于其他明细");
                return existing.getId();
            }
        }
        // 校验数据
        validateProductSalesDetailSaveData(createReqVO);
 
        // 插入
        MesWmProductSalesDetailDO detail = BeanUtils.toBean(createReqVO, MesWmProductSalesDetailDO.class);
        productSalesDetailMapper.insert(detail);
        return detail.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductSalesDetail(MesWmProductSalesDetailSaveReqVO updateReqVO) {
        // 校验存在
        MesWmProductSalesDetailDO existing = validateProductSalesDetailExists(updateReqVO.getId());
        lockPickingOrder(existing.getSalesId());
        if (existing.getShipmentId() != null) throw new IllegalArgumentException("已分配发货记录的明细不能修改");
        if (ObjUtil.notEqual(existing.getSalesId(), updateReqVO.getSalesId())) throw exception(WM_PRODUCT_SALES_DETAIL_LINE_NOT_MATCH);
        if (existing.getBagCodeId() != null) throw new IllegalArgumentException("扫码袋码明细不能手工修改数量或库存,请删除后重新扫码选择");
        // 校验数据
        validateProductSalesDetailSaveData(updateReqVO);
 
        // 更新
        MesWmProductSalesDetailDO updateObj = BeanUtils.toBean(updateReqVO, MesWmProductSalesDetailDO.class);
        productSalesDetailMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductSalesDetail(Long id) {
        // 校验存在
        MesWmProductSalesDetailDO detail = validateProductSalesDetailExists(id);
        lockPickingOrder(detail.getSalesId());
        if (detail.getShipmentId() != null) throw new IllegalArgumentException("已分配发货记录的明细不能删除");
        // 删除
        productSalesDetailMapper.deleteById(id);
    }
 
    @Override
    public MesWmProductSalesDetailDO getProductSalesDetail(Long id) {
        return productSalesDetailMapper.selectById(id);
    }
 
    @Override
    public List<MesWmProductSalesDetailDO> getProductSalesDetailListBySalesId(Long salesId) {
        return productSalesDetailMapper.selectListBySalesId(salesId);
    }
 
    @Override
    public List<MesWmProductSalesDetailDO> getProductSalesDetailListByLineId(Long lineId) {
        return productSalesDetailMapper.selectListByLineId(lineId);
    }
 
    @Override
    public void deleteProductSalesDetailByLineId(Long lineId) {
        productSalesDetailMapper.deleteByLineId(lineId);
    }
 
    @Override
    public void deleteProductSalesDetailBySalesId(Long salesId) {
        productSalesDetailMapper.deleteBySalesId(salesId);
    }
 
    private MesWmProductSalesDetailDO validateProductSalesDetailExists(Long id) {
        MesWmProductSalesDetailDO detail = productSalesDetailMapper.selectById(id);
        if (detail == null) {
            throw exception(WM_PRODUCT_SALES_DETAIL_NOT_EXISTS);
        }
        return detail;
    }
 
    private void lockPickingOrder(Long salesId) {
        var sales = productSalesMapper.selectByIdForUpdate(salesId);
        if (sales == null) throw exception(WM_PRODUCT_SALES_NOT_EXISTS);
        if (!MesWmProductSalesStatusEnum.APPROVING.getStatus().equals(sales.getStatus())) {
            throw exception(WM_PRODUCT_SALES_CANNOT_PICK);
        }
    }
 
    private void validateProductSalesDetailSaveData(MesWmProductSalesDetailSaveReqVO reqVO) {
        if (reqVO.getQuantity() == null || reqVO.getQuantity().signum() <= 0) throw new IllegalArgumentException("拣货数量必须大于零");
        if (reqVO.getMaterialStockId() != null && stockBagMapper.selectOneActiveByMaterialStockId(reqVO.getMaterialStockId()) != null) {
            throw new IllegalArgumentException("该库存存在袋码明细,请选择实际拣出的袋码");
        }
        // 校验父数据(行)存在
        MesWmProductSalesLineDO line = productSalesLineService.getProductSalesLine(reqVO.getLineId());
        if (line == null) {
            throw exception(WM_PRODUCT_SALES_LINE_NOT_EXISTS);
        }
        if (ObjUtil.notEqual(line.getSalesId(), reqVO.getSalesId())) {
            throw exception(WM_PRODUCT_SALES_DETAIL_LINE_NOT_MATCH);
        }
        var picked = productSalesDetailMapper.selectListByLineId(line.getId()).stream()
                .filter(d -> !java.util.Objects.equals(d.getId(), reqVO.getId()))
                .map(MesWmProductSalesDetailDO::getQuantity).reduce(java.math.BigDecimal.ZERO, java.math.BigDecimal::add);
        if (line.getQuantity() == null || picked.add(reqVO.getQuantity()).compareTo(line.getQuantity()) > 0)
            throw new IllegalArgumentException("累计拣货数量不能超过出库单行计划数量");
        // 校验物料存在
        mdmItemService.validateItemExistsAndEnable(reqVO.getItemId());
        if (ObjUtil.notEqual(line.getItemId(), reqVO.getItemId())) {
            throw exception(WM_PRODUCT_SALES_DETAIL_ITEM_MISMATCH);
        }
        if (line.getBatchCode() != null && !line.getBatchCode().isBlank()
                && !java.util.Objects.equals(line.getBatchCode(), reqVO.getBatchCode()))
            throw new IllegalArgumentException("所选库存批次与出库单行批次不一致");
        // 校验库位存在
        warehouseAreaService.validateWarehouseAreaExists(
                reqVO.getWarehouseId(), reqVO.getLocationId(), reqVO.getAreaId());
        // 校验库存记录存在且物料一致
        materialStockService.validateSelectedStock(
                reqVO.getMaterialStockId(), reqVO.getItemId(), reqVO.getBatchId(), reqVO.getBatchCode(),
                reqVO.getWarehouseId(), reqVO.getLocationId(), reqVO.getAreaId(), reqVO.getQuantity());
    }
 
}