From 6b5f7c66fc40d7f6099d561e31a34fbd50dd20d3 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 01 七月 2026 15:54:57 +0800
Subject: [PATCH] 初始化项目
---
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java | 181 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 181 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..13e8dec
--- /dev/null
+++ b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
@@ -0,0 +1,181 @@
+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.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.item.MdmItemDO;
+import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
+import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService;
+import cn.iocoder.yudao.module.mdm.service.unit.MdmUnitMeasureService;
+import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService;
+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.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;
+
+ @Override
+ public Long createItem(MdmItemSaveReqVO createReqVO) {
+ // 鏍¢獙鏁版嵁
+ validateItemSaveData(createReqVO);
+
+ // 鎻掑叆
+ MdmItemDO item = BeanUtils.toBean(createReqVO, MdmItemDO.class);
+ itemMapper.insert(item);
+ return item.getId();
+ }
+
+ @Override
+ public void updateItem(MdmItemSaveReqVO updateReqVO) {
+ // 鏍¢獙瀛樺湪
+ validateItemExists(updateReqVO.getId());
+ // 鏍¢獙鏁版嵁
+ validateItemSaveData(updateReqVO);
+
+ // 鏇存柊
+ MdmItemDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemDO.class);
+ itemMapper.updateById(updateObj);
+ }
+
+ private void validateItemSaveData(MdmItemSaveReqVO reqVO) {
+ // 鏍¢獙鐗╂枡缂栫爜鐨勫敮涓�鎬�
+ validateItemCodeUnique(reqVO.getId(), reqVO.getCode());
+ }
+
+ @Override
+ public void updateItemStatus(Long id, Integer status) {
+ // 鏍¢獙瀛樺湪
+ validateItemExists(id);
+ // 鏇存柊鐘舵��
+ itemMapper.updateById(new MdmItemDO().setId(id).setStatus(status));
+ }
+
+ @Override
+ public void deleteItem(Long id) {
+ // 鏍¢獙瀛樺湪
+ validateItemExists(id);
+ // 鍒犻櫎
+ 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 (itemCategoryService.getItemCategory(categoryId) == 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);
+ }
+ }
+
+ @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