liyong
2026-07-10 80ddf4c9c0bcb0f6c31524e0d9f5599c8001e904
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
package cn.iocoder.yudao.module.erp.service.product.mapping;
 
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.erp.dal.dataobject.mapping.ErpMesWarehouseMappingDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpWarehouseDO;
import cn.iocoder.yudao.module.erp.dal.mysql.mapping.ErpMesWarehouseMappingMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.stock.ErpWarehouseMapper;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
/**
 * ERP 产品与 MDM 物料映射服务实现类
 */
@Service
@Validated
@Slf4j
public class ErpMdmMappingServiceImpl implements ErpMdmMappingService {
 
    @Resource
    private ErpProductMapper productMapper;
 
    @Resource
    private ErpWarehouseMapper warehouseMapper;
 
    @Resource
    private ErpMesWarehouseMappingMapper warehouseMappingMapper;
 
    @Resource
    private MdmItemApi mdmItemApi;
 
    @Override
    public Long getMdmItemId(Long erpProductId) {
        if (erpProductId == null) {
            return null;
        }
        // 1. 查询 ERP 产品
        ErpProductDO product = productMapper.selectById(erpProductId);
        if (product == null || StrUtil.isEmpty(product.getBarCode())) {
            log.warn("[getMdmItemId] ERP产品不存在或条码为空,erpProductId={}", erpProductId);
            return null;
        }
 
        // 2. 通过条码查询 MDM 物料
        return getMdmItemIdByBarCode(product.getBarCode());
    }
 
    @Override
    public Long getMdmItemIdByBarCode(String barCode) {
        if (StrUtil.isEmpty(barCode)) {
            return null;
        }
 
        // 优先通过条码查询
        MdmItemRespDTO item = mdmItemApi.getItemByBarCode(barCode).getCheckedData();
        if (item != null) {
            return item.getId();
        }
 
        // 条码匹配编码
        item = mdmItemApi.getItemByCode(barCode).getCheckedData();
        if (item != null) {
            return item.getId();
        }
 
        log.warn("[getMdmItemIdByBarCode] 未找到对应的MDM物料,barCode={}", barCode);
        return null;
    }
 
    @Override
    public Long getMesWarehouseId(Long erpWarehouseId) {
        if (erpWarehouseId == null) {
            return null;
        }
 
        // 1. 优先查询映射表
        ErpMesWarehouseMappingDO mapping = warehouseMappingMapper.selectByErpWarehouseId(erpWarehouseId);
        if (mapping != null) {
            return mapping.getMesWarehouseId();
        }
 
        // 2. 映射表没有记录,尝试通过名称匹配(备用方案)
        ErpWarehouseDO erpWarehouse = warehouseMapper.selectById(erpWarehouseId);
        if (erpWarehouse == null) {
            log.warn("[getMesWarehouseId] ERP仓库不存在,erpWarehouseId={}", erpWarehouseId);
            return null;
        }
 
        log.warn("[getMesWarehouseId] 未找到仓库映射配置,erpWarehouseId={},请配置仓库映射", erpWarehouseId);
        return null;
    }
 
    @Override
    public Long getMesWarehouseIdByName(String erpWarehouseName) {
        // 此方法已不再推荐使用,建议配置映射表
        log.warn("[getMesWarehouseIdByName] 建议配置仓库映射表替代名称匹配,erpWarehouseName={}", erpWarehouseName);
        return null;
    }
 
}