| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package cn.iocoder.yudao.module.mdm.service.brand; |
| | | |
| | | 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.brand.vo.MdmBrandPageReqVO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.brand.vo.MdmBrandSaveReqVO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.brand.MdmBrandDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.brand.MdmBrandMapper; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper; |
| | | 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.module.mdm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * MDM åç Service å®ç°ç±» |
| | | * |
| | | * @author èéæºç |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class MdmBrandServiceImpl implements MdmBrandService { |
| | | |
| | | @Resource |
| | | private MdmBrandMapper brandMapper; |
| | | |
| | | @Resource |
| | | private MdmItemMapper itemMapper; |
| | | |
| | | @Override |
| | | public Long createBrand(MdmBrandSaveReqVO createReqVO) { |
| | | // æ ¡éªæ°æ® |
| | | validateBrandSaveData(createReqVO); |
| | | |
| | | // æå
¥ |
| | | MdmBrandDO brand = BeanUtils.toBean(createReqVO, MdmBrandDO.class); |
| | | brandMapper.insert(brand); |
| | | return brand.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateBrand(MdmBrandSaveReqVO updateReqVO) { |
| | | // æ ¡éªåå¨ |
| | | validateBrandExists(updateReqVO.getId()); |
| | | // æ ¡éªæ°æ® |
| | | validateBrandSaveData(updateReqVO); |
| | | |
| | | // æ´æ° |
| | | MdmBrandDO updateObj = BeanUtils.toBean(updateReqVO, MdmBrandDO.class); |
| | | brandMapper.updateById(updateObj); |
| | | } |
| | | |
| | | @Override |
| | | public void updateBrandStatus(Long id, Integer status) { |
| | | // æ ¡éªåå¨ |
| | | validateBrandExists(id); |
| | | // æ´æ°ç¶æ |
| | | brandMapper.updateById(new MdmBrandDO().setId(id).setStatus(status)); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteBrand(Long id) { |
| | | // æ ¡éªåå¨ |
| | | validateBrandExists(id); |
| | | // æ ¡éªæ¯å¦è¢«ç©æä½¿ç¨ï¼ç´æ¥ä½¿ç¨ Mapper é¿å
循ç¯ä¾èµï¼ |
| | | if (itemMapper.selectCountByBrandId(id) > 0) { |
| | | throw exception(MDM_BRAND_EXISTS_ITEM); |
| | | } |
| | | // å é¤ |
| | | brandMapper.deleteById(id); |
| | | } |
| | | |
| | | @Override |
| | | public MdmBrandDO validateBrandExists(Long id) { |
| | | MdmBrandDO brand = brandMapper.selectById(id); |
| | | if (brand == null) { |
| | | throw exception(MDM_BRAND_NOT_EXISTS); |
| | | } |
| | | return brand; |
| | | } |
| | | |
| | | private void validateBrandSaveData(MdmBrandSaveReqVO reqVO) { |
| | | // æ ¡éªç¼ç å¯ä¸ |
| | | MdmBrandDO brand = brandMapper.selectByCode(reqVO.getCode()); |
| | | if (brand != null && ObjUtil.notEqual(brand.getId(), reqVO.getId())) { |
| | | throw exception(MDM_BRAND_CODE_DUPLICATE); |
| | | } |
| | | // æ ¡éªåç§°å¯ä¸ |
| | | MdmBrandDO sameName = brandMapper.selectByName(reqVO.getName()); |
| | | if (sameName != null && ObjUtil.notEqual(sameName.getId(), reqVO.getId())) { |
| | | throw exception(MDM_BRAND_NAME_DUPLICATE); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public MdmBrandDO getBrand(Long id) { |
| | | return brandMapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public PageResult<MdmBrandDO> getBrandPage(MdmBrandPageReqVO pageReqVO) { |
| | | return brandMapper.selectPage(pageReqVO); |
| | | } |
| | | |
| | | @Override |
| | | public List<MdmBrandDO> getBrandList(Collection<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return brandMapper.selectBatchIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public MdmBrandDO getBrandByCode(String code) { |
| | | return brandMapper.selectByCode(code); |
| | | } |
| | | |
| | | } |