| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | |
| | | } |