2026-07-01 6b5f7c66fc40d7f6099d561e31a34fbd50dd20d3
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
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.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.erp.dal.mysql.product.ErpProductMapper;
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.enums.MdmItemTypeEnum;
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 实现类
 *
 * 改造说明:产品数据统一使用 MDM 主数据管理(itemType=2 产品类型)
 * - 创建、更新、删除操作通过 MDM API 进行
 * - 查询操作从 MDM 获取数据
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ErpProductServiceImpl implements ErpProductService {
 
    @Resource
    private ErpProductMapper productMapper;
 
    @Resource
    private MdmItemApi mdmItemApi;
 
    @Override
    public Long createProduct(ProductSaveReqVO createReqVO) {
        // 1. 校验条码是否重复
        MdmItemRespDTO existItem = mdmItemApi.getItemByBarCode(createReqVO.getBarCode()).getCheckedData();
        if (existItem != null) {
            throw exception(PRODUCT_BAR_CODE_EXISTS);
        }
        // 2. 通过 MDM API 创建产品
        MdmItemCreateReqDTO createReqDTO = new MdmItemCreateReqDTO();
        createReqDTO.setCode(createReqVO.getBarCode()); // 使用条码作为编码
        createReqDTO.setName(createReqVO.getName());
        createReqDTO.setBarCode(createReqVO.getBarCode());
        createReqDTO.setSpecification(createReqVO.getStandard());
        createReqDTO.setCategoryId(createReqVO.getCategoryId());
        createReqDTO.setUnitMeasureId(createReqVO.getUnitId());
        createReqDTO.setItemType(MdmItemTypeEnum.PRODUCT.getType()); // 产品类型
        createReqDTO.setStatus(createReqVO.getStatus() != null ? createReqVO.getStatus() : CommonStatusEnum.ENABLE.getStatus());
        createReqDTO.setPurchasePrice(createReqVO.getPurchasePrice());
        createReqDTO.setSalesPrice(createReqVO.getSalePrice());
        createReqDTO.setCostPrice(createReqVO.getMinPrice());
        createReqDTO.setExpiryDay(createReqVO.getExpiryDay());
        createReqDTO.setWeight(createReqVO.getWeight());
        createReqDTO.setRemark(createReqVO.getRemark());
        return mdmItemApi.createItem(createReqDTO).getCheckedData();
    }
 
    @Override
    public void updateProduct(ProductSaveReqVO updateReqVO) {
        // 1. 校验存在
        MdmItemRespDTO existItem = mdmItemApi.getItem(updateReqVO.getId()).getCheckedData();
        if (existItem == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
        // 2. 校验条码是否重复
        if (!existItem.getBarCode().equals(updateReqVO.getBarCode())) {
            MdmItemRespDTO barCodeItem = mdmItemApi.getItemByBarCode(updateReqVO.getBarCode()).getCheckedData();
            if (barCodeItem != null) {
                throw exception(PRODUCT_BAR_CODE_EXISTS);
            }
        }
        // 3. 通过 MDM API 更新产品
        MdmItemUpdateReqDTO updateReqDTO = new MdmItemUpdateReqDTO();
        updateReqDTO.setId(updateReqVO.getId());
        updateReqDTO.setCode(updateReqVO.getBarCode());
        updateReqDTO.setName(updateReqVO.getName());
        updateReqDTO.setBarCode(updateReqVO.getBarCode());
        updateReqDTO.setSpecification(updateReqVO.getStandard());
        updateReqDTO.setCategoryId(updateReqVO.getCategoryId());
        updateReqDTO.setUnitMeasureId(updateReqVO.getUnitId());
        updateReqDTO.setItemType(MdmItemTypeEnum.PRODUCT.getType());
        updateReqDTO.setStatus(updateReqVO.getStatus());
        updateReqDTO.setPurchasePrice(updateReqVO.getPurchasePrice());
        updateReqDTO.setSalesPrice(updateReqVO.getSalePrice());
        updateReqDTO.setCostPrice(updateReqVO.getMinPrice());
        updateReqDTO.setExpiryDay(updateReqVO.getExpiryDay());
        updateReqDTO.setWeight(updateReqVO.getWeight());
        updateReqDTO.setRemark(updateReqVO.getRemark());
        mdmItemApi.updateItem(updateReqDTO);
    }
 
    @Override
    public void deleteProduct(Long id) {
        // 校验存在
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
        // 通过 MDM API 删除(更新状态为禁用)
        mdmItemApi.updateItemStatus(id, CommonStatusEnum.DISABLE.getStatus());
    }
 
    @Override
    public List<ErpProductDO> validProductList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        // 从 MDM 获取产品数据
        List<MdmItemRespDTO> items = mdmItemApi.getItemList(ids).getCheckedData();
        Map<Long, MdmItemRespDTO> itemMap = convertMap(items, MdmItemRespDTO::getId);
        List<ErpProductDO> result = new ArrayList<>();
        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());
            }
            // 转换为 ErpProductDO
            result.add(convertToErpProduct(item));
        }
        return result;
    }
 
    private void validateProductExists(Long id) {
        // 从 MDM 校验
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }
 
    @Override
    public ErpProductDO getProduct(Long id) {
        // 从 MDM 获取产品数据
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            return null;
        }
        // 检查是否为产品类型
        if (!MdmItemTypeEnum.PRODUCT.getType().equals(item.getItemType())) {
            return null;
        }
        return convertToErpProduct(item);
    }
 
    @Override
    public ErpProductRespVO getProductVO(Long id) {
        // 从 MDM 获取产品数据
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            return null;
        }
        // 检查是否为产品类型
        if (!MdmItemTypeEnum.PRODUCT.getType().equals(item.getItemType())) {
            return null;
        }
        return convertToErpProductVO(item);
    }
 
    /**
     * 将 MDM 物料转换为 ERP 产品 VO
     */
    private ErpProductRespVO convertToErpProductVO(MdmItemRespDTO item) {
        ErpProductRespVO vo = new ErpProductRespVO();
        vo.setId(item.getId());
        vo.setName(item.getName());
        vo.setBarCode(item.getBarCode());
        vo.setStandard(item.getSpecification());
        vo.setCategoryId(item.getCategoryId());
        vo.setUnitId(item.getUnitMeasureId());
        vo.setStatus(item.getStatus());
        vo.setExpiryDay(item.getExpiryDay());
        vo.setWeight(item.getWeight());
        vo.setPurchasePrice(item.getPurchasePrice());
        vo.setSalePrice(item.getSalesPrice());
        vo.setMinPrice(item.getCostPrice());
        vo.setRemark(item.getRemark());
        return vo;
    }
 
    @Override
    public List<ErpProductRespVO> getProductVOListByStatus(Integer status) {
        // 从 MDM 获取产品数据(itemType=2)
        List<MdmItemRespDTO> items = mdmItemApi.getItemListByStatus(status).getCheckedData();
        // 过滤产品类型
        items = items.stream()
                .filter(item -> MdmItemTypeEnum.PRODUCT.getType().equals(item.getItemType()))
                .toList();
        return buildProductVOListFromMdm(items);
    }
 
    @Override
    public List<ErpProductRespVO> getProductVOList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        // 从 MDM 获取产品数据
        List<MdmItemRespDTO> items = mdmItemApi.getItemList(ids).getCheckedData();
        return buildProductVOListFromMdm(items);
    }
 
    @Override
    public PageResult<ErpProductRespVO> getProductVOPage(ErpProductPageReqVO pageReqVO) {
        // 从 MDM 获取产品分页数据
        cn.iocoder.yudao.framework.common.pojo.PageResult<MdmItemRespDTO> mdmPageResult =
                mdmItemApi.getItemPage(MdmItemTypeEnum.PRODUCT.getType(), null,
                        pageReqVO.getPageNo(), pageReqVO.getPageSize()).getCheckedData();
        return new PageResult<>(buildProductVOListFromMdm(mdmPageResult.getList()), mdmPageResult.getTotal());
    }
 
    /**
     * 从 MDM 数据构建 ERP 产品 VO 列表
     */
    private List<ErpProductRespVO> buildProductVOListFromMdm(List<MdmItemRespDTO> items) {
        if (CollUtil.isEmpty(items)) {
            return Collections.emptyList();
        }
        List<ErpProductRespVO> result = new ArrayList<>();
        for (MdmItemRespDTO item : items) {
            ErpProductRespVO vo = new ErpProductRespVO();
            vo.setId(item.getId());
            vo.setName(item.getName());
            vo.setBarCode(item.getBarCode());
            vo.setStandard(item.getSpecification());
            vo.setCategoryId(item.getCategoryId());
            vo.setUnitId(item.getUnitMeasureId());
            vo.setStatus(item.getStatus());
            vo.setExpiryDay(item.getExpiryDay());
            vo.setWeight(item.getWeight());
            vo.setPurchasePrice(item.getPurchasePrice());
            vo.setSalePrice(item.getSalesPrice());
            vo.setMinPrice(item.getCostPrice()); // 使用成本价作为最低价
            vo.setRemark(item.getRemark());
            // 分类名称和单位名称需要额外查询(简化处理,后续可优化)
            result.add(vo);
        }
        return result;
    }
 
    /**
     * 将 MDM 物料转换为 ERP 产品 DO
     */
    private ErpProductDO convertToErpProduct(MdmItemRespDTO item) {
        ErpProductDO product = new ErpProductDO();
        product.setId(item.getId());
        product.setName(item.getName());
        product.setBarCode(item.getBarCode());
        product.setStandard(item.getSpecification());
        product.setCategoryId(item.getCategoryId());
        product.setUnitId(item.getUnitMeasureId());
        product.setStatus(item.getStatus());
        product.setExpiryDay(item.getExpiryDay());
        product.setWeight(item.getWeight());
        product.setPurchasePrice(item.getPurchasePrice());
        product.setSalePrice(item.getSalesPrice());
        product.setMinPrice(item.getCostPrice());
        product.setRemark(item.getRemark());
        return product;
    }
 
    @Override
    public Long getProductCountByCategoryId(Long categoryId) {
        return mdmItemApi.getItemCountByCategoryId(categoryId).getCheckedData();
    }
 
    @Override
    public Long getProductCountByUnitId(Long unitId) {
        return mdmItemApi.getItemCountByUnitMeasureId(unitId).getCheckedData();
    }
 
}