2026-06-29 940c8481d2fdcf51341db4ccd6fd6fcc2d43debb
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cn.iocoder.yudao.module.erp.service.product;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ProductSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemCreateReqDTO;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemUpdateReqDTO;
import cn.iocoder.yudao.module.mdm.api.category.MdmItemCategoryApi;
import cn.iocoder.yudao.module.mdm.api.category.dto.MdmItemCategoryRespDTO;
import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi;
import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.*;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;
 
/**
 * ERP 产品 Service 实现类
 *
 * 改造说明:ERP 产品数据来源于 MDM 物料主数据
 * - 所有产品操作均调用 MDM API
 * - ERP 产品对应 MDM 物料类型 = 成品(itemType = 2)
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ErpProductServiceImpl implements ErpProductService {
 
    /** ERP 产品对应的 MDM 物料类型:成品 */
    private static final int MDM_ITEM_TYPE_PRODUCT = 2;
 
    @Resource
    private MdmItemApi mdmItemApi;
 
    @Resource
    private MdmItemCategoryApi mdmItemCategoryApi;
 
    @Resource
    private MdmUnitMeasureApi mdmUnitMeasureApi;
 
    @Override
    public Long createProduct(ProductSaveReqVO createReqVO) {
        // 1. 校验条码唯一
        validateProductBarCodeUnique(null, createReqVO.getBarCode());
 
        // 2. 构建创建请求并调用 MDM API
        MdmItemCreateReqDTO createReqDTO = buildCreateReqDTO(createReqVO);
        return mdmItemApi.createItem(createReqDTO).getCheckedData();
    }
 
    @Override
    public void updateProduct(ProductSaveReqVO updateReqVO) {
        // 1. 校验存在
        validateProductExists(updateReqVO.getId());
 
        // 2. 校验条码唯一
        validateProductBarCodeUnique(updateReqVO.getId(), updateReqVO.getBarCode());
 
        // 3. 构建更新请求并调用 MDM API
        MdmItemUpdateReqDTO updateReqDTO = buildUpdateReqDTO(updateReqVO);
        mdmItemApi.updateItem(updateReqDTO);
    }
 
    @Override
    public void deleteProduct(Long id) {
        // 校验存在
        validateProductExists(id);
        // ERP 产品删除实际为禁用(MDM 物料不能物理删除)
        mdmItemApi.updateItemStatus(id, CommonStatusEnum.DISABLE.getStatus());
    }
 
    @Override
    public List<ErpProductDO> validProductList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
 
        // 1. 调用 MDM API 获取物料列表
        List<MdmItemRespDTO> itemList = mdmItemApi.getItemList(ids).getCheckedData();
        Map<Long, MdmItemRespDTO> itemMap = convertMap(itemList, MdmItemRespDTO::getId);
 
        // 2. 校验每个产品
        for (Long id : ids) {
            MdmItemRespDTO item = itemMap.get(id);
            if (item == null) {
                throw exception(PRODUCT_NOT_EXISTS);
            }
            if (CommonStatusEnum.isDisable(item.getStatus())) {
                throw exception(PRODUCT_NOT_ENABLE, item.getName());
            }
        }
 
        // 3. 转换为 ERP Product DO(手动映射字段)
        return convertToErpProductDOList(itemList);
    }
 
    @Override
    public ErpProductDO getProduct(Long id) {
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        return convertToErpProductDO(item);
    }
 
    @Override
    public List<ErpProductRespVO> getProductVOListByStatus(Integer status) {
        List<MdmItemRespDTO> itemList = mdmItemApi.getItemListByStatus(status).getCheckedData();
        return buildProductVOList(itemList);
    }
 
    @Override
    public List<ErpProductRespVO> getProductVOList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<MdmItemRespDTO> itemList = mdmItemApi.getItemList(ids).getCheckedData();
        return buildProductVOList(itemList);
    }
 
    @Override
    public PageResult<ErpProductRespVO> getProductVOPage(ErpProductPageReqVO pageReqVO) {
        // 调用 MDM API 分页查询
        PageResult<MdmItemRespDTO> mdmPageResult = mdmItemApi.getItemPage(
                MDM_ITEM_TYPE_PRODUCT,
                pageReqVO.getStatus(),
                pageReqVO.getPageNo(),
                pageReqVO.getPageSize()
        ).getCheckedData();
 
        List<ErpProductRespVO> productVOList = buildProductVOList(mdmPageResult.getList());
        return new PageResult<>(productVOList, mdmPageResult.getTotal());
    }
 
    @Override
    public Long getProductCountByCategoryId(Long categoryId) {
        return mdmItemApi.getItemCountByCategoryId(categoryId).getCheckedData();
    }
 
    @Override
    public Long getProductCountByUnitId(Long unitId) {
        return mdmItemApi.getItemCountByUnitMeasureId(unitId).getCheckedData();
    }
 
    // ==================== 私有方法 ====================
 
    /**
     * 校验产品存在
     */
    private void validateProductExists(Long id) {
        mdmItemApi.validateItemExists(id).getCheckedData();
    }
 
    /**
     * 校验产品条码唯一
     */
    private void validateProductBarCodeUnique(Long id, String barCode) {
        if (barCode == null) {
            return;
        }
        MdmItemRespDTO existItem = mdmItemApi.getItemByBarCode(barCode).getCheckedData();
        if (existItem != null && !existItem.getId().equals(id)) {
            throw exception(PRODUCT_BAR_CODE_EXISTS);
        }
    }
 
    /**
     * 构建创建请求 DTO
     * 注意:ProductSaveReqVO 使用 standard 字段作为规格,MDM 使用 specification
     */
    private MdmItemCreateReqDTO buildCreateReqDTO(ProductSaveReqVO saveReqVO) {
        MdmItemCreateReqDTO createReqDTO = new MdmItemCreateReqDTO();
        createReqDTO.setCode(saveReqVO.getBarCode()); // 使用条码作为编码
        createReqDTO.setName(saveReqVO.getName());
        createReqDTO.setBarCode(saveReqVO.getBarCode());
        createReqDTO.setSpecification(saveReqVO.getStandard()); // standard -> specification
        createReqDTO.setCategoryId(saveReqVO.getCategoryId());
        createReqDTO.setUnitMeasureId(saveReqVO.getUnitId()); // unitId -> unitMeasureId
        createReqDTO.setItemType(MDM_ITEM_TYPE_PRODUCT);
        createReqDTO.setStatus(saveReqVO.getStatus() != null ? saveReqVO.getStatus() : CommonStatusEnum.ENABLE.getStatus());
        createReqDTO.setPurchasePrice(saveReqVO.getPurchasePrice());
        createReqDTO.setSalesPrice(saveReqVO.getSalePrice());
        createReqDTO.setCostPrice(saveReqVO.getMinPrice()); // minPrice -> costPrice
        createReqDTO.setExpiryDay(saveReqVO.getExpiryDay());
        createReqDTO.setWeight(saveReqVO.getWeight());
        createReqDTO.setRemark(saveReqVO.getRemark());
        return createReqDTO;
    }
 
    /**
     * 构建更新请求 DTO
     */
    private MdmItemUpdateReqDTO buildUpdateReqDTO(ProductSaveReqVO saveReqVO) {
        MdmItemUpdateReqDTO updateReqDTO = new MdmItemUpdateReqDTO();
        updateReqDTO.setId(saveReqVO.getId());
        updateReqDTO.setCode(saveReqVO.getBarCode());
        updateReqDTO.setName(saveReqVO.getName());
        updateReqDTO.setBarCode(saveReqVO.getBarCode());
        updateReqDTO.setSpecification(saveReqVO.getStandard()); // standard -> specification
        updateReqDTO.setCategoryId(saveReqVO.getCategoryId());
        updateReqDTO.setUnitMeasureId(saveReqVO.getUnitId()); // unitId -> unitMeasureId
        updateReqDTO.setStatus(saveReqVO.getStatus());
        updateReqDTO.setPurchasePrice(saveReqVO.getPurchasePrice());
        updateReqDTO.setSalesPrice(saveReqVO.getSalePrice());
        updateReqDTO.setCostPrice(saveReqVO.getMinPrice());
        updateReqDTO.setExpiryDay(saveReqVO.getExpiryDay());
        updateReqDTO.setWeight(saveReqVO.getWeight());
        updateReqDTO.setRemark(saveReqVO.getRemark());
        return updateReqDTO;
    }
 
    /**
     * 构建产品 VO 列表(包含分类和单位名称)
     * 注意:MDM 和 ERP 字段名有差异,需要手动映射
     */
    private List<ErpProductRespVO> buildProductVOList(List<MdmItemRespDTO> itemList) {
        if (CollUtil.isEmpty(itemList)) {
            return Collections.emptyList();
        }
 
        // 1. 获取分类和单位信息
        Set<Long> categoryIds = convertSet(itemList, MdmItemRespDTO::getCategoryId);
        Set<Long> unitIds = convertSet(itemList, MdmItemRespDTO::getUnitMeasureId);
 
        Map<Long, MdmItemCategoryRespDTO> categoryMap = CollUtil.isEmpty(categoryIds)
                ? Collections.emptyMap()
                : convertMap(mdmItemCategoryApi.getItemCategoryList(categoryIds).getCheckedData(), MdmItemCategoryRespDTO::getId);
 
        Map<Long, MdmUnitMeasureRespDTO> unitMap = CollUtil.isEmpty(unitIds)
                ? Collections.emptyMap()
                : convertMap(mdmUnitMeasureApi.getUnitMeasureList(unitIds).getCheckedData(), MdmUnitMeasureRespDTO::getId);
 
        // 2. 手动转换并填充关联信息(处理字段名差异)
        List<ErpProductRespVO> result = new ArrayList<>();
        for (MdmItemRespDTO mdmItem : itemList) {
            ErpProductRespVO product = new ErpProductRespVO();
            product.setId(mdmItem.getId());
            product.setName(mdmItem.getName());
            product.setBarCode(mdmItem.getBarCode());
            product.setStandard(mdmItem.getSpecification()); // specification -> standard
            product.setCategoryId(mdmItem.getCategoryId());
            product.setUnitId(mdmItem.getUnitMeasureId()); // unitMeasureId -> unitId
            product.setStatus(mdmItem.getStatus());
            product.setPurchasePrice(mdmItem.getPurchasePrice());
            product.setSalePrice(mdmItem.getSalesPrice()); // salesPrice -> salePrice
            product.setMinPrice(mdmItem.getCostPrice()); // costPrice -> minPrice
            product.setExpiryDay(mdmItem.getExpiryDay());
            product.setWeight(mdmItem.getWeight());
            product.setRemark(mdmItem.getRemark());
 
            // 设置分类和单位名称
            MapUtils.findAndThen(categoryMap, mdmItem.getCategoryId(),
                    category -> product.setCategoryName(category.getName()));
            MapUtils.findAndThen(unitMap, mdmItem.getUnitMeasureId(),
                    unit -> product.setUnitName(unit.getName()));
 
            result.add(product);
        }
        return result;
    }
 
    /**
     * 转换 MDM 物料为 ERP 产品 DO
     * 注意:字段名差异需要手动映射
     */
    private ErpProductDO convertToErpProductDO(MdmItemRespDTO mdmItem) {
        if (mdmItem == null) {
            return null;
        }
        ErpProductDO product = new ErpProductDO();
        product.setId(mdmItem.getId());
        product.setName(mdmItem.getName());
        product.setBarCode(mdmItem.getBarCode());
        product.setStandard(mdmItem.getSpecification()); // specification -> standard
        product.setCategoryId(mdmItem.getCategoryId());
        product.setUnitId(mdmItem.getUnitMeasureId()); // unitMeasureId -> unitId
        product.setStatus(mdmItem.getStatus());
        product.setPurchasePrice(mdmItem.getPurchasePrice());
        product.setSalePrice(mdmItem.getSalesPrice()); // salesPrice -> salePrice
        product.setMinPrice(mdmItem.getCostPrice()); // costPrice -> minPrice
        product.setExpiryDay(mdmItem.getExpiryDay());
        product.setWeight(mdmItem.getWeight());
        product.setRemark(mdmItem.getRemark());
        return product;
    }
 
    /**
     * 批量转换 MDM 物料列表为 ERP 产品列表
     */
    private List<ErpProductDO> convertToErpProductDOList(List<MdmItemRespDTO> mdmItemList) {
        if (CollUtil.isEmpty(mdmItemList)) {
            return Collections.emptyList();
        }
        List<ErpProductDO> result = new ArrayList<>();
        for (MdmItemRespDTO mdmItem : mdmItemList) {
            result.add(convertToErpProductDO(mdmItem));
        }
        return result;
    }
 
}