9 天以前 2e84a447af471fe5ada907c28849838a6bc3a340
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.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.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;
 
    @Override
    public Long createProductSalesDetail(MesWmProductSalesDetailSaveReqVO createReqVO) {
        // 校验数据
        validateProductSalesDetailSaveData(createReqVO);
 
        // 插入
        MesWmProductSalesDetailDO detail = BeanUtils.toBean(createReqVO, MesWmProductSalesDetailDO.class);
        productSalesDetailMapper.insert(detail);
        return detail.getId();
    }
 
    @Override
    public void updateProductSalesDetail(MesWmProductSalesDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateProductSalesDetailExists(updateReqVO.getId());
        // 校验数据
        validateProductSalesDetailSaveData(updateReqVO);
 
        // 更新
        MesWmProductSalesDetailDO updateObj = BeanUtils.toBean(updateReqVO, MesWmProductSalesDetailDO.class);
        productSalesDetailMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteProductSalesDetail(Long id) {
        // 校验存在
        validateProductSalesDetailExists(id);
        // 删除
        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 validateProductSalesDetailSaveData(MesWmProductSalesDetailSaveReqVO reqVO) {
        // 校验父数据(行)存在
        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);
        }
        // 校验物料存在
        mdmItemService.validateItemExistsAndEnable(reqVO.getItemId());
        if (ObjUtil.notEqual(line.getItemId(), reqVO.getItemId())) {
            throw exception(WM_PRODUCT_SALES_DETAIL_ITEM_MISMATCH);
        }
        // 校验库位存在
        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());
    }
 
}