From 1ab2b329a40decba8ff7eefbeb158ff259aceb6d Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期二, 14 七月 2026 15:23:34 +0800
Subject: [PATCH] 追溯+分类接口
---
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 298 insertions(+), 0 deletions(-)
diff --git a/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
new file mode 100644
index 0000000..dc40a64
--- /dev/null
+++ b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
@@ -0,0 +1,298 @@
+package cn.iocoder.yudao.module.mdm.service.item;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjUtil;
+import cn.iocoder.yudao.framework.common.pojo.CommonResult;
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemPageReqVO;
+import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemSaveReqVO;
+import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO;
+import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO;
+import cn.iocoder.yudao.module.mdm.dal.dataobject.unit.MdmUnitMeasureDO;
+import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
+import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService;
+import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService;
+import cn.iocoder.yudao.module.mdm.service.unit.MdmUnitMeasureService;
+import cn.iocoder.yudao.module.mes.api.item.MesMdItemApi;
+import cn.iocoder.yudao.module.mes.api.item.MesMdItemTypeApi;
+import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSyncReqDTO;
+import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemTypeRespDTO;
+import jakarta.annotation.Resource;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+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.convertSet;
+import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*;
+
+/**
+ * MDM 鐗╂枡涓绘暟鎹� Service 瀹炵幇绫�
+ *
+ * @author 鑺嬮亾婧愮爜
+ */
+@Service
+@Validated
+public class MdmItemServiceImpl implements MdmItemService {
+
+ @Resource
+ private MdmItemMapper itemMapper;
+
+ @Resource
+ private MdmItemCategoryService itemCategoryService;
+ @Resource
+ private MdmUnitMeasureService unitMeasureService;
+ @Resource
+ private MdmBrandService brandService;
+
+ @Resource
+ private MesMdItemApi mesMdItemApi;
+ @Resource
+ private MesMdItemTypeApi mesMdItemTypeApi;
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public Long createItem(MdmItemSaveReqVO createReqVO) {
+ // 鏍¢獙鏁版嵁
+ validateItemSaveData(createReqVO);
+
+ // 鎻掑叆 MDM 鐗╂枡
+ MdmItemDO item = BeanUtils.toBean(createReqVO, MdmItemDO.class);
+ itemMapper.insert(item);
+
+ // 鍚屾鍒� MES锛堝嬀閫� syncMes 鏃讹級
+ if (Boolean.TRUE.equals(createReqVO.getSyncMes())) {
+ syncItemToMes(createReqVO);
+ }
+ return item.getId();
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void updateItem(MdmItemSaveReqVO updateReqVO) {
+ // 鏍¢獙瀛樺湪
+ validateItemExists(updateReqVO.getId());
+ // 鏍¢獙鏁版嵁
+ validateItemSaveData(updateReqVO);
+
+ // 鏇存柊 MDM 鐗╂枡
+ MdmItemDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemDO.class);
+ itemMapper.updateById(updateObj);
+
+ // 鍚屾鍒� MES锛堝嬀閫� syncMes 鏃讹級
+ if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) {
+ syncItemToMes(updateReqVO);
+ }
+ }
+
+ private void validateItemSaveData(MdmItemSaveReqVO reqVO) {
+ // 鏍¢獙鐗╂枡缂栫爜鐨勫敮涓�鎬�
+ validateItemCodeUnique(reqVO.getId(), reqVO.getCode());
+ }
+
+ /**
+ * 鍚屾鐗╂枡鍒� MES锛堝崟浣�/鍒嗙被鎸� code 鏄犲皠锛�
+ *
+ * MDM 鐨� unitMeasureId/categoryId 涓� MES 鐨� unitMeasureId/itemTypeId 鍒嗗睘涓ゅ涓绘暟鎹紝
+ * 杩欓噷鍏堝湪 MDM 渚цВ鏋愬嚭 code锛屽啀浜ょ敱 MesMdItemApi 鍦� MES 渚цВ鏋愪负瀵瑰簲 id銆�
+ * 鍚屾澶辫触灏嗘姏鍑哄紓甯革紝瑙﹀彂澶栧眰 @Transactional 鏁翠綋鍥炴粴锛圡DM 鎿嶄綔涓�骞跺洖婊氾級銆�
+ */
+ private void syncItemToMes(MdmItemSaveReqVO reqVO) {
+ // 瑙f瀽璁¢噺鍗曚綅 id -> code
+ String unitCode = null;
+ if (reqVO.getUnitMeasureId() != null) {
+ MdmUnitMeasureDO unitMeasure = unitMeasureService.getUnitMeasure(reqVO.getUnitMeasureId());
+ if (unitMeasure == null) {
+ throw exception(MDM_UNIT_MEASURE_NOT_EXISTS);
+ }
+ unitCode = unitMeasure.getCode();
+ }
+ // 瑙f瀽鍒嗙被 id -> code锛堜娇鐢� MES 鐗╂枡鍒嗙被 API锛�
+ String itemTypeCode = null;
+ if (reqVO.getCategoryId() != null) {
+ CommonResult<MesMdItemTypeRespDTO> result = mesMdItemTypeApi.getItemType(reqVO.getCategoryId());
+ if (result != null && result.isSuccess() && result.getData() != null) {
+ itemTypeCode = result.getData().getCode();
+ }
+ } else if (reqVO.getItemType() != null) {
+ // 鏍规嵁 itemType 鏌ユ壘瀵瑰簲鍒嗙被缂栫爜
+ // itemType: 1鍘熸枡, 2鍗婃垚鍝�, 3鎴愬搧, 4杈呮枡
+ String itemTypeName = convertItemTypeToName(reqVO.getItemType());
+ CommonResult<List<MesMdItemTypeRespDTO>> listResult = mesMdItemTypeApi.getItemTypeList(null);
+ if (listResult != null && listResult.isSuccess() && listResult.getData() != null) {
+ itemTypeCode = listResult.getData().stream()
+ .filter(c -> c.getName() != null && c.getName().equals(itemTypeName))
+ .findFirst()
+ .map(MesMdItemTypeRespDTO::getCode)
+ .orElse(null);
+ }
+ }
+ // 缁勮鍚屾 DTO
+ MesMdItemSyncReqDTO syncReqDTO = new MesMdItemSyncReqDTO();
+ syncReqDTO.setCode(reqVO.getCode());
+ syncReqDTO.setName(reqVO.getName());
+ syncReqDTO.setSpecification(reqVO.getSpecification());
+ syncReqDTO.setUnitCode(unitCode);
+ syncReqDTO.setItemTypeCode(itemTypeCode);
+ syncReqDTO.setSafeStockFlag(Boolean.TRUE); // 淇濈暀 MDM 鐨� minStock/maxStock锛岄伩鍏嶈 MES 娓呴浂
+ syncReqDTO.setMinStock(reqVO.getMinStock());
+ syncReqDTO.setMaxStock(reqVO.getMaxStock());
+ syncReqDTO.setHighValue(reqVO.getIsHighValue());
+ syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged());
+ syncReqDTO.setRemark(reqVO.getRemark());
+ // create 璧� createItem锛寀pdate 璧� updateItem锛圡ES 渚ф寜 code upsert锛�
+ if (reqVO.getId() == null) {
+ mesMdItemApi.createItem(syncReqDTO);
+ } else {
+ mesMdItemApi.updateItem(syncReqDTO);
+ }
+ }
+
+ @Override
+ public void updateItemStatus(Long id, Integer status) {
+ // 鏍¢獙瀛樺湪
+ validateItemExists(id);
+ // 鏇存柊鐘舵��
+ itemMapper.updateById(new MdmItemDO().setId(id).setStatus(status));
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void deleteItem(Long id) {
+ // 鏍¢獙瀛樺湪
+ MdmItemDO item = validateItemExists(id);
+ // 瑙e喅杞垹闄ゅ敮涓�閿啿绐侊細濡傛灉宸插瓨鍦ㄧ浉鍚� code 涓斿凡杞垹闄ょ殑璁板綍锛屽厛鐗╃悊鍒犻櫎瀹�
+ MdmItemDO existingDeleted = itemMapper.selectByCodeAndDeleted(item.getCode(), true);
+ if (existingDeleted != null && !existingDeleted.getId().equals(id)) {
+ itemMapper.physicalDeleteById(existingDeleted.getId());
+ }
+ // 杞垹闄ゅ綋鍓嶈褰�
+ itemMapper.deleteById(id);
+ }
+
+ @Override
+ public MdmItemDO validateItemExists(Long id) {
+ MdmItemDO item = itemMapper.selectById(id);
+ if (item == null) {
+ throw exception(MDM_ITEM_NOT_EXISTS);
+ }
+ return item;
+ }
+
+ @Override
+ public MdmItemDO validateItemExistsAndEnable(Long id) {
+ MdmItemDO item = validateItemExists(id);
+ if (ObjUtil.notEqual(cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus(), item.getStatus())) {
+ throw exception(MDM_ITEM_IS_DISABLE);
+ }
+ return item;
+ }
+
+ private void validateItemCodeUnique(Long id, String code) {
+ MdmItemDO item = itemMapper.selectByCode(code);
+ if (item == null) {
+ return;
+ }
+ if (ObjUtil.notEqual(item.getId(), id)) {
+ throw exception(MDM_ITEM_CODE_DUPLICATE);
+ }
+ }
+
+ private void validateCategoryExists(Long categoryId) {
+ if (categoryId == null) {
+ return;
+ }
+ CommonResult<MesMdItemTypeRespDTO> result = mesMdItemTypeApi.getItemType(categoryId);
+ if (result == null || !result.isSuccess() || result.getData() == null) {
+ throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS);
+ }
+ }
+
+ private void validateUnitMeasureExists(Long unitMeasureId) {
+ if (unitMeasureService.getUnitMeasure(unitMeasureId) == null) {
+ throw exception(MDM_UNIT_MEASURE_NOT_EXISTS);
+ }
+ }
+
+ private void validateBrandExists(Long brandId) {
+ if (brandService.getBrand(brandId) == null) {
+ throw exception(MDM_BRAND_NOT_EXISTS);
+ }
+ }
+
+ /**
+ * 灏� itemType 杞崲涓轰腑鏂囧悕绉�
+ *
+ * @param itemType 鐗╂枡绫诲瀷锛�1鍘熸枡, 2鍗婃垚鍝�, 3鎴愬搧, 4杈呮枡
+ * @return 涓枃鍚嶇О
+ */
+ private String convertItemTypeToName(Integer itemType) {
+ if (itemType == null) {
+ return null;
+ }
+ switch (itemType) {
+ case 1:
+ return "鍘熸枡";
+ case 2:
+ return "鍗婃垚鍝�";
+ case 3:
+ return "鎴愬搧";
+ case 4:
+ return "杈呮枡";
+ default:
+ return null;
+ }
+ }
+
+ @Override
+ public MdmItemDO getItem(Long id) {
+ return itemMapper.selectById(id);
+ }
+
+ @Override
+ public PageResult<MdmItemDO> getItemPage(MdmItemPageReqVO pageReqVO) {
+ // 鏌ユ壘鏃讹紝濡傛灉鏌ユ壘鏌愪釜鍒嗙被缂栧彿锛屽垯鍖呭惈瀹冪殑瀛愬垎绫�
+ Set<Long> categoryIds = null;
+ if (pageReqVO.getCategoryId() != null) {
+ categoryIds = new HashSet<>();
+ categoryIds.add(pageReqVO.getCategoryId());
+ List<cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO> children =
+ itemCategoryService.getItemCategoryChildrenList(pageReqVO.getCategoryId());
+ categoryIds.addAll(convertSet(children, cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO::getId));
+ }
+ // 鍒嗛〉鏌ヨ
+ return itemMapper.selectPage(pageReqVO, categoryIds);
+ }
+
+ @Override
+ public List<MdmItemDO> getItemList(Collection<Long> ids) {
+ if (CollUtil.isEmpty(ids)) {
+ return Collections.emptyList();
+ }
+ return itemMapper.selectBatchIds(ids);
+ }
+
+ @Override
+ public Long getItemCountByCategoryId(Long categoryId) {
+ return itemMapper.selectCountByCategoryId(categoryId);
+ }
+
+ @Override
+ public Long getItemCountByUnitMeasureId(Long unitMeasureId) {
+ return itemMapper.selectCountByUnitMeasureId(unitMeasureId);
+ }
+
+ @Override
+ public Long getItemCountByBrandId(Long brandId) {
+ return itemMapper.selectCountByBrandId(brandId);
+ }
+
+ @Override
+ public MdmItemDO getItemByCode(String code) {
+ return itemMapper.selectByCode(code);
+ }
+
+}
\ No newline at end of file
--
Gitblit v1.9.3