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
package cn.iocoder.yudao.module.wms.service.md.item;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import cn.iocoder.yudao.module.mdm.enums.MdmItemTypeEnum;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.item.WmsItemListReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.item.WmsItemPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.item.WmsItemSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.md.item.WmsItemDO;
import cn.iocoder.yudao.module.wms.dal.mysql.md.item.WmsItemMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;
 
/**
 * WMS 商品 Service 实现类
 *
 * 改造说明:商品基础数据从 MDM 模块获取(itemType=1 物料类型)
 * WMS 特有功能(SKU、库存等)仍保留 WMS 管理
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WmsItemServiceImpl implements WmsItemService {
 
    @Resource
    private WmsItemMapper itemMapper;
 
    @Resource
    private MdmItemApi mdmItemApi;
 
    @Resource
    @Lazy // 延迟加载,避免循环依赖
    private WmsItemCategoryService categoryService;
    @Resource
    @Lazy // 延迟加载,避免循环依赖
    private WmsItemBrandService brandService;
    @Resource
    private WmsItemSkuService itemSkuService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createItem(WmsItemSaveReqVO createReqVO) {
        // 校验数据
        validateItemSaveData(createReqVO);
 
        // 插入商品(暂时保留原逻辑,后续可完全迁移到 MDM)
        WmsItemDO item = BeanUtils.toBean(createReqVO, WmsItemDO.class);
        itemMapper.insert(item);
        // 插入 SKU
        itemSkuService.createItemSkuList(item.getId(), createReqVO.getSkus());
        return item.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateItem(WmsItemSaveReqVO updateReqVO) {
        // 校验存在
        validateItemExists(updateReqVO.getId());
        // 校验数据
        validateItemSaveData(updateReqVO);
 
        // 更新商品(暂时保留原逻辑)
        WmsItemDO updateObj = BeanUtils.toBean(updateReqVO, WmsItemDO.class);
        itemMapper.updateById(updateObj);
        // 更新 SKU
        itemSkuService.updateItemSkuList(updateReqVO.getId(), updateReqVO.getSkus());
    }
 
    private void validateItemSaveData(WmsItemSaveReqVO reqVO) {
        // 校验商品编号唯一
        validateItemCodeUnique(reqVO.getId(), reqVO.getCode());
        // 校验商品名称唯一
        validateItemNameUnique(reqVO.getId(), reqVO.getName());
        // 校验商品分类存在
        validateCategoryExists(reqVO.getCategoryId());
        // 校验商品品牌存在
        validateBrandExists(reqVO.getBrandId());
    }
 
    private void validateItemCodeUnique(Long id, String code) {
        // 检查 MDM 中编码唯一性
        MdmItemRespDTO item = mdmItemApi.getItemByCode(code).getCheckedData();
        if (item != null && ObjectUtil.notEqual(item.getId(), id)) {
            throw exception(ITEM_CODE_DUPLICATE);
        }
    }
 
    private void validateItemNameUnique(Long id, String name) {
        WmsItemDO item = itemMapper.selectByName(name);
        if (item == null) {
            return;
        }
        if (id == null || ObjectUtil.notEqual(item.getId(), id)) {
            throw exception(ITEM_NAME_DUPLICATE);
        }
    }
 
    private void validateCategoryExists(Long categoryId) {
        categoryService.validateItemCategoryExists(categoryId);
    }
 
    private void validateBrandExists(Long brandId) {
        if (brandId == null) {
            return;
        }
        brandService.validateItemBrandExists(brandId);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteItem(Long id) {
        // 校验存在
        validateItemExists(id);
 
        // 删除商品和 SKU
        itemSkuService.deleteItemSkuListByItemId(id);
        itemMapper.deleteById(id);
    }
 
    @Override
    public WmsItemDO validateItemExists(Long id) {
        // 从 MDM 校验物料存在
        MdmItemRespDTO item = mdmItemApi.validateItemExists(id).getCheckedData();
        if (item == null) {
            // 如果 MDM 中不存在,检查本地表
            WmsItemDO localItem = itemMapper.selectById(id);
            if (localItem == null) {
                throw exception(ITEM_NOT_EXISTS);
            }
            return localItem;
        }
        return convertToWmsItem(item);
    }
 
    @Override
    public WmsItemDO getItem(Long id) {
        // 从 MDM 获取物料数据(注意 WMS ID 偏移 30000)
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            // 如果 MDM 中不存在,查询本地表
            return itemMapper.selectById(id);
        }
        return convertToWmsItem(item);
    }
 
    @Override
    public PageResult<WmsItemDO> getItemPage(WmsItemPageReqVO pageReqVO) {
        // 从 MDM 获取物料分页数据(itemType=1 物料类型)
        cn.iocoder.yudao.framework.common.pojo.PageResult<MdmItemRespDTO> mdmPageResult =
                mdmItemApi.getItemPage(MdmItemTypeEnum.MATERIAL.getType(), null,
                        pageReqVO.getPageNo(), pageReqVO.getPageSize()).getCheckedData();
 
        // 转换为 WMS 商品 DO
        List<WmsItemDO> list = new ArrayList<>();
        for (MdmItemRespDTO item : mdmPageResult.getList()) {
            list.add(convertToWmsItem(item));
        }
        return new PageResult<>(list, mdmPageResult.getTotal());
    }
 
    @Override
    public List<WmsItemDO> getItemList(WmsItemListReqVO listReqVO) {
        // 从 MDM 获取物料列表(itemType=1 物料类型)
        List<MdmItemRespDTO> items = mdmItemApi.getItemListByStatus(null).getCheckedData();
 
        List<WmsItemDO> result = new ArrayList<>();
        for (MdmItemRespDTO item : items) {
            // 只返回物料类型
            if (MdmItemTypeEnum.MATERIAL.getType().equals(item.getItemType())) {
                result.add(convertToWmsItem(item));
            }
        }
        return result;
    }
 
    @Override
    public List<WmsItemDO> getItemList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        // 从 MDM 获取物料列表
        List<MdmItemRespDTO> items = mdmItemApi.getItemList(ids).getCheckedData();
        List<WmsItemDO> result = new ArrayList<>();
        for (MdmItemRespDTO item : items) {
            result.add(convertToWmsItem(item));
        }
        return result;
    }
 
    /**
     * 将 MDM 物料转换为 WMS 商品 DO
     */
    private WmsItemDO convertToWmsItem(MdmItemRespDTO item) {
        WmsItemDO wmsItem = new WmsItemDO();
        wmsItem.setId(item.getId());
        wmsItem.setCode(item.getCode());
        wmsItem.setName(item.getName());
        // MDM categoryId 映射到 WMS categoryId(注意 ID 偏移)
        wmsItem.setCategoryId(item.getCategoryId() != null ? item.getCategoryId() - 20000 : null);
        wmsItem.setBrandId(item.getBrandId());
        // 单位名称需要从 MDM 单位表获取,这里暂时留空
        wmsItem.setRemark(item.getRemark());
        return wmsItem;
    }
 
    @Override
    public long getItemCountByCategoryId(Long categoryId) {
        // 从 MDM 获取分类物料数量(注意 ID 偏移)
        return mdmItemApi.getItemCountByCategoryId(categoryId + 20000).getCheckedData();
    }
 
    @Override
    public long getItemCountByBrandId(Long brandId) {
        // 从 MDM 获取品牌物料数量
        // 注意:MDM 目前没有按品牌统计的 API,暂时返回本地数据
        return itemMapper.selectCountByBrandId(brandId);
    }
 
}