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()); // 校验物料分类存在 validateCategoryExists(reqVO.getCategoryId()); // 校验计量单位存在 validateUnitMeasureExists(reqVO.getUnitMeasureId()); // 校验品牌存在(如果有) if (reqVO.getBrandId() != null) { validateBrandExists(reqVO.getBrandId()); } } @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 getItemPage(MdmItemPageReqVO pageReqVO) { // 查找时,如果查找某个分类编号,则包含它的子分类 Set categoryIds = null; if (pageReqVO.getCategoryId() != null) { categoryIds = new HashSet<>(); categoryIds.add(pageReqVO.getCategoryId()); List 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 getItemList(Collection 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); } }